Cotton code
68 lines · 3.0 KB · KotlinRaw
package com.cottondb.albumsimport com.cottondb.albums.analysis.LookPictureimport com.cottondb.albums.analysis.Pictureimport com.cottondb.albums.analysis.PictureReaderimport com.cottondb.albums.library.LibraryItemimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Test/** * THE PICTURE A LOOK READS IS NEVER SILENTLY TOO SMALL (the iPhone's finding, 27 Nov: its quick thumbnail came back * under 128 px). A camera JPEG with a 160 x 120 EXIF thumbnail comes back from loadThumbnail at 160 x 120 on Android * too. Under the minimum the look needs, the original is decoded larger; failing that the item is owed a closer look, * never a finished one. */class ThumbSizeTest {    private fun item(w: Int, h: Int, video: Boolean = false) =        LibraryItem(1, video, "external_primary", 1, "Camera", "DCIM/Camera/", "IMG_1.jpg", 1000, 1, 1, 1_000, w, h, "image/jpeg", null, false, null, 1, false)    private class Fake(val quick: (Int) -> Picture?, val closer: (Int) -> Picture?) : PictureReader {        val asked = ArrayList<String>()        override fun quick(item: LibraryItem, px: Int): Picture { asked += "quick $px"; return quick(px) ?: throw java.io.IOException("no thumbnail") }        override fun closer(item: LibraryItem, px: Int): Picture? { asked += "closer $px"; return closer(px) }    }    private fun pic(w: Int, h: Int) = Picture(w, h, IntArray(w * h))    @Test    fun an_exif_sized_thumbnail_is_decoded_again_larger() {        val reader = Fake({ pic(160, 120) }, { px -> pic(px, px * 3 / 4) })        val got = LookPicture.fetch(reader, item(4000, 3000))        assertTrue("long edge ${got.picture?.longEdge}", (got.picture?.longEdge ?: 0) >= LookPicture.MIN)        assertNull(got.owed)        assertEquals(listOf("quick 512", "closer 512"), reader.asked)    }    @Test    fun too_small_and_no_closer_picture_is_owed_a_closer_look() {        val got = LookPicture.fetch(Fake({ pic(160, 120) }, { null }), item(4000, 3000))        assertNull(got.picture)        assertEquals(160, got.owed)    }    @Test    fun a_full_size_thumbnail_is_taken_as_it_is() {        val reader = Fake({ pic(512, 384) }, { error("not asked") })        val got = LookPicture.fetch(reader, item(4000, 3000))        assertEquals(512, got.picture?.longEdge)        assertEquals(listOf("quick 512"), reader.asked)    }    @Test    fun a_small_original_is_never_owed_more_than_it_has() {        // A 300 x 200 picture cannot give 384: its own size is a finished look.        val got = LookPicture.fetch(Fake({ pic(300, 200) }, { error("not asked") }), item(300, 200))        assertEquals(300, got.picture?.longEdge)        assertNull(got.owed)    }    @Test    fun no_thumbnail_at_all_tries_the_original() {        val got = LookPicture.fetch(Fake({ null }, { px -> pic(px * 9 / 16, px) }), item(1080, 1920, video = true))        assertEquals(512, got.picture?.longEdge)        assertNull(got.owed)    }}