xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/tests/unittests/DaltonizerTest.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright 2018 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 #include <gtest/gtest.h>
18 #include <math/mat4.h>
19 #include <cmath>
20 #include "Effects/Daltonizer.h"
21 
22 namespace android {
23 
24 class DaltonizerTest {
25 private:
26     Daltonizer& mDaltonizer;
27 
28 public:
DaltonizerTest(Daltonizer & daltonizer)29     DaltonizerTest(Daltonizer& daltonizer) : mDaltonizer(daltonizer) {}
30 
isDirty() const31     bool isDirty() const { return mDaltonizer.mDirty; }
32 
getLevel() const33     float getLevel() const { return mDaltonizer.mLevel; }
34 
getType() const35     ColorBlindnessType getType() const { return mDaltonizer.mType; }
36 };
37 
38 constexpr float TOLERANCE = 0.01f;
39 
isIdentityMatrix(mat4 & matrix)40 static bool isIdentityMatrix(mat4& matrix) {
41     for (size_t i = 0; i < 4; ++i) {
42         for (size_t j = 0; j < 4; ++j) {
43             if (i == j) {
44                 // Check diagonal elements
45                 if (std::fabs(matrix[i][j] - 1.0f) > TOLERANCE) {
46                     return false;
47                 }
48             } else {
49                 // Check off-diagonal elements
50                 if (std::fabs(matrix[i][j]) > TOLERANCE) {
51                     return false;
52                 }
53             }
54         }
55     }
56     return true;
57 }
58 
59 // Test Suite Name : DaltonizerTest, Test name: ConstructionDefaultValues
TEST(DaltonizerTest,ConstructionDefaultValues)60 TEST(DaltonizerTest, ConstructionDefaultValues) {
61     Daltonizer daltonizer;
62     DaltonizerTest test(daltonizer);
63 
64     EXPECT_EQ(test.getLevel(), 0.7f);
65     ASSERT_TRUE(test.isDirty());
66     EXPECT_EQ(test.getType(), ColorBlindnessType::None);
67     mat4 matrix = daltonizer();
68     ASSERT_TRUE(isIdentityMatrix(matrix));
69 }
70 
TEST(DaltonizerTest,NotDirtyAfterColorMatrixReturned)71 TEST(DaltonizerTest, NotDirtyAfterColorMatrixReturned) {
72     Daltonizer daltonizer;
73 
74     mat4 matrix = daltonizer();
75     DaltonizerTest test(daltonizer);
76 
77     ASSERT_FALSE(test.isDirty());
78     ASSERT_TRUE(isIdentityMatrix(matrix));
79 }
80 
TEST(DaltonizerTest,LevelOutOfRangeTooLowIgnored)81 TEST(DaltonizerTest, LevelOutOfRangeTooLowIgnored) {
82     Daltonizer daltonizer;
83     // Get matrix to reset isDirty == false.
84     mat4 matrix = daltonizer();
85 
86     daltonizer.setLevel(-1);
87     DaltonizerTest test(daltonizer);
88 
89     EXPECT_EQ(test.getLevel(), 0.7f);
90     ASSERT_FALSE(test.isDirty());
91 }
92 
TEST(DaltonizerTest,LevelOutOfRangeTooHighIgnored)93 TEST(DaltonizerTest, LevelOutOfRangeTooHighIgnored) {
94     Daltonizer daltonizer;
95     // Get matrix to reset isDirty == false.
96     mat4 matrix = daltonizer();
97 
98     daltonizer.setLevel(11);
99     DaltonizerTest test(daltonizer);
100 
101     EXPECT_EQ(test.getLevel(), 0.7f);
102     ASSERT_FALSE(test.isDirty());
103 }
104 
TEST(DaltonizerTest,ColorCorrectionMatrixNonIdentical)105 TEST(DaltonizerTest, ColorCorrectionMatrixNonIdentical) {
106     Daltonizer daltonizer;
107     daltonizer.setType(ColorBlindnessType::Protanomaly);
108     daltonizer.setMode(ColorBlindnessMode::Correction);
109 
110     mat4 matrix = daltonizer();
111 
112     ASSERT_FALSE(isIdentityMatrix(matrix));
113 }
114 
TEST(DaltonizerTest,LevelZeroColorMatrixEqIdentityMatrix)115 TEST(DaltonizerTest, LevelZeroColorMatrixEqIdentityMatrix) {
116     Daltonizer daltonizer;
117     daltonizer.setType(ColorBlindnessType::Protanomaly);
118     daltonizer.setMode(ColorBlindnessMode::Correction);
119     daltonizer.setLevel(0);
120 
121     mat4 matrix = daltonizer();
122 
123     ASSERT_TRUE(isIdentityMatrix(matrix));
124 }
125 
126 } /* namespace android */
127