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 SessionManagerLiteUTTest : 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 SessionManagerLiteUTTest::SetUpTestCase() {}
44
TearDownTestCase()45 void SessionManagerLiteUTTest::TearDownTestCase() {}
46
SetUp()47 void SessionManagerLiteUTTest::SetUp()
48 {
49 sml_ = std::make_shared<SessionManagerLite>();
50 ASSERT_NE(nullptr, sml_);
51 }
52
TearDown()53 void SessionManagerLiteUTTest::TearDown()
54 {
55 sml_ = nullptr;
56 }
57
58 namespace {
59 /**
60 * @tc.name: GetSceneSessionManagerLiteProxy
61 * @tc.desc: normal function
62 * @tc.type: FUNC
63 */
64 HWTEST_F(SessionManagerLiteUTTest, GetSceneSessionManagerLiteProxy, TestSize.Level1)
65 {
66 ASSERT_NE(nullptr, sml_);
67 sml_->Clear();
68 sml_->ClearSessionManagerProxy();
69 auto sceneSessionManagerLiteProxy = sml_->GetSceneSessionManagerLiteProxy();
70 ASSERT_EQ(nullptr, sceneSessionManagerLiteProxy);
71
72 sml_->ClearSessionManagerProxy();
73 sml_->GetSessionManagerServiceProxy();
74 sceneSessionManagerLiteProxy = sml_->GetSceneSessionManagerLiteProxy();
75 ASSERT_EQ(nullptr, sceneSessionManagerLiteProxy);
76 }
77
78 /**
79 * @tc.name: InitSceneSessionManagerLiteProxy
80 * @tc.desc: normal function
81 * @tc.type: FUNC
82 */
83 HWTEST_F(SessionManagerLiteUTTest, InitSceneSessionManagerLiteProxy01, TestSize.Level1)
84 {
85 ASSERT_NE(nullptr, sml_);
86 sml_->InitSceneSessionManagerLiteProxy();
87 ASSERT_EQ(nullptr, sml_->sceneSessionManagerLiteProxy_);
88 }
89
90 /**
91 * @tc.name: InitSceneSessionManagerLiteProxy
92 * @tc.desc: normal function
93 * @tc.type: FUNC
94 */
95 HWTEST_F(SessionManagerLiteUTTest, InitSceneSessionManagerLiteProxy02, TestSize.Level1)
96 {
97 ASSERT_NE(nullptr, sml_);
98 sml_->GetSceneSessionManagerLiteProxy();
99 sml_->InitSceneSessionManagerLiteProxy();
100 ASSERT_EQ(nullptr, sml_->sceneSessionManagerLiteProxy_);
101 }
102
103 /**
104 * @tc.name: ClearSessionManagerProxy
105 * @tc.desc: normal function
106 * @tc.type: FUNC
107 */
108 HWTEST_F(SessionManagerLiteUTTest, ClearSessionManagerProxy, TestSize.Level1)
109 {
110 ASSERT_NE(nullptr, sml_);
111 sml_->ClearSessionManagerProxy();
112 ASSERT_EQ(sml_->sessionManagerServiceProxy_, nullptr);
113
114 sml_->recoverListenerRegistered_ = true;
115 sml_->GetSessionManagerServiceProxy();
116 sml_->ClearSessionManagerProxy();
117 ASSERT_EQ(sml_->sessionManagerServiceProxy_, nullptr);
118 }
119
120 /**
121 * @tc.name: RecoverSessionManagerService
122 * @tc.desc: normal function
123 * @tc.type: FUNC
124 */
125 HWTEST_F(SessionManagerLiteUTTest, RecoverSessionManagerService, TestSize.Level1)
126 {
127 ASSERT_NE(nullptr, sml_);
128 bool funcInvoked = false;
129 sml_->RecoverSessionManagerService(nullptr);
130 ASSERT_EQ(funcInvoked, false);
131
__anon1b85d48e0202() 132 sml_->userSwitchCallbackFunc_ = [&]() { funcInvoked = true; };
133 sml_->RecoverSessionManagerService(nullptr);
134 ASSERT_EQ(funcInvoked, true);
135 }
136
137 /**
138 * @tc.name: ReregisterSessionListener
139 * @tc.desc: normal function
140 * @tc.type: FUNC
141 */
142 HWTEST_F(SessionManagerLiteUTTest, ReregisterSessionListener, TestSize.Level1)
143 {
144 ASSERT_NE(nullptr, sml_);
145 sml_->ReregisterSessionListener();
146 ASSERT_EQ(nullptr, sml_->sceneSessionManagerLiteProxy_);
147 }
148
149 /**
150 * @tc.name: OnWMSConnectionChangedCallback
151 * @tc.desc: normal function
152 * @tc.type: FUNC
153 */
154 HWTEST_F(SessionManagerLiteUTTest, OnWMSConnectionChangedCallback, TestSize.Level1)
155 {
156 ASSERT_NE(nullptr, sml_);
157 bool funcInvoked = false;
158 sml_->wmsConnectionChangedFunc_ = nullptr;
159 sml_->OnWMSConnectionChangedCallback(101, DEFAULT_SCREEN_ID, true, false);
160 ASSERT_EQ(funcInvoked, false);
161
__anon1b85d48e0302(int32_t userId, int32_t screenId, bool isConnected) 162 sml_->wmsConnectionChangedFunc_ = [&](int32_t userId, int32_t screenId, bool isConnected) { funcInvoked = true; };
163 sml_->OnWMSConnectionChangedCallback(101, DEFAULT_SCREEN_ID, true, true);
164 ASSERT_EQ(funcInvoked, true);
165 }
166
167 /**
168 * @tc.name: OnWMSConnectionChanged1
169 * @tc.desc: wms disconnected
170 * @tc.type: FUNC
171 */
172 HWTEST_F(SessionManagerLiteUTTest, OnWMSConnectionChanged1, TestSize.Level1)
173 {
174 ASSERT_NE(nullptr, sml_);
175 sptr<ISessionManagerService> sessionManagerService;
176 sml_->isWMSConnected_ = true;
177 sml_->currentWMSUserId_ = 100;
178 sml_->OnWMSConnectionChanged(100, DEFAULT_SCREEN_ID, false, sessionManagerService);
179 ASSERT_EQ(sml_->isWMSConnected_, false);
180
181 sml_->currentWMSUserId_ = 101;
182 sml_->isWMSConnected_ = true;
183 sml_->OnWMSConnectionChanged(100, DEFAULT_SCREEN_ID, false, sessionManagerService);
184 ASSERT_EQ(sml_->isWMSConnected_, true);
185 }
186
187 /**
188 * @tc.name: OnWMSConnectionChanged2
189 * @tc.desc: wms connected
190 * @tc.type: FUNC
191 */
192 HWTEST_F(SessionManagerLiteUTTest, OnWMSConnectionChanged2, TestSize.Level1)
193 {
194 ASSERT_NE(nullptr, sml_);
195 sptr<ISessionManagerService> sessionManagerService;
196 sml_->isWMSConnected_ = false;
197 sml_->currentWMSUserId_ = INVALID_USER_ID;
198 sml_->OnWMSConnectionChanged(100, DEFAULT_SCREEN_ID, true, sessionManagerService);
199 ASSERT_EQ(sml_->isWMSConnected_, true);
200
201 // user switch
202 sml_->currentWMSUserId_ = 100;
203 sml_->isWMSConnected_ = true;
204 sml_->OnWMSConnectionChanged(101, DEFAULT_SCREEN_ID, true, sessionManagerService);
205 ASSERT_EQ(sml_->isWMSConnected_, true);
206 }
207
208 /**
209 * @tc.name: OnUserSwitch
210 * @tc.desc: normal function
211 * @tc.type: FUNC
212 */
213 HWTEST_F(SessionManagerLiteUTTest, OnUserSwitch, TestSize.Level1)
214 {
215 ASSERT_NE(nullptr, sml_);
216 sml_->OnUserSwitch(nullptr);
217 ASSERT_EQ(nullptr, sml_->sessionManagerServiceProxy_);
218
219 sml_->recoverListenerRegistered_ = true;
220 bool funInvoked = false;
__anon1b85d48e0402() 221 sml_->userSwitchCallbackFunc_ = [&]() { funInvoked = true; };
222 auto sessionManagerService = sml_->GetSessionManagerServiceProxy();
223 sml_->OnUserSwitch(sessionManagerService);
224 ASSERT_EQ(funInvoked, false);
225 }
226
227 /**
228 * @tc.name: OnRemoteDied1
229 * @tc.desc: foundation died
230 * @tc.type: FUNC
231 */
232 HWTEST_F(SessionManagerLiteUTTest, OnRemoteDied1, TestSize.Level1)
233 {
234 ASSERT_NE(nullptr, sml_);
235 FoundationDeathRecipient foundationDeathRecipient;
236 wptr<IRemoteObject> wptrDeath;
237 foundationDeathRecipient.OnRemoteDied(wptrDeath);
238 ASSERT_EQ(false, sml_->isWMSConnected_);
239 ASSERT_EQ(false, sml_->isFoundationListenerRegistered_);
240 ASSERT_EQ(false, sml_->recoverListenerRegistered_);
241 ASSERT_EQ(nullptr, sml_->mockSessionManagerServiceProxy_);
242 ASSERT_EQ(nullptr, sml_->sessionManagerServiceProxy_);
243 ASSERT_EQ(nullptr, sml_->sceneSessionManagerLiteProxy_);
244 }
245
246 /**
247 * @tc.name: OnRemoteDied2
248 * @tc.desc: scb died
249 * @tc.type: FUNC
250 */
251 HWTEST_F(SessionManagerLiteUTTest, OnRemoteDied2, TestSize.Level1)
252 {
253 ASSERT_NE(nullptr, sml_);
254 SSMDeathRecipient sSMDeathRecipient;
255 wptr<IRemoteObject> wptrDeath;
256 sSMDeathRecipient.OnRemoteDied(wptrDeath);
257 ASSERT_EQ(nullptr, sml_->sessionManagerServiceProxy_);
258 }
259
260 /**
261 * @tc.name: OnFoundationDied
262 * @tc.desc: normal function
263 * @tc.type: FUNC
264 */
265 HWTEST_F(SessionManagerLiteUTTest, OnFoundationDied, TestSize.Level1)
266 {
267 ASSERT_NE(nullptr, sml_);
268 sml_->OnFoundationDied();
269 ASSERT_EQ(false, sml_->isWMSConnected_);
270 ASSERT_EQ(false, sml_->isFoundationListenerRegistered_);
271 ASSERT_EQ(false, sml_->recoverListenerRegistered_);
272 ASSERT_EQ(nullptr, sml_->mockSessionManagerServiceProxy_);
273 ASSERT_EQ(nullptr, sml_->sessionManagerServiceProxy_);
274 ASSERT_EQ(nullptr, sml_->sceneSessionManagerLiteProxy_);
275 }
276
277 /**
278 * @tc.name: RegisterWMSConnectionChangedListener
279 * @tc.desc: WMSConnectionChangedCallbackFunc is null
280 * @tc.type: FUNC
281 */
282 HWTEST_F(SessionManagerLiteUTTest, RegisterWMSConnectionChangedListener, TestSize.Level1)
283 {
284 ASSERT_NE(nullptr, sml_);
285 auto ret = sml_->RegisterWMSConnectionChangedListener(nullptr);
286 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, ret);
287 }
288
289 /**
290 * @tc.name: RegisterWMSConnectionChangedListener1
291 * @tc.desc: normal test
292 * @tc.type: FUNC
293 */
294 HWTEST_F(SessionManagerLiteUTTest, RegisterWMSConnectionChangedListener1, TestSize.Level1)
295 {
296 ASSERT_NE(nullptr, sml_);
297 sml_->recoverListenerRegistered_ = true;
298 sml_->currentWMSUserId_ = 100;
299 sml_->currentScreenId_ = 0;
300 sml_->isWMSConnected_ = true;
__anon1b85d48e0502(int32_t userId, int32_t screenId, bool isConnected) 301 auto callbackFunc = [](int32_t userId, int32_t screenId, bool isConnected) {};
302 auto ret = sml_->RegisterWMSConnectionChangedListener(callbackFunc);
303 ASSERT_EQ(WMError::WM_OK, ret);
304 }
305
306 /**
307 * @tc.name: RegisterSMSRecoverListener1
308 * @tc.desc: mockSessionManagerServiceProxy_ is null
309 * @tc.type: FUNC
310 */
311 HWTEST_F(SessionManagerLiteUTTest, RegisterSMSRecoverListener1, TestSize.Level1)
312 {
313 ASSERT_NE(nullptr, sml_);
314 sml_->recoverListenerRegistered_ = false;
315 sml_->mockSessionManagerServiceProxy_ = nullptr;
316 sml_->RegisterSMSRecoverListener();
317 ASSERT_EQ(sml_->recoverListenerRegistered_, false);
318 }
319
320 /**
321 * @tc.name: RegisterSMSRecoverListener2
322 * @tc.desc: normal test
323 * @tc.type: FUNC
324 */
325 HWTEST_F(SessionManagerLiteUTTest, RegisterSMSRecoverListener2, TestSize.Level1)
326 {
327 ASSERT_NE(nullptr, sml_);
328 sml_->recoverListenerRegistered_ = false;
329 sml_->InitMockSMSProxy();
330 sml_->RegisterSMSRecoverListener();
331 ASSERT_EQ(sml_->recoverListenerRegistered_, true);
332 }
333
334 /**
335 * @tc.name: RegisterUserSwitchListener
336 * @tc.desc: normal function
337 * @tc.type: FUNC
338 */
339 HWTEST_F(SessionManagerLiteUTTest, RegisterUserSwitchListener, TestSize.Level1)
340 {
341 ASSERT_NE(nullptr, sml_);
342 sml_->RegisterUserSwitchListener(nullptr);
343 ASSERT_EQ(sml_->userSwitchCallbackFunc_, nullptr);
344
__anon1b85d48e0602() 345 sml_->RegisterUserSwitchListener([]() {});
346 ASSERT_NE(sml_->userSwitchCallbackFunc_, nullptr);
347 }
348
349 /**
350 * @tc.name: InitMockSMSProxy
351 * @tc.desc: normal function
352 * @tc.type: FUNC
353 */
354 HWTEST_F(SessionManagerLiteUTTest, InitMockSMSProxy, TestSize.Level1)
355 {
356 ASSERT_NE(nullptr, sml_);
357 sml_->InitMockSMSProxy();
358 sml_->InitMockSMSProxy();
359 ASSERT_NE(sml_->foundationDeath_, nullptr);
360 }
361 } // namespace
362 } // namespace Rosen
363 } // namespace OHOS