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