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 <ability_context.h>
19
20 #include "window_test_utils.h"
21 #include "window.h"
22 #include "window_option.h"
23 #include "window_scene.h"
24 #include "wm_common.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace Rosen {
31 class WindowSystemSubWindowTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 virtual void SetUp() override;
36 virtual void TearDown() override;
37 };
38
SetUpTestCase()39 void WindowSystemSubWindowTest::SetUpTestCase()
40 {
41 }
42
TearDownTestCase()43 void WindowSystemSubWindowTest::TearDownTestCase()
44 {
45 }
46
SetUp()47 void WindowSystemSubWindowTest::SetUp()
48 {
49 }
50
TearDown()51 void WindowSystemSubWindowTest::TearDown()
52 {
53 }
54
CreateBaseWindow(WindowType type,struct Rect rect,uint32_t flags)55 static sptr<Window> CreateBaseWindow(WindowType type, struct Rect rect, uint32_t flags)
56 {
57 sptr<WindowOption> baseOp = new WindowOption();
58 baseOp->SetWindowType(type);
59 baseOp->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
60 baseOp->SetWindowRect(rect);
61 baseOp->SetWindowFlags(flags);
62
63 static int baseCount = 0;
64 std::string baseWindowName = "BaseWindow" + std::to_string(baseCount++);
65 sptr<Window> window = Window::Create(baseWindowName, baseOp, nullptr);
66 return window;
67 }
68
CreateAppSubWindow(sptr<Window> parentWindow,WindowType type,struct Rect rect,uint32_t flags,std::string name="")69 static sptr<Window> CreateAppSubWindow(sptr<Window> parentWindow, WindowType type, struct Rect rect,
70 uint32_t flags, std::string name = "")
71 {
72 sptr<WindowOption> subOp = new WindowOption();
73 subOp->SetWindowType(type);
74 subOp->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
75 subOp->SetWindowRect(rect);
76 subOp->SetWindowFlags(flags);
77 subOp->SetParentId(parentWindow->GetWindowId());
78
79 static int cnt = 0;
80 std::string subWinName = (name == "") ? "AppSubWindow" + std::to_string(cnt++) : name;
81 sptr<Window> window = Window::Create(subWinName, subOp);
82 return window;
83 }
84
CreateSystemSubWindow(sptr<Window> parentWindow,struct Rect rect,uint32_t flags,std::string name="")85 static sptr<Window> CreateSystemSubWindow(sptr<Window> parentWindow, struct Rect rect,
86 uint32_t flags, std::string name = "")
87 {
88 sptr<WindowOption> subOp = new WindowOption();
89 subOp->SetWindowType(WindowType::WINDOW_TYPE_SYSTEM_SUB_WINDOW);
90 subOp->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
91 subOp->SetWindowRect(rect);
92 subOp->SetWindowFlags(flags);
93 subOp->SetParentId(parentWindow->GetWindowId());
94
95 static int cnt = 0;
96 std::string subWinName = (name == "") ? "SystemSubWindow" + std::to_string(cnt++) : name;
97 sptr<Window> window = Window::Create(subWinName, subOp);
98 return window;
99 }
100
101 /**
102 * @tc.name: SystemSubWindow01
103 * @tc.desc: create sub windows with below system Windows
104 * @tc.type: FUNC
105 */
106 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow01, Function | MediumTest | Level2)
107 {
108 std::vector<WindowType> windowTypes = {
109 WindowType::WINDOW_TYPE_WALLPAPER,
110 WindowType::WINDOW_TYPE_DESKTOP,
111 };
112 for (auto itor = windowTypes.begin(); itor != windowTypes.end(); itor++) {
113 struct Rect baseRect = {0, 0, 100, 200};
114 uint32_t baseFlags = 0;
115 sptr<Window> baseWindow = CreateBaseWindow(static_cast<WindowType>(*itor), baseRect, baseFlags);
116 if (baseWindow == nullptr) {
117 continue;
118 }
119 struct Rect rect = {0, 0, 100, 200};
120 uint32_t flags = 0;
121 sptr<Window> subWindow = CreateSystemSubWindow(baseWindow, rect, flags);
122 if (subWindow == nullptr) {
123 return;
124 }
125
126 ASSERT_NE(nullptr, subWindow);
127
128 ASSERT_EQ(WMError::WM_OK, baseWindow->Show());
129 ASSERT_EQ(WMError::WM_OK, subWindow->Show());
130
131 ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
132 ASSERT_EQ(WMError::WM_OK, baseWindow->Hide());
133
134 ASSERT_EQ(WMError::WM_OK, subWindow->Destroy());
135 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
136 }
137 }
138
139 /**
140 * @tc.name: SystemSubWindow02
141 * @tc.desc: create sub windows with above system Windows except WINDOW_TYPE_DIALOG
142 * @tc.type: FUNC
143 */
144 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow02, Function | MediumTest | Level2)
145 {
146 std::vector<WindowType> windowTypes = {
147 WindowType::WINDOW_TYPE_APP_LAUNCHING,
148 WindowType::WINDOW_TYPE_DOCK_SLICE,
149 WindowType::WINDOW_TYPE_INCOMING_CALL,
150 WindowType::WINDOW_TYPE_SEARCHING_BAR,
151 WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW,
152 WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT,
153 WindowType::WINDOW_TYPE_FLOAT,
154 WindowType::WINDOW_TYPE_TOAST,
155 WindowType::WINDOW_TYPE_STATUS_BAR,
156 WindowType::WINDOW_TYPE_PANEL,
157 WindowType::WINDOW_TYPE_VOLUME_OVERLAY,
158 WindowType::WINDOW_TYPE_NAVIGATION_BAR,
159 WindowType::WINDOW_TYPE_DRAGGING_EFFECT,
160 WindowType::WINDOW_TYPE_POINTER,
161 WindowType::WINDOW_TYPE_LAUNCHER_RECENT,
162 WindowType::WINDOW_TYPE_LAUNCHER_DOCK,
163 WindowType::WINDOW_TYPE_BOOT_ANIMATION,
164 WindowType::WINDOW_TYPE_FREEZE_DISPLAY,
165 WindowType::WINDOW_TYPE_VOICE_INTERACTION,
166 WindowType::WINDOW_TYPE_FLOAT_CAMERA,
167 WindowType::WINDOW_TYPE_PLACEHOLDER,
168 WindowType::WINDOW_TYPE_SCREENSHOT,
169 WindowType::WINDOW_TYPE_GLOBAL_SEARCH,
170 };
171 for (auto itor = windowTypes.begin(); itor != windowTypes.end(); itor++) {
172 struct Rect baseRect = {0, 0, 100, 200};
173 uint32_t baseFlags = 0;
174 sptr<Window> baseWindow = CreateBaseWindow(static_cast<WindowType>(*itor), baseRect, baseFlags);
175 if (baseWindow == nullptr) {
176 continue;
177 }
178
179 struct Rect rect = {0, 0, 100, 200};
180 uint32_t flags = 0;
181 sptr<Window> subWindow = CreateSystemSubWindow(baseWindow, rect, flags);
182 if (subWindow == nullptr) {
183 return;
184 }
185 ASSERT_NE(nullptr, subWindow);
186
187 ASSERT_EQ(WMError::WM_OK, baseWindow->Show());
188 ASSERT_EQ(WMError::WM_OK, subWindow->Show());
189
190 ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
191 ASSERT_EQ(WMError::WM_OK, baseWindow->Hide());
192
193 ASSERT_EQ(WMError::WM_OK, subWindow->Destroy());
194 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
195 }
196 }
197
198 /**
199 * @tc.name: SystemSubWindow03
200 * @tc.desc: create sub windows with app main Windows, no allow to add as app_main_window's subwindow
201 * @tc.type: FUNC
202 */
203 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow03, Function | MediumTest | Level2)
204 {
205
206 std::vector<WindowType> windowTypes = { WindowType::WINDOW_TYPE_APP_MAIN_WINDOW };
207 for (auto itor = windowTypes.begin(); itor != windowTypes.end(); itor++) {
208 struct Rect baseRect = {0, 0, 100, 200};
209 uint32_t baseFlags = 0;
210 sptr<Window> baseWindow = CreateBaseWindow(static_cast<WindowType>(*itor), baseRect, baseFlags);
211 if (baseWindow == nullptr) {
212 return;
213 }
214 ASSERT_NE(nullptr, baseWindow);
215
216 struct Rect rect = {0, 0, 100, 200};
217 uint32_t flags = 0;
218 sptr<Window> subWindow = CreateSystemSubWindow(baseWindow, rect, flags);
219 if (subWindow == nullptr) {
220 return;
221 }
222 ASSERT_EQ(nullptr, subWindow);
223 }
224 }
225
226 /**
227 * @tc.name: SystemSubWindow04
228 * @tc.desc: create sub windows with app sub Windows
229 * @tc.type: FUNC
230 */
231 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow04, Function | MediumTest | Level2)
232 {
233 std::vector<WindowType> windowTypes = {
234 WindowType::WINDOW_TYPE_MEDIA,
235 WindowType::WINDOW_TYPE_APP_SUB_WINDOW,
236 WindowType::WINDOW_TYPE_APP_COMPONENT,
237 };
238 for (auto itor = windowTypes.begin(); itor != windowTypes.end(); itor++) {
239 struct Rect baseRect = {0, 0, 100, 200};
240 uint32_t baseFlags = 0;
241 sptr<Window> baseWindow = CreateBaseWindow(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW, baseRect, baseFlags);
242 if (baseWindow == nullptr) {
243 return;
244 }
245 ASSERT_NE(nullptr, baseWindow);
246
247 sptr<Window> appSubWindow = CreateAppSubWindow(baseWindow, static_cast<WindowType>(*itor), baseRect, baseFlags);
248 if (appSubWindow == nullptr) {
249 return;
250 }
251 ASSERT_NE(nullptr, appSubWindow);
252
253 struct Rect rect = {0, 0, 100, 200};
254 uint32_t flags = 0;
255 sptr<Window> subWindow = CreateSystemSubWindow(appSubWindow, rect, flags);
256 ASSERT_EQ(nullptr, subWindow);
257 ASSERT_EQ(WMError::WM_OK, appSubWindow->Destroy());
258 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
259 }
260 }
261
262 /**
263 * @tc.name: SystemSubWindow05
264 * @tc.desc: create sub windows with system sub Windows
265 * @tc.type: FUNC
266 */
267 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow05, Function | MediumTest | Level3)
268 {
269 struct Rect baseRect = {0, 0, 100, 200};
270 uint32_t baseFlags = 0;
271 sptr<Window> baseWindow = CreateBaseWindow(WindowType::WINDOW_TYPE_DOCK_SLICE, baseRect, baseFlags);
272 if (baseWindow == nullptr) {
273 return;
274 }
275 ASSERT_NE(nullptr, baseWindow);
276
277 sptr<Window> systemSubWindow = CreateSystemSubWindow(baseWindow, baseRect, baseFlags);
278 if (systemSubWindow == nullptr) {
279 return;
280 }
281 ASSERT_NE(nullptr, systemSubWindow);
282
283 struct Rect rect = {0, 0, 100, 200};
284 uint32_t flags = 0;
285 sptr<Window> subWindow = CreateSystemSubWindow(systemSubWindow, rect, flags);
286 if (subWindow == nullptr) {
287 return;
288 }
289 ASSERT_EQ(nullptr, subWindow);
290
291 ASSERT_EQ(WMError::WM_OK, baseWindow->Show());
292 ASSERT_EQ(WMError::WM_OK, systemSubWindow->Show());
293
294 ASSERT_EQ(WMError::WM_OK, systemSubWindow->Hide());
295 ASSERT_EQ(WMError::WM_OK, baseWindow->Hide());
296
297 ASSERT_EQ(WMError::WM_OK, systemSubWindow->Destroy());
298 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
299 }
300
301 /**
302 * @tc.name: SystemSubWindow06
303 * @tc.desc: FullScreen Main Window + 2 SubWindows
304 * @tc.type: FUNC
305 */
306 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow06, Function | MediumTest | Level3)
307 {
308 struct Rect baseRect = {0, 0, 100, 200};
309 uint32_t baseFlags = 0;
310 sptr<Window> baseWindow = CreateBaseWindow(WindowType::WINDOW_TYPE_DOCK_SLICE, baseRect, baseFlags);
311 if (baseWindow == nullptr) {
312 return;
313 }
314 ASSERT_NE(nullptr, baseWindow);
315
316 struct Rect rect = {0, 0, 100, 200};
317 uint32_t flags = 0;
318 sptr<Window> subWindow = CreateSystemSubWindow(baseWindow, rect, flags);
319 if (subWindow == nullptr) {
320 return;
321 }
322 ASSERT_NE(nullptr, subWindow);
323
324 ASSERT_EQ(WMError::WM_OK, baseWindow->Show());
325 ASSERT_EQ(WMError::WM_OK, subWindow->Show());
326
327 bool isFocus = subWindow->GetFocusable();
328 ASSERT_EQ(WMError::WM_OK, subWindow->SetFocusable(!isFocus));
329 ASSERT_EQ(WMError::WM_OK, subWindow->MoveTo(0, 0));
330 ASSERT_EQ(WMError::WM_OK, subWindow->Resize(200, 400));
331 ASSERT_EQ(WMError::WM_OK, subWindow->SetTurnScreenOn(true));
332
333 ASSERT_EQ(WMError::WM_ERROR_INVALID_TYPE, subWindow->SetBrightness(0.5f));
334 ASSERT_EQ(WMError::WM_OK, subWindow->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN));
335
336 ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
337 ASSERT_EQ(WMError::WM_OK, baseWindow->Hide());
338
339 ASSERT_EQ(WMError::WM_OK, subWindow->Destroy());
340 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
341 }
342 /**
343 * @tc.name: SystemSubWindow07
344 * @tc.desc: create sub windows with dialog
345 * @tc.type: FUNC
346 */
347 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow07, Function | MediumTest | Level3)
348 {
349 struct Rect baseRect = {0, 0, 100, 200};
350 uint32_t baseFlags = 0;
351 sptr<Window> baseWindow = CreateBaseWindow(WindowType::WINDOW_TYPE_DIALOG, baseRect, baseFlags);
352 if (baseWindow == nullptr) {
353 return;
354 }
355 ASSERT_NE(nullptr, baseWindow);
356
357 struct Rect rect = {0, 0, 100, 200};
358 uint32_t flags = 0;
359 sptr<Window> subWindow = CreateSystemSubWindow(baseWindow, rect, flags);
360 if (subWindow == nullptr) {
361 return;
362 }
363 ASSERT_NE(nullptr, subWindow);
364 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
365 }
366 } // namespace Rosen
367 } // namespace OHOS
368