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 "wm_common.h"
19 #include "window_manager.h"
20 #include "window_test_utils.h"
21
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, "WindowVisibilityInfoTest"};
29 }
30
31 using utils = WindowTestUtils;
32 constexpr int WAIT_ASYNC_US = 200000; // 200ms
33
34 class VisibilityChangedListenerImpl : public IVisibilityChangedListener {
35 public:
36 void OnWindowVisibilityChanged(const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfo) override;
37 std::vector<sptr<WindowVisibilityInfo>> windowVisibilityInfos_;
38 };
39
OnWindowVisibilityChanged(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfo)40 void VisibilityChangedListenerImpl::OnWindowVisibilityChanged(
41 const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfo)
42 {
43 WLOGFI("size:%{public}zu", windowVisibilityInfo.size());
44 windowVisibilityInfos_ = std::move(windowVisibilityInfo);
45 for (auto& info : windowVisibilityInfos_) {
46 WLOGFI("windowId:%{public}u, covered:%{public}d, pid:%{public}d, uid:%{public}d", info->windowId_,
47 info->isVisible_, info->pid_, info->uid_);
48 }
49 }
50
51 class WindowVisibilityInfoTest : public testing::Test {
52 public:
53 static void SetUpTestCase();
54
55 static void TearDownTestCase();
56
57 virtual void SetUp() override;
58
59 virtual void TearDown() override;
60
61 static inline sptr<VisibilityChangedListenerImpl> visibilityChangedListener_ = new VisibilityChangedListenerImpl();
62 utils::TestWindowInfo fullScreenAppInfo_;
63 utils::TestWindowInfo floatAppInfo_;
64 utils::TestWindowInfo subAppInfo_;
65 };
66
SetUpTestCase()67 void WindowVisibilityInfoTest::SetUpTestCase()
68 {
69 auto display = DisplayManager::GetInstance().GetDisplayById(0);
70 ASSERT_TRUE((display != nullptr));
71 WLOGFI("GetDefaultDisplay: id %{public}" PRIu64", w %{public}d, h %{public}d, fps %{public}u",
72 display->GetId(), display->GetWidth(), display->GetHeight(), display->GetRefreshRate());
73 Rect displayRect = {0, 0,
74 static_cast<uint32_t>(display->GetWidth()), static_cast<uint32_t>(display->GetHeight())};
75 utils::InitByDisplayRect(displayRect);
76 WindowManager::GetInstance().RegisterVisibilityChangedListener(visibilityChangedListener_);
77 }
78
TearDownTestCase()79 void WindowVisibilityInfoTest::TearDownTestCase()
80 {
81 WindowManager::GetInstance().UnregisterVisibilityChangedListener(visibilityChangedListener_);
82 }
83
SetUp()84 void WindowVisibilityInfoTest::SetUp()
85 {
86 fullScreenAppInfo_ = {
87 .name = "FullWindow",
88 .rect = utils::customAppRect_,
89 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
90 .mode = WindowMode::WINDOW_MODE_FULLSCREEN,
91 .needAvoid = false,
92 .parentLimit = false,
93 .parentName = "",
94 };
95 floatAppInfo_ = {
96 .name = "ParentWindow",
97 .rect = utils::customAppRect_,
98 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
99 .mode = WindowMode::WINDOW_MODE_FLOATING,
100 .needAvoid = false,
101 .parentLimit = false,
102 .parentName = "",
103 };
104 subAppInfo_ = {
105 .name = "SubWindow",
106 .rect = utils::customAppRect_,
107 .type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW,
108 .mode = WindowMode::WINDOW_MODE_FLOATING,
109 .needAvoid = false,
110 .parentLimit = false,
111 .parentName = "",
112 };
113 }
114
TearDown()115 void WindowVisibilityInfoTest::TearDown()
116 {
117 }
118
119 namespace {
120 /**
121 * @tc.name: WindowVisibilityInfoTest01
122 * @tc.desc: add main window and sub window, show and hide and test callback
123 * @tc.type: FUNC
124 */
125 HWTEST_F(WindowVisibilityInfoTest, WindowVisibilityInfoTest01, Function | MediumTest | Level1)
126 {
127 floatAppInfo_.name = "window1";
128 floatAppInfo_.rect = {250, 150, 300, 500};
129 sptr<Window> window1 = utils::CreateTestWindow(floatAppInfo_);
130
131 subAppInfo_.name = "subWindow1";
132 subAppInfo_.rect = {400, 200, 100, 100};
133 subAppInfo_.parentName = window1->GetWindowName();
134 sptr<Window> subWindow1 = utils::CreateTestWindow(subAppInfo_);
135
136 ASSERT_EQ(WMError::WM_OK, window1->Show());
137 usleep(WAIT_ASYNC_US);
138 ASSERT_EQ(1, visibilityChangedListener_->windowVisibilityInfos_.size());
139
140 ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
141 usleep(WAIT_ASYNC_US);
142 ASSERT_EQ(1, visibilityChangedListener_->windowVisibilityInfos_.size());
143
144 ASSERT_EQ(WMError::WM_OK, window1->Hide());
145 usleep(WAIT_ASYNC_US);
146 ASSERT_EQ(2, visibilityChangedListener_->windowVisibilityInfos_.size());
147
148 ASSERT_EQ(WMError::WM_OK, window1->Show());
149 usleep(WAIT_ASYNC_US);
150 ASSERT_EQ(2, visibilityChangedListener_->windowVisibilityInfos_.size());
151
152 ASSERT_EQ(WMError::WM_OK, subWindow1->Hide());
153 usleep(WAIT_ASYNC_US);
154 ASSERT_EQ(1, visibilityChangedListener_->windowVisibilityInfos_.size());
155
156 ASSERT_EQ(WMError::WM_OK, window1->Hide());
157 usleep(WAIT_ASYNC_US);
158 ASSERT_EQ(1, visibilityChangedListener_->windowVisibilityInfos_.size());
159
160 ASSERT_EQ(WMError::WM_OK, window1->Show());
161 usleep(WAIT_ASYNC_US);
162 ASSERT_EQ(1, visibilityChangedListener_->windowVisibilityInfos_.size());
163
164 window1->Destroy();
165 subWindow1->Destroy();
166 }
167
168 /**
169 * @tc.name: WindowVisibilityInfoTest02
170 * @tc.desc: add two fullscreen main window, test callback
171 * @tc.type: FUNC
172 */
173 HWTEST_F(WindowVisibilityInfoTest, WindowVisibilityInfoTest02, Function | MediumTest | Level1)
174 {
175 fullScreenAppInfo_.name = "window1";
176 sptr<Window> window1 = utils::CreateTestWindow(fullScreenAppInfo_);
177
178 fullScreenAppInfo_.name = "window2";
179 sptr<Window> window2 = utils::CreateTestWindow(fullScreenAppInfo_);
180
181 ASSERT_EQ(WMError::WM_OK, window1->Show());
182 usleep(WAIT_ASYNC_US);
183 ASSERT_EQ(2, visibilityChangedListener_->windowVisibilityInfos_.size());
184
185 ASSERT_EQ(WMError::WM_OK, window2->Show());
186 usleep(WAIT_ASYNC_US);
187 ASSERT_EQ(2, visibilityChangedListener_->windowVisibilityInfos_.size());
188
189 window1->Destroy();
190 window2->Destroy();
191 }
192
193 /**
194 * @tc.name: WindowVisibilityInfoTest03
195 * @tc.desc: add two main window and sub windows and test callback
196 * @tc.type: FUNC
197 */
198 HWTEST_F(WindowVisibilityInfoTest, WindowVisibilityInfoTest03, Function | MediumTest | Level1)
199 {
200 floatAppInfo_.name = "window1";
201 floatAppInfo_.rect = {0, 0, 300, 600};
202 sptr<Window> window1 = utils::CreateTestWindow(floatAppInfo_);
203
204 subAppInfo_.name = "subWindow1";
205 subAppInfo_.rect = {400, 200, 100, 100};
206 subAppInfo_.parentName = window1->GetWindowName();
207 sptr<Window> subWindow1 = utils::CreateTestWindow(subAppInfo_);
208
209 floatAppInfo_.name = "window2";
210 floatAppInfo_.rect = {50, 150, 240, 426};
211 sptr<Window> window2 = utils::CreateTestWindow(floatAppInfo_);
212
213 subAppInfo_.name = "subWindow2";
214 subAppInfo_.type = WindowType::WINDOW_TYPE_MEDIA;
215 subAppInfo_.parentName = window2->GetWindowName();
216 sptr<Window> subWindow2 = utils::CreateTestWindow(subAppInfo_);
217
218 ASSERT_EQ(WMError::WM_OK, window2->Show());
219 usleep(WAIT_ASYNC_US);
220 ASSERT_EQ(1, visibilityChangedListener_->windowVisibilityInfos_.size());
221
222 ASSERT_EQ(WMError::WM_OK, subWindow2->Show());
223 usleep(WAIT_ASYNC_US);
224 ASSERT_EQ(1, visibilityChangedListener_->windowVisibilityInfos_.size());
225
226 ASSERT_EQ(WMError::WM_OK, window1->Show());
227 usleep(WAIT_ASYNC_US);
228 ASSERT_EQ(2, visibilityChangedListener_->windowVisibilityInfos_.size());
229
230 ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
231 usleep(WAIT_ASYNC_US);
232 ASSERT_EQ(2, visibilityChangedListener_->windowVisibilityInfos_.size());
233
234 window1->Destroy();
235 subWindow1->Destroy();
236 window2->Destroy();
237 subWindow2->Destroy();
238 }
239 }
240 } // namespace Rosen
241 } // namespace OHOS
242
243