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