• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ASSERT_EQ(false, CheckDisplayChangeEventCallback(true));
242 }
243 
244 /**
245  * @tc.name: CheckDisplayStateChange02
246  * @tc.desc: DisplayState changes if screen sets different mode
247  * @tc.type: FUNC
248  */
249 HWTEST_F(DisplayChangeTest, CheckDisplayStateChange02, Function | SmallTest | Level2)
250 {
251     WLOGI("CheckDisplayStateChange02");
252     auto modes = defaultScreen_->GetSupportedModes();
253     uint32_t usedModeIdx = defaultScreen_->GetModeId();
254     WLOGI("usedModeIdx / SupportMode size: %{public}u %{public}zu", usedModeIdx, modes.size());
255 
256     for (uint32_t modeIdx = 0; modeIdx < modes.size(); modeIdx++) {
257         if (modeIdx != usedModeIdx && CheckModeSizeChange(modes[usedModeIdx], modes[modeIdx])) {
258             defaultScreen_->SetScreenActiveMode(modeIdx);
259             WLOGI("SetScreenActiveMode: %{public}u -> %{public}u", usedModeIdx, modeIdx);
260             ASSERT_EQ(true, CheckDisplayChangeEventCallback(true));
261             // reset usedMode
262             ResetDisplayChangeListener();
263             defaultScreen_->SetScreenActiveMode(usedModeIdx);
264             CheckDisplayChangeEventCallback(true);
265             break;
266         }
267     }
268 }
269 
270 /**
271  * @tc.name: CheckDisplaySizeChange01
272  * @tc.desc: Check screen size change as screen mode set if screen sets another mode
273  * @tc.type: FUNC
274  */
275 HWTEST_F(DisplayChangeTest, CheckDisplaySizeChange01, Function | MediumTest | Level2)
276 {
277     WLOGI("CheckDisplaySizeChange01");
278     auto modes = defaultScreen_->GetSupportedModes();
279     uint32_t usedModeIdx = defaultScreen_->GetModeId();
280     WLOGI("usedModeIdx / SupportMode size: %{public}u %{public}zu", usedModeIdx, modes.size());
281 
282     for (uint32_t modeIdx = 0; modeIdx < modes.size(); modeIdx++) {
283         if (modeIdx != usedModeIdx && CheckModeSizeChange(modes[usedModeIdx], modes[modeIdx])) {
284             defaultScreen_->SetScreenActiveMode(modeIdx);
285             WLOGI("SetScreenActiveMode: %{public}u -> %{public}u", usedModeIdx, modeIdx);
286             ASSERT_EQ(true, ScreenSizeEqual(defaultScreen_, modes[modeIdx]));
287             ASSERT_EQ(true, CheckDisplayChangeEventCallback(true));
288             // reset usedMode
289             ResetDisplayChangeListener();
290             defaultScreen_->SetScreenActiveMode(usedModeIdx);
291             CheckDisplayChangeEventCallback(true);
292             break;
293         }
294     }
295 }
296 
297 /**
298  * @tc.name: CheckDisplaySizeChange02
299  * @tc.desc: Check display size change as screen mode set if screen sets another mode
300  * @tc.type: FUNC
301  */
302 HWTEST_F(DisplayChangeTest, CheckDisplaySizeChange02, Function | MediumTest | Level2)
303 {
304     WLOGI("CheckDisplaySizeChange02");
305     auto modes = defaultScreen_->GetSupportedModes();
306     uint32_t usedModeIdx = defaultScreen_->GetModeId();
307     WLOGI("usedModeIdx / SupportMode size: %{public}u %{public}zu", usedModeIdx, modes.size());
308 
309     for (uint32_t modeIdx = 0; modeIdx < modes.size(); modeIdx++) {
310         if (modeIdx != usedModeIdx && CheckModeSizeChange(modes[usedModeIdx], modes[modeIdx])) {
311             defaultScreen_->SetScreenActiveMode(modeIdx);
312             WLOGI("SetScreenActiveMode: %{public}u -> %{public}u", usedModeIdx, modeIdx);
313             ASSERT_EQ(true, ScreenSizeEqual(defaultScreen_, modes[modeIdx]));
314             ASSERT_EQ(true, CheckDisplayChangeEventCallback(true));
315             sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDisplayById(defaultDisplayId_);
316             ASSERT_NE(nullptr, defaultDisplay);
317             ASSERT_EQ(true, DisplaySizeEqual(defaultDisplay, modes[modeIdx]));
318             // reset usedMode
319             ResetDisplayChangeListener();
320             defaultScreen_->SetScreenActiveMode(usedModeIdx);
321             CheckDisplayChangeEventCallback(true);
322             break;
323         }
324     }
325 }
326 
327 /**
328  * @tc.name: CheckScreenDensityChange01
329  * @tc.desc: Check screen density change as set another density for screen
330  * @tc.type: FUNC
331  */
332 HWTEST_F(DisplayChangeTest, CheckScreenDensityChange01, Function | SmallTest | Level2)
333 {
334     DisplayChangeTest::originalDisplayDpi = static_cast<uint32_t>(DisplayManager::GetInstance().
335         GetDisplayById(defaultDisplayId_)->GetVirtualPixelRatio() * BASELINE_DENSITY);
336     ASSERT_NE(0, DisplayChangeTest::originalDisplayDpi);
337     uint32_t densityDpi = 320;
338     ASSERT_EQ(DMError::DM_OK, defaultScreen_->SetDensityDpi(densityDpi));
339     sleep(SPLIT_TEST_SLEEP_S);
340 }
341 
342 /**
343  * @tc.name: CheckScreenDensityChange02
344  * @tc.desc: Check screen density change as set another density for screen
345  * @tc.type: FUNC
346  */
347 HWTEST_F(DisplayChangeTest, CheckScreenDensityChange02, Function | SmallTest | Level2)
348 {
349     uint32_t densityDpi = 80;
350     ASSERT_EQ(DMError::DM_OK, defaultScreen_->SetDensityDpi(densityDpi));
351     sleep(SPLIT_TEST_SLEEP_S);
352 }
353 
354 /**
355  * @tc.name: CheckScreenDensityChange03
356  * @tc.desc: Check screen density change as set an invalid density for screen
357  * @tc.type: FUNC
358  */
359 HWTEST_F(DisplayChangeTest, CheckScreenDensityChange03, Function | SmallTest | Level2)
360 {
361     uint32_t densityDpi = 700;
362     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, defaultScreen_->SetDensityDpi(densityDpi));
363 }
364 
365 /**
366  * @tc.name: CheckScreenDensityChange04
367  * @tc.desc: Check screen density change as set an invalid density for screen
368  * @tc.type: FUNC
369  */
370 HWTEST_F(DisplayChangeTest, CheckScreenDensityChange04, Function | SmallTest | Level2)
371 {
372     uint32_t densityDpi = 40;
373     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, defaultScreen_->SetDensityDpi(densityDpi));
374 }
375 
376 /**
377  * @tc.name: CheckScreenDensityChange05
378  * @tc.desc: Restore original display density
379  * @tc.type: FUNC
380  */
381 HWTEST_F(DisplayChangeTest, CheckScreenDensityChange05, Function | SmallTest | Level2)
382 {
383     ASSERT_EQ(DMError::DM_OK, defaultScreen_->SetDensityDpi(DisplayChangeTest::originalDisplayDpi));
384     sleep(SPLIT_TEST_SLEEP_S);
385 }
386 
387 /**
388  * @tc.name: CheckWaterfallCompression01
389  * @tc.desc: check function of waterfall display compression.
390  * @tc.type: FUNC
391  */
392 HWTEST_F(DisplayChangeTest, CheckWaterfallCompression01, Function | SmallTest | Level2)
393 {
394     bool originWaterfallEnable = DisplayCutoutController::IsWaterfallDisplay();
395     DisplayCutoutController::SetIsWaterfallDisplay(true);
396 
397     bool originStatus = DisplayCutoutController::IsWaterfallAreaCompressionEnableWhenHorizontal();
398     DisplayCutoutController::SetWaterfallAreaCompressionEnableWhenHorzontal(true);
399 
400     uint32_t originSize = DisplayCutoutController::GetWaterfallAreaCompressionSizeWhenHorizontal();
401     uint32_t testSizeInVp = 24;
402     DisplayCutoutController::SetWaterfallAreaCompressionSizeWhenHorizontal(testSizeInVp);
403 
404     ASSERT_EQ(true, DisplayCutoutController::IsWaterfallAreaCompressionEnableWhenHorizontal());
405     ASSERT_EQ(testSizeInVp, DisplayCutoutController::GetWaterfallAreaCompressionSizeWhenHorizontal());
406 
407     Orientation originOrientation = defaultScreen_->GetOrientation();
408     DisplayCutoutController::SetWaterfallAreaCompressionSizeWhenHorizontal(originSize);
409     ASSERT_EQ(originSize, DisplayCutoutController::GetWaterfallAreaCompressionSizeWhenHorizontal());
410     DisplayCutoutController::SetWaterfallAreaCompressionEnableWhenHorzontal(originStatus);
411     ASSERT_EQ(originStatus, DisplayCutoutController::IsWaterfallAreaCompressionEnableWhenHorizontal());
412     DisplayCutoutController::SetIsWaterfallDisplay(originWaterfallEnable);
413     ASSERT_EQ(originOrientation, defaultScreen_->GetOrientation());
414 }
415 }
416 } // namespace Rosen
417 } // namespace OHOS