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 
18 #include <bundlemgr/launcher_service.h>
19 #include "interfaces/include/ws_common.h"
20 #include "session_manager/include/scene_session_manager.h"
21 #include "session_info.h"
22 #include "session/host/include/scene_session.h"
23 #include "session_manager.h"
24 #include "session/host/include/scene_session.h"
25 #include "mock/mock_ibundle_mgr.h"
26 #include "common/include/task_scheduler.h"
27 #include "session/host/include/multi_instance_manager.h"
28 
29 using namespace testing;
30 using namespace testing::ext;
31 
32 namespace OHOS {
33 namespace Rosen {
34 namespace {
35     const std::string BUNDLE_NAME = "bundleName";
36     const int32_t USER_ID { 100 };
37     const int32_t SLEEP_TIME { 10000 };
38 }
39 class SceneSessionManagerTest11 : public testing::Test {
40 public:
41     static void SetUpTestCase();
42     static void TearDownTestCase();
43     void SetUp() override;
44     void TearDown() override;
45 
46     static sptr<SceneSessionManager> ssm_;
47 private:
48     sptr<SceneSession> GetSceneSession(const std::string& instanceKey = "");
49     void Init(AppExecFwk::MultiAppModeType modeType, uint32_t maxCount);
50     std::shared_ptr<TaskScheduler> GetTaskScheduler();
51 };
52 
53 sptr<SceneSessionManager> SceneSessionManagerTest11::ssm_ = nullptr;
54 
SetUpTestCase()55 void SceneSessionManagerTest11::SetUpTestCase()
56 {
57     ssm_ = &SceneSessionManager::GetInstance();
58     ssm_->systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
59 }
60 
TearDownTestCase()61 void SceneSessionManagerTest11::TearDownTestCase()
62 {
63     ssm_ = nullptr;
64 }
65 
SetUp()66 void SceneSessionManagerTest11::SetUp()
67 {
68 }
69 
TearDown()70 void SceneSessionManagerTest11::TearDown()
71 {
72     sptr<IBundleMgrMocker> bundleMgrMocker = sptr<IBundleMgrMocker>::MakeSptr();
73     EXPECT_CALL(*bundleMgrMocker, GetApplicationInfo(_, _, _, _)).WillOnce(Return(false));
74     MultiInstanceManager::GetInstance().Init(bundleMgrMocker, GetTaskScheduler());
75     ssm_->RefreshAppInfo(BUNDLE_NAME);
76     usleep(SLEEP_TIME);
77 }
78 
GetSceneSession(const std::string& instanceKey)79 sptr<SceneSession> SceneSessionManagerTest11::GetSceneSession(const std::string& instanceKey)
80 {
81     SessionInfo info;
82     info.bundleName_ = BUNDLE_NAME;
83     info.appInstanceKey_ = instanceKey;
84     info.isNewAppInstance_ = true;
85     sptr<SceneSession::SpecificSessionCallback> specificCb = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
86     sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, specificCb);
87     return sceneSession;
88 }
89 
Init(AppExecFwk::MultiAppModeType modeType, uint32_t maxCount)90 void SceneSessionManagerTest11::Init(AppExecFwk::MultiAppModeType modeType, uint32_t maxCount)
91 {
92     sptr<IBundleMgrMocker> bundleMgrMocker = sptr<IBundleMgrMocker>::MakeSptr();
93     EXPECT_CALL(*bundleMgrMocker, GetApplicationInfos(_, _, _)).WillOnce([modeType, maxCount](
94         const AppExecFwk::ApplicationFlag flag, const int32_t userId,
95         std::vector<AppExecFwk::ApplicationInfo>& appInfos) {
96         AppExecFwk::ApplicationInfo appInfo;
97         appInfo.bundleName = BUNDLE_NAME;
98         appInfo.multiAppMode.multiAppModeType = modeType;
99         appInfo.multiAppMode.maxCount = maxCount;
100         appInfos.push_back(appInfo);
101         return true;
102     });
103     MultiInstanceManager::GetInstance().Init(bundleMgrMocker, GetTaskScheduler());
104     MultiInstanceManager::GetInstance().SetCurrentUserId(USER_ID);
105     usleep(SLEEP_TIME);
106 }
107 
GetTaskScheduler()108 std::shared_ptr<TaskScheduler> SceneSessionManagerTest11::GetTaskScheduler()
109 {
110     std::string threadName = "threadName";
111     std::shared_ptr<TaskScheduler> taskScheduler = std::make_shared<TaskScheduler>(threadName);
112     return taskScheduler;
113 }
114 
115 namespace {
116 /**
117  * @tc.name: GetMainWindowStatesByPid
118  * @tc.desc: SceneSesionManager get main window states by pid
119  * @tc.type: FUNC
120  */
HWTEST_F(SceneSessionManagerTest11, GetMainWindowStatesByPid, Function | SmallTest | Level3)121 HWTEST_F(SceneSessionManagerTest11, GetMainWindowStatesByPid, Function | SmallTest | Level3)
122 {
123     int32_t pid = 100;
124     std::vector<MainWindowState> windowStates;
125     WSError result = ssm_->GetMainWindowStatesByPid(pid, windowStates);
126     EXPECT_EQ(result, WSError::WS_OK);
127 }
128 
129 /**
130  * @tc.name: GetMainWindowStatesByPid02
131  * @tc.desc: SceneSesionManager get main window states by pid
132  * @tc.type: FUNC
133  */
HWTEST_F(SceneSessionManagerTest11, GetMainWindowStatesByPid02, Function | SmallTest | Level3)134 HWTEST_F(SceneSessionManagerTest11, GetMainWindowStatesByPid02, Function | SmallTest | Level3)
135 {
136     int32_t invalidPid = -1;
137     std::vector<MainWindowState> windowStates;
138     WSError result = ssm_->GetMainWindowStatesByPid(invalidPid, windowStates);
139     EXPECT_EQ(result, WSError::WS_ERROR_INVALID_PARAM);
140 }
141 
142 /**
143  * @tc.name: GetMainWindowStatesByPid03
144  * @tc.desc: SceneSesionManager get main window states by pid
145  * @tc.type: FUNC
146  */
HWTEST_F(SceneSessionManagerTest11, GetMainWindowStatesByPid03, Function | SmallTest | Level3)147 HWTEST_F(SceneSessionManagerTest11, GetMainWindowStatesByPid03, Function | SmallTest | Level3)
148 {
149     SessionState sessionState = SessionState::STATE_FOREGROUND;
150     bool isVisible = true;
151     bool isForegroundInteractive = true;
152     bool isPcOrPadEnableActivation = true;
153     int32_t callingPid = 1001;
154     SessionInfo sessionInfo;
155     int32_t persistentId = 1005;
156     sptr<SceneSession> sceneSession = new (std::nothrow) SceneSession(sessionInfo, nullptr);
157     EXPECT_NE(sceneSession, nullptr);
158     sceneSession->SetSessionState(sessionState);
159     sceneSession->SetRSVisible(isVisible);
160     sceneSession->SetForegroundInteractiveStatus(isForegroundInteractive);
161     sceneSession->GetSessionProperty()->SetIsPcAppInPad(isPcOrPadEnableActivation);
162     sceneSession->SetCallingPid(callingPid);
163     ssm_->sceneSessionMap_.insert({ persistentId, sceneSession });
164     std::vector<MainWindowState> windowStates;
165     WSError result = ssm_->GetMainWindowStatesByPid(callingPid, windowStates);
166     EXPECT_EQ(result, WSError::WS_OK);
167     EXPECT_EQ(windowStates.size(), 1);
168     EXPECT_EQ(windowStates[0].state_, static_cast<int32_t>(sessionState));
169     EXPECT_EQ(windowStates[0].isVisible_, isVisible);
170     EXPECT_EQ(windowStates[0].isForegroundInteractive_, isForegroundInteractive);
171     EXPECT_EQ(windowStates[0].isPcOrPadEnableActivation_, isPcOrPadEnableActivation);
172 }
173 
174 /**
175  * @tc.name: GetMaxInstanceCount
176  * @tc.desc: test function : GetMaxInstanceCount
177  * @tc.type: FUNC
178  */
HWTEST_F(SceneSessionManagerTest11, GetMaxInstanceCount, Function | SmallTest | Level1)179 HWTEST_F(SceneSessionManagerTest11, GetMaxInstanceCount, Function | SmallTest | Level1)
180 {
181     AppExecFwk::MultiAppModeType modeType = AppExecFwk::MultiAppModeType::MULTI_INSTANCE;
182     uint32_t maxCount = 5;
183     Init(modeType, maxCount);
184     ASSERT_EQ(ssm_->GetMaxInstanceCount(BUNDLE_NAME), maxCount);
185 }
186 
187 /**
188  * @tc.name: GetInstanceCount
189  * @tc.desc: test function : GetInstanceCount
190  * @tc.type: FUNC
191  */
HWTEST_F(SceneSessionManagerTest11, GetInstanceCount, Function | SmallTest | Level1)192 HWTEST_F(SceneSessionManagerTest11, GetInstanceCount, Function | SmallTest | Level1)
193 {
194     AppExecFwk::MultiAppModeType modeType = AppExecFwk::MultiAppModeType::MULTI_INSTANCE;
195     uint32_t maxCount = 5;
196     Init(modeType, maxCount);
197     ASSERT_EQ(ssm_->GetInstanceCount(BUNDLE_NAME), 0);
198     std::string instanceKey0 = "app_instance_0";
199     sptr<SceneSession> sceneSession = GetSceneSession(instanceKey0);
200     ASSERT_EQ(MultiInstanceManager::GetInstance().CreateNewInstanceKey(BUNDLE_NAME), instanceKey0);
201     MultiInstanceManager::GetInstance().IncreaseInstanceKeyRefCount(sceneSession);
202     ASSERT_EQ(ssm_->GetInstanceCount(BUNDLE_NAME), 1);
203     MultiInstanceManager::GetInstance().DecreaseInstanceKeyRefCount(sceneSession);
204     ASSERT_EQ(ssm_->GetInstanceCount(BUNDLE_NAME), 0);
205 }
206 
207 /**
208  * @tc.name: GetLastInstanceKey
209  * @tc.desc: test function : GetLastInstanceKey
210  * @tc.type: FUNC
211  */
HWTEST_F(SceneSessionManagerTest11, GetLastInstanceKey, Function | SmallTest | Level1)212 HWTEST_F(SceneSessionManagerTest11, GetLastInstanceKey, Function | SmallTest | Level1)
213 {
214     AppExecFwk::MultiAppModeType modeType = AppExecFwk::MultiAppModeType::MULTI_INSTANCE;
215     uint32_t maxCount = 5;
216     Init(modeType, maxCount);
217     ASSERT_EQ(ssm_->GetLastInstanceKey(BUNDLE_NAME), "");
218     std::string instanceKey0 = "app_instance_0";
219     sptr<SceneSession> sceneSession = GetSceneSession(instanceKey0);
220     ASSERT_EQ(MultiInstanceManager::GetInstance().CreateNewInstanceKey(BUNDLE_NAME), instanceKey0);
221     MultiInstanceManager::GetInstance().IncreaseInstanceKeyRefCount(sceneSession);
222     ASSERT_EQ(ssm_->GetLastInstanceKey(BUNDLE_NAME), instanceKey0);
223     MultiInstanceManager::GetInstance().DecreaseInstanceKeyRefCount(sceneSession);
224     ASSERT_EQ(ssm_->GetLastInstanceKey(BUNDLE_NAME), "");
225 }
226 
227 /**
228  * @tc.name: UpdateOccupiedAreaIfNeed
229  * @tc.desc: SceneSesionManager update occupiedArea
230  * @tc.type: FUNC
231  */
HWTEST_F(SceneSessionManagerTest11, UpdateOccupiedAreaIfNeed, Function | SmallTest | Level1)232 HWTEST_F(SceneSessionManagerTest11, UpdateOccupiedAreaIfNeed, Function | SmallTest | Level1)
233 {
234     int ret = 0;
235     int32_t persistentId = 0;
236     SessionInfo info;
237     info.abilityName_ = "test1";
238     info.bundleName_ = "test2";
239     info.moduleName_ = "test3";
240     info.persistentId_ = 1;
241     sptr<SceneSession> sceneSession = new (std::nothrow) SceneSession(info, nullptr);
242     ASSERT_NE(nullptr, sceneSession);
243     ssm_->sceneSessionMap_.insert({1, sceneSession});
244     ssm_->UpdateOccupiedAreaIfNeed(persistentId);
245 
246     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
247     ASSERT_NE(nullptr, property);
248     property->SetWindowType(WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT);
249     sceneSession->SetSessionProperty(property);
250     ssm_->UpdateOccupiedAreaIfNeed(persistentId);
251 
252     persistentId = 1;
253     ssm_->UpdateOccupiedAreaIfNeed(persistentId);
254 
255     ssm_->sceneSessionMap_.erase(1);
256     ASSERT_EQ(ret, 0);
257 }
258 
259 /**
260  * @tc.name: GetAllSessionDumpDetailInfo
261  * @tc.desc: SceneSesionManager test GetAllSessionDumpDetailInfo
262  * @tc.type: FUNC
263  */
HWTEST_F(SceneSessionManagerTest11, GetAllSessionDumpDetailInfo, Function | SmallTest | Level1)264 HWTEST_F(SceneSessionManagerTest11, GetAllSessionDumpDetailInfo, Function | SmallTest | Level1)
265 {
266     SessionInfo info1;
267     info1.abilityName_ = "GetAllSessionDumpDetailInfo1";
268     info1.bundleName_ = "GetAllSessionDumpDetailInfo1";
269     info1.persistentId_ = 1;
270     sptr<SceneSession> sceneSession1 = new (std::nothrow) SceneSession(info1, nullptr);
271     ASSERT_NE(sceneSession1, nullptr);
272     sceneSession1->UpdateNativeVisibility(true);
273 
274     SessionInfo info2;
275     info2.abilityName_ = "GetAllSessionDumpDetailInfo2";
276     info2.bundleName_ = "GetAllSessionDumpDetailInfo2";
277     info2.persistentId_ = 2;
278     sptr<SceneSession> sceneSession2 = new (std::nothrow) SceneSession(info2, nullptr);
279     ASSERT_NE(sceneSession2, nullptr);
280     sceneSession2->UpdateNativeVisibility(false);
281 
282     ssm_->sceneSessionMap_.insert({0, nullptr});
283     ssm_->sceneSessionMap_.insert({1, sceneSession1});
284     ssm_->sceneSessionMap_.insert({2, sceneSession2});
285     std::string dumpInfo;
286     ASSERT_EQ(ssm_->GetAllSessionDumpDetailInfo(dumpInfo), WSError::WS_OK);
287 }
288 }  // namespace
289 }
290 }