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 "session_manager.h"
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20 #include <ipc_skeleton.h>
21 #include "scene_board_judgement.h"
22 #include "session_manager_lite.h"
23 #include "session_manager_service_recover_interface.h"
24 #include "singleton_delegator.h"
25 #include "window_manager_hilog.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Rosen {
32 class SessionManagerLiteTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38
39 private:
40 std::shared_ptr<SessionManagerLite> sml_;
41 };
42
SetUpTestCase()43 void SessionManagerLiteTest::SetUpTestCase() {}
44
TearDownTestCase()45 void SessionManagerLiteTest::TearDownTestCase() {}
46
SetUp()47 void SessionManagerLiteTest::SetUp()
48 {
49 sml_ = std::make_shared<SessionManagerLite>();
50 ASSERT_NE(nullptr, sml_);
51 }
52
TearDown()53 void SessionManagerLiteTest::TearDown()
54 {
55 sml_ = nullptr;
56 }
57
58 namespace {
59 /**
60 * @tc.name: RecoverSessionManagerService
61 * @tc.desc: normal function
62 * @tc.type: FUNC
63 */
64 HWTEST_F(SessionManagerLiteTest, RecoverSessionManagerService, TestSize.Level1)
65 {
66 ASSERT_NE(nullptr, sml_);
67 bool funcInvoked = false;
68 sml_->RecoverSessionManagerService(nullptr);
69 ASSERT_EQ(funcInvoked, false);
70
__anonfe3b4fa60202() 71 sml_->userSwitchCallbackFunc_ = [&]() { funcInvoked = true; };
72 sml_->RecoverSessionManagerService(nullptr);
73 ASSERT_EQ(funcInvoked, true);
74 }
75
76 /**
77 * @tc.name: ReregisterSessionListener
78 * @tc.desc: normal function
79 * @tc.type: FUNC
80 */
81 HWTEST_F(SessionManagerLiteTest, ReregisterSessionListener, TestSize.Level1)
82 {
83 ASSERT_NE(nullptr, sml_);
84 sml_->ReregisterSessionListener();
85 ASSERT_EQ(nullptr, sml_->sceneSessionManagerLiteProxy_);
86
87 sml_->recoverListenerRegistered_ = true;
88 sml_->GetSceneSessionManagerLiteProxy();
89 sml_->ReregisterSessionListener();
90 ASSERT_NE(nullptr, sml_->sceneSessionManagerLiteProxy_);
91 }
92
93 /**
94 * @tc.name: OnRemoteDied1
95 * @tc.desc: foundation died
96 * @tc.type: FUNC
97 */
98 HWTEST_F(SessionManagerLiteTest, OnRemoteDied1, TestSize.Level1)
99 {
100 ASSERT_NE(nullptr, sml_);
101 FoundationDeathRecipient foundationDeathRecipient;
102 wptr<IRemoteObject> wptrDeath;
103 foundationDeathRecipient.OnRemoteDied(wptrDeath);
104 ASSERT_EQ(false, sml_->isWMSConnected_);
105 ASSERT_EQ(false, sml_->isFoundationListenerRegistered_);
106 ASSERT_EQ(false, sml_->recoverListenerRegistered_);
107 ASSERT_EQ(nullptr, sml_->mockSessionManagerServiceProxy_);
108 ASSERT_EQ(nullptr, sml_->sessionManagerServiceProxy_);
109 ASSERT_EQ(nullptr, sml_->sceneSessionManagerLiteProxy_);
110 }
111
112 /**
113 * @tc.name: OnRemoteDied2
114 * @tc.desc: scb died
115 * @tc.type: FUNC
116 */
117 HWTEST_F(SessionManagerLiteTest, OnRemoteDied2, TestSize.Level1)
118 {
119 ASSERT_NE(nullptr, sml_);
120 SSMDeathRecipient sSMDeathRecipient;
121 wptr<IRemoteObject> wptrDeath;
122 sSMDeathRecipient.OnRemoteDied(wptrDeath);
123 ASSERT_EQ(nullptr, sml_->sessionManagerServiceProxy_);
124 }
125
126 /**
127 * @tc.name: OnFoundationDied
128 * @tc.desc: normal function
129 * @tc.type: FUNC
130 */
131 HWTEST_F(SessionManagerLiteTest, OnFoundationDied, TestSize.Level1)
132 {
133 ASSERT_NE(nullptr, sml_);
134 sml_->OnFoundationDied();
135 ASSERT_EQ(false, sml_->isWMSConnected_);
136 ASSERT_EQ(false, sml_->isFoundationListenerRegistered_);
137 ASSERT_EQ(false, sml_->recoverListenerRegistered_);
138 ASSERT_EQ(nullptr, sml_->mockSessionManagerServiceProxy_);
139 ASSERT_EQ(nullptr, sml_->sessionManagerServiceProxy_);
140 ASSERT_EQ(nullptr, sml_->sceneSessionManagerLiteProxy_);
141 }
142
143 /**
144 * @tc.name: RegisterSMSRecoverListener1
145 * @tc.desc: mockSessionManagerServiceProxy_ is null
146 * @tc.type: FUNC
147 */
148 HWTEST_F(SessionManagerLiteTest, RegisterSMSRecoverListener1, TestSize.Level1)
149 {
150 ASSERT_NE(nullptr, sml_);
151 sml_->recoverListenerRegistered_ = false;
152 sml_->mockSessionManagerServiceProxy_ = nullptr;
153 sml_->RegisterSMSRecoverListener();
154 ASSERT_EQ(sml_->recoverListenerRegistered_, false);
155 }
156
157 /**
158 * @tc.name: RegisterSMSRecoverListener2
159 * @tc.desc: normal test
160 * @tc.type: FUNC
161 */
162 HWTEST_F(SessionManagerLiteTest, RegisterSMSRecoverListener2, TestSize.Level1)
163 {
164 ASSERT_NE(nullptr, sml_);
165 sml_->recoverListenerRegistered_ = false;
166 sml_->InitMockSMSProxy();
167 sml_->RegisterSMSRecoverListener();
168 ASSERT_EQ(sml_->recoverListenerRegistered_, true);
169 }
170
171 /**
172 * @tc.name: InitMockSMSProxy
173 * @tc.desc: normal function
174 * @tc.type: FUNC
175 */
176 HWTEST_F(SessionManagerLiteTest, InitMockSMSProxy, TestSize.Level1)
177 {
178 ASSERT_NE(nullptr, sml_);
179 sml_->InitMockSMSProxy();
180 sml_->InitMockSMSProxy();
181 ASSERT_NE(sml_->foundationDeath_, nullptr);
182 }
183 } // namespace
184 } // namespace Rosen
185 } // namespace OHOS