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