changeset 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 0dbd924cb5e8
children d723d07d5bc0
files .idea/gradle.xml app/build.gradle app/src/androidTest/assets/testimage.png app/src/androidTest/java/com/bartsent/simpleresizer/ResizerTest.kt
diffstat 4 files changed, 90 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/.idea/gradle.xml	Tue Mar 23 22:30:34 2021 -0700
+++ b/.idea/gradle.xml	Thu Mar 25 19:11:43 2021 -0700
@@ -8,7 +8,6 @@
         <option name="disableWrapperSourceDistributionNotification" value="true" />
         <option name="distributionType" value="DEFAULT_WRAPPED" />
         <option name="externalProjectPath" value="$PROJECT_DIR$" />
-        <option name="gradleJvm" value="1.8" />
         <option name="modules">
           <set>
             <option value="$PROJECT_DIR$" />
--- a/app/build.gradle	Tue Mar 23 22:30:34 2021 -0700
+++ b/app/build.gradle	Thu Mar 25 19:11:43 2021 -0700
@@ -43,7 +43,9 @@
     implementation 'com.google.android.material:material:1.3.0'
     implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
     implementation 'androidx.preference:preference-ktx:1.1.1'
-    testImplementation 'junit:junit:4.+'
+    testImplementation 'junit:junit:4.13.2'
+    androidTestImplementation 'androidx.test:runner:1.3.0'
+    androidTestImplementation 'androidx.test:rules:1.3.0'
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
 }
\ No newline at end of file
Binary file app/src/androidTest/assets/testimage.png has changed
--- /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