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 ASSERT_NE(nullptr, baseWindow);
117 struct Rect rect = {0, 0, 100, 200};
118 uint32_t flags = 0;
119 sptr<Window> subWindow = CreateSystemSubWindow(baseWindow, rect, flags);
120 if (subWindow == nullptr) {
121 return;
122 }
123
124 ASSERT_NE(nullptr, subWindow);
125
126 ASSERT_EQ(WMError::WM_OK, baseWindow->Show());
127 ASSERT_EQ(WMError::WM_OK, subWindow->Show());
128
129 ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
130 ASSERT_EQ(WMError::WM_OK, baseWindow->Hide());
131
132 ASSERT_EQ(WMError::WM_OK, subWindow->Destroy());
133 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
134 }
135 }
136
137 /**
138 * @tc.name: SystemSubWindow02
139 * @tc.desc: create sub windows with above system Windows except WINDOW_TYPE_DIALOG
140 * @tc.type: FUNC
141 */
142 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow02, Function | MediumTest | Level2)
143 {
144 std::vector<WindowType> windowTypes = {
145 WindowType::WINDOW_TYPE_APP_LAUNCHING,
146 WindowType::WINDOW_TYPE_DOCK_SLICE,
147 WindowType::WINDOW_TYPE_INCOMING_CALL,
148 WindowType::WINDOW_TYPE_SEARCHING_BAR,
149 WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW,
150 WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT,
151 WindowType::WINDOW_TYPE_FLOAT,
152 WindowType::WINDOW_TYPE_TOAST,
153 WindowType::WINDOW_TYPE_STATUS_BAR,
154 WindowType::WINDOW_TYPE_PANEL,
155 WindowType::WINDOW_TYPE_VOLUME_OVERLAY,
156 WindowType::WINDOW_TYPE_NAVIGATION_BAR,
157 WindowType::WINDOW_TYPE_DRAGGING_EFFECT,
158 WindowType::WINDOW_TYPE_POINTER,
159 WindowType::WINDOW_TYPE_LAUNCHER_RECENT,
160 WindowType::WINDOW_TYPE_LAUNCHER_DOCK,
161 WindowType::WINDOW_TYPE_BOOT_ANIMATION,
162 WindowType::WINDOW_TYPE_FREEZE_DISPLAY,
163 WindowType::WINDOW_TYPE_VOICE_INTERACTION,
164 WindowType::WINDOW_TYPE_FLOAT_CAMERA,
165 WindowType::WINDOW_TYPE_PLACEHOLDER,
166 WindowType::WINDOW_TYPE_SCREENSHOT,
167 WindowType::WINDOW_TYPE_GLOBAL_SEARCH,
168 };
169 for (auto itor = windowTypes.begin(); itor != windowTypes.end(); itor++) {
170 struct Rect baseRect = {0, 0, 100, 200};
171 uint32_t baseFlags = 0;
172 sptr<Window> baseWindow = CreateBaseWindow(static_cast<WindowType>(*itor), baseRect, baseFlags);
173 ASSERT_NE(nullptr, baseWindow);
174
175 struct Rect rect = {0, 0, 100, 200};
176 uint32_t flags = 0;
177 sptr<Window> subWindow = CreateSystemSubWindow(baseWindow, rect, flags);
178 if (subWindow == nullptr) {
179 return;
180 }
181 ASSERT_NE(nullptr, subWindow);
182
183 ASSERT_EQ(WMError::WM_OK, baseWindow->Show());
184 ASSERT_EQ(WMError::WM_OK, subWindow->Show());
185
186 ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
187 ASSERT_EQ(WMError::WM_OK, baseWindow->Hide());
188
189 ASSERT_EQ(WMError::WM_OK, subWindow->Destroy());
190 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
191 }
192 }
193
194 /**
195 * @tc.name: SystemSubWindow03
196 * @tc.desc: create sub windows with app main Windows, no allow to add as app_main_window's subwindow
197 * @tc.type: FUNC
198 */
199 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow03, Function | MediumTest | Level2)
200 {
201
202 std::vector<WindowType> windowTypes = { WindowType::WINDOW_TYPE_APP_MAIN_WINDOW };
203 for (auto itor = windowTypes.begin(); itor != windowTypes.end(); itor++) {
204 struct Rect baseRect = {0, 0, 100, 200};
205 uint32_t baseFlags = 0;
206 sptr<Window> baseWindow = CreateBaseWindow(static_cast<WindowType>(*itor), baseRect, baseFlags);
207 if (baseWindow == nullptr) {
208 return;
209 }
210 ASSERT_NE(nullptr, baseWindow);
211
212 struct Rect rect = {0, 0, 100, 200};
213 uint32_t flags = 0;
214 sptr<Window> subWindow = CreateSystemSubWindow(baseWindow, rect, flags);
215 if (subWindow == nullptr) {
216 return;
217 }
218 ASSERT_EQ(nullptr, subWindow);
219 }
220 }
221
222 /**
223 * @tc.name: SystemSubWindow04
224 * @tc.desc: create sub windows with app sub Windows
225 * @tc.type: FUNC
226 */
227 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow04, Function | MediumTest | Level2)
228 {
229 std::vector<WindowType> windowTypes = {
230 WindowType::WINDOW_TYPE_MEDIA,
231 WindowType::WINDOW_TYPE_APP_SUB_WINDOW,
232 WindowType::WINDOW_TYPE_APP_COMPONENT,
233 };
234 for (auto itor = windowTypes.begin(); itor != windowTypes.end(); itor++) {
235 struct Rect baseRect = {0, 0, 100, 200};
236 uint32_t baseFlags = 0;
237 sptr<Window> baseWindow = CreateBaseWindow(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW, baseRect, baseFlags);
238 if (baseWindow == nullptr) {
239 return;
240 }
241 ASSERT_NE(nullptr, baseWindow);
242
243 sptr<Window> appSubWindow = CreateAppSubWindow(baseWindow, static_cast<WindowType>(*itor), baseRect, baseFlags);
244 if (appSubWindow == nullptr) {
245 return;
246 }
247 ASSERT_NE(nullptr, appSubWindow);
248
249 struct Rect rect = {0, 0, 100, 200};
250 uint32_t flags = 0;
251 sptr<Window> subWindow = CreateSystemSubWindow(appSubWindow, rect, flags);
252 ASSERT_EQ(nullptr, subWindow);
253 ASSERT_EQ(WMError::WM_OK, appSubWindow->Destroy());
254 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
255 }
256 }
257
258 /**
259 * @tc.name: SystemSubWindow05
260 * @tc.desc: create sub windows with system sub Windows
261 * @tc.type: FUNC
262 */
263 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow05, Function | MediumTest | Level3)
264 {
265 struct Rect baseRect = {0, 0, 100, 200};
266 uint32_t baseFlags = 0;
267 sptr<Window> baseWindow = CreateBaseWindow(WindowType::WINDOW_TYPE_DOCK_SLICE, baseRect, baseFlags);
268 if (baseWindow == nullptr) {
269 return;
270 }
271 ASSERT_NE(nullptr, baseWindow);
272
273 sptr<Window> systemSubWindow = CreateSystemSubWindow(baseWindow, baseRect, baseFlags);
274 if (systemSubWindow == nullptr) {
275 return;
276 }
277 ASSERT_NE(nullptr, systemSubWindow);
278
279 struct Rect rect = {0, 0, 100, 200};
280 uint32_t flags = 0;
281 sptr<Window> subWindow = CreateSystemSubWindow(systemSubWindow, rect, flags);
282 if (subWindow == nullptr) {
283 return;
284 }
285 ASSERT_EQ(nullptr, subWindow);
286
287 ASSERT_EQ(WMError::WM_OK, baseWindow->Show());
288 ASSERT_EQ(WMError::WM_OK, systemSubWindow->Show());
289
290 ASSERT_EQ(WMError::WM_OK, systemSubWindow->Hide());
291 ASSERT_EQ(WMError::WM_OK, baseWindow->Hide());
292
293 ASSERT_EQ(WMError::WM_OK, systemSubWindow->Destroy());
294 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
295 }
296
297 /**
298 * @tc.name: SystemSubWindow06
299 * @tc.desc: FullScreen Main Window + 2 SubWindows
300 * @tc.type: FUNC
301 */
302 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow06, Function | MediumTest | Level3)
303 {
304 struct Rect baseRect = {0, 0, 100, 200};
305 uint32_t baseFlags = 0;
306 sptr<Window> baseWindow = CreateBaseWindow(WindowType::WINDOW_TYPE_DOCK_SLICE, baseRect, baseFlags);
307 if (baseWindow == nullptr) {
308 return;
309 }
310 ASSERT_NE(nullptr, baseWindow);
311
312 struct Rect rect = {0, 0, 100, 200};
313 uint32_t flags = 0;
314 sptr<Window> subWindow = CreateSystemSubWindow(baseWindow, rect, flags);
315 if (subWindow == nullptr) {
316 return;
317 }
318 ASSERT_NE(nullptr, subWindow);
319
320 ASSERT_EQ(WMError::WM_OK, baseWindow->Show());
321 ASSERT_EQ(WMError::WM_OK, subWindow->Show());
322
323 bool isFocus = subWindow->GetFocusable();
324 ASSERT_EQ(WMError::WM_OK, subWindow->SetFocusable(!isFocus));
325 ASSERT_EQ(WMError::WM_OK, subWindow->MoveTo(0, 0));
326 ASSERT_EQ(WMError::WM_OK, subWindow->Resize(200, 400));
327 ASSERT_EQ(WMError::WM_OK, subWindow->SetTurnScreenOn(true));
328
329 ASSERT_EQ(WMError::WM_ERROR_INVALID_TYPE, subWindow->SetBrightness(0.5f));
330 ASSERT_EQ(WMError::WM_OK, subWindow->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN));
331
332 ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
333 ASSERT_EQ(WMError::WM_OK, baseWindow->Hide());
334
335 ASSERT_EQ(WMError::WM_OK, subWindow->Destroy());
336 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
337 }
338 /**
339 * @tc.name: SystemSubWindow07
340 * @tc.desc: create sub windows with dialog
341 * @tc.type: FUNC
342 */
343 HWTEST_F(WindowSystemSubWindowTest, SystemSubWindow07, Function | MediumTest | Level3)
344 {
345 struct Rect baseRect = {0, 0, 100, 200};
346 uint32_t baseFlags = 0;
347 sptr<Window> baseWindow = CreateBaseWindow(WindowType::WINDOW_TYPE_DIALOG, baseRect, baseFlags);
348 if (baseWindow == nullptr) {
349 return;
350 }
351 ASSERT_NE(nullptr, baseWindow);
352
353 struct Rect rect = {0, 0, 100, 200};
354 uint32_t flags = 0;
355 sptr<Window> subWindow = CreateSystemSubWindow(baseWindow, rect, flags);
356 if (subWindow == nullptr) {
357 return;
358 }
359 ASSERT_EQ(nullptr, subWindow);
360 ASSERT_EQ(WMError::WM_OK, baseWindow->Destroy());
361 }
362 } // namespace Rosen
363 } // namespace OHOS
364