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_manager.h"
18
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21 #include <ipc_skeleton.h>
22
23 #include "scene_board_judgement.h"
24 #include "session_manager_service_recover_interface.h"
25 #include "singleton_delegator.h"
26 #include "window_manager_hilog.h"
27 #include "session_manager_lite.h"
28
29 using namespace testing;
30 using namespace testing::ext;
31
32 namespace OHOS {
33 namespace Rosen {
34 class SessionManagerTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp() override;
39 void TearDown() override;
40 private:
41 std::shared_ptr<SessionManager> sm_;
42 };
43
SetUpTestCase()44 void SessionManagerTest::SetUpTestCase()
45 {
46 }
47
TearDownTestCase()48 void SessionManagerTest::TearDownTestCase()
49 {
50 }
51
SetUp()52 void SessionManagerTest::SetUp()
53 {
54 sm_ = std::make_shared<SessionManager>();
55 ASSERT_NE(nullptr, sm_);
56 }
57
TearDown()58 void SessionManagerTest::TearDown()
59 {
60 sm_ = nullptr;
61 }
62
63 namespace {
64 /**
65 * @tc.name: OnRemoteRequest
66 * @tc.desc: normal function
67 * @tc.type: FUNC
68 */
69 HWTEST_F(SessionManagerTest, OnRemoteRequest, Function | SmallTest | Level2)
70 {
71 OHOS::MessageParcel data;
72 OHOS::MessageParcel reply;
73 OHOS::MessageOption option;
74 IPCObjectStub iPCObjectStub;
75
76 uint32_t code = static_cast<uint32_t>(OHOS::Rosen::ISessionManagerServiceRecoverListener::
77 SessionManagerServiceRecoverMessage::TRANS_ID_ON_SESSION_MANAGER_SERVICE_RECOVER);
78 auto ret = iPCObjectStub.OnRemoteRequest(code, data, reply, option);
79 ASSERT_NE(ret, 0);
80
81 code = static_cast<uint32_t>(OHOS::Rosen::ISessionManagerServiceRecoverListener::
82 SessionManagerServiceRecoverMessage::TRANS_ID_ON_WMS_CONNECTION_CHANGED);
83 ret = iPCObjectStub.OnRemoteRequest(code, data, reply, option);
84 ASSERT_NE(ret, 0);
85
86 code = 10;
87 ret = iPCObjectStub.OnRemoteRequest(code, data, reply, option);
88 ASSERT_NE(0, ret);
89 }
90
91 /**
92 * @tc.name: GetSceneSessionManagerProxy
93 * @tc.desc: normal function
94 * @tc.type: FUNC
95 */
96 HWTEST_F(SessionManagerTest, GetSceneSessionManagerProxy, Function | SmallTest | Level2)
97 {
98 ASSERT_NE(nullptr, sm_);
99 sm_->Clear();
100 sm_->ClearSessionManagerProxy();
101 auto sceneSessionManagerProxy = sm_->GetSceneSessionManagerProxy();
102 ASSERT_NE(nullptr, sceneSessionManagerProxy);
103
104 sm_->ClearSessionManagerProxy();
105 sm_->sessionManagerServiceProxy_ = SessionManagerLite::GetInstance().GetSessionManagerServiceProxy();
106 sceneSessionManagerProxy = sm_->GetSceneSessionManagerProxy();
107 ASSERT_NE(nullptr, sceneSessionManagerProxy);
108 }
109
110 /**
111 * @tc.name: ClearSessionManagerProxy
112 * @tc.desc: normal function
113 * @tc.type: FUNC
114 */
115 HWTEST_F(SessionManagerTest, ClearSessionManagerProxy, Function | SmallTest | Level2)
116 {
117 ASSERT_NE(nullptr, sm_);
118 sm_->ClearSessionManagerProxy();
119 ASSERT_EQ(sm_->sessionManagerServiceProxy_, nullptr);
120
121 sm_->isRecoverListenerRegistered_ = true;
122 sm_->sessionManagerServiceProxy_ = SessionManagerLite::GetInstance().GetSessionManagerServiceProxy();
123 sm_->ClearSessionManagerProxy();
124 ASSERT_EQ(sm_->sessionManagerServiceProxy_, nullptr);
125 }
126
127 /**
128 * @tc.name: OnWMSConnectionChangedCallback
129 * @tc.desc: normal function
130 * @tc.type: FUNC
131 */
132 HWTEST_F(SessionManagerTest, OnWMSConnectionChangedCallback, Function | SmallTest | Level2)
133 {
134 ASSERT_NE(nullptr, sm_);
135 bool funcInvoked = false;
136 sm_->wmsConnectionChangedFunc_ = nullptr;
137 sm_->OnWMSConnectionChangedCallback(101, DEFAULT_SCREEN_ID, true, false);
138 ASSERT_EQ(funcInvoked, false);
139
__anon3346eab70202(int32_t userId, int32_t screenId, bool isConnected) 140 sm_->wmsConnectionChangedFunc_ = [&](int32_t userId, int32_t screenId, bool isConnected) { funcInvoked = true; };
141 sm_->OnWMSConnectionChangedCallback(101, DEFAULT_SCREEN_ID, true, true);
142 ASSERT_EQ(funcInvoked, true);
143 }
144
145 /**
146 * @tc.name: OnWMSConnectionChanged1
147 * @tc.desc: wms disconnected
148 * @tc.type: FUNC
149 */
150 HWTEST_F(SessionManagerTest, OnWMSConnectionChanged1, Function | SmallTest | Level2)
151 {
152 ASSERT_NE(nullptr, sm_);
153 sptr<ISessionManagerService> sessionManagerService;
154 sm_->isWMSConnected_ = true;
155 sm_->currentWMSUserId_ = 100;
156 sm_->OnWMSConnectionChanged(100, DEFAULT_SCREEN_ID, false, sessionManagerService);
157 ASSERT_EQ(sm_->isWMSConnected_, false);
158
159 sm_->currentWMSUserId_ = 101;
160 sm_->isWMSConnected_ = true;
161 sm_->OnWMSConnectionChanged(100, DEFAULT_SCREEN_ID, false, sessionManagerService);
162 ASSERT_EQ(sm_->isWMSConnected_, true);
163 }
164
165 /**
166 * @tc.name: OnWMSConnectionChanged2
167 * @tc.desc: wms connected
168 * @tc.type: FUNC
169 */
170 HWTEST_F(SessionManagerTest, OnWMSConnectionChanged2, Function | SmallTest | Level2)
171 {
172 ASSERT_NE(nullptr, sm_);
173 sptr<ISessionManagerService> sessionManagerService;
174 sm_->isWMSConnected_ = false;
175 sm_->currentWMSUserId_ = INVALID_USER_ID;
176 sm_->OnWMSConnectionChanged(100, DEFAULT_SCREEN_ID, true, sessionManagerService);
177 ASSERT_EQ(sm_->isWMSConnected_, true);
178
179 // user switch
180 sm_->currentWMSUserId_ = 100;
181 sm_->isWMSConnected_ = true;
182 sm_->OnWMSConnectionChanged(101, DEFAULT_SCREEN_ID, true, sessionManagerService);
183 ASSERT_EQ(sm_->isWMSConnected_, true);
184 }
185
186 /**
187 * @tc.name: RecoverSessionManagerService
188 * @tc.desc: normal function
189 * @tc.type: FUNC
190 */
191 HWTEST_F(SessionManagerTest, RecoverSessionManagerService, Function | SmallTest | Level2)
192 {
193 ASSERT_NE(nullptr, sm_);
194 bool funcInvoked = false;
195 sm_->RegisterWindowManagerRecoverCallbackFunc(nullptr);
196 sm_->RecoverSessionManagerService(nullptr);
197 ASSERT_EQ(funcInvoked, false);
198
__anon3346eab70302() 199 sm_->RegisterWindowManagerRecoverCallbackFunc([&]() { funcInvoked = true; });
200 sm_->RecoverSessionManagerService(nullptr);
201 ASSERT_EQ(funcInvoked, true);
202 }
203
204 /**
205 * @tc.name: RegisterUserSwitchListener
206 * @tc.desc: normal function
207 * @tc.type: FUNC
208 */
209 HWTEST_F(SessionManagerTest, RegisterUserSwitchListener, Function | SmallTest | Level2)
210 {
211 ASSERT_NE(nullptr, sm_);
212 sm_->RegisterUserSwitchListener(nullptr);
213 ASSERT_EQ(sm_->userSwitchCallbackFunc_, nullptr);
214
__anon3346eab70402() 215 sm_->RegisterUserSwitchListener([]() {});
216 ASSERT_NE(sm_->userSwitchCallbackFunc_, nullptr);
217 }
218
219 /**
220 * @tc.name: OnUserSwitch
221 * @tc.desc: normal function
222 * @tc.type: FUNC
223 */
224 HWTEST_F(SessionManagerTest, OnUserSwitch, Function | SmallTest | Level2)
225 {
226 ASSERT_NE(nullptr, sm_);
227 sm_->OnUserSwitch(nullptr);
228 ASSERT_EQ(nullptr, sm_->sessionManagerServiceProxy_);
229
230 bool funInvoked = false;
__anon3346eab70502() 231 sm_->userSwitchCallbackFunc_ = [&]() { funInvoked = true; };
232 auto sessionManagerService = SessionManagerLite::GetInstance().GetSessionManagerServiceProxy();
233 sm_->OnUserSwitch(sessionManagerService);
234 ASSERT_EQ(funInvoked, true);
235 }
236
237 /**
238 * @tc.name: OnRemoteDied1
239 * @tc.desc: foundation died
240 * @tc.type: FUNC
241 */
242 HWTEST_F(SessionManagerTest, OnRemoteDied1, Function | SmallTest | Level2)
243 {
244 ASSERT_NE(nullptr, sm_);
245 FoundationDeathRecipient foundationDeathRecipient;
246 wptr<IRemoteObject> wptrDeath;
247 foundationDeathRecipient.OnRemoteDied(wptrDeath);
248 ASSERT_EQ(false, sm_->isWMSConnected_);
249 ASSERT_EQ(false, sm_->isFoundationListenerRegistered_);
250 ASSERT_EQ(false, sm_->isRecoverListenerRegistered_);
251 ASSERT_EQ(nullptr, sm_->mockSessionManagerServiceProxy_);
252 ASSERT_EQ(nullptr, sm_->sessionManagerServiceProxy_);
253 ASSERT_EQ(nullptr, sm_->sceneSessionManagerProxy_);
254 }
255
256 /**
257 * @tc.name: OnRemoteDied2
258 * @tc.desc: scb died
259 * @tc.type: FUNC
260 */
261 HWTEST_F(SessionManagerTest, OnRemoteDied2, Function | SmallTest | Level2)
262 {
263 ASSERT_NE(nullptr, sm_);
264 SSMDeathRecipient sSMDeathRecipient;
265 wptr<IRemoteObject> wptrDeath;
266 sSMDeathRecipient.OnRemoteDied(wptrDeath);
267 ASSERT_EQ(nullptr, sm_->sessionManagerServiceProxy_);
268 }
269
270 /**
271 * @tc.name: OnFoundationDied
272 * @tc.desc: normal function
273 * @tc.type: FUNC
274 */
275 HWTEST_F(SessionManagerTest, OnFoundationDied, Function | SmallTest | Level2)
276 {
277 ASSERT_NE(nullptr, sm_);
278 sm_->OnFoundationDied();
279 ASSERT_EQ(false, sm_->isWMSConnected_);
280 ASSERT_EQ(false, sm_->isFoundationListenerRegistered_);
281 ASSERT_EQ(false, sm_->isRecoverListenerRegistered_);
282 ASSERT_EQ(nullptr, sm_->mockSessionManagerServiceProxy_);
283 ASSERT_EQ(nullptr, sm_->sessionManagerServiceProxy_);
284 ASSERT_EQ(nullptr, sm_->sceneSessionManagerProxy_);
285 }
286
287 /**
288 * @tc.name: RegisterWMSConnectionChangedListener
289 * @tc.desc: WMSConnectionChangedCallbackFunc is null
290 * @tc.type: FUNC
291 */
292 HWTEST_F(SessionManagerTest, RegisterWMSConnectionChangedListener, Function | SmallTest | Level2)
293 {
294 ASSERT_NE(nullptr, sm_);
295 auto ret = sm_->RegisterWMSConnectionChangedListener(nullptr);
296 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, ret);
297 }
298
299 /**
300 * @tc.name: RegisterWMSConnectionChangedListener1
301 * @tc.desc: normal test
302 * @tc.type: FUNC
303 */
304 HWTEST_F(SessionManagerTest, RegisterWMSConnectionChangedListener1, Function | SmallTest | Level2)
305 {
306 ASSERT_NE(nullptr, sm_);
307 sm_->isRecoverListenerRegistered_ = true;
308 sm_->currentWMSUserId_ = 100;
309 sm_->currentScreenId_ = 0;
310 sm_->isWMSConnected_ = true;
__anon3346eab70602(int32_t userId, int32_t screenId, bool isConnected) 311 auto callbackFunc = [](int32_t userId, int32_t screenId, bool isConnected) {};
312 auto ret = sm_->RegisterWMSConnectionChangedListener(callbackFunc);
313 ASSERT_EQ(WMError::WM_OK, ret);
314 }
315
316 /**
317 * @tc.name: RegisterSMSRecoverListener1
318 * @tc.desc: mockSessionManagerServiceProxy_ is null
319 * @tc.type: FUNC
320 */
321 HWTEST_F(SessionManagerTest, RegisterSMSRecoverListener1, Function | SmallTest | Level2)
322 {
323 ASSERT_NE(nullptr, sm_);
324 sm_->isRecoverListenerRegistered_ = false;
325 sm_->mockSessionManagerServiceProxy_ = nullptr;
326 sm_->RegisterSMSRecoverListener();
327 ASSERT_EQ(sm_->isRecoverListenerRegistered_, false);
328 }
329
330 /**
331 * @tc.name: RegisterSMSRecoverListener2
332 * @tc.desc: normal test
333 * @tc.type: FUNC
334 */
335 HWTEST_F(SessionManagerTest, RegisterSMSRecoverListener2, Function | SmallTest | Level2)
336 {
337 ASSERT_NE(nullptr, sm_);
338 sm_->isRecoverListenerRegistered_ = false;
339 sm_->InitMockSMSProxy();
340 sm_->RegisterSMSRecoverListener();
341 ASSERT_EQ(sm_->isRecoverListenerRegistered_, true);
342 }
343
344 /**
345 * @tc.name: InitMockSMSProxy
346 * @tc.desc: normal function
347 * @tc.type: FUNC
348 */
349 HWTEST_F(SessionManagerTest, InitMockSMSProxy, Function | SmallTest | Level2)
350 {
351 ASSERT_NE(nullptr, sm_);
352 sm_->InitMockSMSProxy();
353 ASSERT_NE(sm_->foundationDeath_, nullptr);
354 }
355
356 /**
357 * @tc.name: RegisterWindowManagerRecoverCallbackFunc
358 * @tc.desc: normal function
359 * @tc.type: FUNC
360 */
361 HWTEST_F(SessionManagerTest, RegisterWindowManagerRecoverCallbackFunc, Function | SmallTest | Level2)
362 {
363 ASSERT_NE(nullptr, sm_);
__anon3346eab70702() 364 auto testFunc = []() { return; };
365 sm_->RegisterWindowManagerRecoverCallbackFunc(testFunc);
366 ASSERT_NE(sm_->windowManagerRecoverFunc_, nullptr);
367 }
368 }
369 }
370 }