1 /*
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 #include "session/host/include/system_session.h"
18 
19 #include "common/include/session_permission.h"
20 #include "key_event.h"
21 #include "mock/mock_session.h"
22 #include "mock/mock_session_stage.h"
23 #include "session/host/include/session.h"
24 #include <ui/rs_surface_node.h>
25 #include "window_event_channel_base.h"
26 #include "window_helper.h"
27 #include "window_manager_hilog.h"
28 #include "pointer_event.h"
29 
30 using namespace testing;
31 using namespace testing::ext;
32 
33 namespace OHOS {
34 namespace Rosen {
35 constexpr int WAIT_ASYNC_US = 1000000;
36 class SystemSessionTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp() override;
41     void TearDown() override;
42     SessionInfo info;
43     sptr<SystemSession::SpecificSessionCallback> specificCallback = nullptr;
44     sptr<SystemSession> systemSession_;
45 private:
46     RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
47     sptr<SystemSession> GetSystemSession(const std::string& name);
48     sptr<SceneSession> GetSceneSession(const std::string& name);
49 };
50 
SetUpTestCase()51 void SystemSessionTest::SetUpTestCase()
52 {
53 }
54 
TearDownTestCase()55 void SystemSessionTest::TearDownTestCase()
56 {
57 }
58 
SetUp()59 void SystemSessionTest::SetUp()
60 {
61     SessionInfo info;
62     info.abilityName_ = "testSystemSession1";
63     info.moduleName_ = "testSystemSession2";
64     info.bundleName_ = "testSystemSession3";
65     systemSession_ = sptr<SystemSession>::MakeSptr(info, specificCallback);
66     EXPECT_NE(nullptr, systemSession_);
67 }
68 
TearDown()69 void SystemSessionTest::TearDown()
70 {
71     systemSession_ = nullptr;
72 }
73 
CreateRSSurfaceNode()74 RSSurfaceNode::SharedPtr SystemSessionTest::CreateRSSurfaceNode()
75 {
76     struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
77     rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
78     auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
79     return surfaceNode;
80 }
81 
GetSystemSession(const std::string& name)82 sptr<SystemSession> SystemSessionTest::GetSystemSession(const std::string& name)
83 {
84     SessionInfo info;
85     info.abilityName_ = name;
86     info.moduleName_ = name;
87     info.bundleName_ = name;
88     auto sysSession = sptr<SystemSession>::MakeSptr(info, nullptr);
89     return sysSession;
90 }
91 
GetSceneSession(const std::string& name)92 sptr<SceneSession> SystemSessionTest::GetSceneSession(const std::string& name)
93 {
94     SessionInfo info;
95     info.abilityName_ = name;
96     info.moduleName_ = name;
97     info.bundleName_ = name;
98     auto sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
99     return sceneSession;
100 }
101 
102 namespace {
103 
104 /**
105  * @tc.name: TransferKeyEvent01
106  * @tc.desc: check func TransferKeyEvent
107  * @tc.type: FUNC
108  */
HWTEST_F(SystemSessionTest, TransferKeyEvent01, Function | SmallTest | Level1)109 HWTEST_F(SystemSessionTest, TransferKeyEvent01, Function | SmallTest | Level1)
110 {
111     systemSession_->state_ = SessionState::STATE_END;
112 
113     ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, systemSession_->TransferKeyEvent(nullptr));
114 }
115 
116 /**
117  * @tc.name: TransferKeyEvent02
118  * @tc.desc: check func TransferKeyEvent
119  * @tc.type: FUNC
120  */
HWTEST_F(SystemSessionTest, TransferKeyEvent02, Function | SmallTest | Level1)121 HWTEST_F(SystemSessionTest, TransferKeyEvent02, Function | SmallTest | Level1)
122 {
123     systemSession_->state_ = SessionState::STATE_CONNECT;
124     std::shared_ptr<MMI::KeyEvent> keyEvent = nullptr;
125 
126     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, systemSession_->TransferKeyEvent(keyEvent));
127 }
128 
129 /**
130  * @tc.name: ProcessBackEvent01
131  * @tc.desc: check func ProcessBackEvent
132  * @tc.type: FUNC
133  */
HWTEST_F(SystemSessionTest, ProcessBackEvent01, Function | SmallTest | Level1)134 HWTEST_F(SystemSessionTest, ProcessBackEvent01, Function | SmallTest | Level1)
135 {
136     systemSession_->state_ = SessionState::STATE_END;
137 
138     ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, systemSession_->ProcessBackEvent());
139 }
140 
141 /**
142  * @tc.name: NotifyClientToUpdateRect01
143  * @tc.desc: check func NotifyClientToUpdateRect
144  * @tc.type: FUNC
145  */
HWTEST_F(SystemSessionTest, NotifyClientToUpdateRect01, Function | SmallTest | Level1)146 HWTEST_F(SystemSessionTest, NotifyClientToUpdateRect01, Function | SmallTest | Level1)
147 {
148     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
149     ASSERT_NE(mockSessionStage, nullptr);
150     systemSession_->sessionStage_ = mockSessionStage;
151     auto ret = systemSession_->NotifyClientToUpdateRect("SystemSessionTest", nullptr);
152     ASSERT_EQ(WSError::WS_OK, ret);
153 }
154 
155 /**
156  * @tc.name: CheckPointerEventDispatch
157  * @tc.desc: check func CheckPointerEventDispatch
158  * @tc.type: FUNC
159  */
HWTEST_F(SystemSessionTest, CheckPointerEventDispatch, Function | SmallTest | Level1)160 HWTEST_F(SystemSessionTest, CheckPointerEventDispatch, Function | SmallTest | Level1)
161 {
162     std::shared_ptr<MMI::PointerEvent> pointerEvent_ =  MMI::PointerEvent::Create();
163     SessionInfo info;
164     info.abilityName_ = "CheckPointerEventDispatch";
165     info.bundleName_ = "CheckPointerEventDispatchBundleName";
166     info.windowType_ = 2122;
167     sptr<SceneSession::SpecificSessionCallback> specificCallback_ =
168         new (std::nothrow) SceneSession::SpecificSessionCallback();
169     sptr<SystemSession> sysSession =
170         new (std::nothrow) SystemSession(info, specificCallback_);
171     sysSession->SetSessionState(SessionState::STATE_FOREGROUND);
172     bool ret1 = sysSession->CheckPointerEventDispatch(pointerEvent_);
173     ASSERT_EQ(true, ret1);
174 }
175 
176 /**
177  * @tc.name: UpdatePointerArea
178  * @tc.desc: check func UpdatePointerArea
179  * @tc.type: FUNC
180  */
HWTEST_F(SystemSessionTest, UpdatePointerArea, Function | SmallTest | Level1)181 HWTEST_F(SystemSessionTest, UpdatePointerArea, Function | SmallTest | Level1)
182 {
183     WSRect rect = { 1, 1, 1, 1 };
184     SessionInfo info;
185     info.abilityName_ = "UpdatePointerArea";
186     info.bundleName_ = "UpdatePointerAreaBundleName";
187     info.windowType_ = 2122;
188     sptr<SceneSession::SpecificSessionCallback> specificCallback_ =
189         new (std::nothrow) SceneSession::SpecificSessionCallback();
190     sptr<SystemSession> sysSession =
191         new (std::nothrow) SystemSession(info, specificCallback_);
192     sysSession->UpdatePointerArea(rect);
193     ASSERT_NE(sysSession->preRect_, rect);
194 
195     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
196     property->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
197     property->SetDecorEnable(true);
198     sysSession->property_ = property;
199     sysSession->UpdatePointerArea(rect);
200     ASSERT_EQ(sysSession->preRect_, rect);
201 }
202 
203 /**
204  * @tc.name: ProcessPointDownSession
205  * @tc.desc: test function : ProcessPointDownSession
206  * @tc.type: FUNC
207  */
HWTEST_F(SystemSessionTest, ProcessPointDownSession, Function | SmallTest | Level1)208 HWTEST_F(SystemSessionTest, ProcessPointDownSession, Function | SmallTest | Level1)
209 {
210     ASSERT_TRUE(systemSession_ != nullptr);
211 
212     int32_t posX = 2;
213     int32_t posY = 3;
214     auto ret = systemSession_->ProcessPointDownSession(posX, posY);
215     ASSERT_EQ(WSError::WS_OK, ret);
216 }
217 
218 /**
219  * @tc.name: GetMissionId
220  * @tc.desc: test function : GetMissionId
221  * @tc.type: FUNC
222  */
HWTEST_F(SystemSessionTest, GetMissionId, Function | SmallTest | Level1)223 HWTEST_F(SystemSessionTest, GetMissionId, Function | SmallTest | Level1)
224 {
225     ASSERT_TRUE(systemSession_ != nullptr);
226     SessionInfo info;
227     info.abilityName_ = "testSystemSession1";
228     info.moduleName_ = "testSystemSession2";
229     info.bundleName_ = "testSystemSession3";
230     sptr<Session> session = new (std::nothrow) Session(info);
231     systemSession_->parentSession_ = session;
232     auto ret = systemSession_->GetMissionId();
233     ASSERT_EQ(0, ret);
234 }
235 
236 /**
237  * @tc.name: RectCheck
238  * @tc.desc: test function : RectCheck
239  * @tc.type: FUNC
240  */
HWTEST_F(SystemSessionTest, RectCheck, Function | SmallTest | Level1)241 HWTEST_F(SystemSessionTest, RectCheck, Function | SmallTest | Level1)
242 {
243     ASSERT_TRUE(systemSession_ != nullptr);
244     SessionInfo info;
245     info.abilityName_ = "testRectCheck";
246     info.moduleName_ = "testRectCheck";
247     info.bundleName_ = "testRectCheck";
248     sptr<Session> session = new (std::nothrow) Session(info);
249     EXPECT_NE(nullptr, session);
250     systemSession_->parentSession_ = session;
251     uint32_t curWidth = 100;
252     uint32_t curHeight = 200;
253     systemSession_->RectCheck(curWidth, curHeight);
254 
255     curWidth = 0;
256     curHeight = 0;
257     systemSession_->RectCheck(curWidth, curHeight);
258 
259     curWidth = 1930;
260     curHeight = 0;
261     systemSession_->RectCheck(curWidth, curHeight);
262 
263     curWidth = 330;
264     curHeight = 0;
265     systemSession_->RectCheck(curWidth, curHeight);
266 
267     curWidth = 330;
268     curHeight = 1930;
269     systemSession_->RectCheck(curWidth, curHeight);
270 }
271 
272 /**
273  * @tc.name: SetDialogSessionBackGestureEnabled01
274  * @tc.desc: test function : SetDialogSessionBackGestureEnabled
275  * @tc.type: FUNC
276  */
HWTEST_F(SystemSessionTest, SetDialogSessionBackGestureEnabled01, Function | SmallTest | Level1)277 HWTEST_F(SystemSessionTest, SetDialogSessionBackGestureEnabled01, Function | SmallTest | Level1)
278 {
279     ASSERT_TRUE(systemSession_ != nullptr);
280     SessionInfo info;
281     info.abilityName_ = "SetDialogSessionBackGestureEnabled";
282     info.moduleName_ = "SetDialogSessionBackGestureEnabled";
283     info.bundleName_ = "SetDialogSessionBackGestureEnabled";
284     sptr<Session> session = new (std::nothrow) Session(info);
285     EXPECT_NE(nullptr, session);
286 
287     systemSession_->property_->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
288     auto ret = systemSession_->SetDialogSessionBackGestureEnabled(true);
289     ASSERT_EQ(ret, WSError::WS_ERROR_INVALID_CALLING);
290 }
291 
292 /**
293  * @tc.name: SetDialogSessionBackGestureEnabled02
294  * @tc.desc: test function : SetDialogSessionBackGestureEnabled
295  * @tc.type: FUNC
296  */
HWTEST_F(SystemSessionTest, SetDialogSessionBackGestureEnabled02, Function | SmallTest | Level1)297 HWTEST_F(SystemSessionTest, SetDialogSessionBackGestureEnabled02, Function | SmallTest | Level1)
298 {
299     ASSERT_TRUE(systemSession_ != nullptr);
300     SessionInfo info;
301     info.abilityName_ = "SetDialogSessionBackGestureEnabled02";
302     info.moduleName_ = "SetDialogSessionBackGestureEnabled02";
303     info.bundleName_ = "SetDialogSessionBackGestureEnabled02";
304     sptr<Session> session = new (std::nothrow) Session(info);
305     EXPECT_NE(nullptr, session);
306 
307     systemSession_->property_->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
308     auto ret = systemSession_->SetDialogSessionBackGestureEnabled(true);
309     ASSERT_EQ(ret, WSError::WS_OK);
310 }
311 
312 /**
313  * @tc.name: UpdateCameraWindowStatus01
314  * @tc.desc: test function : UpdateCameraWindowStatus
315  * @tc.type: FUNC
316  */
HWTEST_F(SystemSessionTest, UpdateCameraWindowStatus01, Function | SmallTest | Level1)317 HWTEST_F(SystemSessionTest, UpdateCameraWindowStatus01, Function | SmallTest | Level1)
318 {
319     sptr<SceneSession::SpecificSessionCallback> specificCallback =
320         sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
321     ASSERT_NE(specificCallback, nullptr);
322     bool result = false;
323 
324     systemSession_->UpdateCameraWindowStatus(true);
325     ASSERT_EQ(result, false);
326 
327     systemSession_->specificCallback_ = specificCallback;
328     systemSession_->UpdateCameraWindowStatus(true);
329     ASSERT_EQ(result, false);
330 
331     systemSession_->property_->SetWindowType(WindowType::WINDOW_TYPE_FLOAT_CAMERA);
332     systemSession_->UpdateCameraWindowStatus(true);
333     ASSERT_EQ(result, false);
334 
335     systemSession_->specificCallback_->onCameraFloatSessionChange_ =
336         [&result] (uint32_t accessTokenId, bool isShowing) {
337             result = isShowing;
338         };
339     systemSession_->UpdateCameraWindowStatus(true);
340     ASSERT_EQ(result, true);
341 }
342 
343 /**
344  * @tc.name: UpdateCameraWindowStatus02
345  * @tc.desc: test function : UpdateCameraWindowStatus
346  * @tc.type: FUNC
347  */
HWTEST_F(SystemSessionTest, UpdateCameraWindowStatus02, Function | SmallTest | Level1)348 HWTEST_F(SystemSessionTest, UpdateCameraWindowStatus02, Function | SmallTest | Level1)
349 {
350     auto sysSession = GetSystemSession("UpdateCameraWindowStatus02");
351     ASSERT_NE(sysSession, nullptr);
352     sptr<SceneSession::SpecificSessionCallback> specificCallback =
353         sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
354     ASSERT_NE(specificCallback, nullptr);
355     bool result = false;
356     sysSession->specificCallback_ = specificCallback;
357 
358     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_PIP);
359     sysSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_PIP);
360     sysSession->UpdateCameraWindowStatus(true);
361     ASSERT_EQ(result, false);
362 
363     sysSession->specificCallback_->onCameraSessionChange_ =
364         [&result](uint32_t accessTokenId, bool isShowing) {
365             result = isShowing;
366         };
367 
368     result = false;
369     sysSession->pipTemplateInfo_.pipTemplateType =
370         static_cast<uint32_t>(PiPTemplateType::VIDEO_CALL);
371     sysSession->UpdateCameraWindowStatus(true);
372     ASSERT_EQ(result, true);
373 
374     result = false;
375     sysSession->pipTemplateInfo_.pipTemplateType =
376         static_cast<uint32_t>(PiPTemplateType::VIDEO_MEETING);
377     sysSession->UpdateCameraWindowStatus(true);
378     ASSERT_EQ(result, true);
379 
380     result = false;
381     sysSession->pipTemplateInfo_.pipTemplateType =
382         static_cast<uint32_t>(PiPTemplateType::VIDEO_LIVE);
383     sysSession->UpdateCameraWindowStatus(true);
384     ASSERT_EQ(result, false);
385 }
386 
387 /**
388  * @tc.name: Show01
389  * @tc.desc: test function : Show
390  * @tc.type: FUNC
391  */
HWTEST_F(SystemSessionTest, Show01, Function | SmallTest | Level1)392 HWTEST_F(SystemSessionTest, Show01, Function | SmallTest | Level1)
393 {
394     // for CheckPermissionWithPropertyAnimation;
395     auto windowProperty = sptr<WindowSessionProperty>::MakeSptr();
396     ASSERT_NE(windowProperty, nullptr);
397     windowProperty->animationFlag_ = static_cast<uint32_t>(WindowAnimation::CUSTOM);
398 
399     // for TOAST or FLOAT permission
400     systemSession_->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
401 
402     systemSession_->Show(windowProperty);
403     usleep(WAIT_ASYNC_US);
404     ASSERT_EQ(systemSession_->property_->animationFlag_, static_cast<uint32_t>(WindowAnimation::CUSTOM));
405 }
406 
407 /**
408  * @tc.name: Hide01
409  * @tc.desc: test function : Hide
410  * @tc.type: FUNC
411  */
HWTEST_F(SystemSessionTest, Hide01, Function | SmallTest | Level1)412 HWTEST_F(SystemSessionTest, Hide01, Function | SmallTest | Level1)
413 {
414     auto sysSession = GetSystemSession("Hide01");
415     ASSERT_NE(sysSession, nullptr);
416 
417     // for CheckPermissionWithPropertyAnimation;
418     auto windowProperty = sptr<WindowSessionProperty>::MakeSptr();
419     ASSERT_NE(windowProperty, nullptr);
420     windowProperty->animationFlag_ = static_cast<uint32_t>(WindowAnimation::CUSTOM);
421 
422     sysSession->property_ = windowProperty;
423     // for TOAST or FLOAT permission
424     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_SYSTEM_FLOAT);
425 
426     // for IsSessionValid
427     sysSession->sessionInfo_.isSystem_ = false;
428     sysSession->state_ = SessionState::STATE_CONNECT;
429     sysSession->isActive_ = true;
430 
431     auto ret = sysSession->Hide();
432     usleep(WAIT_ASYNC_US);
433     ASSERT_EQ(ret, WSError::WS_OK);
434 
435     windowProperty->animationFlag_ = static_cast<uint32_t>(WindowAnimation::DEFAULT);
436     ret = sysSession->Hide();
437     usleep(WAIT_ASYNC_US);
438     ASSERT_EQ(ret, WSError::WS_OK);
439 
440     ret = sysSession->Hide();
441     sysSession->property_ = nullptr;
442     usleep(WAIT_ASYNC_US);
443     ASSERT_EQ(ret, WSError::WS_OK);
444 }
445 
446 /**
447  * @tc.name: ProcessPointDownSession01
448  * @tc.desc: test function : ProcessPointDownSession
449  * @tc.type: FUNC
450  */
HWTEST_F(SystemSessionTest, ProcessPointDownSession01, Function | SmallTest | Level1)451 HWTEST_F(SystemSessionTest, ProcessPointDownSession01, Function | SmallTest | Level1)
452 {
453     auto sysSession = GetSystemSession("ProcessPointDownSession01");
454     ASSERT_NE(sysSession, nullptr);
455     sysSession->persistentId_ = 1;
456     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
457     sysSession->state_ = SessionState::STATE_ACTIVE;
458 
459     auto parentSesssion = GetSceneSession("parentSession");
460     ASSERT_NE(parentSesssion, nullptr);
461     sysSession->parentSession_ = parentSesssion;
462 
463     parentSesssion->dialogVec_.push_back(sysSession);
464     auto ret = sysSession->ProcessPointDownSession(0, 0);
465     ASSERT_EQ(ret, WSError::WS_OK);
466 
467     // for IsTopDialog true
468     auto topDialog = GetSystemSession("TopDialogSession");
469     ASSERT_NE(topDialog, nullptr);
470     topDialog->persistentId_ = 2;
471     topDialog->property_->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
472     topDialog->state_ = SessionState::STATE_ACTIVE;
473     parentSesssion->dialogVec_.push_back(topDialog);
474 
475     ret = sysSession->ProcessPointDownSession(0, 0);
476     ASSERT_EQ(ret, WSError::WS_OK);
477 }
478 
479 /**
480  * @tc.name: ProcessPointDownSession02
481  * @tc.desc: test function : ProcessPointDownSession
482  * @tc.type: FUNC
483  */
HWTEST_F(SystemSessionTest, ProcessPointDownSession02, Function | SmallTest | Level1)484 HWTEST_F(SystemSessionTest, ProcessPointDownSession02, Function | SmallTest | Level1)
485 {
486     auto sysSession = GetSystemSession("ProcessPointDownSession02");
487     ASSERT_NE(sysSession, nullptr);
488     sysSession->persistentId_ = 1;
489     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
490     sysSession->state_ = SessionState::STATE_ACTIVE;
491 
492     sysSession->property_->raiseEnabled_ = false;
493     auto ret = sysSession->ProcessPointDownSession(0, 0);
494     ASSERT_EQ(ret, WSError::WS_OK);
495 
496     sysSession->property_->raiseEnabled_ = true;
497     ret = sysSession->ProcessPointDownSession(0, 0);
498     ASSERT_EQ(ret, WSError::WS_OK);
499 }
500 
501 /**
502  * @tc.name: TransferKeyEvent04
503  * @tc.desc: test function : TransferKeyEvent
504  * @tc.type: FUNC
505  */
HWTEST_F(SystemSessionTest, TransferKeyEvent04, Function | SmallTest | Level1)506 HWTEST_F(SystemSessionTest, TransferKeyEvent04, Function | SmallTest | Level1)
507 {
508     auto sysSession = GetSystemSession("TransferKeyEvent04");
509     ASSERT_NE(sysSession, nullptr);
510     sysSession->persistentId_ = 1;
511     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
512     sysSession->state_ = SessionState::STATE_ACTIVE;
513 
514     std::shared_ptr<MMI::KeyEvent> keyEvent;
515 
516     auto ret = sysSession->TransferKeyEvent(nullptr);
517     ASSERT_EQ(ret, WSError::WS_ERROR_NULLPTR);
518 
519     ret = sysSession->TransferKeyEvent(keyEvent);
520     ASSERT_EQ(ret, WSError::WS_ERROR_NULLPTR);
521 }
522 
523 /**
524  * @tc.name: TransferKeyEvent05
525  * @tc.desc: test function : TransferKeyEvent
526  * @tc.type: FUNC
527  */
HWTEST_F(SystemSessionTest, TransferKeyEvent05, Function | SmallTest | Level1)528 HWTEST_F(SystemSessionTest, TransferKeyEvent05, Function | SmallTest | Level1)
529 {
530     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
531 
532     auto sysSession = GetSystemSession("TransferKeyEvent05");
533     ASSERT_NE(sysSession, nullptr);
534     sysSession->persistentId_ = 1;
535     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
536     sysSession->state_ = SessionState::STATE_ACTIVE;
537 
538     auto ret = sysSession->TransferKeyEvent(keyEvent);
539     ASSERT_EQ(ret, WSError::WS_DO_NOTHING);
540 
541     auto parentSesssion = GetSceneSession("parentSession");
542     ASSERT_NE(parentSesssion, nullptr);
543     sysSession->parentSession_ = parentSesssion;
544 
545     // for CheckKeyEventDispatch return true;
546     parentSesssion->state_ = SessionState::STATE_FOREGROUND;
547     ret = sysSession->TransferKeyEvent(keyEvent);
548     ASSERT_EQ(ret, WSError::WS_DO_NOTHING);
549 
550     keyEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_BACK);
551     ret = sysSession->TransferKeyEvent(keyEvent);
552     ASSERT_EQ(ret, WSError::WS_ERROR_INVALID_PERMISSION);
553 
554     keyEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_HOME);
555     parentSesssion->dialogVec_.push_back(sysSession);
556     ret = sysSession->TransferKeyEvent(keyEvent);
557     ASSERT_EQ(ret, WSError::WS_DO_NOTHING);
558 
559     auto topDialog = GetSystemSession("TopDialogSession");
560     ASSERT_NE(topDialog, nullptr);
561     topDialog->persistentId_ = 2;
562     topDialog->property_->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
563     topDialog->state_ = SessionState::STATE_ACTIVE;
564     parentSesssion->dialogVec_.push_back(topDialog);
565     ret = sysSession->TransferKeyEvent(keyEvent);
566     ASSERT_EQ(ret, WSError::WS_ERROR_INVALID_PERMISSION);
567 }
568 
569 /**
570  * @tc.name: ProcessBackEvent02
571  * @tc.desc: test function : ProcessBackEvent
572  * @tc.type: FUNC
573  */
HWTEST_F(SystemSessionTest, ProcessBackEvent02, Function | SmallTest | Level1)574 HWTEST_F(SystemSessionTest, ProcessBackEvent02, Function | SmallTest | Level1)
575 {
576     auto sysSession = GetSystemSession("ProcessBackEvent02");
577     ASSERT_NE(sysSession, nullptr);
578     sysSession->persistentId_ = 1;
579     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
580     sysSession->state_ = SessionState::STATE_ACTIVE;
581 
582     sysSession->dialogSessionBackGestureEnabled_ = false;
583     auto ret = sysSession->ProcessBackEvent();
584     ASSERT_EQ(ret, WSError::WS_OK);
585 
586     sysSession->dialogSessionBackGestureEnabled_ = true;
587     ret = sysSession->ProcessBackEvent();
588     ASSERT_EQ(ret, WSError::WS_ERROR_NULLPTR);
589 
590     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
591     ret = sysSession->ProcessBackEvent();
592     ASSERT_EQ(ret, WSError::WS_ERROR_NULLPTR);
593 }
594 
595 /**
596  * @tc.name: CheckKeyEventDispatch03
597  * @tc.desc: test function : CheckKeyEventDispatch
598  * @tc.type: FUNC
599  */
HWTEST_F(SystemSessionTest, CheckKeyEventDispatch03, Function | SmallTest | Level1)600 HWTEST_F(SystemSessionTest, CheckKeyEventDispatch03, Function | SmallTest | Level1)
601 {
602     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
603     auto sysSession = GetSystemSession("CheckKeyEventDispatch03");
604     ASSERT_NE(sysSession, nullptr);
605     sysSession->persistentId_ = 1;
606     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_KEYBOARD_PANEL);
607     sysSession->state_ = SessionState::STATE_ACTIVE;
608     sysSession->isRSVisible_ = false;
609 
610     auto ret = sysSession->CheckKeyEventDispatch(keyEvent);
611     ASSERT_EQ(ret, false);
612 
613     sysSession->isRSVisible_ = true;
614     sysSession->winRect_.width_ = 0;
615     ret = sysSession->CheckKeyEventDispatch(keyEvent);
616     ASSERT_EQ(ret, false);
617 
618     sysSession->isRSVisible_ = true;
619     sysSession->winRect_.width_ = 1;
620     sysSession->winRect_.height_ = 0;
621     ret = sysSession->CheckKeyEventDispatch(keyEvent);
622     ASSERT_EQ(ret, false);
623 
624     sysSession->isRSVisible_ = true;
625     sysSession->winRect_.width_ = 1;
626     sysSession->winRect_.height_ = 1;
627     ret = sysSession->CheckKeyEventDispatch(keyEvent);
628     ASSERT_EQ(ret, false);
629 }
630 
631 /**
632  * @tc.name: CheckKeyEventDispatch04
633  * @tc.desc: test function : CheckKeyEventDispatch
634  * @tc.type: FUNC
635  */
HWTEST_F(SystemSessionTest, CheckKeyEventDispatch04, Function | SmallTest | Level1)636 HWTEST_F(SystemSessionTest, CheckKeyEventDispatch04, Function | SmallTest | Level1)
637 {
638     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
639     auto sysSession = GetSystemSession("CheckKeyEventDispatch04");
640     ASSERT_NE(sysSession, nullptr);
641     sysSession->persistentId_ = 1;
642     sysSession->isRSVisible_ = true;
643     sysSession->winRect_.width_ = 1;
644     sysSession->winRect_.height_ = 1;
645     sysSession->state_ = SessionState::STATE_DISCONNECT;
646 
647     auto parentSesssion = GetSceneSession("parentSession");
648     ASSERT_NE(parentSesssion, nullptr);
649     sysSession->parentSession_ = parentSesssion;
650 
651     parentSesssion->state_ = SessionState::STATE_FOREGROUND;
652     auto ret = sysSession->CheckKeyEventDispatch(keyEvent);
653     ASSERT_EQ(ret, false);
654 
655     parentSesssion->state_ = SessionState::STATE_ACTIVE;
656     ret = sysSession->CheckKeyEventDispatch(keyEvent);
657     ASSERT_EQ(ret, false);
658 
659     parentSesssion->state_ = SessionState::STATE_DISCONNECT;
660     sysSession->state_ = SessionState::STATE_FOREGROUND;
661     ret = sysSession->CheckKeyEventDispatch(keyEvent);
662     ASSERT_EQ(ret, false);
663 
664     sysSession->state_ = SessionState::STATE_ACTIVE;
665     ret = sysSession->CheckKeyEventDispatch(keyEvent);
666     ASSERT_EQ(ret, false);
667 
668     parentSesssion->state_ = SessionState::STATE_DISCONNECT;
669     sysSession->state_ = SessionState::STATE_DISCONNECT;
670     ret = sysSession->CheckKeyEventDispatch(keyEvent);
671     ASSERT_EQ(ret, false);
672 }
673 
674 /**
675  * @tc.name: NotifyClientToUpdateRect02
676  * @tc.desc: test function : NotifyClientToUpdateRect
677  * @tc.type: FUNC
678  */
HWTEST_F(SystemSessionTest, NotifyClientToUpdateRect02, Function | SmallTest | Level1)679 HWTEST_F(SystemSessionTest, NotifyClientToUpdateRect02, Function | SmallTest | Level1)
680 {
681     auto sysSession = GetSystemSession("NotifyClientToUpdateRect02");
682     ASSERT_NE(sysSession, nullptr);
683     sysSession->persistentId_ = 1;
684     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_KEYBOARD_PANEL);
685     sysSession->state_ = SessionState::STATE_ACTIVE;
686 
687     //for NotifyClientToUpdateRectTask
688     sysSession->isKeyboardPanelEnabled_ = true;
689 
690     sysSession->dirtyFlags_ = 0;
691     sysSession->reason_ = SizeChangeReason::MAXIMIZE;
692     sysSession->NotifyClientToUpdateRect("SystemSessionTest", nullptr);
693     usleep(WAIT_ASYNC_US);
694     ASSERT_EQ(sysSession->reason_, SizeChangeReason::UNDEFINED);
695 
696     sysSession->reason_ = SizeChangeReason::DRAG;
697     sysSession->NotifyClientToUpdateRect("SystemSessionTest", nullptr);
698     usleep(WAIT_ASYNC_US);
699     ASSERT_EQ(sysSession->reason_, SizeChangeReason::DRAG);
700 }
701 
702 /**
703  * @tc.name: NotifyClientToUpdateRect03
704  * @tc.desc: test function : NotifyClientToUpdateRect
705  * @tc.type: FUNC
706  */
HWTEST_F(SystemSessionTest, NotifyClientToUpdateRect03, Function | SmallTest | Level1)707 HWTEST_F(SystemSessionTest, NotifyClientToUpdateRect03, Function | SmallTest | Level1)
708 {
709     auto sysSession = GetSystemSession("NotifyClientToUpdateRect03");
710     ASSERT_NE(sysSession, nullptr);
711     sysSession->persistentId_ = 1;
712     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_KEYBOARD_PANEL);
713     sysSession->state_ = SessionState::STATE_ACTIVE;
714 
715     sysSession->isKeyboardPanelEnabled_ = true;
716     sysSession->reason_ = SizeChangeReason::MAXIMIZE;
717 
718     sptr<SceneSession::SpecificSessionCallback> specificCallback =
719         sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
720     ASSERT_NE(specificCallback, nullptr);
721     sysSession->specificCallback_ = specificCallback;
722     sysSession->NotifyClientToUpdateRect("SystemSessionTest", nullptr);
723     usleep(WAIT_ASYNC_US);
724     EXPECT_EQ(sysSession->reason_, SizeChangeReason::UNDEFINED);
725 
726     sysSession->reason_ = SizeChangeReason::MAXIMIZE;
727     sysSession->specificCallback_->onUpdateAvoidArea_ = [](const int32_t& persistentId) {};
728     sysSession->NotifyClientToUpdateRect("SystemSessionTest", nullptr);
729     usleep(WAIT_ASYNC_US);
730     ASSERT_EQ(sysSession->reason_, SizeChangeReason::UNDEFINED);
731 }
732 
733 /**
734  * @tc.name: IsVisibleForeground01
735  * @tc.desc: test function : IsVisibleForeground
736  * @tc.type: FUNC
737  */
HWTEST_F(SystemSessionTest, IsVisibleForeground01, Function | SmallTest | Level1)738 HWTEST_F(SystemSessionTest, IsVisibleForeground01, Function | SmallTest | Level1)
739 {
740     auto sysSession = GetSystemSession("IsVisibleForeground01");
741     ASSERT_NE(sysSession, nullptr);
742     sysSession->persistentId_ = 1;
743     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_KEYBOARD_PANEL);
744     sysSession->state_ = SessionState::STATE_ACTIVE;
745     sysSession->isVisible_ = false;
746 
747     auto ret = sysSession->IsVisibleForeground();
748     ASSERT_EQ(ret, false);
749 
750     sysSession->property_->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
751     ret = sysSession->IsVisibleForeground();
752     ASSERT_EQ(ret, false);
753 
754     auto parentSesssion = GetSceneSession("parentSession");
755     ASSERT_NE(parentSesssion, nullptr);
756     sysSession->parentSession_ = parentSesssion;
757     parentSesssion->property_->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
758     ret = sysSession->IsVisibleForeground();
759     ASSERT_EQ(ret, false);
760 
761     parentSesssion->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
762     ret = sysSession->IsVisibleForeground();
763     ASSERT_EQ(ret, false);
764 }
765 
766 /**
767  * @tc.name: UpdatePiPWindowStateChanged
768  * @tc.desc: test function : UpdatePiPWindowStateChanged
769  * @tc.type: FUNC
770  */
HWTEST_F(SystemSessionTest, UpdatePiPWindowStateChanged, Function | SmallTest | Level1)771 HWTEST_F(SystemSessionTest, UpdatePiPWindowStateChanged, Function | SmallTest | Level1)
772 {
773     SessionInfo sessionInfo;
774     sessionInfo.abilityName_ = "UpdatePiPWindowStateChanged";
775     sessionInfo.moduleName_ = "UpdatePiPWindowStateChanged";
776     sessionInfo.bundleName_ = "UpdatePiPWindowStateChanged";
777     sessionInfo.windowType_ = static_cast<uint32_t>(WindowType::APP_MAIN_WINDOW_BASE);
778     sptr<SceneSession::SpecificSessionCallback> callback =
779         sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
780     EXPECT_NE(nullptr, callback);
781     sptr<SystemSession> systemSession =
782         sptr<SystemSession>::MakeSptr(sessionInfo, callback);
783     EXPECT_NE(nullptr, systemSession);
784     PiPStateChangeCallback callbackFun = [](const std::string& bundleName, bool isForeground) {
785         return;
786     };
787     callback->onPiPStateChange_ = callbackFun;
788     EXPECT_EQ(WindowType::APP_MAIN_WINDOW_BASE, systemSession->GetWindowType());
789     systemSession->UpdatePiPWindowStateChanged(true);
790 
791     sessionInfo.windowType_ = static_cast<uint32_t>(WindowType::WINDOW_TYPE_PIP);
792     sptr<SystemSession> system =
793         sptr<SystemSession>::MakeSptr(sessionInfo, callback);
794     EXPECT_NE(nullptr, system);
795     EXPECT_EQ(WindowType::WINDOW_TYPE_PIP, system->GetWindowType());
796     system->UpdatePiPWindowStateChanged(true);
797 }
798 } // namespace
799 } // namespace Rosen
800 } // namespace OHOS
801