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