annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 package com.bartsent.simpleresizer
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
2
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 import android.graphics.Bitmap
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 import android.graphics.BitmapFactory
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
5 import android.graphics.Color
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import androidx.test.ext.junit.runners.AndroidJUnit4
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import androidx.test.platform.app.InstrumentationRegistry
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import com.bartsent.simpleresizer.lib.getScaledInstance
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import org.junit.Assert
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 import org.junit.Test
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
11 import org.junit.runner.RunWith
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
12
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 @RunWith(AndroidJUnit4::class)
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 class ResizerTest {
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 val ORIG_TILE_SIZE = 576
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 val WIDTH_IN_TILES = 8
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 val HEIGHT_IN_TILES = 6
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 val CSTEP = 0x55
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
19
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 var count = 0
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
21
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 @Test
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 fun canLoadImage() {
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 val image = getBitmap()
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 Assert.assertNotNull(image)
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 checkBitmap(image, WIDTH_IN_TILES * ORIG_TILE_SIZE, HEIGHT_IN_TILES * ORIG_TILE_SIZE)
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 }
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
28
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 @Test
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 fun resizeVertical() {
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 val image = getBitmap()
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 val newWidth = image.width
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 val newHeight = image.height / 2
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 checkBitmap(image.getScaledInstance(newWidth, newHeight), newWidth, newHeight)
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 }
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
36
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 @Test
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 fun resizeHorizontal() {
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
39 val image = getBitmap()
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 val newWidth = image.width / 2
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 val newHeight = image.height
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 checkBitmap(image.getScaledInstance(newWidth, newHeight), newWidth, newHeight)
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 }
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
44
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 @Test
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 fun resizeBoth() {
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 val image = getBitmap()
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 val newWidth = image.width / 2
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 val newHeight = image.height / 2
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 checkBitmap(image.getScaledInstance(newWidth, newHeight), newWidth, newHeight)
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 }
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
52
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 fun getBitmap(): Bitmap =
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 InstrumentationRegistry.getInstrumentation().context.assets.open("testimage.png").use {
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 BitmapFactory.decodeStream(it)
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 }
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
57
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 fun getColor(): Int {
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 count += 1
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 val b = (count and 0x3)*CSTEP
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 val temp = count shr 2
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 val g = (temp and 0x3)*CSTEP
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 val r = ((temp shr 2) and 0x3)*CSTEP
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 return Color.rgb(r, g, b)
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 }
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
66
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 fun resetColor() {
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 count = 0
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 }
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
70
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 fun checkBitmap(image: Bitmap, width: Int, height: Int) {
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 Assert.assertEquals("width", width, image.width)
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 Assert.assertEquals("height", height, image.height)
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 val tileWidth = image.width / WIDTH_IN_TILES
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 val tileHeight = image.height / HEIGHT_IN_TILES
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 val tw2 = tileWidth / 2
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 val th2 = tileHeight / 2
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 resetColor()
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 for (x in 0 until WIDTH_IN_TILES) {
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 val xoff = tw2 + x * tileWidth
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 for (y in 0 until HEIGHT_IN_TILES) {
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 val yoff = th2 + y * tileHeight
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 Assert.assertEquals("color @ tile $x, $y", getColor(), image.getPixel(xoff, yoff))
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 }
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 }
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 }
444fe4416c9b Add unit tests of my resizing code.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 }