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 "common_test_utils.h"
20 #include "ipc_skeleton.h"
21 #include "scene_board_judgement.h"
22 #include "window.h"
23 #include "window_manager.h"
24 #include "window_option.h"
25 #include "window_scene.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
SetUp()80 void WindowAppFloatingWindowTest::SetUp()
81 {
82 CommonTestUtils::GuaranteeFloatWindowPermission("wms_window_app_floating_window_test");
83 }
84
TearDown()85 void WindowAppFloatingWindowTest::TearDown() {}
86
CreateWindowScene()87 static sptr<WindowScene> CreateWindowScene()
88 {
89 sptr<IWindowLifeCycle> listener = nullptr;
90 WindowAppFloatingWindowTest::abilityContext_ = std::make_shared<AbilityRuntime::AbilityContextImpl>();
91
92 sptr<WindowScene> scene = new WindowScene();
93 scene->Init(0, WindowAppFloatingWindowTest::abilityContext_, listener);
94 return scene;
95 }
96
CreateAppFloatingWindow(WindowType type,Rect rect,std::string name="")97 static sptr<Window> CreateAppFloatingWindow(WindowType type, Rect rect, std::string name = "")
98 {
99 sptr<WindowOption> option = new WindowOption();
100 option->SetWindowType(type);
101 option->SetWindowRect(rect);
102
103 static int cnt = 0;
104 std::string winName = (name == "") ? "FloatingWindowTest" + std::to_string(cnt++) : name;
105
106 return Window::Create(winName, option, WindowAppFloatingWindowTest::abilityContext_);
107 }
108
GetRectWithVpr(int32_t x,int32_t y,uint32_t w,uint32_t h)109 static inline Rect GetRectWithVpr(int32_t x, int32_t y, uint32_t w, uint32_t h)
110 {
111 auto vpr = WindowAppFloatingWindowTest::virtualPixelRatio_;
112 return { x, y, static_cast<uint32_t>(w * vpr), static_cast<uint32_t>(h * vpr) };
113 }
114
115 /**
116 * @tc.name: AppFloatingWindow01
117 * @tc.desc: AppFloatingWindow life cycle
118 * @tc.type: FUNC
119 */
120 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow01, TestSize.Level1)
121 {
122 sptr<WindowScene> scene = CreateWindowScene();
123 ASSERT_NE(nullptr, scene);
124
125 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
126 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT, fltWindRect);
127 ASSERT_NE(nullptr, fltWin);
128 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
129
130 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
131
132 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
133 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
134
135 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
136
137 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, 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, TestSize.Level1)
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 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
154
155 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
156
157 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
158 ASSERT_EQ(true, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
159 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
160
161 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
162 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoDestroy());
163 }
164
165 /**
166 * @tc.name: AppFloatingWindow03
167 * @tc.desc: AppFloatingWindow life cycle, app floating window hide first
168 * @tc.type: FUNC
169 */
170 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow03, TestSize.Level1)
171 {
172 sptr<WindowScene> scene = CreateWindowScene();
173 ASSERT_NE(nullptr, scene);
174
175 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
176 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT, fltWindRect);
177 ASSERT_NE(nullptr, fltWin);
178 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
179
180 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
181
182 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
183 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
184
185 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoBackground());
186
187 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
188 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoDestroy());
189 }
190
191 /**
192 * @tc.name: AppFloatingWindow04
193 * @tc.desc: AppFloatingWindow life cycle, main window destroy first
194 * @tc.type: FUNC
195 */
196 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow04, TestSize.Level1)
197 {
198 sptr<WindowScene> scene = CreateWindowScene();
199 ASSERT_NE(nullptr, scene);
200
201 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
202 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT, fltWindRect);
203 ASSERT_NE(nullptr, fltWin);
204
205 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
206 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
207 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoDestroy());
208 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
209 }
210
211 /**
212 * @tc.name: AppFloatingWindow05
213 * @tc.desc: Camera AppFloatingWindow life cycle
214 * @tc.type: FUNC
215 * @tc.require: issueI5NEHO
216 */
217 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow05, TestSize.Level1)
218 {
219 sptr<WindowScene> scene = CreateWindowScene();
220 ASSERT_NE(nullptr, scene);
221
222 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
223 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
224 ASSERT_NE(nullptr, fltWin);
225
226 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
227
228 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
229 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
230 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoBackground());
231 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
232 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoDestroy());
233 }
234
235 /**
236 * @tc.name: AppFloatingWindow06
237 * @tc.desc: Camera AppFloatingWindow life cycle, main window hide first
238 * @tc.type: FUNC
239 * @tc.require: issueI5NEHO
240 */
241 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow06, TestSize.Level1)
242 {
243 sptr<WindowScene> scene = CreateWindowScene();
244 ASSERT_NE(nullptr, scene);
245
246 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
247 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
248 ASSERT_NE(nullptr, fltWin);
249
250 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
251 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
252
253 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoBackground());
254 ASSERT_EQ(true, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
255 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
256
257 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
258 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoDestroy());
259 }
260
261 /**
262 * @tc.name: AppFloatingWindow07
263 * @tc.desc: Camera AppFloatingWindow life cycle, app floating window hide first
264 * @tc.type: FUNC
265 * @tc.require: issueI5NEHO
266 */
267 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow07, TestSize.Level1)
268 {
269 sptr<WindowScene> scene = CreateWindowScene();
270 ASSERT_NE(nullptr, scene);
271
272 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
273 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
274 ASSERT_NE(nullptr, fltWin);
275
276 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
277 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
278
279 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
280 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
281
282 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoBackground());
283
284 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
285 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoDestroy());
286 }
287
288 /**
289 * @tc.name: AppFloatingWindow08
290 * @tc.desc: Camera AppFloatingWindow life cycle, main window destroy first
291 * @tc.type: FUNC
292 * @tc.require: issueI5NEHO
293 */
294 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow08, TestSize.Level1)
295 {
296 sptr<WindowScene> scene = CreateWindowScene();
297 ASSERT_NE(nullptr, scene);
298
299 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
300 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
301 ASSERT_NE(nullptr, fltWin);
302
303 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
304 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
305
306 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoDestroy());
307 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
308 }
309
310 /**
311 * @tc.name: AppFloatingWindow09
312 * @tc.desc: Camera AppFloatingWindow rect check
313 * @tc.type: FUNC
314 * @tc.require: issueI5NEHO
315 */
316 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow09, TestSize.Level1)
317 {
318 sptr<WindowScene> scene = CreateWindowScene();
319 ASSERT_NE(nullptr, scene);
320
321 Rect fltWindRect = GetRectWithVpr(10, 20, 10, 10);
322 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
323 ASSERT_NE(nullptr, fltWin);
324
325 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
326 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
327
328 Rect exceptRect = { 10, 20, 0, 0 };
329 uint32_t smallWidth = displayRect_.height_ <= displayRect_.width_ ? displayRect_.height_ : displayRect_.width_;
330 float hwRatio = static_cast<float>(displayRect_.height_) / static_cast<float>(displayRect_.width_);
331 if (smallWidth <= static_cast<uint32_t>(600 * virtualPixelRatio_)) { // sw <= 600dp
332 if (displayRect_.width_ <= displayRect_.height_) {
333 exceptRect.width_ = static_cast<uint32_t>(smallWidth * 0.3);
334 } else {
335 exceptRect.width_ = static_cast<uint32_t>(smallWidth * 0.5);
336 }
337 } else {
338 if (displayRect_.width_ <= displayRect_.height_) {
339 exceptRect.width_ = static_cast<uint32_t>(smallWidth * 0.12);
340 } else {
341 exceptRect.width_ = static_cast<uint32_t>(smallWidth * 0.3);
342 }
343 }
344 exceptRect.height_ = static_cast<uint32_t>(exceptRect.width_ * hwRatio);
345 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
346 ASSERT_FALSE(WindowTestUtils::RectEqualTo(fltWin, exceptRect));
347 } else {
348 ASSERT_TRUE(WindowTestUtils::RectEqualTo(fltWin, exceptRect));
349 }
350
351 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
352 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoDestroy());
353 }
354
355 /**
356 * @tc.name: AppFloatingWindow10
357 * @tc.desc: Camera AppFloatingWindow multi create
358 * @tc.type: FUNC
359 * @tc.require: issueI5NEHO
360 */
361 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow10, TestSize.Level1)
362 {
363 sptr<WindowScene> scene = CreateWindowScene();
364 ASSERT_NE(nullptr, scene);
365
366 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
367 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
368 ASSERT_NE(nullptr, fltWin);
369
370 sptr<Window> fltWin2 = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
371 ASSERT_EQ(nullptr, fltWin2);
372
373 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
374 sptr<Window> fltWin3 = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
375 ASSERT_NE(nullptr, fltWin3);
376
377 ASSERT_EQ(WMError::WM_OK, fltWin3->Destroy());
378 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoDestroy());
379 }
380
381 /**
382 * @tc.name: AppFloatingWindow11
383 * @tc.desc: Camera AppFloatingWindow listener
384 * @tc.type: FUNC
385 * @tc.require: issueI5NEHR
386 */
387 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow11, TestSize.Level1)
388 {
389 uint32_t tokenId = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
390 WindowManager::GetInstance().RegisterCameraFloatWindowChangedListener(testCameraFloatWindowChangedListener_);
391 sptr<WindowScene> scene = CreateWindowScene();
392 ASSERT_NE(nullptr, scene);
393
394 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
395 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
396 ASSERT_NE(nullptr, fltWin);
397
398 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
399 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
400
401 usleep(500000); // 500000us = 0.5s
402 ASSERT_EQ(tokenId, testCameraFloatWindowChangedListener_->accessTokenId_);
403 ASSERT_EQ(true, testCameraFloatWindowChangedListener_->isShowing_);
404
405 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
406
407 usleep(500000); // 500000us = 0.5s
408 ASSERT_EQ(tokenId, testCameraFloatWindowChangedListener_->accessTokenId_);
409 ASSERT_EQ(false, testCameraFloatWindowChangedListener_->isShowing_);
410
411 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
412 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoDestroy());
413
414 WindowManager::GetInstance().UnregisterCameraFloatWindowChangedListener(testCameraFloatWindowChangedListener_);
415 }
416 } // namespace Rosen
417 } // namespace OHOS
418