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