1 /*
2 * Copyright (c) 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_impl.h"
19 #include "ipc_skeleton.h"
20 #include "window.h"
21 #include "window_manager.h"
22 #include "window_option.h"
23 #include "window_scene.h"
24 #include "window_test_utils.h"
25 #include "wm_common.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Rosen {
32 namespace {
33 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowAppFloatingWindowTest"};
34 }
35
36 class TestCameraFloatWindowChangedListener : public ICameraFloatWindowChangedListener {
37 public:
38 uint32_t accessTokenId_ = 0;
39 bool isShowing_ = false;
40 void OnCameraFloatWindowChange(uint32_t accessTokenId, bool isShowing) override;
41 };
42
43 class WindowAppFloatingWindowTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 virtual void SetUp() override;
48 virtual void TearDown() override;
49
50 static inline float virtualPixelRatio_ = 1.0;
51 static inline Rect displayRect_ {0, 0, 0, 0};
52 static inline std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_ = nullptr;
53 static sptr<TestCameraFloatWindowChangedListener> testCameraFloatWindowChangedListener_;
54 };
55
56 sptr<TestCameraFloatWindowChangedListener> WindowAppFloatingWindowTest::testCameraFloatWindowChangedListener_ =
57 new TestCameraFloatWindowChangedListener();
58
OnCameraFloatWindowChange(uint32_t accessTokenId,bool isShowing)59 void TestCameraFloatWindowChangedListener::OnCameraFloatWindowChange(uint32_t accessTokenId, bool isShowing)
60 {
61 WLOGFI("TestCameraFloatWindowChangedListener [%{public}u, %{public}u]", accessTokenId, isShowing);
62 accessTokenId_ = accessTokenId;
63 isShowing_ = isShowing;
64 }
65
SetUpTestCase()66 void WindowAppFloatingWindowTest::SetUpTestCase()
67 {
68 auto display = DisplayManager::GetInstance().GetDisplayById(0);
69 ASSERT_TRUE((display != nullptr));
70 displayRect_.width_ = display->GetWidth();
71 displayRect_.height_ = display->GetHeight();
72 WindowTestUtils::InitByDisplayRect(displayRect_);
73 virtualPixelRatio_ = WindowTestUtils::GetVirtualPixelRatio(0);
74 }
75
TearDownTestCase()76 void WindowAppFloatingWindowTest::TearDownTestCase()
77 {
78 }
79
SetUp()80 void WindowAppFloatingWindowTest::SetUp()
81 {
82 }
83
TearDown()84 void WindowAppFloatingWindowTest::TearDown()
85 {
86 }
87
CreateWindowScene()88 static sptr<WindowScene> CreateWindowScene()
89 {
90 sptr<IWindowLifeCycle> listener = nullptr;
91 WindowAppFloatingWindowTest::abilityContext_ = std::make_shared<AbilityRuntime::AbilityContextImpl>();
92
93 sptr<WindowScene> scene = new WindowScene();
94 scene->Init(0, WindowAppFloatingWindowTest::abilityContext_, listener);
95 return scene;
96 }
97
CreateAppFloatingWindow(WindowType type,Rect rect,std::string name="")98 static sptr<Window> CreateAppFloatingWindow(WindowType type, Rect rect, std::string name = "")
99 {
100 sptr<WindowOption> option = new WindowOption();
101 option->SetWindowType(type);
102 option->SetWindowRect(rect);
103
104 static int cnt = 0;
105 std::string winName = (name == "") ? "FloatingWindowTest" + std::to_string(cnt++) : name;
106
107 return Window::Create(winName, option, WindowAppFloatingWindowTest::abilityContext_);
108 }
109
GetRectWithVpr(int32_t x,int32_t y,uint32_t w,uint32_t h)110 static inline Rect GetRectWithVpr(int32_t x, int32_t y, uint32_t w, uint32_t h)
111 {
112 auto vpr = WindowAppFloatingWindowTest::virtualPixelRatio_;
113 return {x, y, static_cast<uint32_t>(w * vpr), static_cast<uint32_t>(h * vpr)};
114 }
115
116 /**
117 * @tc.name: AppFloatingWindow01
118 * @tc.desc: AppFloatingWindow life cycle
119 * @tc.type: FUNC
120 */
121 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow01, Function | MediumTest | Level2)
122 {
123 sptr<WindowScene> scene = CreateWindowScene();
124 ASSERT_NE(nullptr, scene);
125
126 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
127 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT, fltWindRect);
128 ASSERT_NE(nullptr, fltWin);
129
130 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
131 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
132
133 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
134 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
135
136 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
137 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
138 }
139
140 /**
141 * @tc.name: AppFloatingWindow02
142 * @tc.desc: AppFloatingWindow life cycle, main window hide first
143 * @tc.type: FUNC
144 */
145 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow02, Function | MediumTest | Level3)
146 {
147 sptr<WindowScene> scene = CreateWindowScene();
148 ASSERT_NE(nullptr, scene);
149
150 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
151 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT, fltWindRect);
152 ASSERT_NE(nullptr, fltWin);
153
154 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
155 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
156
157 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
158 ASSERT_EQ(false, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
159 ASSERT_EQ(true, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
160 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
161
162 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
163 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
164 }
165
166 /**
167 * @tc.name: AppFloatingWindow03
168 * @tc.desc: AppFloatingWindow life cycle, app floating window hide first
169 * @tc.type: FUNC
170 */
171 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow03, Function | MediumTest | Level3)
172 {
173 sptr<WindowScene> scene = CreateWindowScene();
174 ASSERT_NE(nullptr, scene);
175
176 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
177 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT, fltWindRect);
178 ASSERT_NE(nullptr, fltWin);
179
180 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
181 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
182
183 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
184 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
185 ASSERT_EQ(true, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
186 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
187
188 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
189 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
190 }
191
192 /**
193 * @tc.name: AppFloatingWindow04
194 * @tc.desc: AppFloatingWindow life cycle, main window destroy first
195 * @tc.type: FUNC
196 */
197 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow04, Function | MediumTest | Level3)
198 {
199 sptr<WindowScene> scene = CreateWindowScene();
200 ASSERT_NE(nullptr, scene);
201
202 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
203 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT, fltWindRect);
204 ASSERT_NE(nullptr, fltWin);
205
206 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
207 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
208
209 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
210 ASSERT_EQ(nullptr, scene->GetMainWindow());
211 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
212 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
213 }
214
215 /**
216 * @tc.name: AppFloatingWindow05
217 * @tc.desc: Camera AppFloatingWindow life cycle
218 * @tc.type: FUNC
219 * @tc.require: issueI5NEHO
220 */
221 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow05, Function | MediumTest | Level2)
222 {
223 sptr<WindowScene> scene = CreateWindowScene();
224 ASSERT_NE(nullptr, scene);
225
226 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
227 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
228 ASSERT_NE(nullptr, fltWin);
229
230 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
231 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
232
233 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
234 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
235
236 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
237 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
238 }
239
240 /**
241 * @tc.name: AppFloatingWindow06
242 * @tc.desc: Camera AppFloatingWindow life cycle, main window hide first
243 * @tc.type: FUNC
244 * @tc.require: issueI5NEHO
245 */
246 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow06, Function | MediumTest | Level3)
247 {
248 sptr<WindowScene> scene = CreateWindowScene();
249 ASSERT_NE(nullptr, scene);
250
251 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
252 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
253 ASSERT_NE(nullptr, fltWin);
254
255 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
256 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
257
258 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
259 ASSERT_EQ(false, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
260 ASSERT_EQ(true, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
261 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
262
263 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
264 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
265 }
266
267 /**
268 * @tc.name: AppFloatingWindow07
269 * @tc.desc: Camera AppFloatingWindow life cycle, app floating window hide first
270 * @tc.type: FUNC
271 * @tc.require: issueI5NEHO
272 */
273 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow07, Function | MediumTest | Level3)
274 {
275 sptr<WindowScene> scene = CreateWindowScene();
276 ASSERT_NE(nullptr, scene);
277
278 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
279 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
280 ASSERT_NE(nullptr, fltWin);
281
282 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
283 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
284
285 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
286 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
287 ASSERT_EQ(true, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
288 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
289
290 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
291 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
292 }
293
294 /**
295 * @tc.name: AppFloatingWindow08
296 * @tc.desc: Camera AppFloatingWindow life cycle, main window destroy first
297 * @tc.type: FUNC
298 * @tc.require: issueI5NEHO
299 */
300 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow08, Function | MediumTest | Level3)
301 {
302 sptr<WindowScene> scene = CreateWindowScene();
303 ASSERT_NE(nullptr, scene);
304
305 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
306 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
307 ASSERT_NE(nullptr, fltWin);
308
309 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
310 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
311
312 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
313 ASSERT_EQ(nullptr, scene->GetMainWindow());
314 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
315 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
316 }
317
318 /**
319 * @tc.name: AppFloatingWindow09
320 * @tc.desc: Camera AppFloatingWindow rect check
321 * @tc.type: FUNC
322 * @tc.require: issueI5NEHO
323 */
324 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow09, Function | MediumTest | Level3)
325 {
326 sptr<WindowScene> scene = CreateWindowScene();
327 ASSERT_NE(nullptr, scene);
328
329 Rect fltWindRect = GetRectWithVpr(10, 20, 10, 10);
330 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
331 ASSERT_NE(nullptr, fltWin);
332
333 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
334 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
335
336 Rect exceptRect = {10, 20, 0, 0};
337 uint32_t smallWidth = displayRect_.height_ <= displayRect_.width_ ? displayRect_.height_ : displayRect_.width_;
338 float hwRatio = static_cast<float>(displayRect_.height_) / static_cast<float>(displayRect_.width_);
339 if (smallWidth <= static_cast<uint32_t>(600 * virtualPixelRatio_)) { // sw <= 600dp
340 if (displayRect_.width_ <= displayRect_.height_) {
341 exceptRect.width_= static_cast<uint32_t>(smallWidth * 0.3);
342 } else {
343 exceptRect.width_ = static_cast<uint32_t>(smallWidth * 0.5);
344 }
345 } else {
346 if (displayRect_.width_ <= displayRect_.height_) {
347 exceptRect.width_ = static_cast<uint32_t>(smallWidth * 0.12);
348 } else {
349 exceptRect.width_ = static_cast<uint32_t>(smallWidth * 0.3);
350 }
351 }
352 exceptRect.height_ = static_cast<uint32_t>(exceptRect.width_ * hwRatio);
353 ASSERT_TRUE(WindowTestUtils::RectEqualTo(fltWin, exceptRect));
354
355 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
356 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
357 }
358
359 /**
360 * @tc.name: AppFloatingWindow10
361 * @tc.desc: Camera AppFloatingWindow multi create
362 * @tc.type: FUNC
363 * @tc.require: issueI5NEHO
364 */
365 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow10, Function | MediumTest | Level3)
366 {
367 sptr<WindowScene> scene = CreateWindowScene();
368 ASSERT_NE(nullptr, scene);
369
370 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
371 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
372 ASSERT_NE(nullptr, fltWin);
373
374 sptr<Window> fltWin2 = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
375 ASSERT_EQ(nullptr, fltWin2);
376
377 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
378 sptr<Window> fltWin3 = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
379 ASSERT_NE(nullptr, fltWin3);
380
381 ASSERT_EQ(WMError::WM_OK, fltWin3->Destroy());
382 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
383 }
384
385 /**
386 * @tc.name: AppFloatingWindow11
387 * @tc.desc: Camera AppFloatingWindow listener
388 * @tc.type: FUNC
389 * @tc.require: issueI5NEHR
390 */
391 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow11, Function | MediumTest | Level2)
392 {
393 uint32_t tokenId = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
394 WindowManager::GetInstance().RegisterCameraFloatWindowChangedListener(testCameraFloatWindowChangedListener_);
395 sptr<WindowScene> scene = CreateWindowScene();
396 ASSERT_NE(nullptr, scene);
397
398 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
399 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
400 ASSERT_NE(nullptr, fltWin);
401
402 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
403 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
404
405 usleep(500000); // 500000us = 0.5s
406 ASSERT_EQ(tokenId, testCameraFloatWindowChangedListener_->accessTokenId_);
407 ASSERT_EQ(true, testCameraFloatWindowChangedListener_->isShowing_);
408
409 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
410
411 usleep(500000); // 500000us = 0.5s
412 ASSERT_EQ(tokenId, testCameraFloatWindowChangedListener_->accessTokenId_);
413 ASSERT_EQ(false, testCameraFloatWindowChangedListener_->isShowing_);
414
415 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
416 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
417
418 WindowManager::GetInstance().UnregisterCameraFloatWindowChangedListener(testCameraFloatWindowChangedListener_);
419 }
420 } // namespace Rosen
421 } // namespace OHOS
422