• 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 <gtest/gtest.h>
17 #include <pointer_event.h>
18 #include "session/host/include/main_session.h"
19 
20 #include "common/include/session_permission.h"
21 #include "key_event.h"
22 #include "mock/mock_session_stage.h"
23 #include "session/host/include/session.h"
24 #include "session/host/include/system_session.h"
25 #include <ui/rs_surface_node.h>
26 #include "window_event_channel_base.h"
27 #include "window_helper.h"
28 #include "window_manager_hilog.h"
29 
30 using namespace testing;
31 using namespace testing::ext;
32 
33 namespace OHOS {
34 namespace Rosen {
35 class MainSessionTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41     SessionInfo info;
42     sptr<MainSession::SpecificSessionCallback> specificCallback = nullptr;
43     sptr<MainSession> mainSession_;
44 
45 private:
46     RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
47     SystemSessionConfig systemConfig_;
48 };
49 
SetUpTestCase()50 void MainSessionTest::SetUpTestCase() {}
51 
TearDownTestCase()52 void MainSessionTest::TearDownTestCase() {}
53 
SetUp()54 void MainSessionTest::SetUp()
55 {
56     SessionInfo info;
57     info.abilityName_ = "testMainSession1";
58     info.moduleName_ = "testMainSession2";
59     info.bundleName_ = "testMainSession3";
60     mainSession_ = sptr<MainSession>::MakeSptr(info, specificCallback);
61     EXPECT_NE(nullptr, mainSession_);
62 }
63 
TearDown()64 void MainSessionTest::TearDown()
65 {
66     mainSession_ = nullptr;
67 }
68 
CreateRSSurfaceNode()69 RSSurfaceNode::SharedPtr MainSessionTest::CreateRSSurfaceNode()
70 {
71     struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
72     rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
73     auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
74     return surfaceNode;
75 }
76 
77 namespace {
78 
79 /**
80  * @tc.name: MainSession01
81  * @tc.desc: check func MainSession
82  * @tc.type: FUNC
83  */
84 HWTEST_F(MainSessionTest, MainSession01, TestSize.Level1)
85 {
86     MainSession* pMainSession = nullptr;
87     sptr<MainSession::SpecificSessionCallback> pSpecificCallback = nullptr;
88 
89     SessionInfo info;
90     info.persistentId_ = -1;
91     info.abilityName_ = "";
92     info.moduleName_ = "";
93     info.bundleName_ = "";
94     pMainSession = sptr<MainSession>::MakeSptr(info, pSpecificCallback);
95     EXPECT_NE(nullptr, pMainSession);
96 
97     info.persistentId_ = 0;
98     pMainSession = sptr<MainSession>::MakeSptr(info, pSpecificCallback);
99     EXPECT_NE(nullptr, pMainSession);
100 
101     info.persistentId_ = -1;
102     info.abilityName_ = "MainSession01";
103     info.moduleName_ = "MainSession02";
104     info.bundleName_ = "MainSession03";
105     pSpecificCallback = new (std::nothrow) MainSession::SpecificSessionCallback;
106     pMainSession = sptr<MainSession>::MakeSptr(info, pSpecificCallback);
107     EXPECT_NE(nullptr, pMainSession);
108 
109     info.persistentId_ = 0;
110     pMainSession = sptr<MainSession>::MakeSptr(info, pSpecificCallback);
111     EXPECT_NE(nullptr, pMainSession);
112 }
113 
114 /**
115  * @tc.name: MainSession02
116  * @tc.desc: check func MainSession
117  * @tc.type: FUNC
118  */
119 HWTEST_F(MainSessionTest, MainSession02, TestSize.Level1)
120 {
121     MainSession* pMainSession = nullptr;
122     sptr<MainSession::SpecificSessionCallback> pSpecificCallback = nullptr;
123 
124     SessionInfo info;
125     info.persistentId_ = 1999;
126     info.abilityName_ = "test";
127     info.moduleName_ = "test";
128     info.bundleName_ = "test";
129     pMainSession = sptr<MainSession>::MakeSptr(info, pSpecificCallback);
130     ASSERT_NE(nullptr, pMainSession);
131 
132     ASSERT_NE(nullptr, pMainSession->scenePersistence_);
133     auto hasSnapshot = pMainSession->scenePersistence_->HasSnapshot();
134     EXPECT_EQ(hasSnapshot, false);
135 }
136 
137 /**
138  * @tc.name: TransferKeyEvent01
139  * @tc.desc: check func TransferKeyEvent
140  * @tc.type: FUNC
141  */
142 HWTEST_F(MainSessionTest, TransferKeyEvent01, TestSize.Level1)
143 {
144     mainSession_->state_ = SessionState::STATE_END;
145 
146     ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, mainSession_->TransferKeyEvent(nullptr));
147 }
148 
149 /**
150  * @tc.name: TransferKeyEvent02
151  * @tc.desc: check func TransferKeyEvent
152  * @tc.type: FUNC
153  */
154 HWTEST_F(MainSessionTest, TransferKeyEvent02, TestSize.Level1)
155 {
156     mainSession_->state_ = SessionState::STATE_CONNECT;
157     std::shared_ptr<MMI::KeyEvent> keyEvent = nullptr;
158     mainSession_->ClearDialogVector();
159     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, mainSession_->TransferKeyEvent(keyEvent));
160 }
161 
162 /**
163  * @tc.name: TransferKeyEvent03
164  * @tc.desc: check func TransferKeyEvent
165  * @tc.type: FUNC
166  */
167 HWTEST_F(MainSessionTest, TransferKeyEvent03, TestSize.Level1)
168 {
169     mainSession_->state_ = SessionState::STATE_CONNECT;
170     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
171     ASSERT_NE(keyEvent, nullptr);
172     SessionInfo info;
173     info.abilityName_ = "testDialogSession1";
174     info.moduleName_ = "testDialogSession2";
175     info.bundleName_ = "testDialogSession3";
176     sptr<Session> dialogSession = sptr<SystemSession>::MakeSptr(info, nullptr);
177     dialogSession->SetSessionState(SessionState::STATE_ACTIVE);
178     mainSession_->BindDialogToParentSession(dialogSession);
179 
180     ASSERT_EQ(WSError::WS_ERROR_INVALID_PERMISSION, mainSession_->TransferKeyEvent(keyEvent));
181 }
182 
183 /**
184  * @tc.name: TransferKeyEvent04
185  * @tc.desc: check func TransferKeyEvent
186  * @tc.type: FUNC
187  */
188 HWTEST_F(MainSessionTest, TransferKeyEvent04, TestSize.Level1)
189 {
190     mainSession_->state_ = SessionState::STATE_CONNECT;
191     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
192     ASSERT_NE(keyEvent, nullptr);
193     mainSession_->windowEventChannel_ = nullptr;
194     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, mainSession_->TransferKeyEvent(keyEvent));
195 }
196 
197 /**
198  * @tc.name: ProcessPointDownSession01
199  * @tc.desc: check func ProcessPointDownSession
200  * @tc.type: FUNC
201  */
202 HWTEST_F(MainSessionTest, ProcessPointDownSession01, TestSize.Level1)
203 {
204     EXPECT_EQ(WSError::WS_OK, mainSession_->ProcessPointDownSession(100, 200));
205     mainSession_->ClearDialogVector();
206     EXPECT_EQ(WSError::WS_OK, mainSession_->ProcessPointDownSession(10, 20));
207 }
208 
209 /**
210  * @tc.name: ProcessPointDownSession02
211  * @tc.desc: check func ProcessPointDownSession
212  * @tc.type: FUNC
213  */
214 HWTEST_F(MainSessionTest, ProcessPointDownSession02, TestSize.Level1)
215 {
216     mainSession_->BindDialogToParentSession(mainSession_);
217     EXPECT_EQ(WSError::WS_OK, mainSession_->ProcessPointDownSession(10, 20));
218 }
219 
220 /**
221  * @tc.name: ProcessPointDownSession03
222  * @tc.desc: check func ProcessPointDownSession
223  * @tc.type: FUNC
224  */
225 HWTEST_F(MainSessionTest, ProcessPointDownSession03, TestSize.Level1)
226 {
227     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
228     SessionInfo info;
229     info.abilityName_ = "dialogAbilityName";
230     info.moduleName_ = "dialogModuleName";
231     info.bundleName_ = "dialogBundleName";
232     sptr<Session> dialogSession = sptr<Session>::MakeSptr(info);
233     dialogSession->state_ = SessionState::STATE_INACTIVE;
234     mainSession_->dialogVec_.push_back(dialogSession);
235     EXPECT_EQ(WSError::WS_OK, mainSession_->ProcessPointDownSession(0, 0));
236 
237     property->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
238     property->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
239     mainSession_->SetSessionProperty(property);
240     EXPECT_EQ(WSError::WS_OK, mainSession_->ProcessPointDownSession(0, 0));
241 }
242 
243 /**
244  * @tc.name: SetTopmost01
245  * @tc.desc: check func SetTopmost
246  * @tc.type: FUNC
247  */
248 HWTEST_F(MainSessionTest, SetTopmost01, TestSize.Level1)
249 {
250     EXPECT_EQ(WSError::WS_OK, mainSession_->SetTopmost(true));
251     EXPECT_EQ(WSError::WS_OK, mainSession_->SetTopmost(false));
252 }
253 
254 /**
255  * @tc.name: SetTopmost02
256  * @tc.desc: check func SetTopmost
257  * @tc.type: FUNC
258  */
259 HWTEST_F(MainSessionTest, SetTopmost02, TestSize.Level1)
260 {
261     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
262     mainSession_->SetSessionProperty(property);
263     ASSERT_TRUE(mainSession_->GetSessionProperty() != nullptr);
__anone8ff9da30202(bool topmost) 264     mainSession_->RegisterSessionTopmostChangeCallback([](bool topmost) {});
265     EXPECT_EQ(WSError::WS_OK, mainSession_->SetTopmost(true));
266 }
267 
268 /**
269  * @tc.name: SetMainWindowTopmost
270  * @tc.desc: check func SetMainWindowTopmost
271  * @tc.type: FUNC
272  */
273 HWTEST_F(MainSessionTest, SetMainWindowTopmost, TestSize.Level1)
274 {
275     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
276     mainSession_->SetSessionProperty(property);
277     ASSERT_TRUE(mainSession_->GetSessionProperty() != nullptr);
__anone8ff9da30302(bool topmost) 278     mainSession_->SetMainWindowTopmostChangeCallback([](bool topmost) {});
279     EXPECT_EQ(WSError::WS_OK, mainSession_->SetMainWindowTopmost(true));
280 }
281 
282 /**
283  * @tc.name: UpdatePointerArea
284  * @tc.desc: check func UpdatePointerArea
285  * @tc.type: FUNC
286  */
287 HWTEST_F(MainSessionTest, UpdatePointerArea, TestSize.Level1)
288 {
289     WSRect Rect = { 0, 0, 50, 50 };
290     mainSession_->UpdateWindowMode(WindowMode::WINDOW_MODE_UNDEFINED);
291     mainSession_->UpdatePointerArea(Rect);
292     mainSession_->UpdateWindowMode(WindowMode::WINDOW_MODE_FLOATING);
293     mainSession_->UpdatePointerArea(Rect);
294     ASSERT_EQ(Rect, mainSession_->preRect_);
295 }
296 
297 /**
298  * @tc.name: CheckPointerEventDispatch
299  * @tc.desc: check func CheckPointerEventDispatch
300  * @tc.type: FUNC
301  */
302 HWTEST_F(MainSessionTest, CheckPointerEventDispatch, TestSize.Level1)
303 {
304     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
305 
306     mainSession_->SetSessionState(SessionState::STATE_FOREGROUND);
307     bool res = mainSession_->CheckPointerEventDispatch(pointerEvent);
308     ASSERT_EQ(res, true);
309 
310     mainSession_->SetSessionState(SessionState::STATE_ACTIVE);
311     res = mainSession_->CheckPointerEventDispatch(pointerEvent);
312     ASSERT_EQ(res, true);
313 
314     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
315     mainSession_->SetSessionState(SessionState::STATE_DISCONNECT);
316     res = mainSession_->CheckPointerEventDispatch(pointerEvent);
317     ASSERT_EQ(res, true);
318 
319     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_DOWN);
320     mainSession_->SetSessionState(SessionState::STATE_DISCONNECT);
321     res = mainSession_->CheckPointerEventDispatch(pointerEvent);
322     ASSERT_EQ(res, false);
323 }
324 
325 /**
326  * @tc.name: RectCheck03
327  * @tc.desc: check func RectCheck
328  * @tc.type: FUNC
329  */
330 HWTEST_F(MainSessionTest, RectCheck03, TestSize.Level1)
331 {
332     ASSERT_NE(mainSession_, nullptr);
333     SessionInfo info;
334     info.abilityName_ = "testMainSessionRectCheck";
335     info.moduleName_ = "testMainSessionRectCheck";
336     info.bundleName_ = "testMainSessionRectCheck";
337     sptr<Session> session = sptr<Session>::MakeSptr(info);
338     EXPECT_NE(nullptr, session);
339     mainSession_->parentSession_ = session;
340     uint32_t curWidth = 100;
341     uint32_t curHeight = 200;
342     mainSession_->RectCheck(curWidth, curHeight);
343 
344     curWidth = 300;
345     curHeight = 200;
346     mainSession_->RectCheck(curWidth, curHeight);
347 
348     curWidth = 1930;
349     curHeight = 200;
350     mainSession_->RectCheck(curWidth, curHeight);
351 
352     curWidth = 330;
353     curHeight = 200;
354     mainSession_->RectCheck(curWidth, curHeight);
355 
356     curWidth = 330;
357     curHeight = 1930;
358     mainSession_->RectCheck(curWidth, curHeight);
359 }
360 
361 /**
362  * @tc.name: NotifyClientToUpdateInteractive
363  * @tc.desc: check func NotifyClientToUpdateInteractive
364  * @tc.type: FUNC
365  */
366 HWTEST_F(MainSessionTest, NotifyClientToUpdateInteractive, TestSize.Level1)
367 {
368     SessionInfo info;
369     info.abilityName_ = "NotifyClientToUpdateInteractive";
370     info.bundleName_ = "NotifyClientToUpdateInteractive";
371     sptr<MainSession> testSession = sptr<MainSession>::MakeSptr(info, nullptr);
372     ASSERT_NE(testSession, nullptr);
373     testSession->sessionStage_ = sptr<SessionStageMocker>::MakeSptr();
374     ASSERT_NE(testSession->sessionStage_, nullptr);
375 
376     bool interactive = !(testSession->isClientInteractive_);
377     testSession->SetSessionState(SessionState::STATE_ACTIVE);
378     testSession->NotifyClientToUpdateInteractive(interactive);
379     ASSERT_EQ(testSession->isClientInteractive_, interactive);
380     interactive = !interactive;
381     testSession->SetSessionState(SessionState::STATE_FOREGROUND);
382     testSession->NotifyClientToUpdateInteractive(interactive);
383     ASSERT_EQ(testSession->isClientInteractive_, interactive);
384     interactive = !interactive;
385     testSession->SetSessionState(SessionState::STATE_BACKGROUND);
386     testSession->NotifyClientToUpdateInteractive(interactive);
387     ASSERT_NE(testSession->isClientInteractive_, interactive);
388 
389     testSession->sessionStage_ = nullptr;
390     testSession->NotifyClientToUpdateInteractive(interactive);
391     ASSERT_NE(testSession->isClientInteractive_, interactive);
392 }
393 
394 /**
395  * @tc.name: SetExitSplitOnBackground
396  * @tc.desc: check func SetExitSplitOnBackground
397  * @tc.type: FUNC
398  */
399 HWTEST_F(MainSessionTest, SetExitSplitOnBackground, TestSize.Level1)
400 {
401     bool isExitSplitOnBackground = true;
402     mainSession_->SetExitSplitOnBackground(isExitSplitOnBackground);
403     ASSERT_EQ(true, isExitSplitOnBackground);
404 }
405 
406 /**
407  * @tc.name: IsExitSplitOnBackground01
408  * @tc.desc: check func IsExitSplitOnBackground
409  * @tc.type: FUNC
410  */
411 HWTEST_F(MainSessionTest, IsExitSplitOnBackground01, TestSize.Level1)
412 {
413     bool isExitSplitOnBackground = true;
414     mainSession_->SetExitSplitOnBackground(isExitSplitOnBackground);
415     bool ret = mainSession_->IsExitSplitOnBackground();
416     ASSERT_EQ(true, ret);
417 }
418 
419 /**
420  * @tc.name: IsExitSplitOnBackground02
421  * @tc.desc: check func IsExitSplitOnBackground
422  * @tc.type: FUNC
423  */
424 HWTEST_F(MainSessionTest, IsExitSplitOnBackground02, TestSize.Level1)
425 {
426     bool isExitSplitOnBackground = false;
427     mainSession_->SetExitSplitOnBackground(isExitSplitOnBackground);
428     bool ret = mainSession_->IsExitSplitOnBackground();
429     ASSERT_EQ(false, ret);
430 }
431 
432 /**
433  * @tc.name: OnTitleAndDockHoverShowChange
434  * @tc.desc: OnTitleAndDockHoverShowChange
435  * @tc.type: FUNC
436  */
437 HWTEST_F(MainSessionTest, OnTitleAndDockHoverShowChange, TestSize.Level1)
438 {
439     SessionInfo info;
440     info.abilityName_ = "OnTitleAndDockHoverShowChange";
441     info.bundleName_ = "OnTitleAndDockHoverShowChange";
442     sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
__anone8ff9da30402(bool isTitleHoverShown, bool isDockHoverShown) 443     sceneSession->SetTitleAndDockHoverShowChangeCallback([](bool isTitleHoverShown, bool isDockHoverShown) { return; });
444     EXPECT_NE(sceneSession->onTitleAndDockHoverShowChangeFunc_, nullptr);
445     EXPECT_EQ(sceneSession->OnTitleAndDockHoverShowChange(true, true), WSError::WS_OK);
446 }
447 
448 /**
449  * @tc.name: OnTitleAndDockHoverShowChange02
450  * @tc.desc: check func OnTitleAndDockHoverShowChange
451  * @tc.type: FUNC
452  */
453 HWTEST_F(MainSessionTest, OnTitleAndDockHoverShowChange02, TestSize.Level1)
454 {
455     SessionInfo info;
456     info.abilityName_ = "OnTitleAndDockHoverShowChange02";
457     info.bundleName_ = "OnTitleAndDockHoverShowChange02";
458     sptr<MainSession> testSession = sptr<MainSession>::MakeSptr(info, nullptr);
459     ASSERT_NE(testSession, nullptr);
460 
461     auto callbackFlag = 1;
__anone8ff9da30502(bool isTitleHoverShown, bool isDockHoverShown) 462     testSession->onTitleAndDockHoverShowChangeFunc_ = [&callbackFlag](bool isTitleHoverShown, bool isDockHoverShown) {
463         callbackFlag += 1;
464     };
465     testSession->OnTitleAndDockHoverShowChange(true, true);
466     EXPECT_EQ(callbackFlag, 2);
467 }
468 
469 /**
470  * @tc.name: OnRestoreMainWindow
471  * @tc.desc: OnRestoreMainWindow function01
472  * @tc.type: FUNC
473  */
474 HWTEST_F(MainSessionTest, OnRestoreMainWindow, TestSize.Level1)
475 {
476     SessionInfo info;
477     info.abilityName_ = "OnRestoreMainWindow";
478     info.bundleName_ = "OnRestoreMainWindow";
479     sptr<SceneSession> session = sptr<SceneSession>::MakeSptr(info, nullptr);
480     EXPECT_NE(session, nullptr);
481     EXPECT_EQ(WSError::WS_OK, session->OnRestoreMainWindow());
482 
483     session->onRestoreMainWindowFunc_ = nullptr;
484     EXPECT_EQ(WSError::WS_OK, session->OnRestoreMainWindow());
485 
__anone8ff9da30602null486     NotifyRestoreMainWindowFunc func = [] { return; };
487     session->onRestoreMainWindowFunc_ = func;
488     EXPECT_EQ(WSError::WS_OK, session->OnRestoreMainWindow());
489 }
490 
491 /**
492  * @tc.name: OnRestoreMainWindow02
493  * @tc.desc: check func OnRestoreMainWindow
494  * @tc.type: FUNC
495  */
496 HWTEST_F(MainSessionTest, OnRestoreMainWindow02, TestSize.Level1)
497 {
498     SessionInfo info;
499     info.abilityName_ = "OnRestoreMainWindow02";
500     info.bundleName_ = "OnRestoreMainWindow02";
501     sptr<MainSession> testSession = sptr<MainSession>::MakeSptr(info, nullptr);
502     ASSERT_NE(testSession, nullptr);
503 
504     testSession->onRestoreMainWindowFunc_ = nullptr;
505     EXPECT_EQ(testSession->OnRestoreMainWindow(), WSError::WS_OK);
506 
507     auto callbackFlag = 1;
__anone8ff9da30702() 508     testSession->onRestoreMainWindowFunc_ = [&callbackFlag]() { callbackFlag += 1; };
509     testSession->OnRestoreMainWindow();
510     EXPECT_EQ(callbackFlag, 2);
511 }
512 
513 /**
514  * @tc.name: OnSetWindowRectAutoSave
515  * @tc.desc: OnSetWindowRectAutoSave
516  * @tc.type: FUNC
517  */
518 HWTEST_F(MainSessionTest, OnSetWindowRectAutoSave, TestSize.Level1)
519 {
520     SessionInfo info;
521     info.abilityName_ = "OnSetWindowRectAutoSave";
522     info.bundleName_ = "OnSetWindowRectAutoSave";
523     sptr<SceneSession> session = sptr<SceneSession>::MakeSptr(info, nullptr);
524 
525     session->onSetWindowRectAutoSaveFunc_ = nullptr;
526     EXPECT_EQ(nullptr, session->onSetWindowRectAutoSaveFunc_);
527 
__anone8ff9da30802(bool enabled, bool isSaveBySpecifiedFlag) 528     NotifySetWindowRectAutoSaveFunc func = [](bool enabled, bool isSaveBySpecifiedFlag) { return; };
529     session->onSetWindowRectAutoSaveFunc_ = func;
530     EXPECT_NE(nullptr, session->onSetWindowRectAutoSaveFunc_);
531 }
532 
533 /**
534  * @tc.name: OnSetWindowRectAutoSave02
535  * @tc.desc: check func OnSetWindowRectAutoSave
536  * @tc.type: FUNC
537  */
538 HWTEST_F(MainSessionTest, OnSetWindowRectAutoSave02, TestSize.Level1)
539 {
540     SessionInfo info;
541     info.abilityName_ = "OnSetWindowRectAutoSave02";
542     info.bundleName_ = "OnSetWindowRectAutoSave02";
543     sptr<MainSession> testSession = sptr<MainSession>::MakeSptr(info, nullptr);
544     ASSERT_NE(testSession, nullptr);
545 
546     testSession->onSetWindowRectAutoSaveFunc_ = nullptr;
547     EXPECT_EQ(testSession->OnSetWindowRectAutoSave(true, true), WSError::WS_OK);
548 
549     auto callbackFlag = 1;
__anone8ff9da30902(bool enabled, bool isSaveBySpecifiedFlag) 550     testSession->onSetWindowRectAutoSaveFunc_ = [&callbackFlag](bool enabled, bool isSaveBySpecifiedFlag) {
551         callbackFlag += 1;
552     };
553     testSession->OnSetWindowRectAutoSave(true, true);
554     EXPECT_EQ(callbackFlag, 2);
555 }
556 
557 /**
558  * @tc.name: NotifyMainModalTypeChange
559  * @tc.desc: NotifyMainModalTypeChange function01
560  * @tc.type: FUNC
561  */
562 HWTEST_F(MainSessionTest, NotifyMainModalTypeChange, TestSize.Level1)
563 {
564     SessionInfo info;
565     info.abilityName_ = "NotifyMainModalTypeChange";
566     info.bundleName_ = "NotifyMainModalTypeChange";
567     sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
568     EXPECT_NE(sceneSession, nullptr);
__anone8ff9da30a02(bool isModal) 569     sceneSession->RegisterMainModalTypeChangeCallback([](bool isModal) { return; });
570     EXPECT_NE(sceneSession->onMainModalTypeChange_, nullptr);
571     EXPECT_EQ(WSError::WS_OK, sceneSession->NotifyMainModalTypeChange(true));
572 }
573 
574 /**
575  * @tc.name: NotifyMainModalTypeChange02
576  * @tc.desc: check func NotifyMainModalTypeChange
577  * @tc.type: FUNC
578  */
579 HWTEST_F(MainSessionTest, NotifyMainModalTypeChange02, TestSize.Level1)
580 {
581     SessionInfo info;
582     info.abilityName_ = "NotifyMainModalTypeChange02";
583     info.bundleName_ = "NotifyMainModalTypeChange02";
584     sptr<MainSession> testSession = sptr<MainSession>::MakeSptr(info, nullptr);
585     ASSERT_NE(testSession, nullptr);
586 
587     testSession->onMainModalTypeChange_ = nullptr;
588     EXPECT_EQ(testSession->NotifyMainModalTypeChange(true), WSError::WS_OK);
589 
590     auto callbackFlag = 1;
__anone8ff9da30b02(bool isModal) 591     testSession->onMainModalTypeChange_ = [&callbackFlag](bool isModal) { callbackFlag += 1; };
592     testSession->NotifyMainModalTypeChange(true);
593     EXPECT_EQ(callbackFlag, 2);
594 }
595 
596 /**
597  * @tc.name: IsModal
598  * @tc.desc: IsModal function01
599  * @tc.type: FUNC
600  */
601 HWTEST_F(MainSessionTest, IsModal, TestSize.Level1)
602 {
603     SessionInfo info;
604     info.abilityName_ = "IsModal";
605     info.bundleName_ = "IsModal";
606     sptr<MainSession> sceneSession = sptr<MainSession>::MakeSptr(info, nullptr);
607     EXPECT_NE(sceneSession, nullptr);
608     EXPECT_EQ(sceneSession->IsModal(), false);
609     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
610     property->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
611     property->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
612     sceneSession->SetSessionProperty(property);
613     EXPECT_EQ(sceneSession->IsModal(), true);
614 }
615 
616 /**
617  * @tc.name: IsApplicationModal
618  * @tc.desc: IsApplicationModal function01
619  * @tc.type: FUNC
620  */
621 HWTEST_F(MainSessionTest, IsApplicationModal, TestSize.Level1)
622 {
623     SessionInfo info;
624     info.abilityName_ = "IsApplicationModal";
625     info.bundleName_ = "IsApplicationModal";
626     sptr<MainSession> sceneSession = sptr<MainSession>::MakeSptr(info, nullptr);
627     EXPECT_NE(sceneSession, nullptr);
628     EXPECT_EQ(sceneSession->IsApplicationModal(), false);
629     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
630     property->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
631     property->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
632     sceneSession->SetSessionProperty(property);
633     EXPECT_EQ(sceneSession->IsApplicationModal(), true);
634 }
635 
636 /**
637  * @tc.name: SetSessionLabelAndIcon
638  * @tc.desc: SetSessionLabelAndIcon
639  * @tc.type: FUNC
640  */
641 HWTEST_F(MainSessionTest, SetSessionLabelAndIcon, TestSize.Level1)
642 {
643     std::string label = "";
644     std::shared_ptr<Media::PixelMap> icon;
645     ASSERT_EQ(WSError::WS_ERROR_INVALID_PERMISSION, mainSession_->SetSessionLabelAndIcon(label, icon));
646 }
647 
648 /**
649  * @tc.name: SetSessionLabelAndIconInner
650  * @tc.desc: SetSessionLabelAndIconInner
651  * @tc.type: FUNC
652  */
653 HWTEST_F(MainSessionTest, SetSessionLabelAndIconInner, TestSize.Level1)
654 {
655     std::string label = "test";
656     std::shared_ptr<Media::PixelMap> icon;
657 
658     ASSERT_EQ(WSError::WS_OK, mainSession_->SetSessionLabelAndIconInner(label, icon));
659 
660     mainSession_->updateSessionLabelAndIconFunc_=nullptr;
661     ASSERT_EQ(WSError::WS_OK, mainSession_->SetSessionLabelAndIconInner(label, icon));
662 
663     mainSession_->SetUpdateSessionLabelAndIconListener(
__anone8ff9da30c02(const std::string& label, const std::shared_ptr<Media::PixelMap>& icon) 664         [](const std::string& label, const std::shared_ptr<Media::PixelMap>& icon) {
665     });
666     ASSERT_EQ(WSError::WS_OK, mainSession_->SetSessionLabelAndIconInner(label, icon));
667 }
668 
669 /**
670  * @tc.name: SetUpdateSessionLabelAndIconListener
671  * @tc.desc: SetUpdateSessionLabelAndIconListener
672  * @tc.type: FUNC
673  */
674 HWTEST_F(MainSessionTest, SetUpdateSessionLabelAndIconListener, TestSize.Level1)
675 {
676     std::string label = "test";
677     std::shared_ptr<Media::PixelMap> icon;
678     mainSession_->SetUpdateSessionLabelAndIconListener(
__anone8ff9da30d02(const std::string& label, const std::shared_ptr<Media::PixelMap>& icon) 679         [](const std::string& label, const std::shared_ptr<Media::PixelMap>& icon) {
680     });
681     ASSERT_NE(nullptr, mainSession_->updateSessionLabelAndIconFunc_);
682 }
683 
684 /**
685  * @tc.name: NotifySupportWindowModesChange
686  * @tc.desc: NotifySupportWindowModesChange
687  * @tc.type: FUNC
688  */
689 HWTEST_F(MainSessionTest, NotifySupportWindowModesChange, TestSize.Level1)
690 {
691     SessionInfo info;
692     info.abilityName_ = "NotifySupportWindowModesChange";
693     info.bundleName_ = "NotifySupportWindowModesChange";
694     sptr<SceneSession> session = sptr<SceneSession>::MakeSptr(info, nullptr);
695 
696     std::vector<AppExecFwk::SupportWindowMode> supportedWindowModes = { AppExecFwk::SupportWindowMode::FULLSCREEN,
697                                                                         AppExecFwk::SupportWindowMode::SPLIT,
698                                                                         AppExecFwk::SupportWindowMode::FLOATING };
699 
700     EXPECT_EQ(WSError::WS_OK, session->NotifySupportWindowModesChange(supportedWindowModes));
701 
702     session->onSetSupportedWindowModesFunc_ = nullptr;
703     EXPECT_EQ(WSError::WS_OK, session->NotifySupportWindowModesChange(supportedWindowModes));
704 
__anone8ff9da30e02(std::vector<AppExecFwk::SupportWindowMode>&& supportedWindowModes) 705     session->onSetSupportedWindowModesFunc_ = [](std::vector<AppExecFwk::SupportWindowMode>&& supportedWindowModes) {
706         return;
707     };
708 
709     EXPECT_EQ(WSError::WS_OK, session->NotifySupportWindowModesChange(supportedWindowModes));
710 }
711 
712 /**
713  * @tc.name: NotifySupportWindowModesChange02
714  * @tc.desc: check func NotifySupportWindowModesChange
715  * @tc.type: FUNC
716  */
717 HWTEST_F(MainSessionTest, NotifySupportWindowModesChange02, TestSize.Level1)
718 {
719     SessionInfo info;
720     info.abilityName_ = "NotifySupportWindowModesChange02";
721     info.bundleName_ = "NotifySupportWindowModesChange02";
722     sptr<MainSession> testSession = sptr<MainSession>::MakeSptr(info, nullptr);
723     ASSERT_NE(testSession, nullptr);
724     std::vector<AppExecFwk::SupportWindowMode> supportedWindowModes = { AppExecFwk::SupportWindowMode::FULLSCREEN,
725                                                                         AppExecFwk::SupportWindowMode::SPLIT,
726                                                                         AppExecFwk::SupportWindowMode::FLOATING };
727 
728     testSession->onSetSupportedWindowModesFunc_ = nullptr;
729     EXPECT_EQ(testSession->NotifySupportWindowModesChange(supportedWindowModes), WSError::WS_OK);
730 
731     auto callbackFlag = 1;
732     testSession->onSetSupportedWindowModesFunc_ =
__anone8ff9da30f02(std::vector<AppExecFwk::SupportWindowMode>&& supportedWindowModes) 733         [&callbackFlag](std::vector<AppExecFwk::SupportWindowMode>&& supportedWindowModes) { callbackFlag += 1; };
734     testSession->NotifySupportWindowModesChange(supportedWindowModes);
735     EXPECT_EQ(callbackFlag, 2);
736 }
737 
738 /**
739  * @tc.name: NotifySessionLockStateChange
740  * @tc.desc: NotifySessionLockStateChange
741  * @tc.type: FUNC
742  */
743 HWTEST_F(MainSessionTest, NotifySessionLockStateChange, TestSize.Level1)
744 {
745     SessionInfo info;
746     info.bundleName_ = "NotifySessionLockStateChangeBundle";
747     info.moduleName_ = "NotifySessionLockStateChangeModule";
748     info.abilityName_ = "NotifySessionLockStateChangeAbility";
749     info.appIndex_ = 0;
750 
751     sptr<MainSession> session = sptr<MainSession>::MakeSptr(info, nullptr);
752     bool isLockedState = session->GetSessionLockState();
753     session->NotifySessionLockStateChange(isLockedState);
754     EXPECT_EQ(session->GetSessionLockState(), isLockedState);
755 
756     isLockedState = !session->GetSessionLockState();
757     session->NotifySessionLockStateChange(isLockedState);
758     EXPECT_EQ(session->GetSessionLockState(), isLockedState);
759 
760     isLockedState = true;
761     session->SetSessionLockState(isLockedState);
__anone8ff9da31002(bool isLockedState) 762     session->RegisterSessionLockStateChangeCallback([](bool isLockedState) {});
763     session->NotifySessionLockStateChange(isLockedState);
764     EXPECT_EQ(session->GetSessionLockState(), isLockedState);
765 }
766 
767 /**
768  * @tc.name: SessionLockState
769  * @tc.desc: SetSessionLockState setter and getter test
770  * @tc.type: FUNC
771  */
772 HWTEST_F(MainSessionTest, SetSessionLockState, TestSize.Level1)
773 {
774     SessionInfo info;
775     info.bundleName_ = "SetSessionLockStateBundle";
776     info.moduleName_ = "SetSessionLockStateModule";
777     info.abilityName_ = "SetSessionLockStateAbility";
778     info.appIndex_ = 0;
779 
780     sptr<MainSession> session = sptr<MainSession>::MakeSptr(info, nullptr);
781     EXPECT_EQ(session->GetSessionLockState(), false);
782 
783     bool isLockedState = true;
784     session->SetSessionLockState(isLockedState);
785     EXPECT_EQ(session->GetSessionLockState(), isLockedState);
786 
787     isLockedState = false;
788     session->SetSessionLockState(isLockedState);
789     EXPECT_EQ(session->GetSessionLockState(), isLockedState);
790 }
791 
792 /**
793  * @tc.name: RegisterSessionLockStateChangeCallback
794  * @tc.desc: RegisterSessionLockStateChangeCallback
795  * @tc.type: FUNC
796  */
797 HWTEST_F(MainSessionTest, RegisterSessionLockStateChangeCallback, TestSize.Level1)
798 {
799     SessionInfo info;
800     info.bundleName_ = "RegisterSessionLockStateChangeCallbackBundle";
801     info.moduleName_ = "RegisterSessionLockStateChangeCallbackModule";
802     info.abilityName_ = "RegisterSessionLockStateChangeCallbackAbility";
803     info.appIndex_ = 0;
804 
805     sptr<MainSession> session = sptr<MainSession>::MakeSptr(info, nullptr);
806 
807     bool isLockedState = true;
808     session->SetSessionLockState(isLockedState);
__anone8ff9da31102(bool isLockedState) 809     session->RegisterSessionLockStateChangeCallback([](bool isLockedState) {});
810     EXPECT_NE(session->onSessionLockStateChangeCallback_, nullptr);
811     EXPECT_EQ(session->GetSessionLockState(), isLockedState);
812 }
813 
814 /**
815  * @tc.name: SetRecentSessionState
816  * @tc.desc: check func SetRecentSessionState
817  * @tc.type: FUNC
818  */
819 HWTEST_F(MainSessionTest, SetRecentSessionState, TestSize.Level1)
820 {
821     RecentSessionInfo info;
822     SessionState state = SessionState::STATE_DISCONNECT;
823     mainSession_->SetRecentSessionState(info, state);
824     ASSERT_EQ(info.sessionState, RecentSessionState::DISCONNECT);
825 
826     state = SessionState::STATE_CONNECT;
827     mainSession_->SetRecentSessionState(info, state);
828     ASSERT_EQ(info.sessionState, RecentSessionState::CONNECT);
829 
830     state = SessionState::STATE_FOREGROUND;
831     mainSession_->SetRecentSessionState(info, state);
832     ASSERT_EQ(info.sessionState, RecentSessionState::FOREGROUND);
833 
834     state = SessionState::STATE_BACKGROUND;
835     mainSession_->SetRecentSessionState(info, state);
836     ASSERT_EQ(info.sessionState, RecentSessionState::BACKGROUND);
837 
838     state = SessionState::STATE_ACTIVE;
839     mainSession_->SetRecentSessionState(info, state);
840     ASSERT_EQ(info.sessionState, RecentSessionState::ACTIVE);
841 
842     state = SessionState::STATE_INACTIVE;
843     mainSession_->SetRecentSessionState(info, state);
844     ASSERT_EQ(info.sessionState, RecentSessionState::INACTIVE);
845 
846     state = SessionState::STATE_END;
847     mainSession_->SetRecentSessionState(info, state);
848     ASSERT_EQ(info.sessionState, RecentSessionState::END);
849 }
850 
851 /**
852  * @tc.name: UpdateFlag
853  * @tc.desc: UpdateFlag
854  * @tc.type: FUNC
855  */
856 HWTEST_F(MainSessionTest, UpdateFlag, TestSize.Level1)
857 {
858     SessionInfo info;
859     info.abilityName_ = "UpdateFlag";
860     info.bundleName_ = "UpdateFlag";
861     sptr<SceneSession> session = sptr<SceneSession>::MakeSptr(info, nullptr);
862     EXPECT_NE(nullptr, session);
863 
864     session->onUpdateFlagFunc_ = nullptr;
865     EXPECT_EQ(nullptr, session->onUpdateFlagFunc_);
866 
867     std::string flag = "test";
868     EXPECT_EQ(WSError::WS_OK, session->UpdateFlag(flag));
869 
__anone8ff9da31202(const std::string& flag) 870     NotifyUpdateFlagFunc func = [](const std::string& flag) { return; };
871     session->onUpdateFlagFunc_ = func;
872     EXPECT_NE(nullptr, session->onUpdateFlagFunc_);
873     EXPECT_EQ(WSError::WS_OK, session->UpdateFlag(flag));
874 }
875 
876 /**
877  * @tc.name: NotifySubAndDialogFollowRectChange
878  * @tc.desc: NotifySubAndDialogFollowRectChange
879  * @tc.type: FUNC
880  */
881 HWTEST_F(MainSessionTest, NotifySubAndDialogFollowRectChange01, TestSize.Level1)
882 {
883     SessionInfo info;
884     sptr<SceneSession> subSession = sptr<SceneSession>::MakeSptr(info, nullptr);
885     sptr<MainSession> mainSession = sptr<MainSession>::MakeSptr(info, nullptr);
886 
887     bool isCall = false;
__anone8ff9da31302(const WSRect& rect, bool isGlobal, bool needFlush) 888     auto task = [&isCall](const WSRect& rect, bool isGlobal, bool needFlush) { isCall = true; };
889     mainSession->RegisterNotifySurfaceBoundsChangeFunc(subSession->GetPersistentId(), std::move(task));
890     ASSERT_NE(nullptr, mainSession->notifySurfaceBoundsChangeFuncMap_[subSession->GetPersistentId()]);
891 
892     WSRect rect;
893     subSession->isFollowParentLayout_ = false;
894     mainSession->NotifySubAndDialogFollowRectChange(rect, false, false);
895     ASSERT_EQ(false, isCall);
896 
897     subSession->isFollowParentLayout_ = true;
898     sptr<SceneSession::SpecificSessionCallback> callBack = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
899     mainSession->specificCallback_ = callBack;
__anone8ff9da31402(int32_t persistentId) 900     auto getSessionCallBack = [&subSession](int32_t persistentId) { return subSession; };
901     callBack->onGetSceneSessionByIdCallback_ = getSessionCallBack;
902     mainSession->NotifySubAndDialogFollowRectChange(rect, false, false);
903     ASSERT_EQ(true, isCall);
904 }
905 
906 /**
907  * @tc.name: GetRouterStackInfo
908  * @tc.desc: GetRouterStackInfo
909  * @tc.type: FUNC
910  */
911 HWTEST_F(MainSessionTest, GetRouterStackInfo, TestSize.Level1)
912 {
913     SessionInfo info;
914     info.abilityName_ = "GetRouterStackInfo";
915     info.bundleName_ = "GetRouterStackInfo";
916     sptr<MainSession> testSession = sptr<MainSession>::MakeSptr(info, nullptr);
917     testSession->sessionStage_ = sptr<SessionStageMocker>::MakeSptr();
918 
919     std::string routerInfo;
920     auto res = testSession->GetRouterStackInfo(routerInfo);
921     EXPECT_EQ(res, WMError::WM_OK);
922 
923     testSession->sessionStage_ = nullptr;
924     res = testSession->GetRouterStackInfo(routerInfo);
925     EXPECT_EQ(res, WMError::WM_ERROR_NULLPTR);
926 }
927 
928 /**
929  * @tc.name: NotifySubAndDialogFollowRectChange_scaleMode
930  * @tc.desc: NotifySubAndDialogFollowRectChange
931  * @tc.type: FUNC
932  */
933 HWTEST_F(MainSessionTest, NotifySubAndDialogFollowRectChange_scaleMode, TestSize.Level1)
934 {
935     SessionInfo info;
936     info.abilityName_ = "NotifySubAndDialogFollowRectChange_scaleMode";
937     info.bundleName_ = "NotifySubAndDialogFollowRectChange_scaleMode";
938     sptr<MainSession> mainSession = sptr<MainSession>::MakeSptr(info, nullptr);
939     sptr<SceneSession> subSession1 = sptr<SceneSession>::MakeSptr(info, nullptr);
940     sptr<SceneSession> subSession2 = sptr<SceneSession>::MakeSptr(info, nullptr);
941     sptr<SceneSession> subSession3 = nullptr;
942 
943     WSRect updateRect1;
__anone8ff9da31502(const WSRect& rect, bool isGlobal, bool needFlush) 944     auto task1 = [&updateRect1](const WSRect& rect, bool isGlobal, bool needFlush) { updateRect1 = rect; };
945     mainSession->RegisterNotifySurfaceBoundsChangeFunc(subSession1->GetPersistentId(), std::move(task1));
946     WSRect updateRect2;
__anone8ff9da31602(const WSRect& rect, bool isGlobal, bool needFlush) 947     auto task2 = [&updateRect2](const WSRect& rect, bool isGlobal, bool needFlush) { updateRect2 = rect; };
948     mainSession->RegisterNotifySurfaceBoundsChangeFunc(subSession2->GetPersistentId(), std::move(task2));
949     EXPECT_NE(nullptr, mainSession->notifySurfaceBoundsChangeFuncMap_[subSession1->GetPersistentId()]);
950     WSRect rect = { 100, 100, 400, 400 };
951     WSRect resultRect = { 200, 200, 200, 200 };
952     float scaleX = 0.5f;
953     float scaleY = 0.5f;
954     mainSession->SetScale(scaleX, scaleY, 0.5f, 0.5f);
955     mainSession->SetSessionRect(rect);
956     EXPECT_EQ(rect, mainSession->GetSessionRect());
957     sptr<CompatibleModeProperty> compatibleModeProperty = sptr<CompatibleModeProperty>::MakeSptr();
958     compatibleModeProperty->SetIsAdaptToProportionalScale(true);
959     mainSession->property_->SetCompatibleModeProperty(compatibleModeProperty);
960     subSession1->isFollowParentLayout_ = true;
961     subSession2->isFollowParentLayout_ = true;
962     sptr<SceneSession::SpecificSessionCallback> callBack = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
963     mainSession->specificCallback_ = callBack;
__anone8ff9da31702(int32_t persistentId) 964     auto getSessionCallBack = [&subSession1, &subSession2, &subSession3](int32_t persistentId) {
965         if (subSession1->GetPersistentId() == persistentId) {
966             return subSession1;
967         } else if (subSession2->GetPersistentId() == persistentId) {
968             return subSession2;
969         }
970         return subSession3;
971     };
972     callBack->onGetSceneSessionByIdCallback_ = getSessionCallBack;
973     mainSession->NotifySubAndDialogFollowRectChange(rect, false, false);
974     EXPECT_EQ(resultRect, updateRect1);
975     EXPECT_EQ(resultRect, updateRect2);
976 }
977 } // namespace
978 } // namespace Rosen
979 } // namespace OHOS