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