diff app/src/androidTest/java/com/bartsent/simpleresizer/ResizerTest.kt @ 38:444fe4416c9b

Add unit tests of my resizing code.
author David Barts <n5jrn@me.com>
date Thu, 25 Mar 2021 19:11:43 -0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/src/androidTest/java/com/bartsent/simpleresizer/ResizerTest.kt	Thu Mar 25 19:11:43 2021 -0700
@@ -0,0 +1,87 @@
+package com.bartsent.simpleresizer
+
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.graphics.Color
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.platform.app.InstrumentationRegistry
+import com.bartsent.simpleresizer.lib.getScaledInstance
+import org.junit.Assert
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@RunWith(AndroidJUnit4::class)
+class ResizerTest {
+    val ORIG_TILE_SIZE = 576
+    val WIDTH_IN_TILES = 8
+    val HEIGHT_IN_TILES = 6
+    val CSTEP = 0x55
+
+    var count = 0
+
+    @Test
+    fun canLoadImage() {
+        val image = getBitmap()
+        Assert.assertNotNull(image)
+        checkBitmap(image, WIDTH_IN_TILES * ORIG_TILE_SIZE, HEIGHT_IN_TILES * ORIG_TILE_SIZE)
+    }
+
+    @Test
+    fun resizeVertical() {
+        val image = getBitmap()
+        val newWidth = image.width
+        val newHeight = image.height / 2
+        checkBitmap(image.getScaledInstance(newWidth, newHeight), newWidth, newHeight)
+    }
+
+    @Test
+    fun resizeHorizontal() {
+        val image = getBitmap()
+        val newWidth = image.width / 2
+        val newHeight = image.height
+        checkBitmap(image.getScaledInstance(newWidth, newHeight), newWidth, newHeight)
+    }
+
+    @Test
+    fun resizeBoth() {
+        val image = getBitmap()
+        val newWidth = image.width / 2
+        val newHeight = image.height / 2
+        checkBitmap(image.getScaledInstance(newWidth, newHeight), newWidth, newHeight)
+    }
+
+    fun getBitmap(): Bitmap =
+        InstrumentationRegistry.getInstrumentation().context.assets.open("testimage.png").use {
+            BitmapFactory.decodeStream(it)
+        }
+
+    fun getColor(): Int {
+        count += 1
+        val b = (count and 0x3)*CSTEP
+        val temp = count shr 2
+        val g = (temp and 0x3)*CSTEP
+        val r = ((temp shr 2) and 0x3)*CSTEP
+        return Color.rgb(r, g, b)
+    }
+
+    fun resetColor() {
+        count = 0
+    }
+
+    fun checkBitmap(image: Bitmap, width: Int, height: Int) {
+        Assert.assertEquals("width", width, image.width)
+        Assert.assertEquals("height", height, image.height)
+        val tileWidth = image.width / WIDTH_IN_TILES
+        val tileHeight = image.height / HEIGHT_IN_TILES
+        val tw2 = tileWidth / 2
+        val th2 = tileHeight / 2
+        resetColor()
+        for (x in 0 until WIDTH_IN_TILES) {
+            val xoff = tw2 + x * tileWidth
+            for (y in 0 until HEIGHT_IN_TILES) {
+                val yoff = th2 + y * tileHeight
+                Assert.assertEquals("color @ tile $x, $y", getColor(), image.getPixel(xoff, yoff))
+            }
+        }
+    }
+}
\ No newline at end of file