1 /* 2 * Copyright 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package platform.test.screenshot 18 19 import androidx.test.ext.junit.runners.AndroidJUnit4 20 import androidx.test.filters.MediumTest 21 import platform.test.screenshot.matchers.MSSIMMatcher 22 import platform.test.screenshot.utils.loadBitmap 23 import com.google.common.truth.Truth.assertThat 24 import org.junit.Test 25 import org.junit.runner.RunWith 26 27 @RunWith(AndroidJUnit4::class) 28 @MediumTest 29 class MSSIMMatcherTest { 30 31 @Test performDiff_sameBitmapsnull32 fun performDiff_sameBitmaps() { 33 val first = loadBitmap("round_rect_gray") 34 val second = loadBitmap("round_rect_gray") 35 val filter = IntArray(first.width * first.height) { 1 } 36 37 val matcher = MSSIMMatcher() 38 val result = matcher.calculateSSIM( 39 first.toIntArray(), second.toIntArray(), 40 first.width, first.height, filter 41 ) 42 43 // Asserts that all compared pixels are categorized as "similar". 44 assertThat(result.SSIM).isEqualTo(1.0) 45 } 46 47 @Test performDiff_noPixelsComparednull48 fun performDiff_noPixelsCompared() { 49 val first = loadBitmap("checkbox_checked") 50 val second = loadBitmap("round_rect_gray") 51 val filter = IntArray(first.width * first.height) { 0 } 52 53 val matcher = MSSIMMatcher() 54 val result = matcher.calculateSSIM( 55 first.toIntArray(), second.toIntArray(), 56 first.width, first.height, filter 57 ) 58 59 assertThat(result.numPixelsCompared).isEqualTo(0) 60 } 61 62 @Test performDiff_sameRegionnull63 fun performDiff_sameRegion() { 64 val first = loadBitmap("qmc-folder1") 65 val second = loadBitmap("qmc-folder2") 66 val filter = IntArray(first.width * first.height) { 0 } 67 val startHeight = 18 * first.height / 20 68 val endHeight = 37 * first.height / 40 69 val startWidth = 10 * first.width / 20 70 val endWidth = 11 * first.width / 20 71 for (i in startHeight..endHeight) { 72 for (j in startWidth..endWidth) { 73 filter[j + i * first.width] = 1 74 } 75 } 76 77 val matcher = MSSIMMatcher() 78 val result = matcher.calculateSSIM( 79 first.toIntArray(), second.toIntArray(), 80 first.width, first.height, filter 81 ) 82 83 // Asserts that all compared pixels are categorized as "similar". 84 assertThat(result.SSIM).isEqualTo(1.0) 85 } 86 87 @Test performDiff_checkedAgainstUncheckednull88 fun performDiff_checkedAgainstUnchecked() { 89 val first = loadBitmap("checkbox_checked") 90 val second = loadBitmap("round_rect_gray") 91 val filter = IntArray(first.width * first.height) { 1 } 92 93 val matcher = MSSIMMatcher() 94 val result = matcher.calculateSSIM( 95 first.toIntArray(), second.toIntArray(), 96 first.width, first.height, filter 97 ) 98 99 assertThat(result.SSIM).isWithin(0.001).of(0.490) 100 } 101 102 @Test performDiff_differentBordersnull103 fun performDiff_differentBorders() { 104 val first = loadBitmap("round_rect_gray") 105 val second = loadBitmap("round_rect_green") 106 val filter = IntArray(first.width * first.height) { 1 } 107 108 val matcher = MSSIMMatcher() 109 val result = matcher.calculateSSIM( 110 first.toIntArray(), second.toIntArray(), 111 first.width, first.height, filter 112 ) 113 114 assertThat(result.SSIM).isWithin(0.001).of(0.951) 115 } 116 117 @Test performDiff_fullscreen_differentBorders_darknull118 fun performDiff_fullscreen_differentBorders_dark() { 119 val first = loadBitmap("fullscreen_rect_gray") 120 val second = loadBitmap("fullscreen_rect_gray_dark") 121 val filter = IntArray(first.width * first.height) { 1 } 122 123 val matcher = MSSIMMatcher() 124 val result = matcher.calculateSSIM( 125 first.toIntArray(), second.toIntArray(), 126 first.width, first.height, filter 127 ) 128 129 assertThat(result.SSIM).isWithin(0.001).of(0.990) 130 } 131 132 @Test performDiff_differentBorders_darknull133 fun performDiff_differentBorders_dark() { 134 val first = loadBitmap("round_rect_gray") 135 val second = loadBitmap("round_rect_gray_dark") 136 val filter = IntArray(first.width * first.height) { 1 } 137 138 val matcher = MSSIMMatcher() 139 val result = matcher.calculateSSIM( 140 first.toIntArray(), second.toIntArray(), 141 first.width, first.height, filter 142 ) 143 144 assertThat(result.SSIM).isWithin(0.001).of(0.960) 145 } 146 147 @Test performDiff_fullscreen_movedToRightnull148 fun performDiff_fullscreen_movedToRight() { 149 val first = loadBitmap("fullscreen_rect_gray") 150 val second = loadBitmap("fullscreen_rect_gray_moved_1px") 151 val filter = IntArray(first.width * first.height) { 1 } 152 153 val matcher = MSSIMMatcher() 154 val result = matcher.calculateSSIM( 155 first.toIntArray(), second.toIntArray(), 156 first.width, first.height, filter 157 ) 158 159 assertThat(result.SSIM).isWithin(0.001).of(0.695) 160 } 161 162 @Test performDiff_fullscreen_checkboxes_differentRadiusnull163 fun performDiff_fullscreen_checkboxes_differentRadius() { 164 val first = loadBitmap("fullscreen_checked_checkbox") 165 val second = loadBitmap("fullscreen_checked_checkbox_round") 166 val filter = IntArray(first.width * first.height) { 1 } 167 168 val matcher = MSSIMMatcher() 169 val result = matcher.calculateSSIM( 170 first.toIntArray(), second.toIntArray(), 171 first.width, first.height, filter 172 ) 173 174 assertThat(result.SSIM).isWithin(0.001).of(0.921) 175 } 176 } 177