1 /*
<lambda>null2  * Copyright 2024 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.parity
18 
19 import platform.test.screenshot.matchers.MatchResult
20 import platform.test.screenshot.proto.ScreenshotResultProto.DiffResult.ComparisonStatistics
21 
22 /**
23  * A class which collects and reports statistics of screenshot test parity.
24  *
25  * The objects of this class collect screenshot test parity (in terms of percentage of different
26  * pixels between test and golden images), and report parity spectrum upon user's request.
27  *
28  * Screenshot test parity spectrum is interpreted as statements like:
29  * - X1 (Y1 percent of) test images match golden images.
30  * - X2 (Y2 percent of) test images are at least 99% the same as golden images.
31  * - X3 (Y3 percent of) test images are 95% - 99% the same as golden images.
32  * - ...
33  */
34 public class ParityStatsCollector {
35     private val testStats : HashMap<String, MutableList<String>>
36         = HashMap<String, MutableList<String>>()
37 
38     fun clear() {
39         testStats.clear()
40     }
41 
42     fun collectTestStats(testIdentifier: String, matchResult: MatchResult) {
43         if (matchResult.matches) {
44             val tmpList = testStats.getOrDefault(EXACTLY_SAME, mutableListOf<String>())
45             tmpList.add(testIdentifier)
46             testStats[EXACTLY_SAME] = tmpList
47         } else {
48             val numDiffPixels = matchResult.comparisonStatistics.numberPixelsDifferent
49             val numPixels = matchResult.comparisonStatistics.numberPixelsCompared
50             if (numDiffPixels < numPixels * 0.01) {
51                 val tmpList = testStats.getOrDefault(SAME99, mutableListOf<String>())
52                 tmpList.add(testIdentifier)
53                 testStats[SAME99] = tmpList
54             } else if (numDiffPixels < numPixels * 0.05) {
55                 val tmpList = testStats.getOrDefault(SAME95, mutableListOf<String>())
56                 tmpList.add(testIdentifier)
57                 testStats[SAME95] = tmpList
58             } else if (numDiffPixels < numPixels * 0.10) {
59                 val tmpList = testStats.getOrDefault(SAME90, mutableListOf<String>())
60                 tmpList.add(testIdentifier)
61                 testStats[SAME90] = tmpList
62             } else {
63                 val tmpList = testStats.getOrDefault(PIXEL_DIFFERENT, mutableListOf<String>())
64                 tmpList.add(testIdentifier)
65                 testStats[PIXEL_DIFFERENT] = tmpList
66             }
67         }
68     }
69 
70     fun report() {
71         testStats.forEach { entry ->
72             println("${entry.key} : ${entry.value.size} test(s).")
73         }
74         println("Tests with significant different pixel number: ${testStats[PIXEL_DIFFERENT]}")
75     }
76 
77     private companion object {
78         const val EXACTLY_SAME = "exactly_same"
79         const val SAME99 = "pixel_same99"
80         const val SAME95 = "pixel_same95"
81         const val SAME90 = "pixel_same90"
82         const val PIXEL_DIFFERENT = "pixel_different"
83     }
84 }
85