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