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 {
65 /**
66 * @tc.name: UpdateRect01
67 * @tc.desc: UpdateRect
68 * @tc.type: FUNC
69 */
70 HWTEST_F(WindowSessionImplLayoutTest, UpdateRect01, Function | SmallTest | Level2)
71 {
72 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateRect01 start";
73 sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
74 option->SetWindowName("UpdateRect01");
75 sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
76
77 WSRect rect;
78 rect.posX_ = 0;
79 rect.posY_ = 0;
80 rect.height_ = 50;
81 rect.width_ = 50;
82
83 Rect rectW; // GetRect().IsUninitializedRect is false
84 rectW.posX_ = 0;
85 rectW.posY_ = 0;
86 rectW.height_ = 200; // rectW - rect > 50
87 rectW.width_ = 200; // rectW - rect > 50
88
89 window->property_->SetWindowRect(rectW);
90 SizeChangeReason reason = SizeChangeReason::UNDEFINED;
91 WSError res = window->UpdateRect(rect, reason);
92 ASSERT_EQ(res, WSError::WS_OK);
93
94 rectW.height_ = 50;
95 window->property_->SetWindowRect(rectW);
96 res = window->UpdateRect(rect, reason);
97 ASSERT_EQ(res, WSError::WS_OK);
98
99 rectW.height_ = 200;
100 rectW.width_ = 50;
101 window->property_->SetWindowRect(rectW);
102 res = window->UpdateRect(rect, reason);
103 ASSERT_EQ(res, WSError::WS_OK);
104 Rect nowRect = window->property_->GetWindowRect();
105 EXPECT_EQ(nowRect.posX_, rect.posX_);
106 EXPECT_EQ(nowRect.posY_, rect.posY_);
107 EXPECT_EQ(nowRect.width_, rect.width_);
108 EXPECT_EQ(nowRect.height_, rect.height_);
109 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateRect01 end";
110 }
111
112 /**
113 * @tc.name: UpdateRect02
114 * @tc.desc: UpdateRect
115 * @tc.type: FUNC
116 */
117 HWTEST_F(WindowSessionImplLayoutTest, UpdateRect02, Function | SmallTest | Level2)
118 {
119 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateRect02 start";
120 sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
121 option->SetWindowName("UpdateRect02");
122 sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
123
124 WSRect rect;
125 rect.posX_ = 0;
126 rect.posY_ = 0;
127 rect.height_ = 0;
128 rect.width_ = 0;
129
130 Rect rectW; // GetRect().IsUninitializedRect is true
131 rectW.posX_ = 0;
132 rectW.posY_ = 0;
133 rectW.height_ = 0; // rectW - rect > 50
134 rectW.width_ = 0; // rectW - rect > 50
135
136 window->property_->SetWindowRect(rectW);
137 SizeChangeReason reason = SizeChangeReason::ROTATION;
138 WSError res = window->UpdateRect(rect, reason);
139 ASSERT_EQ(res, WSError::WS_OK);
140
141 rect.height_ = 50;
142 rect.width_ = 50;
143 rectW.height_ = 50;
144 rectW.width_ = 50;
145 window->property_->SetWindowRect(rectW);
146 res = window->UpdateRect(rect, reason);
147 ASSERT_EQ(res, WSError::WS_OK);
148 Rect nowRect = window->property_->GetWindowRect();
149 EXPECT_EQ(nowRect.posX_, rect.posX_);
150 EXPECT_EQ(nowRect.posY_, rect.posY_);
151 EXPECT_EQ(nowRect.width_, rect.width_);
152 EXPECT_EQ(nowRect.height_, rect.height_);
153 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateRect02 end";
154 }
155
156 /**
157 * @tc.name: SetResizeByDragEnabled01
158 * @tc.desc: SetResizeByDragEnabled and check the retCode
159 * @tc.type: FUNC
160 */
161 HWTEST_F(WindowSessionImplLayoutTest, SetResizeByDragEnabled01, Function | SmallTest | Level2)
162 {
163 sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
164 option->SetWindowName("SetResizeByDragEnabled01");
165 sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
166 WMError retCode = window->SetResizeByDragEnabled(true);
167 ASSERT_EQ(retCode, WMError::WM_ERROR_INVALID_WINDOW);
168 }
169
170 /**
171 * @tc.name: SetResizeByDragEnabled02
172 * @tc.desc: SetResizeByDragEnabled and check the retCode
173 * @tc.type: FUNC
174 */
175 HWTEST_F(WindowSessionImplLayoutTest, SetResizeByDragEnabled02, Function | SmallTest | Level2)
176 {
177 sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
178 option->SetWindowName("SetResizeByDragEnabled02");
179 sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
180 window->property_->SetPersistentId(1);
181 SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
182 sptr<SessionMocker> session = sptr<SessionMocker>::MakeSptr(sessionInfo);
183 window->hostSession_ = session;
184 window->state_ = WindowState::STATE_CREATED;
185 ASSERT_FALSE(window->IsWindowSessionInvalid());
186 WMError retCode = window->SetResizeByDragEnabled(true);
187 ASSERT_EQ(retCode, WMError::WM_OK);
188 ASSERT_EQ(true, window->property_->GetDragEnabled());
189 }
190
191 /**
192 * @tc.name: SetResizeByDragEnabled03
193 * @tc.desc: SetResizeByDragEnabled and check the retCode
194 * @tc.type: FUNC
195 */
196 HWTEST_F(WindowSessionImplLayoutTest, SetResizeByDragEnabled03, Function | SmallTest | Level2)
197 {
198 sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
199 option->SetWindowName("SetResizeByDragEnabled03");
200 option->SetSubWindowDecorEnable(true);
201 sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
202
203 window->property_->SetPersistentId(1);
204 SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
205 sptr<SessionMocker> session = sptr<SessionMocker>::MakeSptr(sessionInfo);
206 window->hostSession_ = session;
207
208 window->property_->SetWindowType(WindowType::APP_SUB_WINDOW_BASE);
209 WMError retCode = window->SetResizeByDragEnabled(true);
210 ASSERT_EQ(retCode, WMError::WM_OK);
211
212 window->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
213 retCode = window->SetResizeByDragEnabled(true);
214 ASSERT_EQ(retCode, WMError::WM_OK);
215
216 window->property_->SetWindowType(WindowType::SYSTEM_SUB_WINDOW_BASE);
217 retCode = window->SetResizeByDragEnabled(true);
218 ASSERT_EQ(retCode, WMError::WM_ERROR_INVALID_TYPE);
219 }
220
221 /**
222 * @tc.name: UpdateViewportConfig
223 * @tc.desc: UpdateViewportConfig
224 * @tc.type: FUNC
225 */
226 HWTEST_F(WindowSessionImplLayoutTest, UpdateViewportConfig, Function | SmallTest | Level2)
227 {
228 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateViewportConfig start";
229 sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
230 option->SetWindowName("WindowSessionCreateCheck");
231 sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
232
233 Rect rectW; // GetRect().IsUninitializedRect is true
234 rectW.posX_ = 0;
235 rectW.posY_ = 0;
236 rectW.height_ = 0; // rectW - rect > 50
237 rectW.width_ = 0; // rectW - rect > 50
238
239 window->virtualPixelRatio_ = -1.0;
240 window->useUniqueDensity_ = true;
241 WindowSizeChangeReason reason = WindowSizeChangeReason::UNDEFINED;
242 window->UpdateViewportConfig(rectW, reason);
243 ASSERT_EQ(window->virtualPixelRatio_, -1.0);
244
245 window->virtualPixelRatio_ = -2.0;
246 DisplayId displayId = 1;
247 window->property_->SetDisplayId(displayId);
248 window->UpdateViewportConfig(rectW, reason);
249 ASSERT_EQ(window->virtualPixelRatio_, -2.0);
250
251 displayId = 0;
252 rectW.height_ = 500;
253 rectW.width_ = 500;
254 window->useUniqueDensity_ = false;
255 window->property_->SetDisplayId(displayId);
256 window->UpdateViewportConfig(rectW, reason);
257 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: UpdateViewportConfig end";
258 }
259
260 /**
261 * @tc.name: UpdateViewportConfig01
262 * @tc.desc: UpdateViewportConfig
263 * @tc.type: FUNC
264 */
265 HWTEST_F(WindowSessionImplLayoutTest, UpdateViewportConfig01, Function | SmallTest | Level2)
266 {
267 sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
268 option->SetWindowName("UpdateViewportConfig01");
269 sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
270 Rect rectW;
271 rectW.posX_ = 0;
272 rectW.posY_ = 0;
273 rectW.height_ = 0;
274 rectW.width_ = 0;
275 WindowSizeChangeReason reason = WindowSizeChangeReason::UNDEFINED;
276 sptr<DisplayInfo> displayInfo = sptr<DisplayInfo>::MakeSptr();
277 window->UpdateViewportConfig(rectW, reason, nullptr, displayInfo);
278 rectW.width_ = 10;
279 rectW.height_ = 0;
280 window->UpdateViewportConfig(rectW, reason, nullptr, displayInfo);
281 rectW.width_ = 10;
282 rectW.height_ = 10;
283 window->UpdateViewportConfig(rectW, reason, nullptr, displayInfo);
284 ASSERT_NE(window, nullptr);
285 }
286
287 /**
288 * @tc.name: NotifySingleHandTransformChange_TestUIContent
289 * @tc.desc: NotifySingleHandTransformChange
290 * @tc.type: FUNC
291 */
292 HWTEST_F(WindowSessionImplLayoutTest, NotifySingleHandTransformChange_TestUIContent, Function | SmallTest | Level2)
293 {
294 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifySingleHandTransformChange_TestUIContent start";
295 sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
296 option->SetWindowName("NotifySingleHandTransformChange_TestUIContent");
297 option->SetIsUIExtFirstSubWindow(true);
298 sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
299 window->property_->SetPersistentId(2025);
300 std::string url = "";
301 window->SetUIContentInner(
302 url, nullptr, nullptr, WindowSetUIContentType::DEFAULT, BackupAndRestoreType::NONE, nullptr);
303 SingleHandTransform testTransform;
304 testTransform.posX = 100;
305 window->NotifySingleHandTransformChange(testTransform);
306 ASSERT_EQ(testTransform.posX, window->singleHandTransform_.posX);
307 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifySingleHandTransformChange_TestUIContent end";
308 }
309
310 /**
311 * @tc.name: NotifyTransformChange_TestUIContent
312 * @tc.desc: NotifyTransformChange
313 * @tc.type: FUNC
314 */
315 HWTEST_F(WindowSessionImplLayoutTest, NotifyTransformChange_TestUIContent, TestSize.Level1)
316 {
317 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyTransformChange_TestUIContent start";
318 sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
319 option->SetWindowName("NotifyTransformChange_TestUIContent");
320 option->SetIsUIExtFirstSubWindow(true);
321 sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
322 window->property_->SetPersistentId(2025);
323
324 Transform testTransform;
325 window->uiContent_ = nullptr;
326 window->SetNeedRenotifyTransform(true);
327 window->NotifyTransformChange(testTransform);
328 ASSERT_EQ(true, window->IsNeedRenotifyTransform());
329
330 window->uiContent_ = std::make_unique<Ace::UIContentMocker>();
331 window->SetNeedRenotifyTransform(true);
332 window->NotifyTransformChange(testTransform);
333 ASSERT_EQ(false, window->IsNeedRenotifyTransform());
334 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyTransformChange_TestUIContent end";
335 }
336
337 /**
338 * @tc.name: NotifyAfterUIContentReady
339 * @tc.desc: NotifyAfterUIContentReady
340 * @tc.type: FUNC
341 */
342 HWTEST_F(WindowSessionImplLayoutTest, NotifyAfterUIContentReady, TestSize.Level1)
343 {
344 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyAfterUIContentReady start";
345 sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
346 option->SetWindowName("NotifyAfterUIContentReady");
347 option->SetIsUIExtFirstSubWindow(true);
348 sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
349 window->property_->SetPersistentId(2025);
350 window->uiContent_ = std::make_unique<Ace::UIContentMocker>();
351 window->SetNeedRenotifyTransform(false);
352 window->NotifyAfterUIContentReady();
353 ASSERT_EQ(false, window->IsNeedRenotifyTransform());
354 window->SetNeedRenotifyTransform(false);
355 window->NotifyAfterUIContentReady();
356 ASSERT_EQ(false, window->IsNeedRenotifyTransform());
357 GTEST_LOG_(INFO) << "WindowSessionImplLayoutTest: NotifyAfterUIContentReady end";
358 }
359 }
360 } // namespace Rosen
361 } // namespace OHOS
362