Yokan / rebuild
Course 1 8624561

CastBridgeCompatibilityTest.kt

vendor/yokan-ext/src/androidHostTest/kotlin/eu/friedcutlet/yokan/ext/CastBridgeCompatibilityTest.kt

test / fixture99 lines
1package eu.friedcutlet.yokan.ext23import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper4import eu.kanade.tachiyomi.animesource.model.Credit5import eu.kanade.tachiyomi.animesource.model.SAnime6import kotlinx.serialization.json.Json7import m_extension_server.model.toJAnime8import java.net.URLClassLoader9import java.nio.file.Files10import javax.tools.ToolProvider11import kotlin.test.*1213class CastBridgeCompatibilityTest {14    @Test fun classCompiledAgainstAnOlderInterfaceLinksAgainstTheCurrentHost() {15        val directory = Files.createTempDirectory("yokan-legacy-anime-api").toFile()16        try {17            val source = directory.resolve("eu/kanade/tachiyomi/animesource/model").apply { mkdirs() }18            // Deliberately compile without the new cast member, then load against the real host.19            source.resolve("SAnime.java").writeText("""20                package eu.kanade.tachiyomi.animesource.model;21                public interface SAnime {22                    String getUrl(); String getTitle(); String getArtist(); String getAuthor();23                    String getDescription(); String getGenre(); String getThumbnail_url();24                    int getStatus(); boolean getInitialized();25                }26            """.trimIndent())27            source.resolve("LegacyAnime.java").writeText("""28                package eu.kanade.tachiyomi.animesource.model;29                public class LegacyAnime implements SAnime {30                    public String getUrl() { return "/legacy-binary"; }31                    public String getTitle() { return "Old binary"; }32                    public String getArtist() { return null; }33                    public String getAuthor() { return "Author"; }34                    public String getDescription() { return null; }35                    public String getGenre() { return null; }36                    public String getThumbnail_url() { return null; }37                    public int getStatus() { return 1; }38                    public boolean getInitialized() { return true; }39                }40            """.trimIndent())41            val compiler = requireNotNull(ToolProvider.getSystemJavaCompiler()) { "Run bridge unit tests with a JDK" }42            assertEquals(0, compiler.run(null, null, null, "-d", directory.path,43                source.resolve("SAnime.java").path, source.resolve("LegacyAnime.java").path))44            URLClassLoader(arrayOf(directory.toURI().toURL()), SAnime::class.java.classLoader).use { loader ->45                val old = loader.loadClass("eu.kanade.tachiyomi.animesource.model.LegacyAnime")46                    .getDeclaredConstructor().newInstance() as SAnime47                assertEquals("Old binary", old.toJAnime().title)48                assertNull(old.toJAnime().cast)49                assertEquals("Episodes", old.toJAnime().fetch_type)50                assertEquals(-1.0, old.toJAnime().season_number)51                val copy = SAnime.create().apply { url = old.url; copyFrom(old) }52                assertEquals("Author", copy.author)53                assertNull(copy.cast)54                assertEquals(eu.kanade.tachiyomi.animesource.model.FetchType.Episodes, copy.fetch_type)55            }56        } finally { directory.deleteRecursively() }57    }5859    @Test fun hostAnimeCreditsSurviveCopyAndBridgeSerialization() {60        val credit = Credit("Actor", "Voice actor", "Hero", "https://cdn.example/actor.jpg")61        val anime = SAnime.create().apply {62            url = "/title"63            title = "Title"64            status = SAnime.UPCOMING65            cast = listOf(credit)66        }67        val copy = SAnime.create().apply { url = anime.url; copyFrom(anime) }68        assertEquals(listOf(credit), copy.cast)69        assertEquals(SAnime.UPCOMING, copy.status)70        assertEquals(credit, Json.decodeFromString<Credit>(Json.encodeToString(Credit.serializer(), credit)))7172        val mapper = jacksonObjectMapper()73        val json = mapper.readTree(mapper.writeValueAsString(copy.toJAnime()))74        assertEquals("Actor", json["cast"][0]["name"].asText())75        assertEquals("https://cdn.example/actor.jpg", json["cast"][0]["image_url"].asText())76        assertEquals("Hero", json["cast"][0]["character"].asText())77        assertEquals(7, json["status"].asInt())78    }7980    @Test fun legacyExtensionWithoutAccessorStillReturnsDetails() {81        val base = SAnime.create().apply { url = "/legacy"; title = "Legacy title" }82        val oldBinary = object : SAnime by base {83            override var cast: List<Credit>?84                get() = throw AbstractMethodError("Older APK has no getCast accessor")85                set(_) { throw AbstractMethodError("Older APK has no setCast accessor") }86        }87        assertEquals("Legacy title", oldBinary.toJAnime().title)88        assertNull(oldBinary.toJAnime().cast)89        val copy = SAnime.create().apply { url = oldBinary.url; copyFrom(oldBinary) }90        assertEquals("Legacy title", copy.title)91        assertNull(copy.cast)92    }9394    @Test fun explicitEmptyCreditsRemainAnExplicitClear() {95        val anime = SAnime.create().apply { url = "/title"; title = "Title"; cast = emptyList() }96        val copy = SAnime.create().apply { url = anime.url; cast = listOf(Credit("Old")); copyFrom(anime) }97        assertEquals(emptyList(), copy.toJAnime().cast)98    }99}100

Search chapters, reference and source filenames.