1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 // gtest
17 #include <gtest/gtest.h>
18 #include "dm_common.h"
19 #include "screen_manager.h"
20 using namespace testing;
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace Rosen {
25 class ScreenGamutTest : public testing::Test {
26 public:
27     static void SetUpTestCase();
28     static void TearDownTestCase();
29     virtual void SetUp() override;
30     virtual void TearDown() override;
31     static sptr<Screen> defaultScreen_;
32 };
33 
34 sptr<Screen> ScreenGamutTest::defaultScreen_ = nullptr;
35 
SetUpTestCase()36 void ScreenGamutTest::SetUpTestCase()
37 {
38     auto screens = ScreenManager::GetInstance().GetAllScreens();
39     if (screens.size() > 0) {
40         defaultScreen_ = screens[0];
41     }
42 }
43 
TearDownTestCase()44 void ScreenGamutTest::TearDownTestCase()
45 {
46     defaultScreen_ = nullptr;
47 }
48 
SetUp()49 void ScreenGamutTest::SetUp()
50 {
51 }
52 
TearDown()53 void ScreenGamutTest::TearDown()
54 {
55 }
56 
57 namespace {
58 /**
59  * @tc.name: GetScreenSupportedColorGamuts01
60  * @tc.desc: gamut GetScreenSupportedColorGamuts
61  * @tc.type: FUNC
62  */
63 HWTEST_F(ScreenGamutTest, GetScreenSupportedColorGamuts01, Function | MediumTest | Level3)
64 {
65     ASSERT_NE(defaultScreen_, nullptr);
66     DMError ret;
67     std::vector<ScreenColorGamut> colorGamuts;
68     ret = defaultScreen_->GetScreenSupportedColorGamuts(colorGamuts);
69     ASSERT_EQ(ret, DMError::DM_OK);
70     ASSERT_GT(colorGamuts.size(), 0);
71 }
72 
73 /**
74  * @tc.name: GetScreenColorGamut01
75  * @tc.desc: gamut GetScreenColorGamut
76  * @tc.type: FUNC
77  */
78 HWTEST_F(ScreenGamutTest, GetScreenColorGamut01, Function | MediumTest | Level3)
79 {
80     ASSERT_NE(defaultScreen_, nullptr);
81     DMError ret;
82     ScreenColorGamut colorGamut;
83     ret = defaultScreen_->GetScreenColorGamut(colorGamut);
84     ASSERT_EQ(ret, DMError::DM_OK);
85     ASSERT_NE(COLOR_GAMUT_INVALID, colorGamut);
86 }
87 
88 /**
89  * @tc.name: SetScreenColorGamut01
90  * @tc.desc: gamut SetScreenColorGamut, valid index
91  * @tc.type: FUNC
92  */
93 HWTEST_F(ScreenGamutTest, SetScreenColorGamut01, Function | MediumTest | Level3)
94 {
95     ASSERT_NE(defaultScreen_, nullptr);
96     DMError ret;
97     ScreenColorGamut colorGamutBackup;
98     int32_t colorGamutBackupIdx = -1;
99     ScreenColorGamut colorGamut;
100     std::vector<ScreenColorGamut> colorGamuts;
101 
102     ret = defaultScreen_->GetScreenColorGamut(colorGamutBackup); // backup origin
103     ASSERT_EQ(ret, DMError::DM_OK);
104 
105     ret = defaultScreen_->GetScreenSupportedColorGamuts(colorGamuts);
106     ASSERT_EQ(ret, DMError::DM_OK);
107 
108     for (int32_t i = 0; i < static_cast<int32_t>(colorGamuts.size()); i++) {
109         ret = defaultScreen_->SetScreenColorGamut(i);
110         ASSERT_EQ(ret, DMError::DM_OK);
111 
112         ret = defaultScreen_->GetScreenColorGamut(colorGamut);
113         ASSERT_EQ(ret, DMError::DM_OK);
114 
115 #ifdef SCREEN_GAMUT_SET_GET_OK
116         ASSERT_EQ(colorGamut, colorGamuts[i]);
117 #endif
118         if (colorGamutBackup == colorGamuts[i]) {
119             colorGamutBackupIdx = i;
120         }
121     }
122 
123     ASSERT_GE(colorGamutBackupIdx, 0);
124     ret = defaultScreen_->SetScreenColorGamut(colorGamutBackupIdx); // restore
125     ASSERT_EQ(ret, DMError::DM_OK);
126 }
127 
128 /**
129  * @tc.name: SetScreenColorGamut02
130  * @tc.desc: gamut SetScreenColorGamut, invalid index < 0
131  * @tc.type: FUNC
132  */
133 HWTEST_F(ScreenGamutTest, SetScreenColorGamut02, Function | MediumTest | Level3)
134 {
135     ASSERT_NE(defaultScreen_, nullptr);
136     DMError ret;
137     ScreenColorGamut colorGamutBefore;
138     ScreenColorGamut colorGamutAfter;
139 
140     ret = defaultScreen_->GetScreenColorGamut(colorGamutBefore);
141     ASSERT_EQ(ret, DMError::DM_OK);
142 
143     constexpr int32_t invalidColorGamutIndex = -1; // index < 0
144     ret = defaultScreen_->SetScreenColorGamut(invalidColorGamutIndex);
145     ASSERT_NE(ret, DMError::DM_OK);
146 
147     ret = defaultScreen_->GetScreenColorGamut(colorGamutAfter);
148     ASSERT_EQ(ret, DMError::DM_OK);
149 
150     ASSERT_EQ(colorGamutBefore, colorGamutAfter); // don't change colorgamut after invalid set
151 }
152 
153 /**
154  * @tc.name: SetScreenColorGamut03
155  * @tc.desc: gamut SetScreenColorGamut, invalid index >= size
156  * @tc.type: FUNC
157  */
158 HWTEST_F(ScreenGamutTest, SetScreenColorGamut03, Function | MediumTest | Level3)
159 {
160     ASSERT_NE(defaultScreen_, nullptr);
161     DMError ret;
162     ScreenColorGamut colorGamutBefore;
163     ScreenColorGamut colorGamutAfter;
164 
165     ret = defaultScreen_->GetScreenColorGamut(colorGamutBefore);
166     ASSERT_EQ(ret, DMError::DM_OK);
167 
168     std::vector<ScreenColorGamut> colorGamuts;
169     ret = defaultScreen_->GetScreenSupportedColorGamuts(colorGamuts);
170     ASSERT_EQ(ret, DMError::DM_OK);
171 
172     const int32_t invalidColorGamutIndex = static_cast<int32_t>(colorGamuts.size()); // index >= size
173     ret = defaultScreen_->SetScreenColorGamut(invalidColorGamutIndex);
174     ASSERT_NE(ret, DMError::DM_OK);
175 
176     ret = defaultScreen_->GetScreenColorGamut(colorGamutAfter);
177     ASSERT_EQ(ret, DMError::DM_OK);
178 
179     ASSERT_EQ(colorGamutBefore, colorGamutAfter); // don't change colorgamut after invalid set
180 }
181 
182 /**
183  * @tc.name: GetScreenGamutMap01
184  * @tc.desc: gamut GetScreenGamutMap
185  * @tc.type: FUNC
186  */
187 HWTEST_F(ScreenGamutTest, GetScreenGamutMap01, Function | MediumTest | Level3)
188 {
189     ASSERT_NE(defaultScreen_, nullptr);
190     DMError ret;
191     ScreenGamutMap gamutMap;
192 
193     ret = defaultScreen_->GetScreenGamutMap(gamutMap);
194     ASSERT_EQ(ret, DMError::DM_OK);
195 }
196 
197 /**
198  * @tc.name: SetScreenGamutMap01
199  * @tc.desc: gamut SetScreenGamutMap, valid param
200  * @tc.type: FUNC
201  */
202 HWTEST_F(ScreenGamutTest, SetScreenGamutMap01, Function | MediumTest | Level3)
203 {
204     ASSERT_NE(defaultScreen_, nullptr);
205     DMError ret;
206     const ScreenGamutMap gamutMaps[] = {
207         GAMUT_MAP_CONSTANT,
208         GAMUT_MAP_EXTENSION,
209         GAMUT_MAP_HDR_CONSTANT,
210         GAMUT_MAP_HDR_EXTENSION,
211     };
212     ScreenGamutMap gamutMap;
213     ScreenGamutMap gamutMapBackup;
214 
215     ret = defaultScreen_->GetScreenGamutMap(gamutMapBackup); // backup origin
216     ASSERT_EQ(ret, DMError::DM_OK);
217 
218     for (uint32_t i = 0; i < sizeof(gamutMaps) / sizeof(ScreenGamutMap); i++) {
219         ret = defaultScreen_->SetScreenGamutMap(gamutMaps[i]);
220         ASSERT_EQ(ret, DMError::DM_OK);
221 
222         ret = defaultScreen_->GetScreenGamutMap(gamutMap);
223         ASSERT_EQ(ret, DMError::DM_OK);
224 #ifdef SCREEN_GAMUT_SET_GET_OK
225         ASSERT_EQ(gamutMaps[i], gamutMap);
226 #endif
227     }
228 
229     ret = defaultScreen_->SetScreenGamutMap(gamutMapBackup); // restore
230     ASSERT_EQ(ret, DMError::DM_OK);
231 }
232 
233 /**
234  * @tc.name: SetScreenGamutMap02
235  * @tc.desc: gamut SetScreenGamutMap, invalid param
236  * @tc.type: FUNC
237  */
238 HWTEST_F(ScreenGamutTest, SetScreenGamutMap02, Function | MediumTest | Level3)
239 {
240     ASSERT_NE(defaultScreen_, nullptr);
241     DMError ret;
242     ScreenGamutMap gamutMap;
243     ScreenGamutMap gamutMapBefore;
244     ScreenGamutMap gamutMapAfter;
245 
246     ret = defaultScreen_->GetScreenGamutMap(gamutMapBefore);
247     ASSERT_EQ(ret, DMError::DM_OK);
248 
249     gamutMap = static_cast<ScreenGamutMap>(static_cast<uint32_t>(ScreenGamutMap::GAMUT_MAP_HDR_EXTENSION) + 1);
250     ret = defaultScreen_->SetScreenGamutMap(gamutMap);
251     ASSERT_NE(ret, DMError::DM_OK);
252 
253     ret = defaultScreen_->GetScreenGamutMap(gamutMapAfter);
254     ASSERT_EQ(ret, DMError::DM_OK);
255 
256     ASSERT_EQ(gamutMapBefore, gamutMapAfter);
257 }
258 
259 /**
260  * @tc.name: SetScreenColorTransform01
261  * @tc.desc: gamut SetScreenColorTransform
262  * @tc.type: FUNC
263  */
264 HWTEST_F(ScreenGamutTest, SetScreenColorTransform01, Function | MediumTest | Level3)
265 {
266     ASSERT_NE(defaultScreen_, nullptr);
267     DMError ret;
268 
269     ret = defaultScreen_->SetScreenColorTransform();
270     ASSERT_EQ(ret, DMError::DM_OK);
271 }
272 }
273 } // namespace Rosen
274 } // namespace OHOS
275