Cotton code
69 lines · 4.1 KB · KotlinRaw
package com.cottondb.photosimport com.cottondb.shell.Groundimport com.cottondb.shell.strimport org.json.JSONObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.Fileclass PhotosFlavourTest {    private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile }    private fun json(path: String) = JSONObject(File(repo, path).readText())    /** The app stands on the ground its iOS flavour.json names (the darkroom bench), and its window shows that ground's colour before the first frame. */    @Test    fun photos_stands_on_the_darkroom_bench() {        val ground = json("apps/cotton/App/Flavours/photos/flavour.json").getString("ground")        assertEquals(Ground.of(ground), PhotosParts().ground)        val window = json("apps/android/app/flavours.json").getJSONObject("photos").getString("windowGround")        val colour = Ground.darkroomBench.colour        fun hex(channel: Float) = "%02X".format((channel * 255).toInt())        assertEquals("#" + hex(colour.red) + hex(colour.green) + hex(colour.blue), window)        assertTrue("the bench is dark: words beside a print take light ink", Ground.darkroomBench.dark)    }    /** The flavour's kit is the photos kit, whose library the fan opens on; the registry builds it here as com.cottondb.photos. */    @Test    fun photos_opens_on_its_library() {        val flavour = json("apps/cotton/App/Flavours/photos/flavour.json")        assertEquals("photos", flavour.getString("kit"))        assertEquals("photos", flavour.getJSONObject("fan").getString("opens"))        val row = json("apps/registry.json").getJSONArray("apps").let { apps -> (0 until apps.length()).map { apps.getJSONObject(it) }.first { it.getString("slug") == "photos" } }        assertEquals("com.cottondb.photos", row.getJSONObject("android").getString("package"))        assertEquals("com.cottondb.photos.dev", row.getJSONObject("android").getString("devPackage"))    }    /** The bench and the album board's layers are copied into the build (apps/loom/materials.json), each one Loom's catalogue holds; the print's are every app's. */    @Test    fun the_bench_and_the_albums_are_carried() {        val lists = json("apps/loom/materials.json")        fun names(a: org.json.JSONArray) = (0 until a.length()).map { a.getString(it) }        val materials = names(lists.getJSONArray("everyApp")) + names(lists.getJSONObject("apps").getJSONArray("photos"))        val carried = { name: String -> materials.any { it == name || (it.endsWith("*") && name.startsWith(it.removeSuffix("*"))) } }        listOf("photos-bench", "photo-print-shadow", "photo-print-sheen", "album-board", "album-board-shadow", "album-instant", "album-label", "album-pin").forEach { name ->            assertTrue("$name is not carried", carried(name))            assertTrue("$name is no Loom painting", File(repo, "apps/loom/Sources/CottonPaper/Media.xcassets/$name.imageset").isDirectory)        }    }    /** A picked picture is kept as a photo and a picked film as a video, by its media type; anything else is not taken. */    @Test    fun a_picked_file_is_asked_by_its_kind() {        assertEquals("photo", PhotosIntake.ask("/c/pick.jpg", "image/jpeg", "pick.jpg").str("kind"))        assertEquals("video", PhotosIntake.ask("/c/pick.mp4", "video/mp4", "pick.mp4").str("kind"))        assertEquals("image/heic", PhotosIntake.ask("/c/pick.heic", "image/heic", "pick.heic").str("mime"))        assertNull(PhotosIntake.kind("application/pdf"))    }    /** The thumbnail and display copy the phone drew ride the ask (R6): the picture goes up sealed, so the object draws none. */    @Test    fun the_renditions_drawn_here_ride_the_ask() {        val ask = PhotosIntake.ask("/c/pick.jpg", "image/jpeg", "pick.jpg", "/c/t.jpg", "/c/d.jpg")        assertEquals("/c/t.jpg", ask.str("thumbPath"))        assertEquals("/c/d.jpg", ask.str("displayPath"))        assertNull(PhotosIntake.ask("/c/pick.jpg", "image/jpeg", "pick.jpg").str("thumbPath"))    }}