• 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 <bundle_mgr_proxy.h>
18 #include <bundle_mgr_interface.h>
19 #include <bundlemgr/launcher_service.h>
20 
21 #include "mock/mock_session_stage.h"
22 #include "iremote_object_mocker.h"
23 #include "session_manager.h"
24 #include "session_manager/include/scene_session_manager.h"
25 #include "unittest/window_event_channel_base.h"
26 #include "interfaces/include/ws_common.h"
27 #include "session_info.h"
28 #include "session/host/include/scene_session.h"
29 #include "session/host/include/main_session.h"
30 
31 using namespace testing;
32 using namespace testing::ext;
33 
34 namespace OHOS {
35 namespace Rosen {
36 class WindowRecoverSessionTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp() override;
41     void TearDown() override;
42     SessionInfo mainSessionInfo;
43     sptr<MainSession::SpecificSessionCallback> specificCallback = nullptr;
44     sptr<MainSession> mainSession_;
45     SessionInfo sceneSessionInfo;
46     sptr<SceneSession> sceneSession_;
47     sptr<SceneSessionManager> ssm_;
48     std::shared_ptr<SessionManager> sm_;
49 
50 private:
51     RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
52     static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
53 };
54 
SetUpTestCase()55 void WindowRecoverSessionTest::SetUpTestCase() {}
56 
TearDownTestCase()57 void WindowRecoverSessionTest::TearDownTestCase() {}
58 
SetUp()59 void WindowRecoverSessionTest::SetUp()
60 {
61     SessionInfo mainSessionInfo;
62     mainSessionInfo.abilityName_ = "testMainSession1";
63     mainSessionInfo.moduleName_ = "testMainSession2";
64     mainSessionInfo.bundleName_ = "testMainSession3";
65     mainSession_ = sptr<MainSession>::MakeSptr(mainSessionInfo, specificCallback);
66     EXPECT_NE(nullptr, mainSession_);
67 
68     SessionInfo sceneSessionInfo;
69     sceneSessionInfo.abilityName_ = "testSceneSession1";
70     sceneSessionInfo.moduleName_ = "testSceneSession2";
71     sceneSessionInfo.bundleName_ = "testSceneSession3";
72     sceneSession_ = sptr<MainSession>::MakeSptr(sceneSessionInfo, nullptr);
73     EXPECT_NE(nullptr, sceneSession_);
74 
75     ssm_ = &SceneSessionManager::GetInstance();
76 
77     sm_ = std::make_shared<SessionManager>();
78 }
79 
TearDown()80 void WindowRecoverSessionTest::TearDown()
81 {
82     mainSession_ = nullptr;
83     sceneSession_ = nullptr;
84 
85     usleep(WAIT_SYNC_IN_NS);
86     ssm_ = nullptr;
87 
88     sm_ = nullptr;
89 }
90 
CreateRSSurfaceNode()91 RSSurfaceNode::SharedPtr WindowRecoverSessionTest::CreateRSSurfaceNode()
92 {
93     struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
94     rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
95     auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
96     return surfaceNode;
97 }
98 
NotifyRecoverSceneSessionFuncTest(const sptr<SceneSession> & session,const SessionInfo & sessionInfo)99 void NotifyRecoverSceneSessionFuncTest(const sptr<SceneSession>& session, const SessionInfo& sessionInfo) {}
100 
101 namespace {
102 
103 /**
104  * @tc.name: Reconnect01
105  * @tc.desc: check func Reconnect
106  * @tc.type: FUNC
107  */
108 HWTEST_F(WindowRecoverSessionTest, Reconnect01, TestSize.Level1)
109 {
110     auto surfaceNode = CreateRSSurfaceNode();
111     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
112     ASSERT_NE(nullptr, property);
113     sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
114     EXPECT_NE(nullptr, mockSessionStage);
115     sptr<TestWindowEventChannel> testWindowEventChannel = sptr<TestWindowEventChannel>::MakeSptr();
116     EXPECT_NE(nullptr, testWindowEventChannel);
117 
118     auto result = mainSession_->Reconnect(nullptr, nullptr, nullptr, nullptr);
119     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
120 
121     result = mainSession_->Reconnect(nullptr, nullptr, nullptr, property);
122     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
123 
124     result = mainSession_->Reconnect(nullptr, testWindowEventChannel, surfaceNode, property);
125     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
126 
127     result = mainSession_->Reconnect(mockSessionStage, nullptr, surfaceNode, property);
128     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
129 
130     result = mainSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
131     EXPECT_EQ(result, WSError::WS_OK);
132 
133     property->SetWindowState(WindowState::STATE_HIDDEN);
134     result = mainSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
135     EXPECT_EQ(result, WSError::WS_OK);
136 
137     property->SetWindowState(WindowState::STATE_SHOWN);
138     result = mainSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
139     EXPECT_EQ(result, WSError::WS_OK);
140 }
141 
142 /**
143  * @tc.name: Reconnect02
144  * @tc.desc: Reconnect02
145  * @tc.type: FUNC
146  */
147 HWTEST_F(WindowRecoverSessionTest, Reconnect02, TestSize.Level1)
148 {
149     auto surfaceNode = CreateRSSurfaceNode();
150     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
151     ASSERT_NE(nullptr, property);
152     sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
153     EXPECT_NE(nullptr, mockSessionStage);
154     sptr<TestWindowEventChannel> testWindowEventChannel = sptr<TestWindowEventChannel>::MakeSptr();
155     EXPECT_NE(nullptr, testWindowEventChannel);
156 
157     auto result = sceneSession_->Reconnect(nullptr, nullptr, nullptr, property);
158     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
159 
160     result = sceneSession_->Reconnect(nullptr, testWindowEventChannel, surfaceNode, property);
161     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
162 
163     result = sceneSession_->Reconnect(mockSessionStage, nullptr, surfaceNode, property);
164     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
165 
166     result = sceneSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, nullptr);
167     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
168 
169     result = sceneSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
170     EXPECT_EQ(result, WSError::WS_OK);
171 
172     property->SetWindowState(WindowState::STATE_INITIAL);
173     result = sceneSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
174     EXPECT_EQ(result, WSError::WS_OK);
175 
176     property->SetWindowState(WindowState::STATE_CREATED);
177     result = sceneSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
178     EXPECT_EQ(result, WSError::WS_OK);
179 
180     property->SetWindowState(WindowState::STATE_SHOWN);
181     result = sceneSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
182     EXPECT_EQ(result, WSError::WS_OK);
183 
184     property->SetWindowState(WindowState::STATE_HIDDEN);
185     result = sceneSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
186     EXPECT_EQ(result, WSError::WS_OK);
187 
188     property->SetWindowState(WindowState::STATE_FROZEN);
189     result = sceneSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
190     EXPECT_EQ(result, WSError::WS_OK);
191 }
192 
193 /**
194  * @tc.name: ReconnectInner
195  * @tc.desc: ReconnectInner
196  * @tc.type: FUNC
197  */
198 HWTEST_F(WindowRecoverSessionTest, ReconnectInner, TestSize.Level1)
199 {
200     sptr<WindowSessionProperty> property = nullptr;
201     WSError res = sceneSession_->ReconnectInner(property);
202     ASSERT_EQ(res, WSError::WS_ERROR_NULLPTR);
203 
204     property = sptr<WindowSessionProperty>::MakeSptr();
205     property->windowState_ = WindowState::STATE_INITIAL;
206     res = sceneSession_->ReconnectInner(property);
207     ASSERT_EQ(res, WSError::WS_ERROR_INVALID_PARAM);
208 
209     property->windowState_ = WindowState::STATE_CREATED;
210     res = sceneSession_->ReconnectInner(property);
211     ASSERT_EQ(res, WSError::WS_OK);
212 
213     property->windowState_ = WindowState::STATE_SHOWN;
214     res = sceneSession_->ReconnectInner(property);
215     ASSERT_EQ(res, WSError::WS_OK);
216 
217     property->windowState_ = WindowState::STATE_HIDDEN;
218     res = sceneSession_->ReconnectInner(property);
219     ASSERT_EQ(res, WSError::WS_OK);
220 
221     property->windowState_ = WindowState::STATE_DESTROYED;
222     res = sceneSession_->ReconnectInner(property);
223     ASSERT_EQ(res, WSError::WS_ERROR_INVALID_PARAM);
224 }
225 
226 /**
227  * @tc.name: CheckSessionPropertyOnRecovery
228  * @tc.desc: CheckSessionPropertyOnRecovery
229  * @tc.type: FUNC
230  */
231 HWTEST_F(WindowRecoverSessionTest, CheckSessionPropertyOnRecovery, TestSize.Level1)
232 {
233     ASSERT_NE(ssm_, nullptr);
234     sptr<WindowSessionProperty> property = nullptr;
235     WSError result = ssm_->CheckSessionPropertyOnRecovery(property, false);
236     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
237 
238     property = sptr<WindowSessionProperty>::MakeSptr();
239     property->SetWindowType(WindowType::WINDOW_TYPE_UI_EXTENSION);
240     property->SetWindowFlags(123);
241     result = ssm_->CheckSessionPropertyOnRecovery(property, false);
242     ASSERT_EQ(result, WSError::WS_ERROR_NOT_SYSTEM_APP);
243 
244     // 主窗,不需要恢复
245     property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
246     property->SetPersistentId(100);
247     result = ssm_->CheckSessionPropertyOnRecovery(property, false);
248     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_PARAM);
249 
250     // 主窗,需要恢复
251     ssm_->SetAlivePersistentIds({ 100 });
252     result = ssm_->CheckSessionPropertyOnRecovery(property, false);
253     ASSERT_EQ(result, WSError::WS_OK);
254 
255     // 特殊窗,不需要恢复
256     property->SetWindowType(WindowType::APP_SUB_WINDOW_BASE);
257     property->SetParentPersistentId(111);
258     result = ssm_->CheckSessionPropertyOnRecovery(property, true);
259     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_PARAM);
260 
261     // 特殊窗,需要恢复
262     ssm_->SetAlivePersistentIds({ 111 });
263     result = ssm_->CheckSessionPropertyOnRecovery(property, true);
264     ASSERT_EQ(result, WSError::WS_OK);
265 }
266 
267 /**
268  * @tc.name: RecoverAndReconnectSceneSession
269  * @tc.desc: RecoverAndReconnectSceneSession
270  * @tc.type: FUNC
271  */
272 HWTEST_F(WindowRecoverSessionTest, RecoverAndReconnectSceneSession, TestSize.Level1)
273 {
274     ASSERT_NE(ssm_, nullptr);
275     sptr<WindowSessionProperty> property = nullptr;
276     sptr<SessionStageMocker> sessionStage = nullptr;
277     sptr<TestWindowEventChannel> eventChannel = nullptr;
278     std::shared_ptr<RSSurfaceNode> surfaceNode = nullptr;
279     sptr<ISession> session = nullptr;
280     sptr<IRemoteObject> token = nullptr;
281     // 0.property 为空
282     auto result =
283         ssm_->RecoverAndReconnectSceneSession(sessionStage, eventChannel, surfaceNode, session, property, token);
284     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
285 
286     property = sptr<WindowSessionProperty>::MakeSptr();
287     property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
288     property->SetPersistentId(100);
289     ssm_->SetAlivePersistentIds({ 15, 16, 100 });
290     // 1.已经恢复结束
291     ssm_->recoveringFinished_ = true;
292     result = ssm_->RecoverAndReconnectSceneSession(sessionStage, eventChannel, surfaceNode, session, property, token);
293     EXPECT_EQ(result, WSError::WS_ERROR_INVALID_OPERATION);
294 
295     ssm_->recoveringFinished_ = false;
296     SessionInfo sessionInfo;
297     sessionInfo.abilityName_ = "testSceneSession1";
298     sessionInfo.moduleName_ = "testSceneSession2";
299     sessionInfo.bundleName_ = "testSceneSession3";
300     property->SetSessionInfo(sessionInfo);
301 
302     // 2.recoverSceneSessionFunc_ 为空
303     result = ssm_->RecoverAndReconnectSceneSession(sessionStage, eventChannel, surfaceNode, session, property, token);
304     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
305 
306     // 3. sessionStage为空
307     property->SetPersistentId(15);
308     ssm_->recoverSceneSessionFunc_ = NotifyRecoverSceneSessionFuncTest;
309     result = ssm_->RecoverAndReconnectSceneSession(sessionStage, eventChannel, surfaceNode, session, property, token);
310     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
311 
312     // 4.重连
313     sessionStage = sptr<SessionStageMocker>::MakeSptr();
314     EXPECT_NE(nullptr, sessionStage);
315     eventChannel = sptr<TestWindowEventChannel>::MakeSptr();
316     EXPECT_NE(nullptr, eventChannel);
317     surfaceNode = CreateRSSurfaceNode();
318     property->SetPersistentId(16);
319     property->SetWindowState(WindowState::STATE_SHOWN);
320     result = ssm_->RecoverAndReconnectSceneSession(sessionStage, eventChannel, surfaceNode, session, property, token);
321     EXPECT_EQ(result, WSError::WS_OK);
322 }
323 
324 /**
325  * @tc.name: RecoverAndConnectSpecificSession
326  * @tc.desc: RecoverAndConnectSpecificSession
327  * @tc.type: FUNC
328  */
329 HWTEST_F(WindowRecoverSessionTest, RecoverAndConnectSpecificSession, TestSize.Level1)
330 {
331     ASSERT_NE(ssm_, nullptr);
332     sptr<WindowSessionProperty> property = nullptr;
333     sptr<SessionStageMocker> sessionStage = nullptr;
334     sptr<TestWindowEventChannel> eventChannel = nullptr;
335     std::shared_ptr<RSSurfaceNode> surfaceNode = nullptr;
336     sptr<ISession> session = nullptr;
337     sptr<IRemoteObject> token = nullptr;
338     // 0. property 为空
339     auto result =
340         ssm_->RecoverAndConnectSpecificSession(sessionStage, eventChannel, surfaceNode, property, session, token);
341     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
342 
343     property = sptr<WindowSessionProperty>::MakeSptr();
344     property->SetWindowType(WindowType::APP_SUB_WINDOW_BASE);
345     property->SetParentPersistentId(111);
346     ssm_->SetAlivePersistentIds({ 111 });
347     // 1.已经恢复结束
348     ssm_->recoveringFinished_ = true;
349     result = ssm_->RecoverAndConnectSpecificSession(sessionStage, eventChannel, surfaceNode, property, session, token);
350     EXPECT_EQ(result, WSError::WS_ERROR_INVALID_OPERATION);
351 
352     ssm_->recoveringFinished_ = false;
353     SessionInfo sessionInfo;
354     sessionInfo.abilityName_ = "testSceneSession1";
355     sessionInfo.moduleName_ = "testSceneSession2";
356     sessionInfo.bundleName_ = "testSceneSession3";
357     property->SetSessionInfo(sessionInfo);
358 
359     // 2.info persistentId 无效
360     result = ssm_->RecoverAndConnectSpecificSession(sessionStage, eventChannel, surfaceNode, property, session, token);
361     EXPECT_EQ(result, WSError::WS_ERROR_INVALID_SESSION);
362 
363     // 3. sessionStage 为空
364     property->SetPersistentId(20);
365     result = ssm_->RecoverAndConnectSpecificSession(sessionStage, eventChannel, surfaceNode, property, session, token);
366     EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
367 
368     // 4.重连 窗口id:21,在前台
369     sessionStage = sptr<SessionStageMocker>::MakeSptr();
370     EXPECT_NE(nullptr, sessionStage);
371     eventChannel = sptr<TestWindowEventChannel>::MakeSptr();
372     EXPECT_NE(nullptr, eventChannel);
373     surfaceNode = CreateRSSurfaceNode();
374     property->SetPersistentId(21);
375     property->SetWindowState(WindowState::STATE_SHOWN);
376     result = ssm_->RecoverAndConnectSpecificSession(sessionStage, eventChannel, surfaceNode, property, session, token);
377     EXPECT_EQ(result, WSError::WS_OK);
378 }
379 
380 /**
381  * @tc.name: GetBatchAbilityInfos01
382  * @tc.desc: GetBatchAbilityInfos01
383  * @tc.type: FUNC
384  */
385 HWTEST_F(WindowRecoverSessionTest, GetBatchAbilityInfos01, TestSize.Level1)
386 {
387     ASSERT_NE(ssm_, nullptr);
388     auto bundleMgr = ssm_->bundleMgr_;
389     ssm_->bundleMgr_ = nullptr;
390     int32_t userId = 100;
391     std::vector<std::string> bundleNames = { "test1", "test2" };
392     auto scbAbilityInfos = std::make_shared<std::vector<SCBAbilityInfo>>();
393     WSError ret = ssm_->GetBatchAbilityInfos(bundleNames, userId, *scbAbilityInfos);
394     ASSERT_EQ(ret, WSError::WS_ERROR_NULLPTR);
395 }
396 
397 /**
398  * @tc.name: GetBatchAbilityInfos02
399  * @tc.desc: GetBatchAbilityInfos02
400  * @tc.type: FUNC
401  */
402 HWTEST_F(WindowRecoverSessionTest, GetBatchAbilityInfos02, TestSize.Level1)
403 {
404     ASSERT_NE(ssm_, nullptr);
405     sptr<IRemoteObject> iRemoteObjectMocker = sptr<IRemoteObjectMocker>::MakeSptr();
406     ssm_->bundleMgr_ = sptr<AppExecFwk::BundleMgrProxy>::MakeSptr(iRemoteObjectMocker);
407     int32_t userId = 100;
408     std::vector<std::string> bundleNames = {};
409     auto scbAbilityInfos = std::make_shared<std::vector<SCBAbilityInfo>>();
410     WSError ret = ssm_->GetBatchAbilityInfos(bundleNames, userId, *scbAbilityInfos);
411     ASSERT_EQ(ret, WSError::WS_ERROR_INVALID_PARAM);
412 }
413 
414 /**
415  * @tc.name: GetBatchAbilityInfos03
416  * @tc.desc: GetBatchAbilityInfos03
417  * @tc.type: FUNC
418  */
419 HWTEST_F(WindowRecoverSessionTest, GetBatchAbilityInfos03, TestSize.Level1)
420 {
421     ASSERT_NE(ssm_, nullptr);
422     sptr<IRemoteObject> iRemoteObjectMocker = sptr<IRemoteObjectMocker>::MakeSptr();
423     ssm_->bundleMgr_ = sptr<AppExecFwk::BundleMgrProxy>::MakeSptr(iRemoteObjectMocker);
424     int32_t userId = 100;
425     std::vector<std::string> bundleNames = { "" };
426     auto scbAbilityInfos = std::make_shared<std::vector<SCBAbilityInfo>>();
427     WSError ret = ssm_->GetBatchAbilityInfos(bundleNames, userId, *scbAbilityInfos);
428     ASSERT_EQ(ret, WSError::WS_ERROR_INVALID_PARAM);
429 }
430 
431 /**
432  * @tc.name: GetBatchAbilityInfos04
433  * @tc.desc: GetBatchAbilityInfos04
434  * @tc.type: FUNC
435  */
436 HWTEST_F(WindowRecoverSessionTest, GetBatchAbilityInfos04, TestSize.Level1)
437 {
438     ASSERT_NE(ssm_, nullptr);
439     sptr<IRemoteObject> iRemoteObjectMocker = sptr<IRemoteObjectMocker>::MakeSptr();
440     ssm_->bundleMgr_ = sptr<AppExecFwk::BundleMgrProxy>::MakeSptr(iRemoteObjectMocker);
441     int32_t userId = 100;
442     std::vector<std::string> bundleNames = { "test1", "test2" };
443     auto scbAbilityInfos = std::make_shared<std::vector<SCBAbilityInfo>>();
444     WSError ret = ssm_->GetBatchAbilityInfos(bundleNames, userId, *scbAbilityInfos);
445     ASSERT_EQ(ret, WSError::WS_ERROR_INVALID_PARAM);
446 }
447 
448 /**
449  * @tc.name: IsWindowSupportCacheForRecovering
450  * @tc.desc: test function : IsWindowSupportCacheForRecovering
451  * @tc.type: FUNC
452  */
453 HWTEST_F(WindowRecoverSessionTest, IsWindowSupportCacheForRecovering, TestSize.Level1)
454 {
455     std::vector<int32_t> recoveredPersistentIds = { 1 };
456     ssm_->alivePersistentIds_.clear();
457     ssm_->alivePersistentIds_.push_back(1);
458     ssm_->alivePersistentIds_.push_back(2);
459     ssm_->alivePersistentIds_.push_back(3);
460     SessionInfo info;
461     info.bundleName_ = "IsWindowSupportCacheForRecovering";
462     info.abilityName_ = "IsWindowSupportCacheForRecovering";
463     sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
464     ASSERT_NE(sceneSession, nullptr);
465     sceneSession->isRecovered_ = true;
466     ssm_->sceneSessionMap_.insert(std::make_pair(2, sceneSession));
467     sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(info, nullptr);
468     ASSERT_NE(sceneSession1, nullptr);
469     sceneSession1->isRecovered_ = false;
470     ssm_->sceneSessionMap_.insert(std::make_pair(3, sceneSession1));
471     sptr<SceneSession> sceneSession2 = nullptr;
472     ssm_->sceneSessionMap_.insert(std::make_pair(4, sceneSession2));
473     ssm_->ClearUnrecoveredSessions(recoveredPersistentIds);
474 
475     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
476     ASSERT_NE(property, nullptr);
477     ssm_->recoveringFinished_ = true;
478     auto ret = ssm_->IsWindowSupportCacheForRecovering(sceneSession, property);
479     EXPECT_EQ(false, ret);
480 }
481 
482 /**
483  * @tc.name: IsWindowSupportCacheForRecovering01
484  * @tc.desc: test function : IsWindowSupportCacheForRecovering
485  * @tc.type: FUNC
486  */
487 HWTEST_F(WindowRecoverSessionTest, IsWindowSupportCacheForRecovering01, TestSize.Level1)
488 {
489     std::vector<int32_t> windowIds = { 0, 1 };
490     sptr<SceneSession> sceneSession = nullptr;
491     ssm_->sceneSessionMap_.clear();
492     ssm_->sceneSessionMap_.insert(std::make_pair(0, sceneSession));
493     ssm_->OnNotifyAboveLockScreen(windowIds);
494 
495     SessionInfo info;
496     info.bundleName_ = "IsWindowSupportCacheForRecovering01";
497     info.abilityName_ = "IsWindowSupportCacheForRecovering01";
498     sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(info, nullptr);
499     ASSERT_NE(sceneSession1, nullptr);
500     ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession1));
501     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
502     ASSERT_NE(property, nullptr);
503     ssm_->recoveringFinished_ = false;
504     property->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
505     auto ret = ssm_->IsWindowSupportCacheForRecovering(sceneSession1, property);
506     EXPECT_EQ(true, ret);
507     property->SetWindowType(WindowType::APP_SUB_WINDOW_END);
508     ret = ssm_->IsWindowSupportCacheForRecovering(sceneSession1, property);
509     EXPECT_EQ(false, ret);
510     property->SetWindowType(WindowType::APP_SUB_WINDOW_BASE);
511     ret = ssm_->IsWindowSupportCacheForRecovering(sceneSession1, property);
512     EXPECT_EQ(true, ret);
513 }
514 
515 /**
516  * @tc.name: IsWindowSupportCacheForRecovering02
517  * @tc.desc: test function : IsWindowSupportCacheForRecovering
518  * @tc.type: FUNC
519  */
520 HWTEST_F(WindowRecoverSessionTest, IsWindowSupportCacheForRecovering02, TestSize.Level1)
521 {
522     SessionInfo info;
523     info.bundleName_ = "IsWindowSupportCacheForRecovering02";
524     info.abilityName_ = "IsWindowSupportCacheForRecovering02";
525     sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
526     ASSERT_NE(sceneSession, nullptr);
527     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
528     ASSERT_NE(property, nullptr);
529     ssm_->recoveringFinished_ = false;
530     property->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
531     property->parentPersistentId_ = 1;
__anona042a7470202(const sptr<SceneSession>& sceneSession) 532     NotifyBindDialogSessionFunc func = [](const sptr<SceneSession>& sceneSession) {};
533     ssm_->bindDialogTargetFuncMap_.insert(std::make_pair(1, func));
534     auto ret = ssm_->IsWindowSupportCacheForRecovering(sceneSession, property);
535     EXPECT_EQ(false, ret);
536 }
537 
538 /**
539  * @tc.name: CacheSpecificSessionForRecovering
540  * @tc.desc: CacheSpecificSessionForRecovering
541  * @tc.type: FUNC
542  */
543 HWTEST_F(WindowRecoverSessionTest, CacheSpecificSessionForRecovering, TestSize.Level1)
544 {
545     sptr<WindowSessionProperty> property;
546     ASSERT_NE(ssm_, nullptr);
547     ssm_->recoveringFinished_ = false;
548     SessionInfo info;
549     info.abilityName_ = "test1";
550     info.bundleName_ = "test2";
551     sptr<SceneSession> sceneSession = ssm_->CreateSceneSession(info, property);
552     ASSERT_NE(sceneSession, nullptr);
553     ssm_->CacheSpecificSessionForRecovering(nullptr, property);
554     ssm_->CacheSpecificSessionForRecovering(sceneSession, property);
555 
556     property = sptr<WindowSessionProperty>::MakeSptr();
557     ASSERT_NE(property, nullptr);
558     ssm_->CacheSpecificSessionForRecovering(nullptr, property);
559     ssm_->CacheSpecificSessionForRecovering(sceneSession, property);
560     property->SetWindowType(WindowType::APP_WINDOW_BASE);
561     ssm_->CacheSpecificSessionForRecovering(sceneSession, property);
562     property->SetWindowType(WindowType::APP_SUB_WINDOW_BASE);
563     ssm_->CacheSpecificSessionForRecovering(sceneSession, property);
564     int32_t parentPersistentId = 1;
565     property->SetParentPersistentId(parentPersistentId);
566     ssm_->CacheSpecificSessionForRecovering(sceneSession, property);
567     ASSERT_EQ(ssm_->recoverSubSessionCacheMap_[parentPersistentId].size(), 1);
568     ssm_->CacheSpecificSessionForRecovering(sceneSession, property);
569     ASSERT_EQ(ssm_->recoverSubSessionCacheMap_[parentPersistentId].size(), 2);
570     ssm_->RecoverCachedSubSession(parentPersistentId);
571     ASSERT_EQ(ssm_->recoverSubSessionCacheMap_[parentPersistentId].size(), 0);
572     ssm_->recoverSubSessionCacheMap_.clear();
573 }
574 
575 HWTEST_F(WindowRecoverSessionTest, RecoverCachedSubSession, TestSize.Level1)
576 {
577     ASSERT_NE(ssm_, nullptr);
578     // 1. 没有缓存的子窗
579     ssm_->RecoverCachedSubSession(123);
580     ASSERT_EQ(ssm_->recoverSubSessionCacheMap_.size(), 0);
581 
582     // 2. 有缓存的子窗,进行恢复并清理CacheMap_
583     SessionInfo info;
584     info.bundleName_ = "SubSession";
585     info.abilityName_ = "SubSession";
586     sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
587     ssm_->recoverSubSessionCacheMap_[123].emplace_back(sceneSession);
588     ssm_->RecoverCachedSubSession(123);
589     ASSERT_EQ(ssm_->recoverSubSessionCacheMap_.size(), 0);
590 }
591 
592 HWTEST_F(WindowRecoverSessionTest, RecoverCachedDialogSession, TestSize.Level1)
593 {
594     ASSERT_NE(ssm_, nullptr);
595     // 1. 没有缓存的模态窗
596     ssm_->RecoverCachedDialogSession(123);
597     ASSERT_EQ(ssm_->recoverDialogSessionCacheMap_.size(), 0);
598 
599     // 2. 有缓存的模态窗,进行恢复并清理CacheMap_
600     SessionInfo info;
601     info.bundleName_ = "SubSession";
602     info.abilityName_ = "SubSession";
603     sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
604     ssm_->recoverDialogSessionCacheMap_[123].emplace_back(sceneSession);
605     ssm_->RecoverCachedDialogSession(123);
606     ASSERT_EQ(ssm_->recoverDialogSessionCacheMap_.size(), 0);
607 }
608 
609 /**
610  * @tc.name: RemoveFailRecoveredSession
611  * @tc.desc: RemoveFailRecoveredSession
612  * @tc.type: FUNC
613  */
614 HWTEST_F(WindowRecoverSessionTest, RemoveFailRecoveredSession, TestSize.Level1)
615 {
616     ASSERT_NE(nullptr, ssm_);
617     SessionInfo info;
618     info.abilityName_ = "RemoveFailRecoveredSession";
619     sptr<SceneSession> sceneSession01 = sptr<SceneSession>::MakeSptr(info, nullptr);
620     sptr<SceneSession> sceneSession02 = sptr<SceneSession>::MakeSptr(info, nullptr);
621     ASSERT_NE(sceneSession01, nullptr);
622     ASSERT_NE(sceneSession02, nullptr);
623     ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession01));
624     ssm_->sceneSessionMap_.insert(std::make_pair(2, nullptr));
625     ssm_->sceneSessionMap_.insert(std::make_pair(3, sceneSession02));
626     ssm_->failRecoveredPersistentIdSet_.insert(1);
627     ssm_->failRecoveredPersistentIdSet_.insert(2);
628     ssm_->failRecoveredPersistentIdSet_.insert(3);
629     ssm_->RemoveFailRecoveredSession();
630     EXPECT_EQ(ssm_->failRecoveredPersistentIdSet_.size(), 0);
631     EXPECT_EQ(WSError::WS_ERROR_INVALID_SESSION, ssm_->HandleSecureSessionShouldHide(nullptr));
632 }
633 
634 /**
635  * @tc.name: ClearUnrecoveredSessions
636  * @tc.desc: test func ClearUnrecoveredSessions
637  * @tc.type: FUNC
638  */
639 HWTEST_F(WindowRecoverSessionTest, ClearUnrecoveredSessions, TestSize.Level1)
640 {
641     ssm_->alivePersistentIds_.push_back(23);
642     ssm_->alivePersistentIds_.push_back(24);
643     ssm_->alivePersistentIds_.push_back(25);
644     ssm_->alivePersistentIds_.push_back(26);
645     ssm_->alivePersistentIds_.push_back(27);
646     EXPECT_FALSE(ssm_->alivePersistentIds_.empty());
647     std::vector<int32_t> recoveredPersistentIds;
648     recoveredPersistentIds.push_back(23);
649     recoveredPersistentIds.push_back(24);
650     SessionInfo info;
651     info.abilityName_ = "ClearUnrecoveredSessions";
652     sptr<SceneSession> sceneSession01 = sptr<SceneSession>::MakeSptr(info, nullptr);
653     sptr<SceneSession> sceneSession02 = sptr<SceneSession>::MakeSptr(info, nullptr);
654     sceneSession01->SetRecovered(false);
655     sceneSession02->SetRecovered(true);
656     ASSERT_NE(sceneSession01, nullptr);
657     ASSERT_NE(sceneSession02, nullptr);
658     ssm_->sceneSessionMap_.insert(std::make_pair(26, sceneSession01));
659     ssm_->sceneSessionMap_.insert(std::make_pair(27, sceneSession02));
660     ssm_->ClearUnrecoveredSessions(recoveredPersistentIds);
661     EXPECT_EQ(ssm_->alivePersistentIds_.size(), 8);
662 }
663 
664 /**
665  * @tc.name: RecoverSessionInfo
666  * @tc.desc: test func RecoverSessionInfo
667  * @tc.type: FUNC
668  */
669 HWTEST_F(WindowRecoverSessionTest, RecoverSessionInfo, TestSize.Level1)
670 {
671     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
672     ASSERT_NE(nullptr, property);
673     SessionInfo sessionInfo;
674     sessionInfo.abilityName_ = "RecoverSessionInfo";
675     property->SetSessionInfo(sessionInfo);
676     ssm_->RecoverSessionInfo(property);
677     SessionInfo info = property->GetSessionInfo();
678     ASSERT_EQ(info.abilityName_, sessionInfo.abilityName_);
679 }
680 
681 /**
682  * @tc.name: UpdateRecoverPropertyForSuperFold
683  * @tc.desc: normal function
684  * @tc.type: FUNC
685  */
686 HWTEST_F(WindowRecoverSessionTest, UpdateRecoverPropertyForSuperFold, TestSize.Level1)
687 {
688     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
689     Rect rect = {
690         static_cast<int32_t>(10), static_cast<int32_t>(10), static_cast<uint32_t>(100), static_cast<uint32_t>(100)
691     };
692     property->SetWindowRect(rect);
693     property->SetRequestRect(rect);
694     property->SetDisplayId(0);
695     ssm_->UpdateRecoverPropertyForSuperFold(property);
696     EXPECT_EQ(property->GetWindowRect().posY_, 10);
697 
698     property->SetDisplayId(999);
699     ssm_->UpdateRecoverPropertyForSuperFold(property);
700     EXPECT_EQ(property->GetWindowRect().posX_, 10);
701 }
702 
703 /**
704  * @tc.name: RecoverSessionManagerService
705  * @tc.desc: normal function
706  * @tc.type: FUNC
707  */
708 HWTEST_F(WindowRecoverSessionTest, RecoverSessionManagerService, TestSize.Level1)
709 {
710     ASSERT_NE(nullptr, sm_);
711     bool funcInvoked = false;
712     sm_->RegisterWindowManagerRecoverCallbackFunc(nullptr);
713     sm_->RecoverSessionManagerService(nullptr);
714     ASSERT_EQ(funcInvoked, false);
715 
__anona042a7470302() 716     sm_->RegisterWindowManagerRecoverCallbackFunc([&]() { funcInvoked = true; });
717     sm_->RecoverSessionManagerService(nullptr);
718     ASSERT_EQ(funcInvoked, true);
719 }
720 
721 /**
722  * @tc.name: RegisterSMSRecoverListener1
723  * @tc.desc: mockSessionManagerServiceProxy_ is null
724  * @tc.type: FUNC
725  */
726 HWTEST_F(WindowRecoverSessionTest, RegisterSMSRecoverListener1, TestSize.Level1)
727 {
728     ASSERT_NE(nullptr, sm_);
729     sm_->isRecoverListenerRegistered_ = false;
730     sm_->mockSessionManagerServiceProxy_ = nullptr;
731     sm_->RegisterSMSRecoverListener();
732     ASSERT_EQ(sm_->isRecoverListenerRegistered_, false);
733 }
734 
735 /**
736  * @tc.name: RegisterSMSRecoverListener2
737  * @tc.desc: normal test
738  * @tc.type: FUNC
739  */
740 HWTEST_F(WindowRecoverSessionTest, RegisterSMSRecoverListener2, TestSize.Level1)
741 {
742     ASSERT_NE(nullptr, sm_);
743     sm_->isRecoverListenerRegistered_ = false;
744     sm_->InitMockSMSProxy();
745     sm_->RegisterSMSRecoverListener();
746     ASSERT_EQ(sm_->isRecoverListenerRegistered_, true);
747 }
748 
749 /**
750  * @tc.name: RegisterSessionRecoverStateChangeListener
751  * @tc.desc: normal test
752  * @tc.type: FUNC
753  */
754 HWTEST_F(WindowRecoverSessionTest, RegisterSessionRecoverStateChangeListener, TestSize.Level1)
755 {
756     EXPECT_NE(nullptr, ssm_);
757     ssm_->RegisterSessionRecoverStateChangeListener();
758     EXPECT_NE(ssm_->sessionRecoverStateChangeFunc_, nullptr);
759 }
760 
761 /**
762  * @tc.name: OnSessionRecoverStateChange
763  * @tc.desc: normal test
764  * @tc.type: FUNC
765  */
766 HWTEST_F(WindowRecoverSessionTest, OnSessionRecoverStateChange, TestSize.Level1)
767 {
768     EXPECT_NE(nullptr, ssm_);
769     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
770     property->SetPersistentId(160);
771     ssm_->OnSessionRecoverStateChange(SessionRecoverState::SESSION_START_RECONNECT, property);
772     EXPECT_EQ(property->GetSessionInfo().persistentId_, 160);
773 
774     ssm_->sceneSessionMap_.clear();
775     // main window
776     property->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
777     SessionInfo info;
778     info.abilityName_ = "OnSessionRecoverStateChange";
779     info.bundleName_ = "OnSessionRecoverStateChange";
780     sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
781     sceneSession->SetSessionProperty(property);
782     ASSERT_NE(nullptr, sceneSession);
783     ssm_->sceneSessionMap_.insert({ 160, sceneSession });
784     ssm_->OnSessionRecoverStateChange(SessionRecoverState::SESSION_FINISH_RECONNECT, property);
785     EXPECT_EQ(ssm_->GetSceneSession(160)->IsRecovered(), true);
786 
787     // specifc window
788     ssm_->GetSceneSession(160)->SetRecovered(false);
789     property->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
790     sceneSession->SetSessionProperty(property);
791     ssm_->OnSessionRecoverStateChange(SessionRecoverState::SESSION_FINISH_RECONNECT, property);
792     EXPECT_EQ(ssm_->GetSceneSession(160)->IsRecovered(), false);
793 
794     ssm_->OnSessionRecoverStateChange(SessionRecoverState::SESSION_NOT_RECONNECT, property);
795     EXPECT_EQ(ssm_->GetSceneSession(160)->IsRecovered(), false);
796 }
797 
798 /**
799  * @tc.name: RegisterRecoverStateChangeListener
800  * @tc.desc: normal test
801  * @tc.type: FUNC
802  */
803 HWTEST_F(WindowRecoverSessionTest, RegisterRecoverStateChangeListener, TestSize.Level1)
804 {
805     EXPECT_NE(nullptr, ssm_);
806     ssm_->RegisterRecoverStateChangeListener();
807     EXPECT_NE(ssm_->recoverStateChangeFunc_, nullptr);
808 }
809 
810 /**
811  * @tc.name: OnRecoverStateChange
812  * @tc.desc: normal test
813  * @tc.type: FUNC
814  */
815 HWTEST_F(WindowRecoverSessionTest, OnRecoverStateChange, TestSize.Level1)
816 {
817     EXPECT_NE(nullptr, ssm_);
818     ssm_->displayBrightness_ = UNDEFINED_BRIGHTNESS;
819     ssm_->OnRecoverStateChange(RecoverState::RECOVER_INITIAL);
820     EXPECT_EQ(ssm_->displayBrightness_, UNDEFINED_BRIGHTNESS);
821     ssm_->OnRecoverStateChange(RecoverState::RECOVER_END);
822     EXPECT_EQ(ssm_->displayBrightness_, UNDEFINED_BRIGHTNESS);
823     ssm_->OnRecoverStateChange(RecoverState::RECOVER_ENABLE_INPUT);
824     EXPECT_EQ(ssm_->displayBrightness_, INVALID_BRIGHTNESS);
825 }
826 
827 /**
828  * @tc.name: SetEnableInputEvent
829  * @tc.desc: normal test
830  * @tc.type: FUNC
831  */
832 HWTEST_F(WindowRecoverSessionTest, SetEnableInputEvent, TestSize.Level1)
833 {
834     EXPECT_NE(nullptr, ssm_);
835     ssm_->displayBrightness_ = UNDEFINED_BRIGHTNESS;
836     ssm_->SetEnableInputEvent(false);
837     EXPECT_EQ(ssm_->displayBrightness_, UNDEFINED_BRIGHTNESS);
838     ssm_->SetEnableInputEvent(true);
839     EXPECT_EQ(ssm_->displayBrightness_, INVALID_BRIGHTNESS);
840 }
841 
842 } // namespace
843 } // namespace Rosen
844 } // namespace OHOS