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