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
18 #include "display_cutout_controller.h"
19 #include "display_info.h"
20 #include "display_manager.h"
21 #include "display_manager_proxy.h"
22 #include "screen_manager.h"
23 #include "screen_manager/rs_screen_mode_info.h"
24 #include "window_manager_hilog.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace Rosen {
31 namespace {
32 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayChangeTest"};
33 constexpr uint32_t MAX_TIME_WAITING_FOR_CALLBACK = 20;
34 constexpr uint32_t SLEEP_TIME_IN_US = 10000; // 10ms
35 constexpr uint32_t SPLIT_TEST_SLEEP_S = 2;
36 }
37
38 class DisplayChangeEventListener : public DisplayManager::IDisplayListener {
39 public:
OnCreate(DisplayId displayId)40 virtual void OnCreate(DisplayId displayId)
41 {
42 WLOGI("DisplayChangeEventListener::OnCreate displayId=%{public}" PRIu64"", displayId);
43 }
44
OnDestroy(DisplayId displayId)45 virtual void OnDestroy(DisplayId displayId)
46 {
47 WLOGI("DisplayChangeEventListener::OnDestroy displayId=%{public}" PRIu64"", displayId);
48 }
49
OnChange(DisplayId displayId)50 virtual void OnChange(DisplayId displayId)
51 {
52 WLOGI("DisplayChangeEventListener::OnChange displayId=%{public}" PRIu64"", displayId);
53 isCallbackCalled_ = true;
54 displayId_ = displayId;
55 }
56 bool isCallbackCalled_ = false;
57 DisplayId displayId_ = DISPLAY_ID_INVALID;
58 };
59
60 class DisplayChangeTest : public testing::Test {
61 public:
62 static void SetUpTestCase();
63 static void TearDownTestCase();
64 virtual void SetUp() override;
65 virtual void TearDown() override;
66 void ResetDisplayChangeListener();
67 bool CheckDisplayChangeEventCallback(bool valueExpected);
68 bool ScreenSizeEqual(const sptr<Screen> screen, const sptr<SupportedScreenModes> curInfo) const;
69 bool DisplaySizeEqual(const sptr<Display> display, const sptr<SupportedScreenModes> curInfo) const;
70 inline bool CheckModeSizeChange(const sptr<SupportedScreenModes> usedInfo,
71 const sptr<SupportedScreenModes> curInfo) const;
72
73 static DisplayId defaultDisplayId_;
74 static sptr<Screen> defaultScreen_;
75 static sptr<DisplayChangeEventListener> listener_;
76 static uint32_t originalDisplayDpi;
77 static inline uint32_t times_ = 0;
78 };
79 DisplayId DisplayChangeTest::defaultDisplayId_ = DISPLAY_ID_INVALID;
80 sptr<Screen> DisplayChangeTest::defaultScreen_ = nullptr;
81 sptr<DisplayChangeEventListener> DisplayChangeTest::listener_ = new DisplayChangeEventListener();
82 uint32_t DisplayChangeTest::originalDisplayDpi = 0;
83
SetUpTestCase()84 void DisplayChangeTest::SetUpTestCase()
85 {
86 defaultDisplayId_ = DisplayManager::GetInstance().GetDefaultDisplayId();
87 ASSERT_NE(DISPLAY_ID_INVALID, defaultDisplayId_);
88 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDisplayById(defaultDisplayId_);
89 ASSERT_NE(nullptr, defaultDisplay);
90 ScreenId screenId = defaultDisplay->GetScreenId();
91 ASSERT_NE(INVALID_SCREEN_ID, screenId);
92 defaultScreen_ = ScreenManager::GetInstance().GetScreenById(screenId);
93 ASSERT_NE(nullptr, defaultScreen_);
94
95 ASSERT_EQ(DMError::DM_OK, DisplayManager::GetInstance().RegisterDisplayListener(listener_));
96 }
97
TearDownTestCase()98 void DisplayChangeTest::TearDownTestCase()
99 {
100 DisplayManager::GetInstance().UnregisterDisplayListener(listener_);
101 }
102
SetUp()103 void DisplayChangeTest::SetUp()
104 {
105 times_ = 0;
106 ResetDisplayChangeListener();
107 }
108
TearDown()109 void DisplayChangeTest::TearDown()
110 {
111 }
112
ResetDisplayChangeListener()113 void DisplayChangeTest::ResetDisplayChangeListener()
114 {
115 ASSERT_NE(nullptr, listener_);
116 listener_->isCallbackCalled_ = false;
117 listener_->displayId_ = DISPLAY_ID_INVALID;
118 }
119
CheckDisplayChangeEventCallback(bool valueExpected)120 bool DisplayChangeTest::CheckDisplayChangeEventCallback(bool valueExpected)
121 {
122 WLOGI("CheckDisplayChangeEventCallback in");
123 do {
124 if (listener_->isCallbackCalled_ == valueExpected) {
125 WLOGI("CheckDisplayChangeEventCallback: get valueExpected %{public}d for display %{public}" PRIu64"",
126 static_cast<int>(valueExpected), listener_->displayId_);
127 WLOGI("CheckDisplayChangeEventCallback: already wait times %{public}d", times_);
128 return true;
129 }
130 usleep(SLEEP_TIME_IN_US);
131 ++times_;
132 } while (times_ <= MAX_TIME_WAITING_FOR_CALLBACK);
133 WLOGI("CheckDisplayChangeEventCallback: cannot get valueExpected");
134 return false;
135 }
136
ScreenSizeEqual(const sptr<Screen> screen,const sptr<SupportedScreenModes> curInfo) const137 bool DisplayChangeTest::ScreenSizeEqual(const sptr<Screen> screen, const sptr<SupportedScreenModes> curInfo) const
138 {
139 if (screen == nullptr || curInfo == nullptr) {
140 WLOGI("param is nullptr");
141 return false;
142 }
143 uint32_t sWidth = screen->GetWidth();
144 uint32_t sHeight = screen->GetHeight();
145 WLOGI("ScreenSizeEqual: ScreenSize: %{public}u %{public}u, ActiveModeInfoSize: %{public}u %{public}u",
146 sWidth, sHeight, curInfo->width_, curInfo->height_);
147 return ((curInfo->width_ == sWidth) && (curInfo->height_ == sHeight));
148 }
149
DisplaySizeEqual(const sptr<Display> display,const sptr<SupportedScreenModes> curInfo) const150 bool DisplayChangeTest::DisplaySizeEqual(const sptr<Display> display, const sptr<SupportedScreenModes> curInfo) const
151 {
152 if (display == nullptr || curInfo == nullptr) {
153 WLOGI("param is nullptr");
154 return false;
155 }
156 uint32_t dWidth = static_cast<uint32_t>(display->GetWidth());
157 uint32_t dHeight = static_cast<uint32_t>(display->GetHeight());
158 WLOGI("DisplaySizeEqual:: DisplaySize: %{public}u %{public}u, ActiveModeInfoSize: %{public}u %{public}u",
159 dWidth, dHeight, curInfo->width_, curInfo->height_);
160 return ((curInfo->width_ == dWidth) && (curInfo->height_ == dHeight));
161 }
162
163
CheckModeSizeChange(const sptr<SupportedScreenModes> usedInfo,const sptr<SupportedScreenModes> curInfo) const164 inline bool DisplayChangeTest::CheckModeSizeChange(const sptr<SupportedScreenModes> usedInfo,
165 const sptr<SupportedScreenModes> curInfo) const
166 {
167 return (usedInfo->width_ != curInfo->width_ || usedInfo->height_ != curInfo->height_);
168 }
169
170 namespace {
171 /**
172 * @tc.name: RegisterDisplayChangeListener01
173 * @tc.desc: Register displayChangeListener with valid listener and check return true
174 * @tc.type: FUNC
175 */
176 HWTEST_F(DisplayChangeTest, RegisterDisplayChangeListener01, Function | SmallTest | Level2)
177 {
178 sptr<DisplayChangeEventListener> listener = new DisplayChangeEventListener();
179 DMError ret = DisplayManager::GetInstance().RegisterDisplayListener(listener);
180 ASSERT_EQ(DMError::DM_OK, ret);
181 }
182
183 /**
184 * @tc.name: RegisterDisplayChangeListener02
185 * @tc.desc: Register displayChangeListener with nullptr and check return false
186 * @tc.type: FUNC
187 */
188 HWTEST_F(DisplayChangeTest, RegisterDisplayChangeListener02, Function | SmallTest | Level2)
189 {
190 DMError ret = DisplayManager::GetInstance().RegisterDisplayListener(nullptr);
191 ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ret);
192 }
193
194 /**
195 * @tc.name: UnregisterDisplayChangeListener01
196 * @tc.desc: Unregister displayChangeListener with valid listener and check return true
197 * @tc.type: FUNC
198 */
199 HWTEST_F(DisplayChangeTest, UnregisterDisplayChangeListener01, Function | SmallTest | Level2)
200 {
201 sptr<DisplayChangeEventListener> listener = new DisplayChangeEventListener();
202 DisplayManager::GetInstance().RegisterDisplayListener(listener);
203 DMError ret = DisplayManager::GetInstance().UnregisterDisplayListener(listener);
204 ASSERT_EQ(DMError::DM_OK, ret);
205 }
206
207 /**
208 * @tc.name: UnregisterDisplayChangeListener02
209 * @tc.desc: Register displayChangeListener with nullptr and check return false
210 * @tc.type: FUNC
211 */
212 HWTEST_F(DisplayChangeTest, UnregisterDisplayChangeListener02, Function | SmallTest | Level2)
213 {
214 DMError ret = DisplayManager::GetInstance().UnregisterDisplayListener(nullptr);
215 ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ret);
216 }
217
218 /**
219 * @tc.name: UnregisterDisplayChangeListener03
220 * @tc.desc: Register displayChangeListener with invalid listener and check return false
221 * @tc.type: FUNC
222 */
223 HWTEST_F(DisplayChangeTest, UnregisterDisplayChangeListener03, Function | SmallTest | Level2)
224 {
225 sptr<DisplayChangeEventListener> listener = new DisplayChangeEventListener();
226 DMError ret = DisplayManager::GetInstance().UnregisterDisplayListener(listener);
227 ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ret);
228 }
229
230 /**
231 * @tc.name: CheckDisplayStateChange01
232 * @tc.desc: DisplayState not change if screen sets same mode
233 * @tc.type: FUNC
234 */
235 HWTEST_F(DisplayChangeTest, CheckDisplayStateChange01, Function | SmallTest | Level2)
236 {
237 WLOGI("CheckDisplayStateChange01");
238 uint32_t usedModeIdx = defaultScreen_->GetModeId();
239 defaultScreen_->SetScreenActiveMode(usedModeIdx);
240 WLOGI("SetScreenActiveMode: %{public}u", usedModeIdx);
241 if (CheckDisplayChangeEventCallback(true)) {
242 ASSERT_EQ(true, CheckDisplayChangeEventCallback(true));
243 }
244 }
245
246 /**
247 * @tc.name: CheckDisplayStateChange02
248 * @tc.desc: DisplayState changes if screen sets different mode
249 * @tc.type: FUNC
250 */
251 HWTEST_F(DisplayChangeTest, CheckDisplayStateChange02, Function | SmallTest | Level2)
252 {
253 WLOGI("CheckDisplayStateChange02");
254 auto modes = defaultScreen_->GetSupportedModes();
255 uint32_t usedModeIdx = defaultScreen_->GetModeId();
256 WLOGI("usedModeIdx / SupportMode size: %{public}u %{public}zu", usedModeIdx, modes.size());
257
258 for (uint32_t modeIdx = 0; modeIdx < modes.size(); modeIdx++) {
259 if (modeIdx != usedModeIdx && CheckModeSizeChange(modes[usedModeIdx], modes[modeIdx])) {
260 defaultScreen_->SetScreenActiveMode(modeIdx);
261 WLOGI("SetScreenActiveMode: %{public}u -> %{public}u", usedModeIdx, modeIdx);
262 ASSERT_EQ(true, CheckDisplayChangeEventCallback(true));
263 // reset usedMode
264 ResetDisplayChangeListener();
265 defaultScreen_->SetScreenActiveMode(usedModeIdx);
266 CheckDisplayChangeEventCallback(true);
267 break;
268 }
269 }
270 }
271
272 /**
273 * @tc.name: CheckDisplaySizeChange01
274 * @tc.desc: Check screen size change as screen mode set if screen sets another mode
275 * @tc.type: FUNC
276 */
277 HWTEST_F(DisplayChangeTest, CheckDisplaySizeChange01, Function | MediumTest | Level2)
278 {
279 WLOGI("CheckDisplaySizeChange01");
280 auto modes = defaultScreen_->GetSupportedModes();
281 uint32_t usedModeIdx = defaultScreen_->GetModeId();
282 WLOGI("usedModeIdx / SupportMode size: %{public}u %{public}zu", usedModeIdx, modes.size());
283
284 for (uint32_t modeIdx = 0; modeIdx < modes.size(); modeIdx++) {
285 if (modeIdx != usedModeIdx && CheckModeSizeChange(modes[usedModeIdx], modes[modeIdx])) {
286 defaultScreen_->SetScreenActiveMode(modeIdx);
287 WLOGI("SetScreenActiveMode: %{public}u -> %{public}u", usedModeIdx, modeIdx);
288 ASSERT_EQ(true, ScreenSizeEqual(defaultScreen_, modes[modeIdx]));
289 ASSERT_EQ(true, CheckDisplayChangeEventCallback(true));
290 // reset usedMode
291 ResetDisplayChangeListener();
292 defaultScreen_->SetScreenActiveMode(usedModeIdx);
293 CheckDisplayChangeEventCallback(true);
294 break;
295 }
296 }
297 }
298
299 /**
300 * @tc.name: CheckDisplaySizeChange02
301 * @tc.desc: Check display size change as screen mode set if screen sets another mode
302 * @tc.type: FUNC
303 */
304 HWTEST_F(DisplayChangeTest, CheckDisplaySizeChange02, Function | MediumTest | Level2)
305 {
306 WLOGI("CheckDisplaySizeChange02");
307 auto modes = defaultScreen_->GetSupportedModes();
308 uint32_t usedModeIdx = defaultScreen_->GetModeId();
309 WLOGI("usedModeIdx / SupportMode size: %{public}u %{public}zu", usedModeIdx, modes.size());
310
311 for (uint32_t modeIdx = 0; modeIdx < modes.size(); modeIdx++) {
312 if (modeIdx != usedModeIdx && CheckModeSizeChange(modes[usedModeIdx], modes[modeIdx])) {
313 defaultScreen_->SetScreenActiveMode(modeIdx);
314 WLOGI("SetScreenActiveMode: %{public}u -> %{public}u", usedModeIdx, modeIdx);
315 ASSERT_EQ(true, ScreenSizeEqual(defaultScreen_, modes[modeIdx]));
316 ASSERT_EQ(true, CheckDisplayChangeEventCallback(true));
317 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDisplayById(defaultDisplayId_);
318 ASSERT_NE(nullptr, defaultDisplay);
319 ASSERT_EQ(true, DisplaySizeEqual(defaultDisplay, modes[modeIdx]));
320 // reset usedMode
321 ResetDisplayChangeListener();
322 defaultScreen_->SetScreenActiveMode(usedModeIdx);
323 CheckDisplayChangeEventCallback(true);
324 break;
325 }
326 }
327 }
328
329 /**
330 * @tc.name: CheckScreenDensityChange01
331 * @tc.desc: Check screen density change as set another density for screen
332 * @tc.type: FUNC
333 */
334 HWTEST_F(DisplayChangeTest, CheckScreenDensityChange01, Function | SmallTest | Level2)
335 {
336 DisplayChangeTest::originalDisplayDpi = static_cast<uint32_t>(DisplayManager::GetInstance().
337 GetDisplayById(defaultDisplayId_)->GetVirtualPixelRatio() * BASELINE_DENSITY);
338 ASSERT_NE(0, DisplayChangeTest::originalDisplayDpi);
339 uint32_t densityDpi = 320;
340 ASSERT_EQ(DMError::DM_OK, defaultScreen_->SetDensityDpi(densityDpi));
341 sleep(SPLIT_TEST_SLEEP_S);
342 }
343
344 /**
345 * @tc.name: CheckScreenDensityChange02
346 * @tc.desc: Check screen density change as set another density for screen
347 * @tc.type: FUNC
348 */
349 HWTEST_F(DisplayChangeTest, CheckScreenDensityChange02, Function | SmallTest | Level2)
350 {
351 uint32_t densityDpi = 80;
352 ASSERT_EQ(DMError::DM_OK, defaultScreen_->SetDensityDpi(densityDpi));
353 sleep(SPLIT_TEST_SLEEP_S);
354 }
355
356 /**
357 * @tc.name: CheckScreenDensityChange03
358 * @tc.desc: Check screen density change as set an invalid density for screen
359 * @tc.type: FUNC
360 */
361 HWTEST_F(DisplayChangeTest, CheckScreenDensityChange03, Function | SmallTest | Level2)
362 {
363 uint32_t densityDpi = 700;
364 ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, defaultScreen_->SetDensityDpi(densityDpi));
365 }
366
367 /**
368 * @tc.name: CheckScreenDensityChange04
369 * @tc.desc: Check screen density change as set an invalid density for screen
370 * @tc.type: FUNC
371 */
372 HWTEST_F(DisplayChangeTest, CheckScreenDensityChange04, Function | SmallTest | Level2)
373 {
374 uint32_t densityDpi = 40;
375 ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, defaultScreen_->SetDensityDpi(densityDpi));
376 }
377
378 /**
379 * @tc.name: CheckScreenDensityChange05
380 * @tc.desc: Restore original display density
381 * @tc.type: FUNC
382 */
383 HWTEST_F(DisplayChangeTest, CheckScreenDensityChange05, Function | SmallTest | Level2)
384 {
385 ASSERT_EQ(DMError::DM_OK, defaultScreen_->SetDensityDpi(DisplayChangeTest::originalDisplayDpi));
386 sleep(SPLIT_TEST_SLEEP_S);
387 }
388
389 /**
390 * @tc.name: CheckWaterfallCompression01
391 * @tc.desc: check function of waterfall display compression.
392 * @tc.type: FUNC
393 */
394 HWTEST_F(DisplayChangeTest, CheckWaterfallCompression01, Function | SmallTest | Level2)
395 {
396 bool originWaterfallEnable = DisplayCutoutController::IsWaterfallDisplay();
397 DisplayCutoutController::SetIsWaterfallDisplay(true);
398
399 bool originStatus = DisplayCutoutController::IsWaterfallAreaCompressionEnableWhenHorizontal();
400 DisplayCutoutController::SetWaterfallAreaCompressionEnableWhenHorzontal(true);
401
402 uint32_t originSize = DisplayCutoutController::GetWaterfallAreaCompressionSizeWhenHorizontal();
403 uint32_t testSizeInVp = 24;
404 DisplayCutoutController::SetWaterfallAreaCompressionSizeWhenHorizontal(testSizeInVp);
405
406 ASSERT_EQ(true, DisplayCutoutController::IsWaterfallAreaCompressionEnableWhenHorizontal());
407 ASSERT_EQ(testSizeInVp, DisplayCutoutController::GetWaterfallAreaCompressionSizeWhenHorizontal());
408
409 Orientation originOrientation = defaultScreen_->GetOrientation();
410 DisplayCutoutController::SetWaterfallAreaCompressionSizeWhenHorizontal(originSize);
411 ASSERT_EQ(originSize, DisplayCutoutController::GetWaterfallAreaCompressionSizeWhenHorizontal());
412 DisplayCutoutController::SetWaterfallAreaCompressionEnableWhenHorzontal(originStatus);
413 ASSERT_EQ(originStatus, DisplayCutoutController::IsWaterfallAreaCompressionEnableWhenHorizontal());
414 DisplayCutoutController::SetIsWaterfallDisplay(originWaterfallEnable);
415 ASSERT_EQ(originOrientation, defaultScreen_->GetOrientation());
416 }
417 }
418 } // namespace Rosen
419 } // namespace OHOS