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 <regex>
18 #include <bundle_mgr_interface.h>
19 #include <bundlemgr/launcher_service.h>
20 #include "iremote_object_mocker.h"
21 #include "interfaces/include/ws_common.h"
22 #include "screen_fold_data.h"
23 #include "session_manager/include/scene_session_manager.h"
24 #include "session_info.h"
25 #include "session/host/include/scene_session.h"
26 #include "session/host/include/main_session.h"
27 #include "window_manager_agent.h"
28 #include "session_manager.h"
29 #include "zidl/window_manager_agent_interface.h"
30 #include "mock/mock_session_stage.h"
31 #include "mock/mock_window_event_channel.h"
32 #include "application_info.h"
33 #include "context.h"
34
35 using namespace testing;
36 using namespace testing::ext;
37
38 namespace OHOS {
39 namespace Rosen {
40 namespace {
41 const std::string EMPTY_DEVICE_ID = "";
42 }
43 class SceneSessionManagerLifecycleTest2 : public testing::Test {
44 public:
45 static void SetUpTestCase();
46
47 static void TearDownTestCase();
48
49 void SetUp() override;
50
51 void TearDown() override;
52
53 static void SetVisibleForAccessibility(sptr<SceneSession>& sceneSession);
54 int32_t GetTaskCount(sptr<SceneSession>& session);
55 static sptr<SceneSessionManager> ssm_;
56 private:
57 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
58 };
59
60 sptr<SceneSessionManager> SceneSessionManagerLifecycleTest2::ssm_ = nullptr;
61
WindowChangedFuncTest(int32_t persistentId,WindowUpdateType type)62 void WindowChangedFuncTest(int32_t persistentId, WindowUpdateType type)
63 {
64 }
65
ProcessStatusBarEnabledChangeFuncTest(bool enable)66 void ProcessStatusBarEnabledChangeFuncTest(bool enable)
67 {
68 }
69
DumpRootSceneElementInfoFuncTest(const std::vector<std::string> & params,std::vector<std::string> & infos)70 void DumpRootSceneElementInfoFuncTest(const std::vector<std::string>& params, std::vector<std::string>& infos)
71 {
72 }
73
SetUpTestCase()74 void SceneSessionManagerLifecycleTest2::SetUpTestCase()
75 {
76 ssm_ = &SceneSessionManager::GetInstance();
77 }
78
TearDownTestCase()79 void SceneSessionManagerLifecycleTest2::TearDownTestCase()
80 {
81 ssm_ = nullptr;
82 }
83
SetUp()84 void SceneSessionManagerLifecycleTest2::SetUp()
85 {
86 ssm_->sceneSessionMap_.clear();
87 }
88
TearDown()89 void SceneSessionManagerLifecycleTest2::TearDown()
90 {
91 usleep(WAIT_SYNC_IN_NS);
92 ssm_->sceneSessionMap_.clear();
93 }
94
SetVisibleForAccessibility(sptr<SceneSession> & sceneSession)95 void SceneSessionManagerLifecycleTest2::SetVisibleForAccessibility(sptr<SceneSession>& sceneSession)
96 {
97 sceneSession->SetTouchable(true);
98 sceneSession->forceTouchable_ = true;
99 sceneSession->systemTouchable_ = true;
100 sceneSession->state_ = SessionState::STATE_FOREGROUND;
101 sceneSession->foregroundInteractiveStatus_.store(true);
102 }
103
GetTaskCount(sptr<SceneSession> & session)104 int32_t SceneSessionManagerLifecycleTest2::GetTaskCount(sptr<SceneSession>& session)
105 {
106 std::string dumpInfo = session->handler_->GetEventRunner()->GetEventQueue()->DumpCurrentQueueSize();
107 std::regex pattern("\\d+");
108 std::smatch matches;
109 int32_t taskNum = 0;
110 while (std::regex_search(dumpInfo, matches, pattern)) {
111 taskNum += std::stoi(matches.str());
112 dumpInfo = matches.suffix();
113 }
114 return taskNum;
115 }
116
117 namespace {
118 /**
119 * @tc.name: OnSessionStateChange
120 * @tc.desc: OnSessionStateChange
121 * @tc.type: FUNC
122 */
123 HWTEST_F(SceneSessionManagerLifecycleTest2, OnSessionStateChange, Function | SmallTest | Level3)
124 {
125 SessionState state = SessionState::STATE_FOREGROUND;
126 ASSERT_NE(nullptr, ssm_);
127 ssm_->sceneSessionMap_.clear();
128 ssm_->OnSessionStateChange(1, state);
129 SessionInfo sessionInfo;
130 sessionInfo.bundleName_ = "SceneSessionManagerLifecycleTest22";
131 sessionInfo.abilityName_ = "DumpSessionWithId";
132 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
133 ASSERT_NE(nullptr, ssm_);
134 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
135 ASSERT_NE(nullptr, sceneSession);
136 ASSERT_NE(nullptr, sceneSession->property_);
137 sceneSession->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_END);
138 ASSERT_NE(nullptr, ssm_);
139 ssm_->OnSessionStateChange(1, state);
140 auto focusGroup = ssm_->windowFocusController_->GetFocusGroup(DEFAULT_DISPLAY_ID);
141 focusGroup->SetFocusedSessionId(1);
142 ssm_->OnSessionStateChange(1, state);
143 sceneSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
144 ASSERT_NE(nullptr, ssm_);
145 ssm_->needBlockNotifyFocusStatusUntilForeground_ = true;
146 ssm_->OnSessionStateChange(1, state);
147 ASSERT_NE(nullptr, ssm_);
148 ssm_->needBlockNotifyFocusStatusUntilForeground_ = false;
149 ssm_->OnSessionStateChange(1, state);
150 focusGroup->SetFocusedSessionId(0);
151 ASSERT_NE(nullptr, ssm_);
152 ssm_->OnSessionStateChange(1, state);
153 }
154
155 /**
156 * @tc.name: OnSessionStateChange01
157 * @tc.desc: OnSessionStateChange01
158 * @tc.type: FUNC
159 */
160 HWTEST_F(SceneSessionManagerLifecycleTest2, OnSessionStateChange01, Function | SmallTest | Level3)
161 {
162 SessionState state = SessionState::STATE_BACKGROUND;
163 ASSERT_NE(nullptr, ssm_);
164 ssm_->sceneSessionMap_.clear();
165 SessionInfo sessionInfo;
166 sessionInfo.bundleName_ = "SceneSessionManagerLifecycleTest22";
167 sessionInfo.abilityName_ = "DumpSessionWithId";
168 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
169 ASSERT_NE(nullptr, ssm_);
170 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
171 ASSERT_NE(nullptr, sceneSession);
172 ASSERT_NE(nullptr, sceneSession->property_);
173 sceneSession->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_END);
174 ASSERT_NE(nullptr, ssm_);
175 ssm_->OnSessionStateChange(1, state);
176 sceneSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
177 ASSERT_NE(nullptr, ssm_);
178 ssm_->OnSessionStateChange(1, state);
179 state = SessionState::STATE_END;
180 ASSERT_NE(nullptr, ssm_);
181 ssm_->OnSessionStateChange(1, state);
182 }
183
184 /**
185 * @tc.name: OnSessionStateChange02
186 * @tc.desc: OnSessionStateChange02
187 * @tc.type: FUNC
188 */
189 HWTEST_F(SceneSessionManagerLifecycleTest2, OnSessionStateChange02, Function | SmallTest | Level3)
190 {
191 SessionState state = SessionState::STATE_FOREGROUND;
192 ASSERT_NE(nullptr, ssm_);
193 ssm_->sceneSessionMap_.clear();
194 SessionInfo sessionInfo;
195 sessionInfo.bundleName_ = "SceneSessionManagerLifecycleTest22";
196 sessionInfo.abilityName_ = "DumpSessionWithId";
197 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
198 ASSERT_NE(nullptr, ssm_);
199 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
200 ASSERT_NE(nullptr, sceneSession);
201 ASSERT_NE(nullptr, sceneSession->property_);
202 sceneSession->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_END);
203 sceneSession->SetFocusedOnShow(true);
204 ASSERT_NE(nullptr, ssm_);
205 ssm_->OnSessionStateChange(1, state);
206 sceneSession->SetFocusedOnShow(false);
207 ASSERT_NE(nullptr, ssm_);
208 ssm_->OnSessionStateChange(1, state);
209 }
210
211 /**
212 * @tc.name: NotifyWindowStateErrorFromMMI
213 * @tc.desc: NotifyWindowStateErrorFromMMI
214 * @tc.type: FUNC
215 */
216 HWTEST_F(SceneSessionManagerLifecycleTest2, NotifyWindowStateErrorFromMMI, Function | SmallTest | Level3)
217 {
218 int ret = 0;
219 ssm_->sceneSessionMap_.clear();
220 SessionInfo info;
221 info.abilityName_ = "SceneSessionManagerLifecycleTest2";
222 info.bundleName_ = "NotifyWindowStateErrorFromMMI";
223 info.screenId_ = 0;
224 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
225 ASSERT_NE(nullptr, sceneSession);
226 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
227 ASSERT_NE(nullptr, property);
228 property->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
229 sceneSession->property_ = property;
230 sceneSession->SetCallingPid(100);
231
232 SessionInfo info1;
233 info1.abilityName_ = "SceneSessionManagerLifecycleTest2";
234 info1.bundleName_ = "NotifyWindowStateErrorFromMMI1";
235 info1.screenId_ = 0;
236 sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(info1, nullptr);
237 ASSERT_NE(nullptr, sceneSession1);
238 sceneSession1->SetCallingPid(200);
239
240 SessionInfo info2;
241 info2.abilityName_ = "SceneSessionManagerLifecycleTest2";
242 info2.bundleName_ = "NotifyWindowStateErrorFromMMI2";
243 info2.screenId_ = 0;
244 sptr<SceneSession> sceneSession2 = sptr<SceneSession>::MakeSptr(info2, nullptr);
245 ASSERT_NE(nullptr, sceneSession2);
246 sptr<WindowSessionProperty> property2 = sptr<WindowSessionProperty>::MakeSptr();
247 ASSERT_NE(nullptr, property2);
248 property2->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
249 sceneSession2->property_ = property2;
250 sceneSession2->SetCallingPid(100);
251
252 ssm_->sceneSessionMap_.insert({10086, sceneSession});
253 ssm_->sceneSessionMap_.insert({10087, sceneSession1});
254 ssm_->sceneSessionMap_.insert({10088, sceneSession2});
255 ssm_->sceneSessionMap_.insert({10089, nullptr});
256 ssm_->NotifyWindowStateErrorFromMMI(-1, 10086);
257 ssm_->NotifyWindowStateErrorFromMMI(100, 10086);
258 ASSERT_EQ(ret, 0);
259 }
260 }
261 } // namespace Rosen
262 } // namespace OHOS
263