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 // gtest
17 #include <gtest/gtest.h>
18
19 #include "window_manager.h"
20 #include "window_test_utils.h"
21 #include "wm_common.h"
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowImmersiveTest" };
29
30 const Rect SYS_BAR_REGION_NULL = { 0, 0, 0, 0 };
31 const SystemBarProperty SYS_BAR_PROP_DEFAULT;
32 const SystemBarProperty SYS_BAR_PROP_1(true, 0xE5111111, 0xE5222222);
33 const SystemBarProperty SYS_BAR_PROP_2(false, 0xE5222222, 0xE5333333);
34 const SystemBarProperty SYS_BAR_PROP_3(false, 0xE5333333, 0xE5444444);
35 const SystemBarProperty SYS_BAR_PROP_4(true, 0xE5444444, 0x66555555);
36 const SystemBarRegionTints TEST_PROPS_DEFAULT = {
37 { WindowType::WINDOW_TYPE_STATUS_BAR, SYS_BAR_PROP_DEFAULT, SYS_BAR_REGION_NULL },
38 { WindowType::WINDOW_TYPE_NAVIGATION_BAR, SYS_BAR_PROP_DEFAULT, SYS_BAR_REGION_NULL },
39 };
40 const SystemBarRegionTints TEST_PROPS_1 = {
41 { WindowType::WINDOW_TYPE_STATUS_BAR, SYS_BAR_PROP_1, SYS_BAR_REGION_NULL },
42 { WindowType::WINDOW_TYPE_NAVIGATION_BAR, SYS_BAR_PROP_2, SYS_BAR_REGION_NULL },
43 };
44 const SystemBarRegionTints TEST_PROPS_2 = {
45 { WindowType::WINDOW_TYPE_STATUS_BAR, SYS_BAR_PROP_1, SYS_BAR_REGION_NULL },
46 { WindowType::WINDOW_TYPE_NAVIGATION_BAR, SYS_BAR_PROP_3, SYS_BAR_REGION_NULL },
47 };
48
49 const Rect EMPTY_RECT = { 0, 0, 0, 0 };
50 const float RATIO = 0.3;
51 } // namespace
52
53 using Utils = WindowTestUtils;
54 const int WAIT_ASYNC_US = 100000; // 100000us
55
56 class TestSystemBarChangedListener : public ISystemBarChangedListener {
57 public:
58 SystemBarRegionTints tints_ = TEST_PROPS_DEFAULT;
59 void OnSystemBarPropertyChange(DisplayId displayId, const SystemBarRegionTints& tints) override;
60 };
61
62 class TestAvoidAreaChangedListener : public IAvoidAreaChangedListener {
63 public:
64 AvoidArea avoidArea_;
65 void OnAvoidAreaChanged(const AvoidArea avoidArea, AvoidAreaType type,
66 const sptr<OccupiedAreaChangeInfo>& info) override;
67 };
68
69 class WindowImmersiveTest : public testing::Test {
70 public:
71 static void SetUpTestCase();
72 static void TearDownTestCase();
73 virtual void SetUp() override;
74 virtual void TearDown() override;
75 void SetWindowSystemProps(const sptr<Window>& window, const SystemBarRegionTints& props);
76 bool SystemBarPropsEqualsTo(const SystemBarRegionTints& expect);
77 void DumpFailedInfo(const SystemBarRegionTints& expect);
78 void DumpFailedInfo(bool expectStatus, bool expectNav);
79 bool SystemBarEnableState(bool expectStatus, bool expectNav);
80 DisplayId displayId_ = 0;
81 std::vector<sptr<Window>> activeWindows_;
82 static vector<Rect> fullScreenExpecteds_;
83 static sptr<TestSystemBarChangedListener> testSystemBarChangedListener_;
84 static sptr<TestAvoidAreaChangedListener> testAvoidAreaChangedListener_;
85 Utils::TestWindowInfo fullScreenAppinfo_;
86 Utils::TestWindowInfo avoidBarInfo_;
87 uint32_t leftAvoidW_;
88 uint32_t leftAvoidH_;
89 uint32_t topAvoidW_;
90 uint32_t topAvoidH_;
91 sptr<Window> backgroundWindow_;
92 };
93
94 vector<Rect> WindowImmersiveTest::fullScreenExpecteds_;
95 sptr<TestSystemBarChangedListener> WindowImmersiveTest::testSystemBarChangedListener_ =
96 new TestSystemBarChangedListener();
97 sptr<TestAvoidAreaChangedListener> WindowImmersiveTest::testAvoidAreaChangedListener_ =
98 new TestAvoidAreaChangedListener();
99
SetWindowSystemProps(const sptr<Window> & window,const SystemBarRegionTints & tints)100 void WindowImmersiveTest::SetWindowSystemProps(const sptr<Window>& window, const SystemBarRegionTints& tints)
101 {
102 for (auto tint : tints) {
103 window->SetSystemBarProperty(tint.type_, tint.prop_);
104 }
105 }
106
DumpFailedInfo(const SystemBarRegionTints & expect)107 void WindowImmersiveTest::DumpFailedInfo(const SystemBarRegionTints& expect)
108 {
109 auto act = testSystemBarChangedListener_->tints_;
110 WLOGI("WindowImmersiveTest Expected:");
111 for (auto tint : expect) {
112 WLOGI("WindowType: %{public}4d, Enable: %{public}4d, Color: %{public}x | %{public}x",
113 static_cast<uint32_t>(tint.type_),
114 tint.prop_.enable_,
115 tint.prop_.backgroundColor_,
116 tint.prop_.contentColor_);
117 }
118 WLOGI("WindowImmersiveTest Act: ");
119 for (auto tint : act) {
120 WLOGI("WindowType: %{public}4d, Enable: %{public}4d, Color: %{public}x | %{public}x",
121 static_cast<uint32_t>(tint.type_),
122 tint.prop_.enable_,
123 tint.prop_.backgroundColor_,
124 tint.prop_.contentColor_);
125 }
126 }
127
DumpFailedInfo(bool expectStatus,bool expectNav)128 void WindowImmersiveTest::DumpFailedInfo(bool expectStatus, bool expectNav)
129 {
130 auto act = testSystemBarChangedListener_->tints_;
131 WLOGI("WindowImmersiveTest Expected:");
132 WLOGI("expectStatus: %{public}4d, expectNav: %{public}4d", expectStatus, expectNav);
133 WLOGI("WindowImmersiveTest Act: ");
134 for (auto tint : act) {
135 WLOGI("WindowType: %{public}4d, Enable: %{public}4d, Color: %{public}x | %{public}x",
136 static_cast<uint32_t>(tint.type_),
137 tint.prop_.enable_,
138 tint.prop_.backgroundColor_,
139 tint.prop_.contentColor_);
140 }
141 }
142
SystemBarPropsEqualsTo(const SystemBarRegionTints & expect)143 bool WindowImmersiveTest::SystemBarPropsEqualsTo(const SystemBarRegionTints& expect)
144 {
145 usleep(WAIT_ASYNC_US);
146 auto act = testSystemBarChangedListener_->tints_;
147 if (act.size() != expect.size()) {
148 DumpFailedInfo(expect);
149 return false;
150 }
151 for (auto item : expect) {
152 bool check = false;
153 for (auto tint : act) {
154 if (item.prop_ == tint.prop_ && item.type_ == tint.type_) {
155 check = true;
156 break;
157 }
158 }
159 if (!check) {
160 DumpFailedInfo(expect);
161 return false;
162 }
163 check = false;
164 }
165 return true;
166 }
167
SystemBarEnableState(bool expectStatus,bool expectNav)168 bool WindowImmersiveTest::SystemBarEnableState(bool expectStatus, bool expectNav)
169 {
170 usleep(WAIT_ASYNC_US);
171 auto act = testSystemBarChangedListener_->tints_;
172 bool check = false;
173 for (auto tint : act) {
174 if ((tint.type_ == WindowType::WINDOW_TYPE_STATUS_BAR && tint.prop_.enable_ == expectStatus) ||
175 (tint.type_ == WindowType::WINDOW_TYPE_NAVIGATION_BAR && tint.prop_.enable_ == expectNav)) {
176 check = true;
177 } else {
178 check = false;
179 }
180 }
181 if (!check) {
182 DumpFailedInfo(expectStatus, expectNav);
183 }
184 return check;
185 }
186
OnSystemBarPropertyChange(DisplayId displayId,const SystemBarRegionTints & tints)187 void TestSystemBarChangedListener::OnSystemBarPropertyChange(DisplayId displayId, const SystemBarRegionTints& tints)
188 {
189 WLOGI("TestSystemBarChangedListener Display ID: %{public}" PRIu64 "", displayId);
190 WLOGI("TestSystemBarChangedListener tints size: %{public}zu", tints.size());
191 for (auto tint : tints) {
192 auto type = tint.type_;
193 for (uint32_t i = 0; i < tints_.size(); i++) {
194 if (tints_[i].type_ == type) {
195 tints_[i] = tint;
196 }
197 }
198 }
199 }
200
OnAvoidAreaChanged(const AvoidArea avoidArea,AvoidAreaType type,const sptr<OccupiedAreaChangeInfo> & info)201 void TestAvoidAreaChangedListener::OnAvoidAreaChanged(const AvoidArea avoidArea, AvoidAreaType type,
202 const sptr<OccupiedAreaChangeInfo>& info)
203 {
204 avoidArea_ = avoidArea;
205 }
206
SetUpTestCase()207 void WindowImmersiveTest::SetUpTestCase()
208 {
209 auto display = DisplayManager::GetInstance().GetDisplayById(0);
210 ASSERT_TRUE((display != nullptr));
211 WLOGI("GetDefaultDisplay: id %{public}" PRIu64 ", w %{public}d, h %{public}d, fps %{public}u",
212 display->GetId(),
213 display->GetWidth(),
214 display->GetHeight(),
215 display->GetRefreshRate());
216 Rect displayRect = { 0, 0, display->GetWidth(), display->GetHeight() };
217 Utils::InitByDisplayRect(displayRect);
218 }
219
TearDownTestCase()220 void WindowImmersiveTest::TearDownTestCase() {}
221
SetUp()222 void WindowImmersiveTest::SetUp()
223 {
224 fullScreenAppinfo_ = {
225 .name = "main",
226 .rect = Utils::customAppRect_,
227 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
228 .mode = WindowMode::WINDOW_MODE_FULLSCREEN, // immersive setting
229 .needAvoid = false, // immersive setting
230 .parentLimit = false,
231 .parentId = INVALID_WINDOW_ID,
232 };
233 avoidBarInfo_ = {
234 .name = "LeftAvoidTest",
235 .rect = EMPTY_RECT,
236 .type = WindowType::WINDOW_TYPE_STATUS_BAR,
237 .mode = WindowMode::WINDOW_MODE_FLOATING,
238 };
239 // makesure left avoid win w < h
240 leftAvoidW_ = std::min(Utils::displayRect_.width_, static_cast<uint32_t>(Utils::displayRect_.height_ * RATIO));
241 leftAvoidH_ = Utils::displayRect_.height_;
242 // makesure top avoid win h < w
243 topAvoidW_ = Utils::displayRect_.width_;
244 topAvoidH_ = std::min(Utils::displayRect_.height_, static_cast<uint32_t>(Utils::displayRect_.width_ * RATIO));
245
246 WindowManager::GetInstance().RegisterSystemBarChangedListener(testSystemBarChangedListener_);
247 activeWindows_.clear();
248 sleep(1);
249 }
250
TearDown()251 void WindowImmersiveTest::TearDown()
252 {
253 while (!activeWindows_.empty()) {
254 ASSERT_EQ(WMError::WM_OK, activeWindows_.back()->Destroy());
255 activeWindows_.pop_back();
256 }
257 WindowManager::GetInstance().UnregisterSystemBarChangedListener(testSystemBarChangedListener_);
258 sleep(1);
259 }
260
261 namespace {
262 /**
263 * @tc.name: ImmersiveTest01
264 * @tc.desc: Add one immersive window and hide
265 * @tc.type: FUNC
266 */
267 HWTEST_F(WindowImmersiveTest, ImmersiveTest01, TestSize.Level1)
268 {
269 fullScreenAppinfo_.name = "immer01";
270 const sptr<Window>& window = Utils::CreateTestWindow(fullScreenAppinfo_);
271 ASSERT_NE(window, nullptr);
272 activeWindows_.push_back(window);
273 SetWindowSystemProps(window, TEST_PROPS_1);
274 ASSERT_EQ(WMError::WM_OK, window->Show());
275 EXPECT_TRUE(SystemBarPropsEqualsTo(TEST_PROPS_1));
276 ASSERT_EQ(WMError::WM_OK, window->Hide());
277 }
278
279 /**
280 * @tc.name: ImmersiveTest02
281 * @tc.desc: Add two immersive window and switch
282 * @tc.type: FUNC
283 */
284 HWTEST_F(WindowImmersiveTest, ImmersiveTest02, TestSize.Level1)
285 {
286 const sptr<Window>& window1 = Utils::CreateTestWindow(fullScreenAppinfo_);
287 ASSERT_NE(window1, nullptr);
288 activeWindows_.push_back(window1);
289 SetWindowSystemProps(window1, TEST_PROPS_1);
290 fullScreenAppinfo_.name = "Immer02";
291 const sptr<Window>& window2 = Utils::CreateTestWindow(fullScreenAppinfo_);
292 ASSERT_NE(nullptr, window2);
293 activeWindows_.push_back(window2);
294 SetWindowSystemProps(window2, TEST_PROPS_2);
295 EXPECT_EQ(WMError::WM_OK, window1->Show());
296
297 EXPECT_TRUE(SystemBarPropsEqualsTo(TEST_PROPS_1));
298 EXPECT_EQ(WMError::WM_OK, window2->Show());
299
300 EXPECT_TRUE(SystemBarPropsEqualsTo(TEST_PROPS_2));
301 EXPECT_EQ(WMError::WM_OK, window2->Hide());
302
303 EXPECT_TRUE(SystemBarPropsEqualsTo(TEST_PROPS_1));
304 EXPECT_EQ(WMError::WM_OK, window1->Hide());
305 }
306
307 /**
308 * @tc.name: ImmersiveTest03
309 * @tc.desc: Add one no immersive window
310 * @tc.type: FUNC
311 */
312 HWTEST_F(WindowImmersiveTest, ImmersiveTest03, TestSize.Level1)
313 {
314 const sptr<Window>& window1 = Utils::CreateTestWindow(fullScreenAppinfo_);
315 ASSERT_NE(window1, nullptr);
316 activeWindows_.push_back(window1);
317 SetWindowSystemProps(window1, TEST_PROPS_1);
318 fullScreenAppinfo_.name = "Immer03";
319 fullScreenAppinfo_.needAvoid = true; // no immersive setting
320 const sptr<Window>& window2 = Utils::CreateTestWindow(fullScreenAppinfo_);
321 ASSERT_NE(nullptr, window2);
322 activeWindows_.push_back(window2);
323 SetWindowSystemProps(window2, TEST_PROPS_2);
324 EXPECT_EQ(WMError::WM_OK, window1->Show());
325 EXPECT_TRUE(SystemBarPropsEqualsTo(TEST_PROPS_1));
326
327 EXPECT_EQ(WMError::WM_OK, window2->Show());
328 EXPECT_TRUE(SystemBarPropsEqualsTo(TEST_PROPS_2));
329
330 EXPECT_EQ(WMError::WM_OK, window1->Hide());
331 EXPECT_TRUE(SystemBarPropsEqualsTo(TEST_PROPS_2));
332 }
333
334 /**
335 * @tc.name: ImmersiveTest04
336 * @tc.desc: SetLayoutFullScreen
337 * @tc.type: FUNC
338 */
339 HWTEST_F(WindowImmersiveTest, ImmersiveTest04, TestSize.Level1)
340 {
341 fullScreenAppinfo_.needAvoid = true; // no immersive setting
342 const sptr<Window>& window1 = Utils::CreateTestWindow(fullScreenAppinfo_);
343 ASSERT_NE(window1, nullptr);
344 activeWindows_.push_back(window1);
345 SetWindowSystemProps(window1, TEST_PROPS_1);
346 EXPECT_EQ(WMError::WM_OK, window1->Show());
347 EXPECT_TRUE(SystemBarPropsEqualsTo(TEST_PROPS_1));
348
349 EXPECT_EQ(WMError::WM_OK, window1->SetLayoutFullScreen(true));
350 EXPECT_EQ(true, window1->IsLayoutFullScreen());
351 EXPECT_TRUE(SystemBarPropsEqualsTo(TEST_PROPS_1));
352
353 EXPECT_EQ(WMError::WM_OK, window1->SetLayoutFullScreen(false));
354 EXPECT_EQ(false, window1->IsLayoutFullScreen());
355 EXPECT_TRUE(SystemBarPropsEqualsTo(TEST_PROPS_1));
356
357 EXPECT_EQ(WMError::WM_OK, window1->Hide());
358 }
359
360 /**
361 * @tc.name: ImmersiveTest05
362 * @tc.desc: SetFullScreen
363 * @tc.type: FUNC
364 */
365 HWTEST_F(WindowImmersiveTest, ImmersiveTest05, TestSize.Level1)
366 {
367 fullScreenAppinfo_.needAvoid = true; // no immersive setting
368 const sptr<Window>& window1 = Utils::CreateTestWindow(fullScreenAppinfo_);
369 ASSERT_NE(window1, nullptr);
370 activeWindows_.push_back(window1);
371 SetWindowSystemProps(window1, TEST_PROPS_1);
372 EXPECT_EQ(WMError::WM_OK, window1->Show());
373 EXPECT_TRUE(SystemBarPropsEqualsTo(TEST_PROPS_1));
374
375 EXPECT_EQ(WMError::WM_OK, window1->SetFullScreen(true));
376 EXPECT_EQ(true, window1->IsFullScreen());
377 EXPECT_TRUE(SystemBarEnableState(false, false));
378
379 EXPECT_EQ(WMError::WM_OK, window1->SetFullScreen(false));
380 EXPECT_EQ(false, window1->IsFullScreen());
381 EXPECT_EQ(WMError::WM_OK, window1->Hide());
382 }
383
384 /**
385 * @tc.name: ImmersiveNegativeTest01
386 * @tc.desc: set systembar props with wrong window type
387 * @tc.type: FUNC
388 */
389 HWTEST_F(WindowImmersiveTest, ImmersiveNegativeTest01, TestSize.Level1)
390 {
391 const SystemBarRegionTints TEST_PROPS_NEGATIVE = {
392 { WindowType::WINDOW_TYPE_KEYGUARD, SYS_BAR_PROP_1, SYS_BAR_REGION_NULL },
393 { WindowType::WINDOW_TYPE_POINTER, SYS_BAR_PROP_2, SYS_BAR_REGION_NULL },
394 };
395 const sptr<Window>& window = Utils::CreateTestWindow(fullScreenAppinfo_);
396 ASSERT_NE(window, nullptr);
397 activeWindows_.push_back(window);
398 SetWindowSystemProps(window, TEST_PROPS_NEGATIVE);
399 ASSERT_EQ(WMError::WM_OK, window->Show());
400 ASSERT_FALSE(SystemBarPropsEqualsTo(TEST_PROPS_NEGATIVE));
401
402 ASSERT_EQ(WMError::WM_OK, window->Hide());
403 ASSERT_FALSE(SystemBarPropsEqualsTo(TEST_PROPS_NEGATIVE));
404 }
405
406 /**
407 * @tc.name: GetAvoidAreaByTypeTest01
408 * @tc.desc: Test GetAvoidArea use unsupported Type(TYPE_CUTOUT).
409 * @tc.type: FUNC
410 */
411 HWTEST_F(WindowImmersiveTest, GetAvoidAreaByTypeTest01, TestSize.Level1)
412 {
413 // Add full screenwindow for call GetAvoidArea, and push_back in activeWindows_
414 const sptr<Window>& win = Utils::CreateTestWindow(fullScreenAppinfo_);
415 ASSERT_NE(win, nullptr);
416 activeWindows_.push_back(win);
417
418 // Test GetAvoidArea
419 AvoidArea avoidarea;
420 WMError ret = win->GetAvoidAreaByType(AvoidAreaType::TYPE_CUTOUT, avoidarea);
421 ASSERT_EQ(WMError::WM_OK, ret);
422 ASSERT_TRUE(Utils::RectEqualToRect(EMPTY_RECT, avoidarea.leftRect_));
423 ASSERT_TRUE(Utils::RectEqualToRect(EMPTY_RECT, avoidarea.rightRect_));
424 ASSERT_TRUE(Utils::RectEqualToRect(EMPTY_RECT, avoidarea.topRect_));
425 ASSERT_TRUE(Utils::RectEqualToRect(EMPTY_RECT, avoidarea.bottomRect_));
426 ASSERT_EQ(WMError::WM_OK, win->Hide());
427 }
428
429 /**
430 * @tc.name: DockWindowTest01
431 * @tc.desc: Add unexistavoid and remove this avoid. Test OnAvoidAreaChanged listener
432 * @tc.type: FUNC
433 */
434 HWTEST_F(WindowImmersiveTest, DockWindowTest01, TestSize.Level1)
435 {
436 const sptr<Window>& dockWindow = Utils::CreateDockWindow();
437 if (dockWindow == nullptr) {
438 return;
439 }
440
441 if (WMError::WM_ERROR_INVALID_WINDOW == dockWindow->Show()) {
442 ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW, dockWindow->Show());
443 } else if (WMError::WM_OK == dockWindow->Show()) {
444 ASSERT_EQ(WMError::WM_OK, dockWindow->Show());
445 }
446
447 const sptr<Window>& window = Utils::CreateTestWindow(fullScreenAppinfo_);
448 if (window == nullptr) {
449 return;
450 }
451 usleep(WAIT_ASYNC_US);
452 auto act = testSystemBarChangedListener_->tints_;
453 for (SystemBarRegionTint tint : act) {
454 if (tint.type_ == WindowType::WINDOW_TYPE_LAUNCHER_DOCK) {
455 ASSERT_FALSE(tint.prop_.enable_);
456 }
457 }
458
459 ASSERT_EQ(WMError::WM_OK, window->Hide());
460
461 usleep(WAIT_ASYNC_US);
462 act = testSystemBarChangedListener_->tints_;
463 for (SystemBarRegionTint tint : act) {
464 if (tint.type_ == WindowType::WINDOW_TYPE_LAUNCHER_DOCK) {
465 ASSERT_TRUE(tint.prop_.enable_);
466 }
467 }
468 ASSERT_EQ(WMError::WM_OK, dockWindow->Destroy());
469 }
470 } // namespace
471 } // namespace Rosen
472 } // namespace OHOS
473