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