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 #include <gtest/gtest.h>
17 #include "display_manager.h"
18 #include "display_manager_proxy.h"
19 #include "screen_manager.h"
20 #include "screen_manager_utils.h"
21 #include "mock_display_manager_adapter.h"
22 #include "singleton_mocker.h"
23 #include "scene_board_judgement.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace Rosen {
30 using Mocker = SingletonMocker<ScreenManagerAdapter, MockScreenManagerAdapter>;
31 class ScreenTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     virtual void SetUp() override;
36     virtual void TearDown() override;
37 
38     static sptr<Display> defaultDisplay_;
39     static ScreenId defaultScreenId_;
40     static sptr<Screen> screen_;
41 };
42 sptr<Display> ScreenTest::defaultDisplay_ = nullptr;
43 ScreenId ScreenTest::defaultScreenId_ = SCREEN_ID_INVALID;
44 sptr<Screen> ScreenTest::screen_ = nullptr;
45 
SetUpTestCase()46 void ScreenTest::SetUpTestCase()
47 {
48     defaultDisplay_ = DisplayManager::GetInstance().GetDefaultDisplay();
49     defaultScreenId_ = static_cast<ScreenId>(defaultDisplay_->GetId());
50     screen_ = ScreenManager::GetInstance().GetScreenById(defaultScreenId_);
51 }
52 
TearDownTestCase()53 void ScreenTest::TearDownTestCase()
54 {
55     defaultDisplay_ = nullptr;
56     screen_ = nullptr;
57 }
58 
SetUp()59 void ScreenTest::SetUp()
60 {
61 }
62 
TearDown()63 void ScreenTest::TearDown()
64 {
65 }
66 
67 namespace {
68 /**
69  * @tc.name: GetBasicProperty01
70  * @tc.desc: Basic property getter test
71  * @tc.type: FUNC
72  */
HWTEST_F(ScreenTest, GetBasicProperty01, Function | SmallTest | Level1)73 HWTEST_F(ScreenTest, GetBasicProperty01, Function | SmallTest | Level1)
74 {
75     ASSERT_GT(screen_->GetName().size(), 0);
76     ASSERT_GT(screen_->GetWidth(), 0);
77     ASSERT_GT(screen_->GetHeight(), 0);
78     ASSERT_GT(screen_->GetVirtualWidth(), 0);
79     ASSERT_GT(screen_->GetVirtualHeight(), 0);
80     ASSERT_GT(screen_->GetVirtualPixelRatio(), 0);
81     ASSERT_EQ(screen_->IsReal(), true);
82     ASSERT_NE(screen_->GetScreenInfo(), nullptr);
83 }
84 
85 /**
86  * @tc.name: SetScreenActiveMode01
87  * @tc.desc: SetScreenActiveMode with valid modeId and return success
88  * @tc.type: FUNC
89  */
HWTEST_F(ScreenTest, SetScreenActiveMode01, Function | SmallTest | Level1)90 HWTEST_F(ScreenTest, SetScreenActiveMode01, Function | SmallTest | Level1)
91 {
92     auto supportedModes = screen_->GetSupportedModes();
93     ASSERT_GT(supportedModes.size(), 0);
94     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
95     EXPECT_CALL(m->Mock(), SetScreenActiveMode(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
96     DMError res = screen_->SetScreenActiveMode(supportedModes.size() - 1);
97     ASSERT_EQ(DMError::DM_OK, res);
98 }
99 
100 /**
101  * @tc.name: SetScreenActiveMode02
102  * @tc.desc: SetScreenActiveMode with valid modeId and return failed
103  * @tc.type: FUNC
104  */
HWTEST_F(ScreenTest, SetScreenActiveMode02, Function | SmallTest | Level1)105 HWTEST_F(ScreenTest, SetScreenActiveMode02, Function | SmallTest | Level1)
106 {
107     auto supportedModes = screen_->GetSupportedModes();
108     ASSERT_GT(supportedModes.size(), 0);
109     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
110     EXPECT_CALL(m->Mock(), SetScreenActiveMode(_, _)).Times(1).WillOnce(Return(DMError::DM_ERROR_NULLPTR));
111     DMError res = screen_->SetScreenActiveMode(supportedModes.size() - 1);
112     ASSERT_TRUE(DMError::DM_OK != res);
113 }
114 
115 /**
116  * @tc.name: GetScreenSupportedColorGamuts01
117  * @tc.desc: GetScreenSupportedColorGamuts
118  * @tc.type: FUNC
119  */
HWTEST_F(ScreenTest, GetScreenSupportedColorGamuts01, Function | SmallTest | Level2)120 HWTEST_F(ScreenTest, GetScreenSupportedColorGamuts01, Function | SmallTest | Level2)
121 {
122     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
123     EXPECT_CALL(m->Mock(), GetScreenSupportedColorGamuts(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
124     std::vector<ScreenColorGamut> colorGamuts;
125     auto res = screen_->GetScreenSupportedColorGamuts(colorGamuts);
126     ASSERT_EQ(DMError::DM_OK, res);
127 }
128 
129 /**
130  * @tc.name: GetScreenColorGamut01
131  * @tc.desc: GetScreenColorGamut
132  * @tc.type: FUNC
133  */
HWTEST_F(ScreenTest, GetScreenColorGamut01, Function | SmallTest | Level2)134 HWTEST_F(ScreenTest, GetScreenColorGamut01, Function | SmallTest | Level2)
135 {
136     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
137     EXPECT_CALL(m->Mock(), GetScreenColorGamut(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
138     ScreenColorGamut colorGamut = ScreenColorGamut::COLOR_GAMUT_SRGB;
139     auto res = screen_->GetScreenColorGamut(colorGamut);
140     ASSERT_EQ(DMError::DM_OK, res);
141 }
142 
143 /**
144  * @tc.name: SetScreenColorGamut01
145  * @tc.desc: SetScreenColorGamut
146  * @tc.type: FUNC
147  */
HWTEST_F(ScreenTest, SetScreenColorGamut01, Function | SmallTest | Level2)148 HWTEST_F(ScreenTest, SetScreenColorGamut01, Function | SmallTest | Level2)
149 {
150     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
151     EXPECT_CALL(m->Mock(), SetScreenColorGamut(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
152     ScreenColorGamut colorGamut = ScreenColorGamut::COLOR_GAMUT_SRGB;
153     auto res = screen_->SetScreenColorGamut(colorGamut);
154     ASSERT_EQ(DMError::DM_OK, res);
155 }
156 
157 /**
158  * @tc.name: GetScreenGamutMap01
159  * @tc.desc: GetScreenGamutMap
160  * @tc.type: FUNC
161  */
HWTEST_F(ScreenTest, GetScreenGamutMap01, Function | SmallTest | Level2)162 HWTEST_F(ScreenTest, GetScreenGamutMap01, Function | SmallTest | Level2)
163 {
164     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
165     EXPECT_CALL(m->Mock(), GetScreenGamutMap(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
166     ScreenGamutMap gamutMap = ScreenGamutMap::GAMUT_MAP_CONSTANT;
167     auto res = screen_->GetScreenGamutMap(gamutMap);
168     ASSERT_EQ(DMError::DM_OK, res);
169 }
170 
171 /**
172  * @tc.name: SetScreenGamutMap01
173  * @tc.desc: SetScreenGamutMap
174  * @tc.type: FUNC
175  */
HWTEST_F(ScreenTest, SetScreenGamutMap01, Function | SmallTest | Level2)176 HWTEST_F(ScreenTest, SetScreenGamutMap01, Function | SmallTest | Level2)
177 {
178     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
179     EXPECT_CALL(m->Mock(), SetScreenGamutMap(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
180     ScreenGamutMap gamutMap = ScreenGamutMap::GAMUT_MAP_CONSTANT;
181     auto res = screen_->SetScreenGamutMap(gamutMap);
182     ASSERT_EQ(DMError::DM_OK, res);
183 }
184 
185 /**
186  * @tc.name: SetScreenColorTransform01
187  * @tc.desc: SetScreenColorTransform
188  * @tc.type: FUNC
189  */
HWTEST_F(ScreenTest, SetScreenColorTransform01, Function | SmallTest | Level2)190 HWTEST_F(ScreenTest, SetScreenColorTransform01, Function | SmallTest | Level2)
191 {
192     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
193     EXPECT_CALL(m->Mock(), SetScreenColorTransform(_)).Times(1).WillOnce(Return(DMError::DM_OK));
194     auto res = screen_->SetScreenColorTransform();
195     ASSERT_EQ(DMError::DM_OK, res);
196 }
197 
198 /**
199  * @tc.name: IsGroup
200  * @tc.desc: for interface coverage and check IsGroup
201  * @tc.type: FUNC
202  */
HWTEST_F(ScreenTest, IsGroup, Function | SmallTest | Level2)203 HWTEST_F(ScreenTest, IsGroup, Function | SmallTest | Level2)
204 {
205     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
206     sptr<ScreenInfo> screenInfo = screen_->GetScreenInfo();
207     screenInfo->SetIsScreenGroup(true);
208     EXPECT_CALL(m->Mock(), GetScreenInfo(_)).Times(1).WillOnce(Return(screenInfo));
209     ASSERT_EQ(true, screen_->IsGroup());
210     screenInfo->SetIsScreenGroup(false);
211     EXPECT_CALL(m->Mock(), GetScreenInfo(_)).Times(1).WillOnce(Return(screenInfo));
212     ASSERT_EQ(false, screen_->IsGroup());
213 }
214 
215 /**
216  * @tc.name: GetParentId
217  * @tc.desc: for interface coverage and check GetParentId
218  * @tc.type: FUNC
219  */
HWTEST_F(ScreenTest, GetParentId, Function | SmallTest | Level2)220 HWTEST_F(ScreenTest, GetParentId, Function | SmallTest | Level2)
221 {
222     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
223     sptr<ScreenInfo> screenInfo = screen_->GetScreenInfo();
224     screenInfo->SetParentId(0);
225     EXPECT_CALL(m->Mock(), GetScreenInfo(_)).Times(1).WillOnce(Return(screenInfo));
226     ASSERT_EQ(0, screen_->GetParentId());
227     screenInfo->SetParentId(SCREEN_ID_INVALID);
228     EXPECT_CALL(m->Mock(), GetScreenInfo(_)).Times(1).WillOnce(Return(screenInfo));
229     ASSERT_EQ(SCREEN_ID_INVALID, screen_->GetParentId());
230 }
231 
232 /**
233  * @tc.name: GetRotation
234  * @tc.desc: for interface coverage and check GetRotation
235  * @tc.type: FUNC
236  */
HWTEST_F(ScreenTest, GetRotation, Function | SmallTest | Level2)237 HWTEST_F(ScreenTest, GetRotation, Function | SmallTest | Level2)
238 {
239     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
240     sptr<ScreenInfo> screenInfo = screen_->GetScreenInfo();
241     screenInfo->SetParentId(0);
242     EXPECT_CALL(m->Mock(), GetScreenInfo(_)).Times(1).WillOnce(Return(screenInfo));
243     ASSERT_EQ(Rotation::ROTATION_0, screen_->GetRotation());
244 }
245 
246 /**
247  * @tc.name: GetOrientation
248  * @tc.desc: for interface coverage and check GetOrientation
249  * @tc.type: FUNC
250  */
HWTEST_F(ScreenTest, GetOrientation, Function | SmallTest | Level2)251 HWTEST_F(ScreenTest, GetOrientation, Function | SmallTest | Level2)
252 {
253     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
254     sptr<ScreenInfo> screenInfo = screen_->GetScreenInfo();
255     screenInfo->SetParentId(0);
256     EXPECT_CALL(m->Mock(), GetScreenInfo(_)).Times(1).WillOnce(Return(screenInfo));
257     ASSERT_EQ(Orientation::BEGIN, screen_->GetOrientation());
258 }
259 
260 /**
261  * @tc.name: SetOrientation
262  * @tc.desc: SetOrientation
263  * @tc.type: FUNC
264  */
HWTEST_F(ScreenTest, SetOrientation, Function | SmallTest | Level2)265 HWTEST_F(ScreenTest, SetOrientation, Function | SmallTest | Level2)
266 {
267     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
268     EXPECT_CALL(m->Mock(), SetOrientation(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
269     Orientation orientation = Orientation{0};
270     auto res = screen_->SetOrientation(orientation);
271     ASSERT_EQ(DMError::DM_OK, res);
272 }
273 
274 /**
275  * @tc.name: GetPixelFormat
276  * @tc.desc: GetPixelFormat
277  * @tc.type: FUNC
278  */
HWTEST_F(ScreenTest, GetPixelFormat, Function | SmallTest | Level2)279 HWTEST_F(ScreenTest, GetPixelFormat, Function | SmallTest | Level2)
280 {
281     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
282     EXPECT_CALL(m->Mock(), GetPixelFormat(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
283     GraphicPixelFormat pixelFormat = GraphicPixelFormat{GRAPHIC_PIXEL_FMT_CLUT8};
284     auto res = screen_->GetPixelFormat(pixelFormat);
285     ASSERT_EQ(DMError::DM_OK, res);
286 }
287 
288 /**
289  * @tc.name: SetPixelFormat
290  * @tc.desc: SetPixelFormat
291  * @tc.type: FUNC
292  */
HWTEST_F(ScreenTest, SetPixelFormat, Function | SmallTest | Level2)293 HWTEST_F(ScreenTest, SetPixelFormat, Function | SmallTest | Level2)
294 {
295     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
296     EXPECT_CALL(m->Mock(), SetPixelFormat(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
297     GraphicPixelFormat pixelFormat = GraphicPixelFormat{GRAPHIC_PIXEL_FMT_CLUT8};
298     auto res = screen_->SetPixelFormat(pixelFormat);
299     ASSERT_EQ(DMError::DM_OK, res);
300 }
301 
302 /**
303  * @tc.name: GetSupportedHDRFormats
304  * @tc.desc: GetSupportedHDRFormats
305  * @tc.type: FUNC
306  */
HWTEST_F(ScreenTest, GetSupportedHDRFormats, Function | SmallTest | Level2)307 HWTEST_F(ScreenTest, GetSupportedHDRFormats, Function | SmallTest | Level2)
308 {
309     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
310     EXPECT_CALL(m->Mock(), GetSupportedHDRFormats(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
311     std::vector<ScreenHDRFormat> hdrFormats;
312     auto res = screen_->GetSupportedHDRFormats(hdrFormats);
313     ASSERT_EQ(DMError::DM_OK, res);
314 }
315 
316 /**
317  * @tc.name: GetScreenHDRFormat
318  * @tc.desc: GetScreenHDRFormat
319  * @tc.type: FUNC
320  */
HWTEST_F(ScreenTest, GetScreenHDRFormat, Function | SmallTest | Level2)321 HWTEST_F(ScreenTest, GetScreenHDRFormat, Function | SmallTest | Level2)
322 {
323     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
324     EXPECT_CALL(m->Mock(), GetScreenHDRFormat(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
325     ScreenHDRFormat hdrFormat = ScreenHDRFormat{0};
326     auto res = screen_->GetScreenHDRFormat(hdrFormat);
327     ASSERT_EQ(DMError::DM_OK, res);
328 }
329 
330 /**
331  * @tc.name: SetScreenHDRFormat
332  * @tc.desc: SetScreenHDRFormat
333  * @tc.type: FUNC
334  */
HWTEST_F(ScreenTest, SetScreenHDRFormat, Function | SmallTest | Level2)335 HWTEST_F(ScreenTest, SetScreenHDRFormat, Function | SmallTest | Level2)
336 {
337     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
338     EXPECT_CALL(m->Mock(), SetScreenHDRFormat(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
339     auto res = screen_->SetScreenHDRFormat(0);
340     ASSERT_EQ(DMError::DM_OK, res);
341 }
342 
343 /**
344  * @tc.name: GetSupportedColorSpaces
345  * @tc.desc: GetSupportedColorSpaces
346  * @tc.type: FUNC
347  */
HWTEST_F(ScreenTest, GetSupportedColorSpaces, Function | SmallTest | Level2)348 HWTEST_F(ScreenTest, GetSupportedColorSpaces, Function | SmallTest | Level2)
349 {
350     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
351     EXPECT_CALL(m->Mock(), GetSupportedColorSpaces(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
352     std::vector<GraphicCM_ColorSpaceType> colorSpaces;
353     auto res = screen_->GetSupportedColorSpaces(colorSpaces);
354     ASSERT_EQ(DMError::DM_OK, res);
355 }
356 
357 /**
358  * @tc.name: GetScreenColorSpace
359  * @tc.desc: GetScreenColorSpace
360  * @tc.type: FUNC
361  */
HWTEST_F(ScreenTest, GetScGetScreenColorSpacereenHDRFormat, Function | SmallTest | Level2)362 HWTEST_F(ScreenTest, GetScGetScreenColorSpacereenHDRFormat, Function | SmallTest | Level2)
363 {
364     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
365     EXPECT_CALL(m->Mock(), GetScreenColorSpace(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
366     GraphicCM_ColorSpaceType colorSpace = GraphicCM_ColorSpaceType{GRAPHIC_CM_COLORSPACE_NONE};
367     auto res = screen_->GetScreenColorSpace(colorSpace);
368     ASSERT_EQ(DMError::DM_OK, res);
369 }
370 
371 /**
372  * @tc.name: SetScreenColorSpace
373  * @tc.desc: SetScreenColorSpace
374  * @tc.type: FUNC
375  */
HWTEST_F(ScreenTest, SetScreenColorSpace, Function | SmallTest | Level2)376 HWTEST_F(ScreenTest, SetScreenColorSpace, Function | SmallTest | Level2)
377 {
378     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
379     EXPECT_CALL(m->Mock(), SetScreenColorSpace(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
380     GraphicCM_ColorSpaceType colorSpace = GraphicCM_ColorSpaceType{GRAPHIC_CM_COLORSPACE_NONE};
381     auto res = screen_->SetScreenColorSpace(colorSpace);
382     ASSERT_EQ(DMError::DM_OK, res);
383 }
384 
385 /**
386  * @tc.name: SetDensityDpi01
387  * @tc.desc: SetDensityDpi
388  * @tc.type: FUNC
389  */
HWTEST_F(ScreenTest, SetDensityDpi, Function | SmallTest | Level2)390 HWTEST_F(ScreenTest, SetDensityDpi, Function | SmallTest | Level2)
391 {
392     auto res = screen_->SetDensityDpi(DOT_PER_INCH_MAXIMUM_VALUE + 1);
393     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, res);
394 
395     res = screen_->SetDensityDpi(100);
396     ASSERT_EQ(DMError::DM_OK, res);
397 }
398 
399 /**
400  * @tc.name: SetResolution
401  * @tc.desc: SetResolution
402  * @tc.type: FUNC
403  */
HWTEST_F(ScreenTest, SetResolution, Function | SmallTest | Level2)404 HWTEST_F(ScreenTest, SetResolution, Function | SmallTest | Level2)
405 {
406     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
407     EXPECT_CALL(m->Mock(), SetResolution(_, _, _, _)).Times(1).WillOnce(Return(DMError::DM_OK));
408     auto res = screen_->SetResolution(0, 0, 1000);
409     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, res);
410 
411     res = screen_->SetResolution(1, 1, 100);
412     ASSERT_EQ(DMError::DM_OK, res);
413 }
414 
415 /**
416  * @tc.name: SetDensityDpiSystem01
417  * @tc.desc: SetDensityDpiSystem
418  * @tc.type: FUNC
419  */
HWTEST_F(ScreenTest, SetDensityDpiSystem, Function | SmallTest | Level2)420 HWTEST_F(ScreenTest, SetDensityDpiSystem, Function | SmallTest | Level2)
421 {
422     auto res = screen_->SetDensityDpiSystem(DOT_PER_INCH_MAXIMUM_VALUE + 1);
423     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, res);
424 
425     res = screen_->SetDensityDpiSystem(100);
426     if (SceneBoardJudgement::IsSceneBoardEnabled()) {
427         ASSERT_EQ(DMError::DM_OK, res);
428     } else {
429         ASSERT_NE(DMError::DM_OK, res);
430     }
431 }
432 
433 /**
434  * @tc.name: GetDensityInCurResolution
435  * @tc.desc: GetDensityInCurResolution
436  * @tc.type: FUNC
437  */
HWTEST_F(ScreenTest, GetDensityInCurResolution, Function | SmallTest | Level2)438 HWTEST_F(ScreenTest, GetDensityInCurResolution, Function | SmallTest | Level2)
439 {
440     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
441     EXPECT_CALL(m->Mock(), GetDensityInCurResolution(_, _)).Times(1).WillOnce(Return(DMError::DM_OK));
442     float virtualPixelRatio;
443     auto res = screen_->GetDensityInCurResolution(virtualPixelRatio);
444     ASSERT_EQ(DMError::DM_OK, res);
445 }
446 }
447 } // namespace Rosen
448 } // namespace OHOS