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 <regex>
18 #include <pointer_event.h>
19 #include <ui/rs_surface_node.h>
20 
21 #include "mock/mock_session_stage.h"
22 #include "mock/mock_window_event_channel.h"
23 #include "mock/mock_pattern_detach_callback.h"
24 #include "session/host/include/extension_session.h"
25 #include "session/host/include/move_drag_controller.h"
26 #include "session/host/include/scene_session.h"
27 #include "session_manager/include/scene_session_manager.h"
28 #include "session/host/include/session.h"
29 #include "session_info.h"
30 #include "key_event.h"
31 #include "wm_common.h"
32 #include "window_event_channel_base.h"
33 #include "window_manager_hilog.h"
34 
35 using namespace testing;
36 using namespace testing::ext;
37 
38 namespace OHOS {
39 namespace Rosen {
40 namespace {
41 const std::string UNDEFINED = "undefined";
42 }
43 
44 class WindowSessionLifecycleTest : public testing::Test {
45 public:
46     static void SetUpTestCase();
47     static void TearDownTestCase();
48     void SetUp() override;
49     void TearDown() override;
50     int32_t GetTaskCount();
51     sptr <SceneSessionManager> ssm_;
52 
53 private:
54     RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
55 
56     sptr <Session> session_ = nullptr;
57     static constexpr uint32_t
58     WAIT_SYNC_IN_NS = 500000;
59 
60     class TLifecycleListener : public ILifecycleListener {
61     public:
~TLifecycleListener()62         virtual ~TLifecycleListener() {}
63         void OnActivation() override {}
64         void OnConnect() override {}
65         void OnForeground() override {}
66         void OnBackground() override {}
67         void OnDisconnect() override {}
68         void OnExtensionDied() override {}
69         void OnExtensionTimeout(int32_t errorCode) override {}
70         void OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info,
71             int64_t uiExtensionIdLevel) override {}
72         void OnDrawingCompleted() override {}
73     };
74     std::shared_ptr<TLifecycleListener> lifecycleListener_ = std::make_shared<TLifecycleListener>();
75 };
76 
SetUpTestCase()77 void WindowSessionLifecycleTest::SetUpTestCase()
78 {
79 }
80 
TearDownTestCase()81 void WindowSessionLifecycleTest::TearDownTestCase()
82 {
83 }
84 
SetUp()85 void WindowSessionLifecycleTest::SetUp()
86 {
87     SessionInfo info;
88     info.abilityName_ = "testSession1";
89     info.moduleName_ = "testSession2";
90     info.bundleName_ = "testSession3";
91     session_ = new (std::nothrow) Session(info);
92     session_->surfaceNode_ = CreateRSSurfaceNode();
93     EXPECT_NE(nullptr, session_);
94     ssm_ = new SceneSessionManager();
95     session_->SetEventHandler(ssm_->taskScheduler_->GetEventHandler(), ssm_->eventHandler_);
96     auto isScreenLockedCallback = [this]() {
97         return ssm_->IsScreenLocked();
98     };
99     session_->RegisterIsScreenLockedCallback(isScreenLockedCallback);
100 }
101 
TearDown()102 void WindowSessionLifecycleTest::TearDown()
103 {
104     session_ = nullptr;
105     usleep(WAIT_SYNC_IN_NS);
106 }
107 
CreateRSSurfaceNode()108 RSSurfaceNode::SharedPtr WindowSessionLifecycleTest::CreateRSSurfaceNode()
109 {
110     struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
111     rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
112     auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
113     if (surfaceNode == nullptr) {
114         GTEST_LOG_(INFO) << "WindowSessionTest::CreateRSSurfaceNode surfaceNode is nullptr";
115     }
116     return surfaceNode;
117 }
118 
GetTaskCount()119 int32_t WindowSessionLifecycleTest::GetTaskCount()
120 {
121     std::string dumpInfo = session_->handler_->GetEventRunner()->GetEventQueue()->DumpCurrentQueueSize();
122     std::regex pattern("\\d+");
123     std::smatch matches;
124     int32_t taskNum = 0;
125     while (std::regex_search(dumpInfo, matches, pattern)) {
126         taskNum += std::stoi(matches.str());
127         dumpInfo = matches.suffix();
128     }
129     return taskNum;
130 }
131 
132 namespace {
133 
134 /**
135  * @tc.name: Connect01
136  * @tc.desc: check func Connect
137  * @tc.type: FUNC
138  */
HWTEST_F(WindowSessionLifecycleTest, Connect01, Function | SmallTest | Level2)139 HWTEST_F(WindowSessionLifecycleTest, Connect01, Function | SmallTest | Level2)
140 {
141     auto surfaceNode = CreateRSSurfaceNode();
142     session_->state_ = SessionState::STATE_CONNECT;
143     SystemSessionConfig systemConfig;
144     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
145     ASSERT_NE(nullptr, property);
146     auto result = session_->Connect(nullptr, nullptr, nullptr, systemConfig, property);
147     ASSERT_EQ(result, WSError::WS_OK);
148 
149     session_->state_ = SessionState::STATE_DISCONNECT;
150     result = session_->Connect(nullptr, nullptr, nullptr, systemConfig, property);
151     ASSERT_EQ(result, WSError::WS_OK);
152 
153     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
154     EXPECT_NE(nullptr, mockSessionStage);
155     result = session_->Connect(mockSessionStage, nullptr, surfaceNode, systemConfig, property);
156     ASSERT_EQ(result, WSError::WS_OK);
157 
158     sptr<TestWindowEventChannel> testWindowEventChannel = new (std::nothrow) TestWindowEventChannel();
159     EXPECT_NE(nullptr, testWindowEventChannel);
160     result = session_->Connect(mockSessionStage, testWindowEventChannel, surfaceNode, systemConfig, property);
161     ASSERT_EQ(result, WSError::WS_OK);
162 }
163 
164 /**
165  * @tc.name: Reconnect01
166  * @tc.desc: check func Reconnect01
167  * @tc.type: FUNC
168  */
HWTEST_F(WindowSessionLifecycleTest, Reconnect01, Function | SmallTest | Level2)169 HWTEST_F(WindowSessionLifecycleTest, Reconnect01, Function | SmallTest | Level2)
170 {
171     auto surfaceNode = CreateRSSurfaceNode();
172 
173     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
174     ASSERT_NE(nullptr, property);
175     auto result = session_->Reconnect(nullptr, nullptr, nullptr, property);
176     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
177 
178     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
179     EXPECT_NE(nullptr, mockSessionStage);
180     result = session_->Reconnect(mockSessionStage, nullptr, surfaceNode, property);
181     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
182 
183     sptr<TestWindowEventChannel> testWindowEventChannel = new (std::nothrow) TestWindowEventChannel();
184     EXPECT_NE(nullptr, testWindowEventChannel);
185     result = session_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, nullptr);
186     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
187 
188     result = session_->Reconnect(nullptr, testWindowEventChannel, surfaceNode, property);
189     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
190 
191     result = session_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
192     ASSERT_EQ(result, WSError::WS_OK);
193 }
194 
195 /**
196  * @tc.name: Foreground01
197  * @tc.desc: check func Foreground
198  * @tc.type: FUNC
199  */
HWTEST_F(WindowSessionLifecycleTest, Foreground01, Function | SmallTest | Level2)200 HWTEST_F(WindowSessionLifecycleTest, Foreground01, Function | SmallTest | Level2)
201 {
202     session_->state_ = SessionState::STATE_DISCONNECT;
203     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
204     ASSERT_NE(nullptr, property);
205     auto result = session_->Foreground(property);
206     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_SESSION);
207 
208     session_->state_ = SessionState::STATE_CONNECT;
209     session_->isActive_ = true;
210     result = session_->Foreground(property);
211     ASSERT_EQ(result, WSError::WS_OK);
212 
213     session_->isActive_ = false;
214     ASSERT_EQ(result, WSError::WS_OK);
215 }
216 
217 /**
218  * @tc.name: Foreground02
219  * @tc.desc: Foreground Test
220  * @tc.type: FUNC
221  */
HWTEST_F(WindowSessionLifecycleTest, Foreground02, Function | SmallTest | Level2)222 HWTEST_F(WindowSessionLifecycleTest, Foreground02, Function | SmallTest | Level2)
223 {
224     ASSERT_NE(session_, nullptr);
225     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
226     ASSERT_NE(nullptr, property);
227     session_->SetSessionState(SessionState::STATE_BACKGROUND);
228     session_->isActive_ = true;
229     auto result = session_->Foreground(property);
230     ASSERT_EQ(result, WSError::WS_OK);
231 
232     session_->SetSessionState(SessionState::STATE_INACTIVE);
233     session_->isActive_ = false;
234     sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
235     ASSERT_NE(mockSessionStage, nullptr);
236     session_->sessionStage_ = mockSessionStage;
237     result = session_->Foreground(property);
238     ASSERT_EQ(result, WSError::WS_OK);
239 }
240 
241 /**
242  * @tc.name: Foreground03
243  * @tc.desc: Foreground Test
244  * @tc.type: FUNC
245  */
HWTEST_F(WindowSessionLifecycleTest, Foreground03, Function | SmallTest | Level2)246 HWTEST_F(WindowSessionLifecycleTest, Foreground03, Function | SmallTest | Level2)
247 {
248     ASSERT_NE(session_, nullptr);
249     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
250     ASSERT_NE(nullptr, property);
251     session_->SetSessionState(SessionState::STATE_BACKGROUND);
252     session_->isActive_ = true;
253 
254     property->type_ = WindowType::WINDOW_TYPE_DIALOG;
255     auto result = session_->Foreground(property);
256     ASSERT_EQ(result, WSError::WS_OK);
257 
258     SessionInfo parentInfo;
259     parentInfo.abilityName_ = "testSession1";
260     parentInfo.moduleName_ = "testSession2";
261     parentInfo.bundleName_ = "testSession3";
262     sptr<Session> parentSession = sptr<Session>::MakeSptr(parentInfo);
263     ASSERT_NE(parentSession, nullptr);
264     session_->SetParentSession(parentSession);
265     session_->SetSessionState(SessionState::STATE_INACTIVE);
266     result = session_->Foreground(property);
267     ASSERT_EQ(result, WSError::WS_OK);
268 }
269 
270 
271 /**
272  * @tc.name: Background01
273  * @tc.desc: check func Background
274  * @tc.type: FUNC
275  */
HWTEST_F(WindowSessionLifecycleTest, Background01, Function | SmallTest | Level2)276 HWTEST_F(WindowSessionLifecycleTest, Background01, Function | SmallTest | Level2)
277 {
278     session_->state_ = SessionState::STATE_CONNECT;
279     auto result = session_->Background();
280     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_SESSION);
281 
282     session_->state_ = SessionState::STATE_INACTIVE;
283     result = session_->Background();
284     ASSERT_EQ(result, WSError::WS_OK);
285     ASSERT_EQ(session_->state_, SessionState::STATE_BACKGROUND);
286 }
287 
288 /**
289  * @tc.name: Background2
290  * @tc.desc: Background2 Test
291  * @tc.type: FUNC
292  */
HWTEST_F(WindowSessionLifecycleTest, Background2, Function | SmallTest | Level2)293 HWTEST_F(WindowSessionLifecycleTest, Background2, Function | SmallTest | Level2)
294 {
295     ASSERT_NE(session_, nullptr);
296     session_->SetSessionState(SessionState::STATE_ACTIVE);
297     auto result = session_->Background();
298     EXPECT_EQ(result, WSError::WS_OK);
299 }
300 
301 /**
302  * @tc.name: Background03
303  * @tc.desc: Background03 Test
304  * @tc.type: FUNC
305  */
HWTEST_F(WindowSessionLifecycleTest, Background03, Function | SmallTest | Level2)306 HWTEST_F(WindowSessionLifecycleTest, Background03, Function | SmallTest | Level2)
307 {
308     ASSERT_NE(session_, nullptr);
309     session_->SetSessionState(SessionState::STATE_ACTIVE);
310     session_->property_ = sptr<WindowSessionProperty>::MakeSptr();
311     EXPECT_NE(session_->property_, nullptr);
312     session_->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_END);
313     auto result = session_->Background();
314     EXPECT_EQ(result, WSError::WS_ERROR_INVALID_SESSION);
315 }
316 
317 /**
318  * @tc.name: Disconnect01
319  * @tc.desc: check func Disconnect
320  * @tc.type: FUNC
321  */
HWTEST_F(WindowSessionLifecycleTest, Disconnect01, Function | SmallTest | Level2)322 HWTEST_F(WindowSessionLifecycleTest, Disconnect01, Function | SmallTest | Level2)
323 {
324     session_->state_ = SessionState::STATE_CONNECT;
325     auto result = session_->Disconnect();
326     ASSERT_EQ(result, WSError::WS_OK);
327     ASSERT_EQ(session_->state_, SessionState::STATE_DISCONNECT);
328 
329     session_->state_ = SessionState::STATE_BACKGROUND;
330     result = session_->Disconnect();
331     ASSERT_EQ(result, WSError::WS_OK);
332     ASSERT_EQ(session_->state_, SessionState::STATE_DISCONNECT);
333 }
334 
335 /**
336  * @tc.name: TerminateSessionNew01
337  * @tc.desc: check func TerminateSessionNew
338  * @tc.type: FUNC
339  */
HWTEST_F(WindowSessionLifecycleTest, TerminateSessionNew01, Function | SmallTest | Level2)340 HWTEST_F(WindowSessionLifecycleTest, TerminateSessionNew01, Function | SmallTest | Level2)
341 {
342     NotifyTerminateSessionFuncNew callback =
343             [](const SessionInfo& info, bool needStartCaller, bool isFromBroker)
344             {
345             };
346 
347     bool needStartCaller = false;
348     bool isFromBroker = false;
349     sptr<AAFwk::SessionInfo> info = new (std::nothrow)AAFwk::SessionInfo();
350     session_->terminateSessionFuncNew_ = nullptr;
351     session_->TerminateSessionNew(info, needStartCaller, isFromBroker);
352 
353     ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION,
354             session_->TerminateSessionNew(nullptr, needStartCaller, isFromBroker));
355 }
356 
357 /**
358  * @tc.name: TerminateSessionNew02
359  * @tc.desc: terminateSessionFuncNew_ is not nullptr
360  * @tc.type: FUNC
361  */
HWTEST_F(WindowSessionLifecycleTest, TerminateSessionNew02, Function | SmallTest | Level2)362 HWTEST_F(WindowSessionLifecycleTest, TerminateSessionNew02, Function | SmallTest | Level2)
363 {
364     NotifyTerminateSessionFuncNew callback =
365         [](const SessionInfo& info, bool needStartCaller, bool isFromBroker)
366     {
367     };
368 
369     bool needStartCaller = true;
370     bool isFromBroker = true;
371     sptr<AAFwk::SessionInfo> info = new (std::nothrow)AAFwk::SessionInfo();
372     session_->SetTerminateSessionListenerNew(callback);
373     auto result = session_->TerminateSessionNew(info, needStartCaller, isFromBroker);
374     EXPECT_EQ(result, WSError::WS_OK);
375 }
376 
377 /**
378  * @tc.name: NotifyDestroy
379  * @tc.desc: check func NotifyDestroy
380  * @tc.type: FUNC
381  */
HWTEST_F(WindowSessionLifecycleTest, NotifyDestroy, Function | SmallTest | Level2)382 HWTEST_F(WindowSessionLifecycleTest, NotifyDestroy, Function | SmallTest | Level2)
383 {
384     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
385     ASSERT_NE(mockSessionStage, nullptr);
386     session_->sessionStage_ = mockSessionStage;
387     EXPECT_CALL(*(mockSessionStage), NotifyDestroy()).Times(1).WillOnce(Return(WSError::WS_OK));
388     ASSERT_EQ(WSError::WS_OK, session_->NotifyDestroy());
389     session_->sessionStage_ = nullptr;
390     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, session_->NotifyDestroy());
391 }
392 
393 /**
394  * @tc.name: IsActive01
395  * @tc.desc: IsActive, normal scene
396  * @tc.type: FUNC
397  */
HWTEST_F(WindowSessionLifecycleTest, IsActive, Function | SmallTest | Level2)398 HWTEST_F(WindowSessionLifecycleTest, IsActive, Function | SmallTest | Level2)
399 {
400     ASSERT_NE(session_, nullptr);
401     session_->isActive_ = false;
402     if (!session_->IsActive()) {
403         ASSERT_EQ(false, session_->IsActive());
404     }
405 }
406 
407 /**
408 * @tc.name: IsActive43
409 * @tc.desc: IsActive
410 * @tc.type: FUNC
411 */
HWTEST_F(WindowSessionLifecycleTest, IsActive43, Function | SmallTest | Level2)412 HWTEST_F(WindowSessionLifecycleTest, IsActive43, Function | SmallTest | Level2)
413 {
414     ASSERT_NE(session_, nullptr);
415     bool res = session_->IsActive();
416     ASSERT_EQ(res, false);
417 }
418 
419 /**
420  * @tc.name: IsSessionForeground01
421  * @tc.desc: IsSessionForeground, normal scene
422  * @tc.type: FUNC
423  */
HWTEST_F(WindowSessionLifecycleTest, IsSessionForeground, Function | SmallTest | Level2)424 HWTEST_F(WindowSessionLifecycleTest, IsSessionForeground, Function | SmallTest | Level2)
425 {
426     ASSERT_NE(session_, nullptr);
427     session_->state_ = SessionState::STATE_FOREGROUND;
428     ASSERT_EQ(true, session_->IsSessionForeground());
429     session_->state_ = SessionState::STATE_ACTIVE;
430     ASSERT_EQ(true, session_->IsSessionForeground());
431     session_->state_ = SessionState::STATE_INACTIVE;
432     ASSERT_EQ(false, session_->IsSessionForeground());
433     session_->state_ = SessionState::STATE_BACKGROUND;
434     ASSERT_EQ(false, session_->IsSessionForeground());
435     session_->state_ = SessionState::STATE_DISCONNECT;
436     ASSERT_EQ(false, session_->IsSessionForeground());
437     session_->state_ = SessionState::STATE_CONNECT;
438     ASSERT_EQ(false, session_->IsSessionForeground());
439 }
440 
441 /**
442  * @tc.name: NotifyActivation
443  * @tc.desc: NotifyActivation Test
444  * @tc.type: FUNC
445  */
HWTEST_F(WindowSessionLifecycleTest, NotifyActivation, Function | SmallTest | Level2)446 HWTEST_F(WindowSessionLifecycleTest, NotifyActivation, Function | SmallTest | Level2)
447 {
448     ASSERT_NE(session_, nullptr);
449     session_->state_ = SessionState::STATE_DISCONNECT;
450     session_->NotifyActivation();
451 
452     ASSERT_EQ(WSError::WS_OK, session_->SetFocusable(false));
453 }
454 
455 /**
456  * @tc.name: NotifyActivation022
457  * @tc.desc: NotifyActivation
458  * @tc.type: FUNC
459  */
HWTEST_F(WindowSessionLifecycleTest, NotifyActivation022, Function | SmallTest | Level2)460 HWTEST_F(WindowSessionLifecycleTest, NotifyActivation022, Function | SmallTest | Level2)
461 {
462     ASSERT_NE(session_, nullptr);
463     session_->NotifyActivation();
464 
465     session_->RegisterLifecycleListener(lifecycleListener_);
466     session_->NotifyActivation();
467     uint64_t screenId = 0;
468     session_->SetScreenId(screenId);
469     session_->UnregisterLifecycleListener(lifecycleListener_);
470     ASSERT_EQ(0, session_->sessionInfo_.screenId_);
471 }
472 
473 /**
474  * @tc.name: NotifyForeground
475  * @tc.desc: NotifyForeground Test
476  * @tc.type: FUNC
477  */
HWTEST_F(WindowSessionLifecycleTest, NotifyForeground, Function | SmallTest | Level2)478 HWTEST_F(WindowSessionLifecycleTest, NotifyForeground, Function | SmallTest | Level2)
479 {
480     ASSERT_NE(session_, nullptr);
481     session_->state_ = SessionState::STATE_DISCONNECT;
482     session_->NotifyForeground();
483 
484     ASSERT_EQ(WSError::WS_OK, session_->SetFocusable(false));
485 }
486 
487 /**
488  * @tc.name: NotifyForeground024
489  * @tc.desc: NotifyForeground
490  * @tc.type: FUNC
491  */
HWTEST_F(WindowSessionLifecycleTest, NotifyForeground024, Function | SmallTest | Level2)492 HWTEST_F(WindowSessionLifecycleTest, NotifyForeground024, Function | SmallTest | Level2)
493 {
494     ASSERT_NE(session_, nullptr);
495     session_->NotifyForeground();
496 
497     session_->RegisterLifecycleListener(lifecycleListener_);
498     session_->NotifyForeground();
499     uint64_t screenId = 0;
500     session_->SetScreenId(screenId);
501     session_->UnregisterLifecycleListener(lifecycleListener_);
502     ASSERT_EQ(0, session_->sessionInfo_.screenId_);
503 }
504 
505 /**
506  * @tc.name: NotifyBackground
507  * @tc.desc: NotifyBackground Test
508  * @tc.type: FUNC
509  */
HWTEST_F(WindowSessionLifecycleTest, NotifyBackground, Function | SmallTest | Level2)510 HWTEST_F(WindowSessionLifecycleTest, NotifyBackground, Function | SmallTest | Level2)
511 {
512     ASSERT_NE(session_, nullptr);
513     session_->state_ = SessionState::STATE_DISCONNECT;
514     session_->NotifyBackground();
515 
516     ASSERT_EQ(WSError::WS_OK, session_->SetFocusable(false));
517 }
518 
519 /**
520  * @tc.name: NotifyBackground025
521  * @tc.desc: NotifyBackground
522  * @tc.type: FUNC
523  */
HWTEST_F(WindowSessionLifecycleTest, NotifyBackground025, Function | SmallTest | Level2)524 HWTEST_F(WindowSessionLifecycleTest, NotifyBackground025, Function | SmallTest | Level2)
525 {
526     ASSERT_NE(session_, nullptr);
527     session_->NotifyBackground();
528 
529     session_->RegisterLifecycleListener(lifecycleListener_);
530     session_->NotifyBackground();
531     uint64_t screenId = 0;
532     session_->SetScreenId(screenId);
533     session_->UnregisterLifecycleListener(lifecycleListener_);
534     ASSERT_EQ(0, session_->sessionInfo_.screenId_);
535 }
536 
537 /**
538  * @tc.name: TerminateSessionTotal01
539  * @tc.desc: abilitySessionInfo is nullptr
540  * @tc.type: FUNC
541  */
HWTEST_F(WindowSessionLifecycleTest, TerminateSessionTotal01, Function | SmallTest | Level2)542 HWTEST_F(WindowSessionLifecycleTest, TerminateSessionTotal01, Function | SmallTest | Level2)
543 {
544     ASSERT_NE(session_, nullptr);
545     ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION,
546             session_->TerminateSessionTotal(nullptr, TerminateType::CLOSE_AND_KEEP_MULTITASK));
547 }
548 
549 /**
550  * @tc.name: TerminateSessionTotal02
551  * @tc.desc: abilitySessionInfo is not nullptr, isTerminating is true
552  * @tc.type: FUNC
553  */
HWTEST_F(WindowSessionLifecycleTest, TerminateSessionTotal02, Function | SmallTest | Level2)554 HWTEST_F(WindowSessionLifecycleTest, TerminateSessionTotal02, Function | SmallTest | Level2)
555 {
556     ASSERT_NE(session_, nullptr);
557     sptr<AAFwk::SessionInfo> abilitySessionInfo = new AAFwk::SessionInfo();
558     session_->isTerminating_ = true;
559     ASSERT_EQ(WSError::WS_ERROR_INVALID_OPERATION,
560             session_->TerminateSessionTotal(abilitySessionInfo, TerminateType::CLOSE_AND_KEEP_MULTITASK));
561 }
562 
563 /**
564  * @tc.name: TerminateSessionTotal03
565  * @tc.desc: abilitySessionInfo is not nullptr, isTerminating is false
566  * @tc.type: FUNC
567  */
HWTEST_F(WindowSessionLifecycleTest, TerminateSessionTotal03, Function | SmallTest | Level2)568 HWTEST_F(WindowSessionLifecycleTest, TerminateSessionTotal03, Function | SmallTest | Level2)
569 {
570     ASSERT_NE(session_, nullptr);
571     sptr<AAFwk::SessionInfo> abilitySessionInfo = new AAFwk::SessionInfo();
572     session_->isTerminating_ = false;
573     NotifyTerminateSessionFuncTotal func = nullptr;
574     session_->SetTerminateSessionListenerTotal(func);
575     ASSERT_EQ(WSError::WS_OK,
576             session_->TerminateSessionTotal(abilitySessionInfo, TerminateType::CLOSE_AND_KEEP_MULTITASK));
577 }
578 
579 /**
580  * @tc.name: PendingSessionToBackgroundForDelegator
581  * @tc.desc: PendingSessionToBackgroundForDelegator Test
582  * @tc.type: FUNC
583  */
HWTEST_F(WindowSessionLifecycleTest, PendingSessionToBackgroundForDelegator, Function | SmallTest | Level2)584 HWTEST_F(WindowSessionLifecycleTest, PendingSessionToBackgroundForDelegator, Function | SmallTest | Level2)
585 {
586     ASSERT_NE(session_, nullptr);
587     session_->SetPendingSessionToBackgroundForDelegatorListener(nullptr);
588     ASSERT_EQ(WSError::WS_OK, session_->PendingSessionToBackgroundForDelegator(true));
589 }
590 
591 /**
592  * @tc.name: NotifyConnect023
593  * @tc.desc: NotifyConnect
594  * @tc.type: FUNC
595  */
HWTEST_F(WindowSessionLifecycleTest, NotifyConnect023, Function | SmallTest | Level2)596 HWTEST_F(WindowSessionLifecycleTest, NotifyConnect023, Function | SmallTest | Level2)
597 {
598     ASSERT_NE(session_, nullptr);
599     session_->NotifyConnect();
600 
601     session_->RegisterLifecycleListener(lifecycleListener_);
602     session_->NotifyConnect();
603     uint64_t screenId = 0;
604     session_->SetScreenId(screenId);
605     session_->UnregisterLifecycleListener(lifecycleListener_);
606     ASSERT_EQ(0, session_->sessionInfo_.screenId_);
607 }
608 
609 /**
610  * @tc.name: NotifyDisconnect026
611  * @tc.desc: NotifyDisconnect
612  * @tc.type: FUNC
613  */
HWTEST_F(WindowSessionLifecycleTest, NotifyDisconnect026, Function | SmallTest | Level2)614 HWTEST_F(WindowSessionLifecycleTest, NotifyDisconnect026, Function | SmallTest | Level2)
615 {
616     ASSERT_NE(session_, nullptr);
617     session_->NotifyDisconnect();
618 
619     session_->RegisterLifecycleListener(lifecycleListener_);
620     session_->NotifyDisconnect();
621     uint64_t screenId = 0;
622     session_->SetScreenId(screenId);
623     session_->UnregisterLifecycleListener(lifecycleListener_);
624     ASSERT_EQ(0, session_->sessionInfo_.screenId_);
625 }
626 
627 /**
628  * @tc.name: UpdateSessionState32
629  * @tc.desc: UpdateSessionState
630  * @tc.type: FUNC
631  */
HWTEST_F(WindowSessionLifecycleTest, UpdateSessionState32, Function | SmallTest | Level2)632 HWTEST_F(WindowSessionLifecycleTest, UpdateSessionState32, Function | SmallTest | Level2)
633 {
634     ASSERT_NE(session_, nullptr);
635     SessionState state = SessionState::STATE_CONNECT;
636     session_->UpdateSessionState(state);
637     ASSERT_EQ(session_->state_, SessionState::STATE_CONNECT);
638 }
639 
640 /**
641  * @tc.name: IsSystemSession44
642  * @tc.desc: IsSystemSession
643  * @tc.type: FUNC
644  */
HWTEST_F(WindowSessionLifecycleTest, IsSystemSession44, Function | SmallTest | Level2)645 HWTEST_F(WindowSessionLifecycleTest, IsSystemSession44, Function | SmallTest | Level2)
646 {
647     ASSERT_NE(session_, nullptr);
648     bool res = session_->IsSystemSession();
649     ASSERT_EQ(res, false);
650 }
651 
652 /**
653  * @tc.name: Hide45
654  * @tc.desc: Hide
655  * @tc.type: FUNC
656  */
HWTEST_F(WindowSessionLifecycleTest, Hide45, Function | SmallTest | Level2)657 HWTEST_F(WindowSessionLifecycleTest, Hide45, Function | SmallTest | Level2)
658 {
659     ASSERT_NE(session_, nullptr);
660     auto result = session_->Hide();
661     ASSERT_EQ(result, WSError::WS_OK);
662 }
663 
664 /**
665  * @tc.name: Show46
666  * @tc.desc: Show
667  * @tc.type: FUNC
668  */
HWTEST_F(WindowSessionLifecycleTest, Show46, Function | SmallTest | Level2)669 HWTEST_F(WindowSessionLifecycleTest, Show46, Function | SmallTest | Level2)
670 {
671     ASSERT_NE(session_, nullptr);
672     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
673     ASSERT_NE(nullptr, property);
674     auto result = session_->Show(property);
675     ASSERT_EQ(result, WSError::WS_OK);
676 }
677 
678 /**
679  * @tc.name: IsSystemActive47
680  * @tc.desc: IsSystemActive
681  * @tc.type: FUNC
682  */
HWTEST_F(WindowSessionLifecycleTest, IsSystemActive47, Function | SmallTest | Level2)683 HWTEST_F(WindowSessionLifecycleTest, IsSystemActive47, Function | SmallTest | Level2)
684 {
685     ASSERT_NE(session_, nullptr);
686     bool res = session_->IsSystemActive();
687     ASSERT_EQ(res, false);
688 }
689 
690 /**
691  * @tc.name: IsTerminated49
692  * @tc.desc: IsTerminated
693  * @tc.type: FUNC
694  */
HWTEST_F(WindowSessionLifecycleTest, IsTerminated49, Function | SmallTest | Level2)695 HWTEST_F(WindowSessionLifecycleTest, IsTerminated49, Function | SmallTest | Level2)
696 {
697     ASSERT_NE(session_, nullptr);
698     session_->state_ = SessionState::STATE_DISCONNECT;
699     bool res = session_->IsTerminated();
700     ASSERT_EQ(true, res);
701     session_->state_ = SessionState::STATE_FOREGROUND;
702     res = session_->IsTerminated();
703     ASSERT_EQ(false, res);
704     session_->state_ = SessionState::STATE_ACTIVE;
705     res = session_->IsTerminated();
706     ASSERT_EQ(false, res);
707     session_->state_ = SessionState::STATE_INACTIVE;
708     res = session_->IsTerminated();
709     ASSERT_EQ(false, res);
710     session_->state_ = SessionState::STATE_BACKGROUND;
711     res = session_->IsTerminated();
712     ASSERT_EQ(false, res);
713     session_->state_ = SessionState::STATE_CONNECT;
714     res = session_->IsTerminated();
715     ASSERT_EQ(false, res);
716 }
717 }
718 }
719 }
720