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