• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.matchers
18 
19 import android.graphics.Bitmap
20 import android.graphics.Rect
21 import kotlin.collections.List
22 import platform.test.screenshot.proto.ScreenshotResultProto.DiffResult.ComparisonStatistics
23 
24 /**
25  * The abstract class to implement to provide custom bitmap matchers.
26  */
27 abstract class BitmapMatcher {
28     /**
29      * Compares the given bitmaps and returns result of the operation.
30      *
31      * The images need to have same size.
32      *
33      * @param expected The reference / golden image.
34      * @param given The image taken during the test.
35      * @param width Width of both of the images.
36      * @param height Height of both of the images.
37      * @param regions An optional array of interesting regions for screenshot diff.
38      */
compareBitmapsnull39     abstract fun compareBitmaps(
40         expected: IntArray,
41         given: IntArray,
42         width: Int,
43         height: Int,
44         regions: List<Rect> = emptyList<Rect>()
45     ): MatchResult
46 
47     protected fun getFilter(width: Int, height: Int, regions: List<Rect>): IntArray {
48         if (regions.isEmpty()) { return IntArray(width * height) { 1 } }
49         val bitmapArray = IntArray(width * height) { 0 }
50         for (region in regions) {
51             for (i in region.top..region.bottom) {
52                 if (i >= height) { break }
53                 for (j in region.left..region.right) {
54                     if (j >= width) { break }
55                     bitmapArray[j + i * width] = 1
56                 }
57             }
58         }
59         return bitmapArray
60     }
61 }
62 
63 /**
64  * Result of the matching performed by [BitmapMatcher].
65  *
66  * @param matches True if bitmaps match.
67  * @param comparisonStatistics Matching statistics provided by this matcher that performed the
68  * comparison.
69  * @param diff Diff bitmap that highlights the differences between the images. Can be null if match
70  * was found.
71  */
72 class MatchResult(
73     val matches: Boolean,
74     val comparisonStatistics: ComparisonStatistics,
75     val diff: Bitmap?
76 )
77