feat: games show their logo, and store games get one from the store (#999)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-22 14:48:23 -04:00
commit f61525162d
24 files changed
+561 -55

No files matched your search

@@ -0,0 +1,163 @@
package com.droiddeck.launcher.frontend
import com.droiddeck.launcher.frontend.AddedGameArt.Slot
import com.droiddeck.launcher.session.SessionPrefs
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import java.io.File
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class AddedGameArtFillTest {
private val app get() = RuntimeEnvironment.getApplication()
@get:Rule val tmp = TemporaryFolder()
private val coverBytes = byteArrayOf(9, 9, 9)
private fun game(name: String, appId: Long, cover: Boolean = true, source: String = Library.ADDED): AddedGames.Game {
val dir = tmp.newFolder(name)
File(dir, "steam_appid.txt").writeText("504230")
if (cover) File(dir, "cover.jpg").writeBytes(coverBytes)
val exe = File(dir, "$name.exe").apply { writeText("x") }
return AddedGames.Game(dir, name, exe, "/g/$name.exe", "/g", appId, appId, emptyList(), source = source)
}
private class Calls {
val steam = ArrayList<Set<Slot>>()
val sgdb = ArrayList<Set<Slot>>()
var lookups = 0
var store = 0
}
private val steamNames = mapOf(Slot.COVER to "p.jpg", Slot.WIDE to "header.jpg", Slot.BACKGROUND to "hero.jpg", Slot.LOGO to "logo.png")
private fun sources(
calls: Calls, steamHas: Set<Slot> = emptySet(), sgdbHas: Set<Slot> = emptySet(),
storeHas: Boolean = false, sgdbFails: Boolean = false,
) = AddedGameArt.Sources(
storeLogo = { g ->
calls.store++
if (storeHas) File(g.folder, "logo.png").writeBytes(byteArrayOf(4))
storeHas
},
steamSearch = { fail("searched although the game names its appid"); null },
steam = { id, cache, slots ->
assertEquals("504230", id)
calls.steam += slots
(slots intersect steamHas).onEach { File(cache, steamNames.getValue(it)).writeBytes(byteArrayOf(1)) }.isNotEmpty()
},
sgdbGame = { _, steamId, _ ->
calls.lookups++
assertEquals(504230, steamId)
77
},
sgdb = { _, id, cache, slots ->
assertEquals(77, id)
if (sgdbFails) throw SteamGridDb.Failed(SteamGridDb.Status.OFFLINE)
calls.sgdb += slots
(slots intersect sgdbHas).onEach { File(cache, SteamGridDb.cacheName(it)).writeBytes(byteArrayOf(2)) }.isNotEmpty()
},
)
@Test fun aGameWithItsOwnCoverStillGetsItsLogoAndIcon() {
val g = game("Celeste", 0x81000001L)
val calls = Calls()
assertTrue(AddedGameArt.fetchMissing(app, listOf(g), "key", sources(calls, steamHas = setOf(Slot.LOGO), sgdbHas = setOf(Slot.ICON))))
assertEquals(listOf(setOf(Slot.LOGO)), calls.steam)
assertEquals(listOf(setOf(Slot.ICON)), calls.sgdb)
assertEquals(0, calls.store)
val cache = AddedGameArt.cacheDir(app, g)
assertFalse(File(cache, "p.jpg").exists())
assertArrayEquals(coverBytes, File(g.folder, "cover.jpg").readBytes())
val art = AddedGameArt.resolve(app, g)
assertEquals(File(g.folder, "cover.jpg"), art.portrait)
assertEquals(File(cache, "logo.png"), art.logo)
assertEquals(File(cache, "icon-sgdb.png"), art.icon)
assertFalse(File(cache, AddedGameArt.EXTRAS_TRIED).exists())
}
@Test fun filledOrChosenSlotsAreNeverAskedFor() {
val g = game("Hades", 0x81000002L)
File(g.folder, "logo.png").writeBytes(byteArrayOf(5))
val picked = File(tmp.root, "mine.png").apply { writeBytes(byteArrayOf(6)) }
assertTrue(AddedGameArt.store(AddedGameArt.cacheDir(app, g), Slot.ICON, AddedGameArt.Option(AddedGameArt.FILE, picked.path, picked.path)))
val calls = Calls()
assertFalse(AddedGameArt.fetchMissing(app, listOf(g), "key", sources(calls, steamHas = Slot.values().toSet(), sgdbHas = Slot.values().toSet())))
assertTrue(calls.steam.isEmpty() && calls.sgdb.isEmpty() && calls.lookups == 0)
assertArrayEquals(byteArrayOf(5), File(g.folder, "logo.png").readBytes())
}
@Test fun withoutAKeyAMissingIconAloneAsksNothing() {
val g = game("Inside", 0x81000003L)
File(g.folder, "logo.png").writeBytes(byteArrayOf(5))
val calls = Calls()
assertFalse(AddedGameArt.fetchMissing(app, listOf(g), "", sources(calls)))
assertTrue(calls.steam.isEmpty() && calls.sgdb.isEmpty() && calls.lookups == 0)
assertFalse(AddedGameArt.cacheDir(app, g).exists())
}
@Test fun aMissIsAskedAgainOnlyAfterAWeek() {
val g = game("Limbo", 0x81000004L)
val calls = Calls()
assertFalse(AddedGameArt.fetchMissing(app, listOf(g), "key", sources(calls)))
assertEquals(listOf(setOf(Slot.LOGO)), calls.steam)
assertEquals(listOf(setOf(Slot.LOGO, Slot.ICON)), calls.sgdb)
AddedGameArt.fetchMissing(app, listOf(g), "key", sources(calls))
assertEquals(1, calls.steam.size)
assertEquals(1, calls.sgdb.size)
val tried = File(AddedGameArt.cacheDir(app, g), AddedGameArt.EXTRAS_TRIED)
assertTrue(tried.setLastModified(System.currentTimeMillis() - 8L * 24 * 3600 * 1000))
assertTrue(AddedGameArt.fetchMissing(app, listOf(g), "key", sources(calls, steamHas = setOf(Slot.LOGO), sgdbHas = setOf(Slot.ICON))))
assertEquals(2, calls.steam.size)
assertEquals(setOf(Slot.ICON), calls.sgdb.last())
assertFalse(tried.exists())
}
@Test fun theStoresOwnLogoComesFirst() {
val g = game("Metalstorm", 0x81000005L, source = "gog")
val calls = Calls()
assertTrue(AddedGameArt.fetchMissing(app, listOf(g), "key", sources(calls, storeHas = true, steamHas = setOf(Slot.LOGO), sgdbHas = setOf(Slot.ICON))))
assertEquals(1, calls.store)
assertTrue(calls.steam.isEmpty())
assertEquals(listOf(setOf(Slot.ICON)), calls.sgdb)
assertEquals(File(g.folder, "logo.png"), AddedGameArt.resolve(app, g).logo)
}
@Test fun aGameWithoutACoverStillGetsEverySlot() {
val g = game("Tunic", 0x81000006L, cover = false)
val calls = Calls()
assertTrue(AddedGameArt.fetchMissing(app, listOf(g), "key",
sources(calls, steamHas = setOf(Slot.COVER, Slot.WIDE, Slot.BACKGROUND), sgdbHas = setOf(Slot.LOGO, Slot.ICON))))
assertEquals(listOf(setOf(Slot.COVER, Slot.WIDE, Slot.BACKGROUND, Slot.LOGO)), calls.steam)
assertEquals(listOf(setOf(Slot.LOGO, Slot.ICON)), calls.sgdb)
assertTrue(AddedGameArt.missing(app, g).isEmpty())
}
@Test fun aSteamGridDbFailureIsNotRememberedAndStopsThePass() {
val first = game("Gris", 0x81000007L)
val second = game("Hollow", 0x81000008L)
val calls = Calls()
AddedGameArt.fetchMissing(app, listOf(first, second), "key", sources(calls, sgdbFails = true))
assertEquals(2, calls.steam.size)
assertEquals(1, calls.lookups)
assertFalse(File(AddedGameArt.cacheDir(app, first), AddedGameArt.EXTRAS_TRIED).exists())
assertFalse(File(AddedGameArt.cacheDir(app, second), AddedGameArt.EXTRAS_TRIED).exists())
}
@Test fun nothingIsFetchedWhenArtIsOff() {
SessionPrefs.setAddedGamesArt(app, false)
val calls = Calls()
assertFalse(AddedGameArt.fetchMissing(app, listOf(game("Off", 0x81000009L)), "key", sources(calls)))
assertTrue(calls.steam.isEmpty())
}
}
@@ -0,0 +1,80 @@
package com.droiddeck.launcher.frontend
import com.droiddeck.launcher.session.SessionPrefs
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import java.io.File
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class GameLogoTest {
private val app get() = RuntimeEnvironment.getApplication()
@get:Rule val tmp = TemporaryFolder()
@Before fun noGamesFolders() {
AddedGamesPublisher.now = true
SessionPrefs.setGameStorage(app, SessionPrefs.GAME_STORAGE_OFF, "")
SessionPrefs.setAddedGamesDirs(app, emptyList())
}
private fun image(name: String) = File(tmp.root, name).apply { parentFile?.mkdirs(); writeBytes(ByteArray(8) { 1 }) }
@Test fun steamsCachedLogoIsFoundInTheAppFolderOrAHashedOne() {
val cache = tmp.newFolder("librarycache")
val logo = File(cache, "620/3f1c/logo.png").apply { parentFile?.mkdirs(); writeBytes(ByteArray(4) { 1 }) }
assertEquals(logo, Library.steamCacheImage(cache, 620, Library.STEAM_LOGOS))
File(cache, "730").mkdirs()
File(cache, "730/logo.png").writeBytes(ByteArray(0))
assertNull(Library.steamCacheImage(cache, 730, Library.STEAM_LOGOS))
}
@Test fun aChosenLogoWinsOverSteamsAndKeepsTheRest() {
val stock = image("logo.png")
val game = Library.SteamGame(620, "Portal 2", null, "Steam library", logo = stock)
assertTrue(SteamGameArt.withOverrides(app, game) === game)
val picked = image("mine.png")
assertTrue(SteamGameArt.choose(app, game, AddedGameArt.Slot.LOGO, AddedGameArt.Option(AddedGameArt.FILE, picked.path, picked.path)))
val shown = SteamGameArt.withOverrides(app, game)
assertTrue(shown.logo!!.name.startsWith("chosen-logo"))
assertNull(shown.art)
SteamGameArt.reset(app, game, AddedGameArt.Slot.LOGO)
assertEquals(stock, SteamGameArt.withOverrides(app, game).logo)
}
@Test fun anAddedGameShowsItsFolderLogoUnlessOneWasChosen() {
val dir = tmp.newFolder("Celeste")
val own = File(dir, "logo.png").apply { writeBytes(ByteArray(4) { 1 }) }
val exe = File(dir, "Celeste.exe").apply { writeText("x") }
val g = AddedGames.Game(dir, "Celeste", exe, "/g/Celeste.exe", "/g", 0x82000001L, 0x82000001L, emptyList(), source = "gog")
assertEquals(own, Library.addedGame(app, g, emptyList()).logo)
val picked = image("pick.png")
assertTrue(AddedGameArt.choose(app, g, AddedGameArt.Slot.LOGO, AddedGameArt.Option(AddedGameArt.FILE, picked.path, picked.path)))
assertTrue(Library.addedGame(app, g, emptyList()).logo!!.name.startsWith("chosen-logo"))
val bare = AddedGames.Game(tmp.newFolder("Bare"), "Bare", exe, "/g/Bare.exe", "/g", 0x82000002L, 0x82000002L, emptyList())
assertNull(Library.addedGame(app, bare, emptyList()).logo)
}
@Test fun theLibraryCacheKeepsTheLogoAndReadsOlderEntries() {
val logo = image("logo.png")
val game = Library.SteamGame(620, "Portal 2", null, "Steam library", logo = logo)
LibraryCache.save(app, listOf(game, Library.SteamGame(730, "CS2", null, "Steam library")))
val back = LibraryCache.load(app)
assertEquals(logo, back[0].logo)
assertNull(back[1].logo)
assertEquals(logo, game.withArt(null, null, null).logo)
val file = File(app.filesDir, "library-cache.json")
file.writeText(JSONArray().put(JSONObject().put("appId", 1).put("name", "Old").put("library", "x").put("gameId", 1L)).toString())
assertNull(LibraryCache.load(app).single().logo)
}
}
@@ -34,6 +34,17 @@ class StoreGameSidecarTest {
assertEquals("ns", back.extra["namespace"])
}
@Test fun theStoreLogoRoundTripsAndOlderFilesHaveNone() {
val withLogo = sample.copy(logo = "https://x/logo.png")
val back = StoreGameSidecar.parse(withLogo.toJson().toString())!!
assertEquals("https://x/logo.png", back.logo)
assertEquals("https://x/logo.png", back.copy(state = StoreGameSidecar.STATE_UPDATING).logo)
assertFalse(sample.toJson().has("logo"))
assertNull(StoreGameSidecar.parse(sample.toJson().toString())!!.logo)
assertNull(StoreGameSidecar.parse("""{"store":"gog","id":"1","title":"T","exe":"game.exe","cover":"c","hero":"h"}""")!!.logo)
assertNull(StoreGameSidecar.parse("""{"store":"gog","id":"1","title":"T","exe":"game.exe","logo":""}""")!!.logo)
}
@Test fun defaultsWhenFieldsAreAbsent() {
val back = StoreGameSidecar.parse("""{"store":"gog","id":"1","title":"T","exe":"bin\\game.exe","addToSteam":false}""")!!
assertEquals("bin/game.exe", back.exe)
@@ -0,0 +1,88 @@
package com.droiddeck.launcher.stores
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import com.droiddeck.launcher.stores.amazon.AmazonLibrary
import com.droiddeck.launcher.stores.gog.GogBackend
import org.json.JSONObject
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import java.io.ByteArrayOutputStream
import java.io.File
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class StoreLogoTest {
@get:Rule val tmp = TemporaryFolder()
private fun encoded(format: Bitmap.CompressFormat): ByteArray {
val bmp = Bitmap.createBitmap(8, 4, Bitmap.Config.ARGB_8888)
bmp.eraseColor(Color.TRANSPARENT)
bmp.setPixel(1, 1, Color.RED)
return ByteArrayOutputStream().also { bmp.compress(format, 100, it) }.toByteArray()
}
@Test fun aPngLogoIsKeptAsItCameWithItsTransparency() {
val png = encoded(Bitmap.CompressFormat.PNG)
val dst = File(tmp.root, StoreArt.LOGO)
assertTrue(StoreArt.writeLogo(png, dst))
assertArrayEquals(png, dst.readBytes())
val back = BitmapFactory.decodeFile(dst.path)
assertEquals(0, Color.alpha(back.getPixel(0, 0)))
assertEquals(Color.RED, back.getPixel(1, 1))
assertFalse(File(dst.path + ".tmp").exists())
}
@Test fun anotherImageFormatIsStoredAsPng() {
val dst = File(tmp.root, StoreArt.LOGO)
assertTrue(StoreArt.writeLogo(encoded(Bitmap.CompressFormat.JPEG), dst))
val head = dst.readBytes().take(4)
assertEquals(listOf(0x89.toByte(), 'P'.code.toByte(), 'N'.code.toByte(), 'G'.code.toByte()), head)
assertEquals(8, BitmapFactory.decodeFile(dst.path).width)
}
@Test fun whatIsNotAnImageIsRefusedAndAnExistingLogoStays() {
val dst = File(tmp.root, StoreArt.LOGO)
assertFalse(StoreArt.writeLogo("<html>not found</html>".toByteArray(), dst))
assertFalse(dst.exists())
dst.writeBytes(byteArrayOf(1, 2, 3))
assertTrue(StoreArt.writeLogo(encoded(Bitmap.CompressFormat.PNG), dst))
assertArrayEquals(byteArrayOf(1, 2, 3), dst.readBytes())
assertTrue(StoreArt.fetchLogo(tmp.root, "https://invalid.example/logo.png"))
}
@Test fun gogTakesTheV2LogoLinkOnly() {
val v2 = JSONObject("""{"_links":{"logo":{"href":"https://images.gog-statics.com/abc_logo.png"},"galaxyBackgroundImage":{"href":"https://x/bg.jpg"}}}""")
assertEquals("https://images.gog-statics.com/abc_logo.png", GogBackend.logoOf(v2))
assertEquals("https://images.gog-statics.com/rel.png", GogBackend.logoOf(JSONObject("""{"_links":{"logo":{"href":"//images.gog-statics.com/rel.png"}}}""")))
assertNull(GogBackend.logoOf(JSONObject("""{"_links":{}}""")))
assertNull(GogBackend.logoOf(JSONObject("""{"images":{"logo2x":"https://x/glx_logo_2x.jpg"}}""")))
assertNull(GogBackend.logoOf(JSONObject("""{"_links":{"logo":{"href":"http://x/logo.png"}}}""")))
}
@Test fun amazonKeepsTheLogoFromTheLibraryAndItsCache() {
val app = RuntimeEnvironment.getApplication()
val fresh = AmazonLibrary.gameFrom(JSONObject().put("productId", "p").put("title", "Game").put("logo", "https://m/logo.png"), null)
assertEquals("https://m/logo.png", fresh.logoUrl)
assertEquals("https://m/logo.png", AmazonLibrary.toCatalogItem(app, fresh).logoUrl)
assertEquals(listOf(fresh), AmazonLibrary.fromCacheJson(AmazonLibrary.toCacheJson(listOf(fresh))))
val kept = AmazonLibrary.gameFrom(JSONObject().put("productId", "p").put("title", "Game"), fresh)
assertEquals("https://m/logo.png", kept.logoUrl)
val none = AmazonLibrary.gameFrom(JSONObject().put("productId", "q").put("title", "Other"), null)
assertNull(AmazonLibrary.toCatalogItem(app, none).logoUrl)
assertEquals("", AmazonLibrary.fromCacheJson("""[{"productId":"old","title":"Old"}]""").single().logoUrl)
}
}
@@ -83,6 +83,18 @@ class EpicLibraryTest {
assertEquals("Fig", EpicLibrary.gameFrom(record, null, null).title)
}
@Test fun theCatalogLogoIsKeptInTheCacheAndHandedToTheInstall() {
val record = JSONObject().put("appName", "Fig").put("namespace", "ns").put("catalogItemId", "cat")
val fresh = EpicLibrary.gameFrom(record, JSONObject().put("logo", "https://e/logo.png"), null)
assertEquals("https://e/logo.png", fresh.artLogo)
assertEquals("https://e/logo.png", EpicLibrary.gameFrom(record, JSONObject().put("title", "Fig"), fresh).artLogo)
assertEquals(listOf(fresh), EpicLibrary.fromCacheJson(EpicLibrary.toCacheJson(listOf(fresh))))
assertEquals("https://e/logo.png", EpicLibrary.toCatalogItem(app, fresh).logoUrl)
val plain = EpicGame("Old", "ns", "c", "Old")
assertFalse(JSONArray(EpicLibrary.toCacheJson(listOf(plain))).getJSONObject(0).has("artLogo"))
assertEquals(null, EpicLibrary.toCatalogItem(app, plain).logoUrl)
}
@Test fun signOutForgetsOnlyTheLibrary() {
EpicPrefs.get(app).edit().putString(EpicLibrary.KEY_CACHE, "[]").putLong(EpicLibrary.KEY_SYNCED_AT, 1L)
.putString("cloud_template_Fig", "{AppData}/x").commit()