+package com.alexandria.reader
+
+import android.content.Context
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.platform.app.InstrumentationRegistry
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import java.io.File
+
+@RunWith(AndroidJUnit4::class)
+class CoverImageDecoderInstrumentedTest {
+ @Test
+ fun decodesProvidedEpubCoversAtLibraryThumbnailSize() {
+ val context = ApplicationProvider.getApplicationContext<Context>()
+ val testRoot = File(context.cacheDir, "cover-decoder-test").apply {
+ deleteRecursively()
+ mkdirs()
+ }
+
+ try {
+ listOf("odyssey.epub", "the_prize.epub", "money_promises.epub").forEachIndexed { index, fixture ->
+ val source = File(testRoot, fixture)
+ InstrumentationRegistry.getInstrumentation().context.assets.open(fixture).use { input ->
+ source.outputStream().use(input::copyTo)
+ }
+ val book = LibraryBook(
+ id = (index + 1).toString().repeat(24),
+ title = "Test book",
+ author = "Test author",
+ fileName = fixture,
+ )
+ val publication = EpubParser.parse(source, File(testRoot, "content-$index"), book)
+ val cover = publication.book.coverPath?.let(::File)
+ assertNotNull("$fixture should identify a cover", cover)
+ assertTrue("$fixture cover should exist", cover?.isFile == true)
+
+ val bitmap = CoverImageDecoder.decode(requireNotNull(cover), TARGET_WIDTH, TARGET_HEIGHT)
+ assertNotNull("$fixture cover should decode", bitmap)
+ requireNotNull(bitmap).run {
+ assertEquals(TARGET_WIDTH, width)
+ assertEquals(TARGET_HEIGHT, height)
+ recycle()
+ }
+ }
+ } finally {
+ testRoot.deleteRecursively()
+ }
+ }
+
+ private companion object {
+ const val TARGET_WIDTH = 93
+ const val TARGET_HEIGHT = 132
+ }
+}