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