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 #include <gtest/gtest.h>
16
17 #include "display_manager.h"
18 #include "input_event.h"
19 #include "key_event.h"
20 #include "mock/mock_session_stage.h"
21 #include "pointer_event.h"
22
23 #include "session/host/include/main_session.h"
24 #include "session/host/include/keyboard_session.h"
25 #include "session/host/include/scene_session.h"
26 #include "session/host/include/sub_session.h"
27 #include "session/host/include/system_session.h"
28 #include "window_helper.h"
29 #include "wm_common.h"
30
31
32 using namespace testing;
33 using namespace testing::ext;
34 namespace OHOS {
35 namespace Rosen {
36 class SceneSessionTest3 : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp() override;
41 void TearDown() override;
42 };
43
SetUpTestCase()44 void SceneSessionTest3::SetUpTestCase()
45 {
46 }
47
TearDownTestCase()48 void SceneSessionTest3::TearDownTestCase()
49 {
50 }
51
SetUp()52 void SceneSessionTest3::SetUp()
53 {
54 }
55
TearDown()56 void SceneSessionTest3::TearDown()
57 {
58 }
59
60 namespace {
61
62 /**
63 * @tc.name: SetAspectRatio11
64 * @tc.desc: normal function
65 * @tc.type: FUNC
66 */
67 HWTEST_F(SceneSessionTest3, SetAspectRatio1, Function | SmallTest | Level2)
68 {
69 SessionInfo info;
70 info.abilityName_ = "SetAspectRatio11";
71 info.bundleName_ = "SetAspectRatio11";
72 sptr<SceneSession::SpecificSessionCallback> specificCallback_ =
73 new (std::nothrow) SceneSession::SpecificSessionCallback();
74 EXPECT_NE(specificCallback_, nullptr);
75 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
76 EXPECT_NE(scensession, nullptr);
77 scensession->isActive_ = true;
78 scensession->property_ = nullptr;
79 float ratio = 0.0001;
80 auto result = scensession->SetAspectRatio(ratio);
81 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
82 }
83
84
85 /**
86 * @tc.name: SetAspectRatio12
87 * @tc.desc: normal function
88 * @tc.type: FUNC
89 */
90 HWTEST_F(SceneSessionTest3, SetAspectRatio12, Function | SmallTest | Level2)
91 {
92 SessionInfo info;
93 info.abilityName_ = "SetAspectRatio12";
94 info.bundleName_ = "SetAspectRatio12";
95 sptr<SceneSession::SpecificSessionCallback> specificCallback_ =
96 new (std::nothrow) SceneSession::SpecificSessionCallback();
97 EXPECT_NE(specificCallback_, nullptr);
98 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
99 EXPECT_NE(scensession, nullptr);
100 scensession->isActive_ = true;
101
102 float ratio = 0.0001;
103 scensession->moveDragController_ = nullptr;
104 auto result = scensession->SetAspectRatio(ratio);
105 ASSERT_EQ(result, WSError::WS_OK);
106 scensession->moveDragController_ = new (std::nothrow) MoveDragController(0);
107 result = scensession->SetAspectRatio(ratio);
108 ASSERT_EQ(result, WSError::WS_OK);
109
110 sptr<WindowSessionProperty> property = new(std::nothrow) WindowSessionProperty();
111 EXPECT_NE(property, nullptr);
112 property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
113 scensession->SetSessionProperty(property);
114 result = scensession->SetAspectRatio(ratio);
115 ASSERT_EQ(result, WSError::WS_OK);
116 }
117
118 /**
119 * @tc.name: SetAspectRatio15
120 * @tc.desc: normal function
121 * @tc.type: FUNC
122 */
123 HWTEST_F(SceneSessionTest3, SetAspectRatio15, Function | SmallTest | Level2)
124 {
125 SessionInfo info;
126 info.abilityName_ = "SetAspectRatio15";
127 info.bundleName_ = "SetAspectRatio15";
128 sptr<SceneSession::SpecificSessionCallback> specificCallback_ =
129 new (std::nothrow) SceneSession::SpecificSessionCallback();
130 EXPECT_NE(specificCallback_, nullptr);
131 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
132 EXPECT_NE(scensession, nullptr);
133 scensession->isActive_ = true;
134
135 float ratio = 0.1;
136 sptr<WindowSessionProperty> property = new(std::nothrow) WindowSessionProperty();
137 EXPECT_NE(property, nullptr);
138 property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
139 scensession->SetSessionProperty(property);
140 WindowLimits limits;
141 limits.maxHeight_ = 10;
142 limits.minWidth_ = 0;
143 property->SetWindowLimits(limits);
144 scensession->SetAspectRatio(ratio);
145
146 limits.maxHeight_ = 0;
147 limits.minWidth_ = 10;
148 property->SetWindowLimits(limits);
149 scensession->SetAspectRatio(ratio);
150 EXPECT_NE(scensession, nullptr);
151 }
152
153 /**
154 * @tc.name: SetAspectRatio8
155 * @tc.desc: normal function
156 * @tc.type: FUNC
157 */
158 HWTEST_F(SceneSessionTest3, SetAspectRatio8, Function | SmallTest | Level2)
159 {
160 SessionInfo info;
161 info.abilityName_ = "SetAspectRatio8";
162 info.bundleName_ = "SetAspectRatio8";
163 sptr<SceneSession::SpecificSessionCallback> specificCallback_ =
164 new (std::nothrow) SceneSession::SpecificSessionCallback();
165 EXPECT_NE(specificCallback_, nullptr);
166 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
167 EXPECT_NE(scensession, nullptr);
168 scensession->isActive_ = true;
169 sptr<WindowSessionProperty> property = new(std::nothrow) WindowSessionProperty();
170 EXPECT_NE(property, nullptr);
171 property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
172 scensession->SetSessionProperty(property);
173
174 float ratio = 0.1;
175 WindowLimits limits;
176 limits.maxHeight_ = 10;
177 limits.minWidth_ = 10;
178 property->SetWindowLimits(limits);
179 auto result = scensession->SetAspectRatio(ratio);
180
181 ratio = 10;
182 result = scensession->SetAspectRatio(ratio);
183 ASSERT_EQ(result, WSError::WS_OK);
184 }
185
186 /**
187 * @tc.name: UpdateRect1
188 * @tc.desc: normal function
189 * @tc.type: FUNC
190 */
191 HWTEST_F(SceneSessionTest3, UpdateRect1, Function | SmallTest | Level2)
192 {
193 SessionInfo info;
194 info.abilityName_ = "UpdateRect1";
195 info.bundleName_ = "UpdateRect1";
196 sptr<SceneSession::SpecificSessionCallback> specificCallback_ =
197 new (std::nothrow) SceneSession::SpecificSessionCallback();
198 EXPECT_NE(specificCallback_, nullptr);
199 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
200 EXPECT_NE(scensession, nullptr);
201 scensession->isActive_ = true;
202
203 sptr<WindowSessionProperty> property = new(std::nothrow) WindowSessionProperty();
204 EXPECT_NE(property, nullptr);
205 property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
206
207 scensession->SetSessionProperty(property);
208 WSRect rect({1, 1, 1, 1});
209 SizeChangeReason reason = SizeChangeReason::UNDEFINED;
210 WSError result = scensession->UpdateRect(rect, reason, "SceneSessionTest3");
211 ASSERT_EQ(result, WSError::WS_OK);
212
213 scensession->winRect_ = rect;
214 result = scensession->UpdateRect(rect, reason, "SceneSessionTest3");
215 ASSERT_EQ(result, WSError::WS_OK);
216
217 scensession->reason_ = SizeChangeReason::DRAG_END;
218 result = scensession->UpdateRect(rect, reason, "SceneSessionTest3");
219 ASSERT_EQ(result, WSError::WS_OK);
220
221 WSRect rect2({0, 0, 0, 0});
222 result = scensession->UpdateRect(rect2, reason, "SceneSessionTest3");
223 ASSERT_EQ(result, WSError::WS_OK);
224 }
225
226 /**
227 * @tc.name: FixKeyboardPositionByKeyboardPanel
228 * @tc.desc: normal function
229 * @tc.type: FUNC
230 */
231 HWTEST_F(SceneSessionTest3, FixKeyboardPositionByKeyboardPanel, Function | SmallTest | Level2)
232 {
233 SessionInfo info;
234 info.abilityName_ = "FixKeyboardPositionByKeyboardPanel";
235 info.bundleName_ = "FixKeyboardPositionByKeyboardPanel";
236 sptr<SceneSession::SpecificSessionCallback> specificCallback_ =
237 new (std::nothrow) SceneSession::SpecificSessionCallback();
238 EXPECT_NE(specificCallback_, nullptr);
239 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
240 EXPECT_NE(scensession, nullptr);
241 scensession->isActive_ = true;
242
243 sptr<WindowSessionProperty> property = new(std::nothrow) WindowSessionProperty();
244 EXPECT_NE(property, nullptr);
245 property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
246
247 scensession->SetSessionProperty(property);
248 scensession->FixKeyboardPositionByKeyboardPanel(nullptr, nullptr);
249 ASSERT_NE(scensession, nullptr);
250 }
251
252 /**
253 * @tc.name: FixKeyboardPositionByKeyboardPanel1
254 * @tc.desc: normal function
255 * @tc.type: FUNC
256 */
257 HWTEST_F(SceneSessionTest3, FixKeyboardPositionByKeyboardPanel1, Function | SmallTest | Level2)
258 {
259 SessionInfo info;
260 info.abilityName_ = "FixKeyboardPositionByKeyboardPanel1";
261 info.bundleName_ = "FixKeyboardPositionByKeyboardPanel1";
262 sptr<SceneSession::SpecificSessionCallback> specificCallback_ =
263 new (std::nothrow) SceneSession::SpecificSessionCallback();
264 EXPECT_NE(specificCallback_, nullptr);
265 sptr<SceneSession> scenceSession = new (std::nothrow) SceneSession(info, nullptr);
266 EXPECT_NE(scenceSession, nullptr);
267 scenceSession->isActive_ = true;
268
269 sptr<WindowSessionProperty> property = new(std::nothrow) WindowSessionProperty();
270 EXPECT_NE(property, nullptr);
271 property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
272
273 scenceSession->SetSessionProperty(property);
274 scenceSession->FixKeyboardPositionByKeyboardPanel(scenceSession, scenceSession);
275 ASSERT_NE(scenceSession, nullptr);
276
277 sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, nullptr, nullptr);
278 ASSERT_NE(keyboardSession, nullptr);
279 property->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_FLOAT, 1);
280 scenceSession->FixKeyboardPositionByKeyboardPanel(scenceSession, keyboardSession);
281 }
282
283 /**
284 * @tc.name: NotifyClientToUpdateRectTask
285 * @tc.desc: normal function
286 * @tc.type: FUNC
287 */
288 HWTEST_F(SceneSessionTest3, NotifyClientToUpdateRectTask, Function | SmallTest | Level2)
289 {
290 SessionInfo info;
291 info.abilityName_ = "NotifyClientToUpdateRectTask";
292 info.bundleName_ = "NotifyClientToUpdateRectTask";
293 sptr<SceneSession> sceneSession = new (std::nothrow) SceneSession(info, nullptr);
294 EXPECT_NE(sceneSession, nullptr);
295 sceneSession->isActive_ = true;
296
297 sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
298 EXPECT_NE(property, nullptr);
299 property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
300
301 sceneSession->SetSessionProperty(property);
302 auto result = sceneSession->NotifyClientToUpdateRectTask("SceneSessionTest3", nullptr);
303 ASSERT_NE(result, WSError::WS_OK);
304
305 property->SetWindowType(WindowType::WINDOW_TYPE_KEYBOARD_PANEL);
306 sceneSession->SetSessionProperty(property);
307 sceneSession->isKeyboardPanelEnabled_ = true;
308 ASSERT_EQ(WSError::WS_OK, sceneSession->NotifyClientToUpdateRectTask("SceneSessionTest3", nullptr));
309
310 property->SetWindowType(WindowType::WINDOW_TYPE_KEYBOARD_PANEL);
311 sceneSession->SetSessionProperty(property);
312 sceneSession->isKeyboardPanelEnabled_ = true;
313 ASSERT_EQ(WSError::WS_OK, sceneSession->NotifyClientToUpdateRectTask("SceneSessionTest3", nullptr));
314
315 std::shared_ptr<RSTransaction> rs;
316 ASSERT_EQ(WSError::WS_OK, sceneSession->NotifyClientToUpdateRectTask("SceneSessionTest3", rs));
317 }
318
319 /**
320 * @tc.name: BindDialogSessionTarget1
321 * @tc.desc: normal function
322 * @tc.type: FUNC
323 */
324 HWTEST_F(SceneSessionTest3, BindDialogSessionTarget1, Function | SmallTest | Level2)
325 {
326 SessionInfo info;
327 info.abilityName_ = "BindDialogSessionTarget1";
328 info.bundleName_ = "BindDialogSessionTarget1";
329 sptr<SceneSession::SpecificSessionCallback> specificCallback_ =
330 new (std::nothrow) SceneSession::SpecificSessionCallback();
331 EXPECT_NE(specificCallback_, nullptr);
332 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
333 EXPECT_NE(scensession, nullptr);
334 scensession->isActive_ = true;
335
336 sptr<WindowSessionProperty> property = new(std::nothrow) WindowSessionProperty();
337 EXPECT_NE(property, nullptr);
338 property->SetWindowType(WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT);
339 uint32_t p = 10;
340 property->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_BOTTOM, p);
341 scensession->SetSessionProperty(property);
342
343 sptr<SceneSession> sceneSession = nullptr;
344 WSError result = scensession->BindDialogSessionTarget(sceneSession);
345 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
346
347 scensession->sessionChangeCallback_ = nullptr;
348 sptr<SceneSession> sceneSession1 = scensession;
349 result = scensession->BindDialogSessionTarget(sceneSession1);
350 ASSERT_EQ(result, WSError::WS_OK);
351
352 sceneSession1->sessionChangeCallback_ = new (std::nothrow) MainSession::SessionChangeCallback();
353 EXPECT_NE(sceneSession1->sessionChangeCallback_, nullptr);
__anon7d60391d0202(const sptr<SceneSession>&) 354 sceneSession1->sessionChangeCallback_->onBindDialogTarget_ = [](const sptr<SceneSession>&) {};
355 result = scensession->BindDialogSessionTarget(sceneSession1);
356 ASSERT_EQ(result, WSError::WS_OK);
357 }
358
359 /**
360 * @tc.name: ClearSpecificSessionCbMap1
361 * @tc.desc: normal function
362 * @tc.type: FUNC
363 */
364 HWTEST_F(SceneSessionTest3, ClearSpecificSessionCbMap1, Function | SmallTest | Level2)
365 {
366 SessionInfo info;
367 info.abilityName_ = "ClearSpecificSessionCbMap1";
368 info.bundleName_ = "ClearSpecificSessionCbMap1";
369 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
370 EXPECT_NE(nullptr, scensession);
371 scensession->ClearSpecificSessionCbMap();
372
373 sptr<MainSession::SessionChangeCallback> sessionChangeCallback =
374 new (std::nothrow) MainSession::SessionChangeCallback();
__anon7d60391d0302(bool, int32_t) 375 scensession->clearCallbackMapFunc_ = [](bool, int32_t) {};
376 scensession->sessionChangeCallback_ = sessionChangeCallback;
377 scensession->ClearSpecificSessionCbMap();
378 }
379
380 /**
381 * @tc.name: IsMovableWindowType
382 * @tc.desc: normal function
383 * @tc.type: FUNC
384 */
385 HWTEST_F(SceneSessionTest3, IsMovableWindowType, Function | SmallTest | Level2)
386 {
387 SessionInfo info;
388 info.abilityName_ = "IsMovableWindowType";
389 info.bundleName_ = "IsMovableWindowType";
390 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
391 EXPECT_NE(nullptr, scensession);
392
393 scensession->property_ = nullptr;
394 EXPECT_EQ(scensession->IsMovableWindowType(), false);
395 }
396
397 /**
398 * @tc.name: SetBlankFlag
399 * @tc.desc: check func SetBlankFlag
400 * @tc.type: FUNC
401 */
402 HWTEST_F(SceneSessionTest3, SetBlankFlag, Function | SmallTest | Level2)
403 {
404 SessionInfo info;
405 info.abilityName_ = "SetBlankFlag";
406 info.bundleName_ = "SetBlankFlag";
407 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
408 EXPECT_NE(nullptr, scensession);
409
410 bool isAddBlank = true;
411 scensession->SetBlankFlag(isAddBlank);
412 ASSERT_EQ(isAddBlank, scensession->GetBlankFlag());
413 }
414
415 /**
416 * @tc.name: GetBlankFlag
417 * @tc.desc: check func GetBlankFlag
418 * @tc.type: FUNC
419 */
420 HWTEST_F(SceneSessionTest3, GetBlankFlag, Function | SmallTest | Level2)
421 {
422 SessionInfo info;
423 info.abilityName_ = "GetBlankFlag";
424 info.bundleName_ = "GetBlankFlag";
425 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
426 EXPECT_NE(nullptr, scensession);
427
428 bool isAddBlank = true;
429 scensession->SetBlankFlag(isAddBlank);
430 ASSERT_EQ(isAddBlank, scensession->GetBlankFlag());
431 }
432
433 /**
434 * @tc.name: SetBufferAvailableCallbackEnable
435 * @tc.desc: check func SetBufferAvailableCallbackEnable
436 * @tc.type: FUNC
437 */
438 HWTEST_F(SceneSessionTest3, SetBufferAvailableCallbackEnable, Function | SmallTest | Level2)
439 {
440 SessionInfo info;
441 info.abilityName_ = "SetBufferAvailableCallbackEnable";
442 info.bundleName_ = "SetBufferAvailableCallbackEnable";
443 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
444 EXPECT_NE(nullptr, scensession);
445
446 bool enable = true;
447 scensession->SetBufferAvailableCallbackEnable(enable);
448 ASSERT_EQ(enable, scensession->GetBufferAvailableCallbackEnable());
449 }
450
451 /**
452 * @tc.name: GetBufferAvailableCallbackEnable
453 * @tc.desc: check func GetBufferAvailableCallbackEnable
454 * @tc.type: FUNC
455 */
456 HWTEST_F(SceneSessionTest3, GetBufferAvailableCallbackEnable, Function | SmallTest | Level2)
457 {
458 SessionInfo info;
459 info.abilityName_ = "GetBufferAvailableCallbackEnable";
460 info.bundleName_ = "GetBufferAvailableCallbackEnable";
461 sptr<SceneSession> scensession = new (std::nothrow) SceneSession(info, nullptr);
462 EXPECT_NE(nullptr, scensession);
463
464 bool enable = true;
465 scensession->SetBufferAvailableCallbackEnable(enable);
466 ASSERT_EQ(enable, scensession->GetBufferAvailableCallbackEnable());
467 }
468
469 /**
470 * @tc.name: UpdateScaleInner
471 * @tc.desc: check func UpdateScaleInner
472 * @tc.type: FUNC
473 */
474 HWTEST_F(SceneSessionTest3, UpdateScaleInner, Function | SmallTest | Level2)
475 {
476 SessionInfo info;
477 info.abilityName_ = "UpdateScaleInner";
478 info.bundleName_ = "UpdateScaleInner";
479 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
480 EXPECT_NE(nullptr, sceneSession);
481
482 sceneSession->sessionStage_ = nullptr;
483 sceneSession->state_ = SessionState::STATE_FOREGROUND;
484 bool res = sceneSession->UpdateScaleInner(10.0f, 10.0f, 10.0f, 10.0f);
485 EXPECT_EQ(true, res);
486
487 res = sceneSession->UpdateScaleInner(10.0f, 9.0f, 10.0f, 10.0f);
488 res = sceneSession->UpdateScaleInner(10.0f, 9.0f, 9.0f, 10.0f);
489 res = sceneSession->UpdateScaleInner(10.0f, 9.0f, 9.0f, 9.0f);
490 EXPECT_EQ(true, res);
491
492 sceneSession->state_ = SessionState::STATE_BACKGROUND;
493 res = sceneSession->UpdateScaleInner(10.0f, 9.0f, 9.0f, 9.0f);
494 EXPECT_EQ(false, res);
495
496 sceneSession->state_ = SessionState::STATE_FOREGROUND;
497 sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
498 ASSERT_NE(mockSessionStage, nullptr);
499 sceneSession->sessionStage_ = mockSessionStage;
500 res = sceneSession->UpdateScaleInner(1.0f, 2.0f, 3.0f, 4.0f);
501 EXPECT_EQ(true, res);
502 res = sceneSession->UpdateScaleInner(1.0f, 2.0f, 3.0f, 4.0f);
503 EXPECT_EQ(false, res);
504 }
505 }
506 }
507 }