• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include <filesystem>
17 #include <fstream>
18 #include <gtest/gtest.h>
19 
20 #include "ability_context_impl.h"
21 #include "accessibility_event_info.h"
22 #include "color_parser.h"
23 #include "mock_session.h"
24 #include "mock_uicontent.h"
25 #include "mock_window.h"
26 #include "parameters.h"
27 #include "window_helper.h"
28 #include "window_session_impl.h"
29 #include "wm_common.h"
30 
31 using namespace testing;
32 using namespace testing::ext;
33 
34 namespace OHOS {
35 namespace Rosen {
36 class WindowSessionImplLayoutTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp() override;
41     void TearDown() override;
42 
43     std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_;
44 
45 private:
46     static constexpr uint32_t WAIT_SYNC_IN_NS = 50000;
47 };
48 
SetUpTestCase()49 void WindowSessionImplLayoutTest::SetUpTestCase() {}
50 
TearDownTestCase()51 void WindowSessionImplLayoutTest::TearDownTestCase() {}
52 
SetUp()53 void WindowSessionImplLayoutTest::SetUp()
54 {
55     abilityContext_ = std::make_shared<AbilityRuntime::AbilityContextImpl>();
56 }
57 
TearDown()58 void WindowSessionImplLayoutTest::TearDown()
59 {
60     usleep(WAIT_SYNC_IN_NS);
61     abilityContext_ = nullptr;
62 }
63 
64 namespace {
GetTestWindowImpl(const std::string & name)65 sptr<WindowSessionImpl> GetTestWindowImpl(const std::string& name)
66 {
67     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
68     option->SetWindowName(name);
69     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
70 
71     SessionInfo sessionInfo = { name, name, name };
72     sptr<SessionMocker> session = sptr<SessionMocker>::MakeSptr(sessionInfo);
73 
74     window->hostSession_ = session;
75     return window;
76 }
77 
GetListenerList()78 template <typename TListener, typename MockListener> std::vector<sptr<TListener>> GetListenerList()
79 {
80     std::vector<sptr<TListener>> listeners;
81     sptr<TListener> listener = sptr<TListener>::MakeSptr();
82     listeners.insert(listeners.begin(), listener);
83     return listeners;
84 }
85 
86 /**
87  * @tc.name: UpdateRect01
88  * @tc.desc: UpdateRect
89  * @tc.type: FUNC
90  */
91 HWTEST_F(WindowSessionImplLayoutTest, UpdateRect01, TestSize.Level0)
92 {
93     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateRect01 start";
94     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
95     option->SetWindowName("UpdateRect01");
96     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
97 
98     WSRect rect;
99     rect.posX_ = 0;
100     rect.posY_ = 0;
101     rect.height_ = 50;
102     rect.width_ = 50;
103 
104     Rect rectW; // GetRect().IsUninitializedRect is false
105     rectW.posX_ = 0;
106     rectW.posY_ = 0;
107     rectW.height_ = 200; // rectW - rect > 50
108     rectW.width_ = 200;  // rectW - rect > 50
109 
110     window->property_->SetWindowRect(rectW);
111     SizeChangeReason reason = SizeChangeReason::UNDEFINED;
112     WSError res = window->UpdateRect(rect, reason);
113     ASSERT_EQ(res, WSError::WS_OK);
114 
115     rectW.height_ = 50;
116     window->property_->SetWindowRect(rectW);
117     res = window->UpdateRect(rect, reason);
118     ASSERT_EQ(res, WSError::WS_OK);
119 
120     rectW.height_ = 200;
121     rectW.width_ = 50;
122     window->property_->SetWindowRect(rectW);
123     res = window->UpdateRect(rect, reason);
124     ASSERT_EQ(res, WSError::WS_OK);
125     Rect nowRect = window->property_->GetWindowRect();
126     EXPECT_EQ(nowRect.posX_, rect.posX_);
127     EXPECT_EQ(nowRect.posY_, rect.posY_);
128     EXPECT_EQ(nowRect.width_, rect.width_);
129     EXPECT_EQ(nowRect.height_, rect.height_);
130     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateRect01 end";
131 }
132 
133 /**
134  * @tc.name: UpdateRect02
135  * @tc.desc: UpdateRect
136  * @tc.type: FUNC
137  */
138 HWTEST_F(WindowSessionImplLayoutTest, UpdateRect02, TestSize.Level0)
139 {
140     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateRect02 start";
141     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
142     option->SetWindowName("UpdateRect02");
143     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
144 
145     WSRect rect;
146     rect.posX_ = 0;
147     rect.posY_ = 0;
148     rect.height_ = 0;
149     rect.width_ = 0;
150 
151     Rect rectW; // GetRect().IsUninitializedRect is true
152     rectW.posX_ = 0;
153     rectW.posY_ = 0;
154     rectW.height_ = 0; // rectW - rect > 50
155     rectW.width_ = 0;  // rectW - rect > 50
156 
157     window->property_->SetWindowRect(rectW);
158     SizeChangeReason reason = SizeChangeReason::ROTATION;
159     WSError res = window->UpdateRect(rect, reason);
160     ASSERT_EQ(res, WSError::WS_OK);
161 
162     rect.height_ = 50;
163     rect.width_ = 50;
164     rectW.height_ = 50;
165     rectW.width_ = 50;
166     window->property_->SetWindowRect(rectW);
167     res = window->UpdateRect(rect, reason);
168     ASSERT_EQ(res, WSError::WS_OK);
169     Rect nowRect = window->property_->GetWindowRect();
170     EXPECT_EQ(nowRect.posX_, rect.posX_);
171     EXPECT_EQ(nowRect.posY_, rect.posY_);
172     EXPECT_EQ(nowRect.width_, rect.width_);
173     EXPECT_EQ(nowRect.height_, rect.height_);
174     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateRect02 end";
175 }
176 
177 /**
178  * @tc.name: SetResizeByDragEnabled01
179  * @tc.desc: SetResizeByDragEnabled and check the retCode
180  * @tc.type: FUNC
181  */
182 HWTEST_F(WindowSessionImplLayoutTest, SetResizeByDragEnabled01, TestSize.Level0)
183 {
184     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
185     option->SetWindowName("SetResizeByDragEnabled01");
186     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
187     WMError retCode = window->SetResizeByDragEnabled(true);
188     ASSERT_EQ(retCode, WMError::WM_ERROR_INVALID_WINDOW);
189 }
190 
191 /**
192  * @tc.name: SetResizeByDragEnabled02
193  * @tc.desc: SetResizeByDragEnabled and check the retCode
194  * @tc.type: FUNC
195  */
196 HWTEST_F(WindowSessionImplLayoutTest, SetResizeByDragEnabled02, TestSize.Level0)
197 {
198     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
199     option->SetWindowName("SetResizeByDragEnabled02");
200     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
201     window->property_->SetPersistentId(1);
202     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
203     sptr<SessionMocker> session = sptr<SessionMocker>::MakeSptr(sessionInfo);
204     window->hostSession_ = session;
205     window->state_ = WindowState::STATE_CREATED;
206     ASSERT_FALSE(window->IsWindowSessionInvalid());
207     WMError retCode = window->SetResizeByDragEnabled(true);
208     ASSERT_EQ(retCode, WMError::WM_OK);
209     ASSERT_EQ(true, window->property_->GetDragEnabled());
210 }
211 
212 /**
213  * @tc.name: SetResizeByDragEnabled03
214  * @tc.desc: SetResizeByDragEnabled and check the retCode
215  * @tc.type: FUNC
216  */
217 HWTEST_F(WindowSessionImplLayoutTest, SetResizeByDragEnabled03, TestSize.Level0)
218 {
219     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
220     option->SetWindowName("SetResizeByDragEnabled03");
221     option->SetSubWindowDecorEnable(true);
222     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
223 
224     window->property_->SetPersistentId(1);
225     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
226     sptr<SessionMocker> session = sptr<SessionMocker>::MakeSptr(sessionInfo);
227     window->hostSession_ = session;
228 
229     window->property_->SetWindowType(WindowType::APP_SUB_WINDOW_BASE);
230     WMError retCode = window->SetResizeByDragEnabled(true);
231     ASSERT_EQ(retCode, WMError::WM_OK);
232 
233     window->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
234     retCode = window->SetResizeByDragEnabled(true);
235     ASSERT_EQ(retCode, WMError::WM_OK);
236 
237     window->property_->SetWindowType(WindowType::SYSTEM_SUB_WINDOW_BASE);
238     retCode = window->SetResizeByDragEnabled(true);
239     ASSERT_EQ(retCode, WMError::WM_ERROR_INVALID_TYPE);
240 }
241 
242 /**
243  * @tc.name: UpdateViewportConfig
244  * @tc.desc: UpdateViewportConfig
245  * @tc.type: FUNC
246  */
247 HWTEST_F(WindowSessionImplLayoutTest, UpdateViewportConfig, TestSize.Level0)
248 {
249     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateViewportConfig start";
250     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
251     option->SetWindowName("WindowSessionCreateCheck");
252     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
253 
254     Rect rectW; // GetRect().IsUninitializedRect is true
255     rectW.posX_ = 0;
256     rectW.posY_ = 0;
257     rectW.height_ = 0; // rectW - rect > 50
258     rectW.width_ = 0;  // rectW - rect > 50
259 
260     window->virtualPixelRatio_ = -1.0;
261     window->useUniqueDensity_ = true;
262     WindowSizeChangeReason reason = WindowSizeChangeReason::UNDEFINED;
263     window->UpdateViewportConfig(rectW, reason);
264     ASSERT_EQ(window->virtualPixelRatio_, -1.0);
265 
266     window->virtualPixelRatio_ = -2.0;
267     DisplayId displayId = 1;
268     window->property_->SetDisplayId(displayId);
269     window->UpdateViewportConfig(rectW, reason);
270     ASSERT_EQ(window->virtualPixelRatio_, -2.0);
271 
272     displayId = 0;
273     rectW.height_ = 500;
274     rectW.width_ = 500;
275     window->useUniqueDensity_ = false;
276     window->property_->SetDisplayId(displayId);
277     window->UpdateViewportConfig(rectW, reason);
278     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateViewportConfig end";
279 }
280 
281 /**
282  * @tc.name: UpdateViewportConfig01
283  * @tc.desc: UpdateViewportConfig
284  * @tc.type: FUNC
285  */
286 HWTEST_F(WindowSessionImplLayoutTest, UpdateViewportConfig01, TestSize.Level1)
287 {
288     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
289     option->SetWindowName("UpdateViewportConfig01");
290     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
291     Rect rectW;
292     rectW.posX_ = 0;
293     rectW.posY_ = 0;
294     rectW.height_ = 0;
295     rectW.width_ = 0;
296     WindowSizeChangeReason reason = WindowSizeChangeReason::UNDEFINED;
297     sptr<DisplayInfo> displayInfo = sptr<DisplayInfo>::MakeSptr();
298     window->UpdateViewportConfig(rectW, reason, nullptr, displayInfo);
299     rectW.width_ = 10;
300     rectW.height_ = 0;
301     window->UpdateViewportConfig(rectW, reason, nullptr, displayInfo);
302     rectW.width_ = 10;
303     rectW.height_ = 10;
304     window->UpdateViewportConfig(rectW, reason, nullptr, displayInfo);
305     ASSERT_NE(window, nullptr);
306 }
307 
308 /**
309  * @tc.name: NotifySingleHandTransformChange_TestUIContent
310  * @tc.desc: NotifySingleHandTransformChange
311  * @tc.type: FUNC
312  */
313 HWTEST_F(WindowSessionImplLayoutTest, NotifySingleHandTransformChange_TestUIContent, TestSize.Level1)
314 {
315     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifySingleHandTransformChange_TestUIContent start";
316     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
317     option->SetWindowName("NotifySingleHandTransformChange_TestUIContent");
318     option->SetIsUIExtFirstSubWindow(true);
319     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
320     window->property_->SetPersistentId(2025);
321     std::string url = "";
322     window->SetUIContentInner(
323         url, nullptr, nullptr, WindowSetUIContentType::DEFAULT, BackupAndRestoreType::NONE, nullptr);
324     SingleHandTransform testTransform;
325     testTransform.posX = 100;
326     window->NotifySingleHandTransformChange(testTransform);
327     ASSERT_EQ(testTransform.posX, window->singleHandTransform_.posX);
328     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifySingleHandTransformChange_TestUIContent end";
329 }
330 
331 /**
332  * @tc.name: NotifyTransformChange_TestUIContent
333  * @tc.desc: NotifyTransformChange
334  * @tc.type: FUNC
335  */
336 HWTEST_F(WindowSessionImplLayoutTest, NotifyTransformChange_TestUIContent, TestSize.Level1)
337 {
338     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyTransformChange_TestUIContent start";
339     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
340     option->SetWindowName("NotifyTransformChange_TestUIContent");
341     option->SetIsUIExtFirstSubWindow(true);
342     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
343     window->property_->SetPersistentId(2025);
344 
345     Transform testTransform;
346     window->uiContent_ = nullptr;
347     window->SetNeedRenotifyTransform(true);
348     window->NotifyTransformChange(testTransform);
349     ASSERT_EQ(true, window->IsNeedRenotifyTransform());
350 
351     window->uiContent_ = std::make_unique<Ace::UIContentMocker>();
352     window->SetNeedRenotifyTransform(true);
353     window->NotifyTransformChange(testTransform);
354     ASSERT_EQ(false, window->IsNeedRenotifyTransform());
355     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyTransformChange_TestUIContent end";
356 }
357 
358 /**
359  * @tc.name: NotifyWindowStatusDidChange
360  * @tc.desc: NotifyWindowStatusDidChange
361  * @tc.type: FUNC
362  */
363 HWTEST_F(WindowSessionImplLayoutTest, NotifyWindowStatusDidChange, TestSize.Level1)
364 {
365     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyWindowStatusDidChange start";
366     auto window = GetTestWindowImpl("NotifyWindowStatusDidChange");
367     auto listeners = GetListenerList<IWindowStatusDidChangeListener, MockWindowStatusDidChangeListener>();
368     EXPECT_NE(listeners.size(), 0);
369     listeners.insert(listeners.begin(), nullptr);
370     window->windowStatusDidChangeListeners_.insert({ window->GetPersistentId(), listeners });
371     window->NotifyWindowStatusDidChange(WindowMode::WINDOW_MODE_FLOATING);
372     EXPECT_EQ(WMError::WM_ERROR_INVALID_WINDOW, window->Destroy());
373     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyWindowStatusDidChange end";
374 }
375 
376 /**
377  * @tc.name: NotifyAfterUIContentReady
378  * @tc.desc: NotifyAfterUIContentReady
379  * @tc.type: FUNC
380  */
381 HWTEST_F(WindowSessionImplLayoutTest, NotifyAfterUIContentReady, TestSize.Level1)
382 {
383     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyAfterUIContentReady start";
384     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
385     option->SetWindowName("NotifyAfterUIContentReady");
386     option->SetIsUIExtFirstSubWindow(true);
387     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
388     window->property_->SetPersistentId(2025);
389     window->uiContent_ = std::make_unique<Ace::UIContentMocker>();
390     window->SetNeedRenotifyTransform(false);
391     window->NotifyAfterUIContentReady();
392     ASSERT_EQ(false, window->IsNeedRenotifyTransform());
393     window->SetNeedRenotifyTransform(false);
394     window->NotifyAfterUIContentReady();
395     ASSERT_EQ(false, window->IsNeedRenotifyTransform());
396     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyAfterUIContentReady end";
397 }
398 
399 /**
400  * @tc.name: NotifyFirstValidLayoutUpdate
401  * @tc.desc: NotifyFirstValidLayoutUpdate
402  * @tc.type: FUNC
403  */
404 HWTEST_F(WindowSessionImplLayoutTest, NotifyFirstValidLayoutUpdate, TestSize.Level1)
405 {
406     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyFirstValidLayoutUpdate start";
407     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
408     option->SetWindowName("NotifyFirstValidLayoutUpdate");
409     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
410     window->property_->SetPersistentId(2025);
411 
412     Rect preRect = { 0, 0, 1000, 1000 };
413     Rect newRect = { 0, 0, 0, 0 };
414     window->isFirstValidLayoutUpdate_ = true;
415     window->NotifyFirstValidLayoutUpdate(preRect, newRect);
416     EXPECT_EQ(window->isFirstValidLayoutUpdate_, true);
417 
418     preRect = { 0, 0, 0, 0 };
419     window->NotifyFirstValidLayoutUpdate(preRect, newRect);
420     EXPECT_EQ(window->isFirstValidLayoutUpdate_, true);
421 
422 
423     newRect = { 0, 0, 1000, 1000 };
424     window->NotifyFirstValidLayoutUpdate(preRect, newRect);
425     EXPECT_EQ(window->isFirstValidLayoutUpdate_, false);
426 
427     window->NotifyFirstValidLayoutUpdate(preRect, newRect);
428     EXPECT_EQ(window->isFirstValidLayoutUpdate_, false);
429     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyFirstValidLayoutUpdate end";
430 }
431 
432 /**
433  * @tc.name: HookWindowSizeByHookWindowInfo
434  * @tc.desc: HookWindowSizeByHookWindowInfo
435  * @tc.type: FUNC
436  */
437 HWTEST_F(WindowSessionImplLayoutTest, HookWindowSizeByHookWindowInfo, TestSize.Level1)
438 {
439     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: HookWindowSizeByHookWindowInfo start";
440     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
441     option->SetWindowName("HookWindowSizeByHookWindowInfo");
442     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
443     window->property_->SetPersistentId(2025);
444 
445     // Case 1: not enable hookWindow
446     HookWindowInfo hookWindowInfo;
447     hookWindowInfo.enableHookWindow = false;
448     hookWindowInfo.widthHookRatio = 0.5f;
449     window->SetAppHookWindowInfo(hookWindowInfo);
450     const uint32_t defaultSize = 800;
451     Rect rect = { 0, 0, defaultSize, defaultSize };
452     window->HookWindowSizeByHookWindowInfo(rect);
453     EXPECT_EQ(rect.width_, defaultSize);
454 
455     // Case 2: not main window
456     hookWindowInfo.enableHookWindow = true;
457     window->SetAppHookWindowInfo(hookWindowInfo);
458     window->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
459     window->HookWindowSizeByHookWindowInfo(rect);
460     EXPECT_EQ(rect.width_, defaultSize);
461 
462     // Case 3: default window size hook ratio
463     window->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
464     hookWindowInfo.widthHookRatio = HookWindowInfo::DEFAULT_WINDOW_SIZE_HOOK_RATIO;
465     window->SetAppHookWindowInfo(hookWindowInfo);
466     window->HookWindowSizeByHookWindowInfo(rect);
467     EXPECT_EQ(rect.width_, defaultSize);
468 
469     // Case 4: success
470     hookWindowInfo.widthHookRatio = 0.5f;
471     window->SetAppHookWindowInfo(hookWindowInfo);
472     window->HookWindowSizeByHookWindowInfo(rect);
473     EXPECT_NE(rect.width_, defaultSize);
474     GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: HookWindowSizeByHookWindowInfo end";
475 }
476 }
477 } // namespace Rosen
478 } // namespace OHOS
479