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_OK, scene->GoForeground());
133 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
134 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
135 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
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 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
154 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
155 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
156 ASSERT_EQ(true, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
157 ASSERT_EQ(true, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
158 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
159 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
160 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
161 }
162
163 /**
164 * @tc.name: AppFloatingWindow03
165 * @tc.desc: AppFloatingWindow life cycle, app floating window hide first
166 * @tc.type: FUNC
167 */
168 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow03, Function | MediumTest | Level3)
169 {
170 sptr<WindowScene> scene = CreateWindowScene();
171 ASSERT_NE(nullptr, scene);
172
173 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
174 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT, fltWindRect);
175 ASSERT_NE(nullptr, fltWin);
176 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
177 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
178
179 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
180 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
181 ASSERT_EQ(true, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
182 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
183 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
184 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
185 }
186
187 /**
188 * @tc.name: AppFloatingWindow04
189 * @tc.desc: AppFloatingWindow life cycle, main window destroy first
190 * @tc.type: FUNC
191 */
192 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow04, Function | MediumTest | Level3)
193 {
194 sptr<WindowScene> scene = CreateWindowScene();
195 ASSERT_NE(nullptr, scene);
196
197 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
198 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT, fltWindRect);
199 ASSERT_NE(nullptr, fltWin);
200 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
201 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
202 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
203 ASSERT_EQ(nullptr, scene->GetMainWindow());
204 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
205 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
206 }
207
208 /**
209 * @tc.name: AppFloatingWindow05
210 * @tc.desc: Camera AppFloatingWindow life cycle
211 * @tc.type: FUNC
212 * @tc.require: issueI5NEHO
213 */
214 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow05, Function | MediumTest | Level2)
215 {
216 sptr<WindowScene> scene = CreateWindowScene();
217 ASSERT_NE(nullptr, scene);
218
219 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
220 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
221 ASSERT_NE(nullptr, fltWin);
222 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
223 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
224 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
225 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
226 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
227 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
228 }
229
230 /**
231 * @tc.name: AppFloatingWindow06
232 * @tc.desc: Camera AppFloatingWindow life cycle, main window hide first
233 * @tc.type: FUNC
234 * @tc.require: issueI5NEHO
235 */
236 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow06, Function | MediumTest | Level3)
237 {
238 sptr<WindowScene> scene = CreateWindowScene();
239 ASSERT_NE(nullptr, scene);
240
241 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
242 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
243 ASSERT_NE(nullptr, fltWin);
244 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
245 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
246 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
247 ASSERT_EQ(false, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
248 ASSERT_EQ(true, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
249 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
250
251 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
252 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
253 }
254
255 /**
256 * @tc.name: AppFloatingWindow07
257 * @tc.desc: Camera AppFloatingWindow life cycle, app floating window hide first
258 * @tc.type: FUNC
259 * @tc.require: issueI5NEHO
260 */
261 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow07, Function | MediumTest | Level3)
262 {
263 sptr<WindowScene> scene = CreateWindowScene();
264 ASSERT_NE(nullptr, scene);
265
266 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
267 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
268 ASSERT_NE(nullptr, fltWin);
269 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
270 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
271
272 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
273 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
274 ASSERT_EQ(true, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
275 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
276
277 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
278 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
279 }
280
281 /**
282 * @tc.name: AppFloatingWindow08
283 * @tc.desc: Camera AppFloatingWindow life cycle, main window destroy first
284 * @tc.type: FUNC
285 * @tc.require: issueI5NEHO
286 */
287 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow08, Function | MediumTest | Level3)
288 {
289 sptr<WindowScene> scene = CreateWindowScene();
290 ASSERT_NE(nullptr, scene);
291
292 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
293 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
294 ASSERT_NE(nullptr, fltWin);
295 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
296 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
297
298 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
299
300 ASSERT_EQ(nullptr, scene->GetMainWindow());
301 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
302 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
303 }
304
305 /**
306 * @tc.name: AppFloatingWindow09
307 * @tc.desc: Camera AppFloatingWindow rect check
308 * @tc.type: FUNC
309 * @tc.require: issueI5NEHO
310 */
311 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow09, Function | MediumTest | Level3)
312 {
313 sptr<WindowScene> scene = CreateWindowScene();
314 ASSERT_NE(nullptr, scene);
315
316 Rect fltWindRect = GetRectWithVpr(10, 20, 10, 10);
317 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
318 ASSERT_NE(nullptr, fltWin);
319 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
320 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
321
322 Rect exceptRect = {10, 20, 0, 0};
323 uint32_t smallWidth = displayRect_.height_ <= displayRect_.width_ ? displayRect_.height_ : displayRect_.width_;
324 float hwRatio = static_cast<float>(displayRect_.height_) / static_cast<float>(displayRect_.width_);
325 if (smallWidth <= static_cast<uint32_t>(600 * virtualPixelRatio_)) { // sw <= 600dp
326 if (displayRect_.width_ <= displayRect_.height_) {
327 exceptRect.width_= static_cast<uint32_t>(smallWidth * 0.3);
328 } else {
329 exceptRect.width_ = static_cast<uint32_t>(smallWidth * 0.5);
330 }
331 } else {
332 if (displayRect_.width_ <= displayRect_.height_) {
333 exceptRect.width_ = static_cast<uint32_t>(smallWidth * 0.12);
334 } else {
335 exceptRect.width_ = static_cast<uint32_t>(smallWidth * 0.3);
336 }
337 }
338 exceptRect.height_ = static_cast<uint32_t>(exceptRect.width_ * hwRatio);
339 ASSERT_TRUE(WindowTestUtils::RectEqualTo(fltWin, exceptRect));
340
341 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
342 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
343 }
344
345 /**
346 * @tc.name: AppFloatingWindow10
347 * @tc.desc: Camera AppFloatingWindow multi create
348 * @tc.type: FUNC
349 * @tc.require: issueI5NEHO
350 */
351 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow10, Function | MediumTest | Level3)
352 {
353 sptr<WindowScene> scene = CreateWindowScene();
354 ASSERT_NE(nullptr, scene);
355
356 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
357 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
358 ASSERT_NE(nullptr, fltWin);
359
360 sptr<Window> fltWin2 = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
361 ASSERT_EQ(nullptr, fltWin2);
362
363 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
364 sptr<Window> fltWin3 = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
365 ASSERT_NE(nullptr, fltWin3);
366
367 ASSERT_EQ(WMError::WM_OK, fltWin3->Destroy());
368 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
369 }
370
371 /**
372 * @tc.name: AppFloatingWindow11
373 * @tc.desc: Camera AppFloatingWindow listener
374 * @tc.type: FUNC
375 * @tc.require: issueI5NEHR
376 */
377 HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow11, Function | MediumTest | Level2)
378 {
379 uint32_t tokenId = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
380 WindowManager::GetInstance().RegisterCameraFloatWindowChangedListener(testCameraFloatWindowChangedListener_);
381 sptr<WindowScene> scene = CreateWindowScene();
382 ASSERT_NE(nullptr, scene);
383
384 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
385 sptr<Window> fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
386 ASSERT_NE(nullptr, fltWin);
387
388 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
389 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
390
391 usleep(500000); // 500000us = 0.5s
392 ASSERT_EQ(tokenId, testCameraFloatWindowChangedListener_->accessTokenId_);
393 ASSERT_EQ(true, testCameraFloatWindowChangedListener_->isShowing_);
394
395 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
396
397 usleep(500000); // 500000us = 0.5s
398 ASSERT_EQ(tokenId, testCameraFloatWindowChangedListener_->accessTokenId_);
399 ASSERT_EQ(false, testCameraFloatWindowChangedListener_->isShowing_);
400
401 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
402 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
403
404 WindowManager::GetInstance().UnregisterCameraFloatWindowChangedListener(testCameraFloatWindowChangedListener_);
405 }
406 } // namespace Rosen
407 } // namespace OHOS
408