• 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 <ability_context.h>
19 #include "window.h"
20 #include "window_option.h"
21 #include "window_scene.h"
22 #include "wm_common.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Rosen {
29 class WindowSubWindowTest : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33     virtual void SetUp() override;
34     virtual void TearDown() override;
35 };
36 
SetUpTestCase()37 void WindowSubWindowTest::SetUpTestCase()
38 {
39 }
40 
TearDownTestCase()41 void WindowSubWindowTest::TearDownTestCase()
42 {
43 }
44 
SetUp()45 void WindowSubWindowTest::SetUp()
46 {
47 }
48 
TearDown()49 void WindowSubWindowTest::TearDown()
50 {
51 }
52 
CreateWindowScene()53 static sptr<WindowScene> CreateWindowScene()
54 {
55     sptr<IWindowLifeCycle> listener = nullptr;
56     std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext = nullptr;
57 
58     sptr<WindowScene> scene = new WindowScene();
59     scene->Init(0, abilityContext, listener);
60     return scene;
61 }
62 
CreateSubWindow(sptr<WindowScene> scene,WindowType type,struct Rect rect,uint32_t flags,std::string name="")63 static sptr<Window> CreateSubWindow(sptr<WindowScene> scene, WindowType type,
64     struct Rect rect, uint32_t flags, std::string name = "")
65 {
66     sptr<WindowOption> subOp = new WindowOption();
67     subOp->SetWindowType(type);
68     subOp->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
69     subOp->SetWindowRect(rect);
70     subOp->SetWindowFlags(flags);
71 
72     static int cnt = 0;
73     std::string subWinName = (name == "") ? "SubWindow" + std::to_string(cnt++) : name;
74 
75     return scene->CreateWindow(subWinName, subOp);
76 }
77 
78 /**
79  * @tc.name: SubWindow01
80  * @tc.desc: FullScreen Main Window + Floating SubWindow
81  * @tc.type: FUNC
82  */
83 HWTEST_F(WindowSubWindowTest, SubWindow01, Function | MediumTest | Level2)
84 {
85     sptr<WindowScene> scene = CreateWindowScene();
86 
87     struct Rect rect = {0, 0, 100, 200};
88     uint32_t flags = 0;
89     sptr<Window> subWindow = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, flags);
90     ASSERT_NE(nullptr, subWindow);
91 
92     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
93     ASSERT_EQ(WMError::WM_OK, subWindow->Show());
94 
95     ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
96     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
97 
98     subWindow->Destroy();
99     scene->GoDestroy();
100 }
101 
102 /**
103  * @tc.name: SubWindow02
104  * @tc.desc: FullScreen Main Window + Floating SubWindow & Parent Limit work
105  * @tc.type: FUNC
106  */
107 HWTEST_F(WindowSubWindowTest, SubWindow02, Function | MediumTest | Level2)
108 {
109     sptr<WindowScene> scene = CreateWindowScene();
110 
111     struct Rect rect = {0, 0, 100, 200};
112     uint32_t flags = static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_PARENT_LIMIT);
113     sptr<Window> subWindow = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, flags);
114     ASSERT_NE(nullptr, subWindow);
115 
116     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
117     ASSERT_EQ(WMError::WM_OK, subWindow->Show());
118 
119     ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
120     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
121 
122     subWindow->Destroy();
123     scene->GoDestroy();
124 }
125 
126 /**
127  * @tc.name: SubWindow03
128  * @tc.desc: FullScreen Main Window + Floating MediaWindow
129  * @tc.type: FUNC
130  */
131 HWTEST_F(WindowSubWindowTest, SubWindow03, Function | MediumTest | Level2)
132 {
133     sptr<WindowScene> scene = CreateWindowScene();
134 
135     struct Rect rect = {0, 2000, 100, 200};
136     uint32_t flags = static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_PARENT_LIMIT);
137     sptr<Window> subWindow = CreateSubWindow(scene, WindowType::WINDOW_TYPE_MEDIA, rect, flags);
138     ASSERT_NE(nullptr, subWindow);
139 
140     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
141     ASSERT_EQ(WMError::WM_OK, subWindow->Show());
142 
143     ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
144     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
145 
146     subWindow->Destroy();
147     scene->GoDestroy();
148 }
149 
150 /**
151  * @tc.name: SubWindow04
152  * @tc.desc: FullScreen Main Window + Floating MediaWindow
153  * @tc.type: FUNC
154  */
155 HWTEST_F(WindowSubWindowTest, SubWindow04, Function | MediumTest | Level2)
156 {
157     sptr<WindowScene> scene = CreateWindowScene();
158 
159     struct Rect rect = {0, 2000, 3000, 2000};
160     uint32_t flags = static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_PARENT_LIMIT);
161     sptr<Window> subWindow = CreateSubWindow(scene, WindowType::WINDOW_TYPE_MEDIA, rect, flags);
162     ASSERT_NE(nullptr, subWindow);
163 
164     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
165     ASSERT_EQ(WMError::WM_OK, subWindow->Show());
166 
167     ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
168     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
169 
170     subWindow->Destroy();
171     scene->GoDestroy();
172 }
173 
174 /**
175  * @tc.name: SubWindow05
176  * @tc.desc: FullScreen Main Window + Floating MediaWindow + Floating SubWindow
177  * @tc.type: FUNC
178  */
179 HWTEST_F(WindowSubWindowTest, SubWindow05, Function | MediumTest | Level3)
180 {
181     sptr<WindowScene> scene = CreateWindowScene();
182 
183     struct Rect rect = {0, 0, 100, 200};
184     uint32_t flags = static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_PARENT_LIMIT);
185     sptr<Window> subWindow = CreateSubWindow(scene, WindowType::WINDOW_TYPE_MEDIA, rect, flags);
186     ASSERT_NE(nullptr, subWindow);
187 
188     sptr<Window> subWindow2 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, flags);
189     ASSERT_NE(nullptr, subWindow2);
190 
191     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
192     ASSERT_EQ(WMError::WM_OK, subWindow->Show());
193     ASSERT_EQ(WMError::WM_OK, subWindow2->Show());
194 
195     ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
196     ASSERT_EQ(WMError::WM_OK, subWindow2->Hide());
197     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
198 
199     subWindow->Destroy();
200     subWindow2->Destroy();
201     scene->GoDestroy();
202 }
203 
204 /**
205  * @tc.name: SubWindow06
206  * @tc.desc: FullScreen Main Window + 2 SubWindows
207  * @tc.type: FUNC
208  */
209 HWTEST_F(WindowSubWindowTest, SubWindow06, Function | MediumTest | Level3)
210 {
211     sptr<WindowScene> scene = CreateWindowScene();
212 
213     struct Rect rect = {0, 0, 100, 200};
214     sptr<Window> subWindow0 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, 0);
215     ASSERT_NE(nullptr, subWindow0);
216 
217     sptr<Window> subWindow1 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, 0);
218     ASSERT_NE(nullptr, subWindow1);
219 
220     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
221     ASSERT_EQ(WMError::WM_OK, subWindow0->Show());
222     ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
223 
224     ASSERT_EQ(WMError::WM_OK, subWindow0->Hide());
225     ASSERT_EQ(WMError::WM_OK, subWindow1->Hide());
226     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
227 
228     subWindow0->Destroy();
229     subWindow1->Destroy();
230     scene->GoDestroy();
231 }
232 
233 /**
234  * @tc.name: SubWindow07
235  * @tc.desc: FullScreen Main Window + Floating SubWindow & MainWindow First GoBackground
236  * @tc.type: FUNC
237  */
238 HWTEST_F(WindowSubWindowTest, SubWindow07, Function | MediumTest | Level4)
239 {
240     sptr<WindowScene> scene = CreateWindowScene();
241 
242     struct Rect rect = {0, 0, 100, 200};
243     uint32_t flags = static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_PARENT_LIMIT);
244     sptr<Window> subWindow = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, flags);
245     ASSERT_NE(nullptr, subWindow);
246 
247     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
248     ASSERT_EQ(WMError::WM_OK, subWindow->Show());
249 
250     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
251     ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
252 
253     subWindow->Destroy();
254     scene->GoDestroy();
255 }
256 
257 /**
258  * @tc.name: SubWindow08
259  * @tc.desc: FullScreen Main Window + Floating SubWindow & only show SubWindow
260  * @tc.type: FUNC
261  */
262 HWTEST_F(WindowSubWindowTest, SubWindow08, Function | MediumTest | Level4)
263 {
264     sptr<WindowScene> scene = CreateWindowScene();
265 
266     struct Rect rect = {0, 0, 100, 200};
267     uint32_t flags = static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_PARENT_LIMIT);
268     sptr<Window> subWindow = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, flags);
269     ASSERT_NE(nullptr, subWindow);
270 
271     ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM, subWindow->Show());
272 }
273 
274 /**
275  * @tc.name: SubWindow09
276  * @tc.desc: FullScreen Main Window + Floating SubWindow & first destroy SubWindow, then destroy MainWindow
277  * @tc.type: FUNC
278  */
279 HWTEST_F(WindowSubWindowTest, SubWindow09, Function | MediumTest | Level2)
280 {
281     sptr<WindowScene> scene = CreateWindowScene();
282 
283     struct Rect rect = {0, 0, 100, 200};
284     uint32_t flags = static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_PARENT_LIMIT);
285     sptr<Window> subWindow = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, flags);
286     ASSERT_NE(nullptr, subWindow);
287 
288     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
289     ASSERT_EQ(WMError::WM_OK, subWindow->Show());
290 
291     ASSERT_EQ(WMError::WM_OK, subWindow->Hide());
292     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
293 
294     ASSERT_EQ(WMError::WM_OK, subWindow->Destroy());
295 
296     subWindow->Destroy();
297     scene->GoDestroy();
298 }
299 
300 /**
301  * @tc.name: SubWindow10
302  * @tc.desc: FullScreen Main Window + Floating SubWindow & first destroy MainWindow, then destroy SubWindow
303  * @tc.type: FUNC
304  */
305 HWTEST_F(WindowSubWindowTest, SubWindow10, Function | MediumTest | Level2)
306 {
307     sptr<WindowScene> scene = CreateWindowScene();
308 
309     struct Rect rect = {0, 0, 100, 200};
310     uint32_t flags = static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_PARENT_LIMIT);
311     sptr<Window> subWindow = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, flags);
312     ASSERT_NE(nullptr, subWindow);
313 
314     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
315     ASSERT_EQ(WMError::WM_OK, subWindow->Show());
316 
317     sptr<Window> mainWindow = scene->GetMainWindow();
318     ASSERT_EQ(WMError::WM_OK, mainWindow->Destroy());
319     ASSERT_EQ(WMError::WM_ERROR_DESTROYED_OBJECT, subWindow->Destroy());
320 
321     scene->GoDestroy();
322 }
323 
324 /**
325  * @tc.name: SubWindow11
326  * @tc.desc: FullScreen Main Window + 5 subWindows
327  * @tc.type: FUNC
328  */
329 HWTEST_F(WindowSubWindowTest, SubWindow11, Function | MediumTest | Level3)
330 {
331     sptr<WindowScene> scene = CreateWindowScene();
332 
333     struct Rect rect = {0, 0, 100, 200};
334     sptr<Window> subWindow0 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, 0);
335     ASSERT_NE(nullptr, subWindow0);
336 
337     sptr<Window> subWindow1 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, 0);
338     ASSERT_NE(nullptr, subWindow1);
339 
340     sptr<Window> subWindow2 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, 0);
341     ASSERT_NE(nullptr, subWindow2);
342 
343     sptr<Window> subWindow3 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, 0);
344     ASSERT_NE(nullptr, subWindow3);
345 
346     sptr<Window> subWindow4 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, 0);
347     ASSERT_NE(nullptr, subWindow4);
348 
349     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
350     ASSERT_EQ(WMError::WM_OK, subWindow0->Show());
351     ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
352     ASSERT_EQ(WMError::WM_OK, subWindow2->Show());
353     ASSERT_EQ(WMError::WM_OK, subWindow3->Show());
354     ASSERT_EQ(WMError::WM_OK, subWindow4->Show());
355 
356     ASSERT_EQ(WMError::WM_OK, subWindow0->Hide());
357     ASSERT_EQ(WMError::WM_OK, subWindow1->Hide());
358     ASSERT_EQ(WMError::WM_OK, subWindow2->Hide());
359     ASSERT_EQ(WMError::WM_OK, subWindow3->Hide());
360     ASSERT_EQ(WMError::WM_OK, subWindow4->Hide());
361     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
362 
363     subWindow0->Destroy();
364     subWindow1->Destroy();
365     subWindow2->Destroy();
366     subWindow3->Destroy();
367     subWindow4->Destroy();
368     scene->GoDestroy();
369 }
370 
371 /**
372  * @tc.name: SubWindow12
373  * @tc.desc: FullScreen Main Window + 2 SubWindows with the same name
374  * @tc.type: FUNC
375  */
376 HWTEST_F(WindowSubWindowTest, SubWindow12, Function | MediumTest | Level3)
377 {
378     sptr<WindowScene> scene = CreateWindowScene();
379 
380     struct Rect rect = {0, 0, 100, 200};
381     sptr<Window> subWindow0 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, 0, "sub0");
382     sptr<Window> subWindow1 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, 0, "sub0");
383     ASSERT_NE(nullptr, subWindow0);
384     ASSERT_EQ(nullptr, subWindow1);
385 
386     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
387     ASSERT_EQ(WMError::WM_OK, subWindow0->Show());
388 
389     ASSERT_EQ(WMError::WM_OK, subWindow0->Hide());
390     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
391 
392     subWindow0->Destroy();
393     scene->GoDestroy();
394 }
395 
396 /**
397  * @tc.name: SubWindow13
398  * @tc.desc: FullScreen Main Window + 2 subwindows with the same name but one create after another destroyed
399  * @tc.type: FUNC
400  */
401 HWTEST_F(WindowSubWindowTest, SubWindow13, Function | MediumTest | Level3)
402 {
403     sptr<WindowScene> scene = CreateWindowScene();
404     ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
405 
406     struct Rect rect = {0, 0, 100, 200};
407     sptr<Window> subWindow0 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, 0, "sub1");
408     ASSERT_NE(nullptr, subWindow0);
409     ASSERT_EQ(WMError::WM_OK, subWindow0->Show());
410     ASSERT_EQ(WMError::WM_OK, subWindow0->Destroy());
411 
412 
413     sptr<Window> subWindow1 = CreateSubWindow(scene, WindowType::WINDOW_TYPE_APP_SUB_WINDOW, rect, 0, "sub1");
414     ASSERT_NE(nullptr, subWindow1);
415     ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
416     ASSERT_EQ(WMError::WM_OK, subWindow1->Destroy());
417 
418     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
419     scene->GoDestroy();
420 }
421 } // namespace Rosen
422 } // namespace OHOS
423