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