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
57 private:
58 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
59 };
60
61 sptr<SceneSessionManager> SceneSessionManagerLifecycleTest2::ssm_ = nullptr;
62
WindowChangedFuncTest(int32_t persistentId,WindowUpdateType type)63 void WindowChangedFuncTest(int32_t persistentId, WindowUpdateType type) {}
64
ProcessStatusBarEnabledChangeFuncTest(bool enable)65 void ProcessStatusBarEnabledChangeFuncTest(bool enable) {}
66
SetUpTestCase()67 void SceneSessionManagerLifecycleTest2::SetUpTestCase()
68 {
69 ssm_ = &SceneSessionManager::GetInstance();
70 }
71
TearDownTestCase()72 void SceneSessionManagerLifecycleTest2::TearDownTestCase()
73 {
74 ssm_ = nullptr;
75 }
76
SetUp()77 void SceneSessionManagerLifecycleTest2::SetUp()
78 {
79 ssm_->sceneSessionMap_.clear();
80 }
81
TearDown()82 void SceneSessionManagerLifecycleTest2::TearDown()
83 {
84 usleep(WAIT_SYNC_IN_NS);
85 ssm_->sceneSessionMap_.clear();
86 }
87
SetVisibleForAccessibility(sptr<SceneSession> & sceneSession)88 void SceneSessionManagerLifecycleTest2::SetVisibleForAccessibility(sptr<SceneSession>& sceneSession)
89 {
90 sceneSession->SetTouchable(true);
91 sceneSession->forceTouchable_ = true;
92 sceneSession->systemTouchable_ = true;
93 sceneSession->state_ = SessionState::STATE_FOREGROUND;
94 sceneSession->foregroundInteractiveStatus_.store(true);
95 }
96
GetTaskCount(sptr<SceneSession> & session)97 int32_t SceneSessionManagerLifecycleTest2::GetTaskCount(sptr<SceneSession>& session)
98 {
99 std::string dumpInfo = session->handler_->GetEventRunner()->GetEventQueue()->DumpCurrentQueueSize();
100 std::regex pattern("\\d+");
101 std::smatch matches;
102 int32_t taskNum = 0;
103 while (std::regex_search(dumpInfo, matches, pattern)) {
104 taskNum += std::stoi(matches.str());
105 dumpInfo = matches.suffix();
106 }
107 return taskNum;
108 }
109
110 namespace {
111 /**
112 * @tc.name: OnSessionStateChange
113 * @tc.desc: OnSessionStateChange
114 * @tc.type: FUNC
115 */
116 HWTEST_F(SceneSessionManagerLifecycleTest2, OnSessionStateChange, TestSize.Level1)
117 {
118 SessionState state = SessionState::STATE_FOREGROUND;
119 ASSERT_NE(nullptr, ssm_);
120 ssm_->sceneSessionMap_.clear();
121 ssm_->OnSessionStateChange(1, state);
122 SessionInfo sessionInfo;
123 sessionInfo.bundleName_ = "SceneSessionManagerLifecycleTest22";
124 sessionInfo.abilityName_ = "DumpSessionWithId";
125 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
126 ASSERT_NE(nullptr, ssm_);
127 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
128 ASSERT_NE(nullptr, sceneSession);
129 ASSERT_NE(nullptr, sceneSession->property_);
130 sceneSession->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_END);
131 ASSERT_NE(nullptr, ssm_);
132 ssm_->OnSessionStateChange(1, state);
133 auto focusGroup = ssm_->windowFocusController_->GetFocusGroup(DEFAULT_DISPLAY_ID);
134 focusGroup->SetFocusedSessionId(1);
135 ssm_->OnSessionStateChange(1, state);
136 sceneSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
137 ASSERT_NE(nullptr, ssm_);
138 ssm_->needBlockNotifyFocusStatusUntilForeground_ = true;
139 ssm_->OnSessionStateChange(1, state);
140 ASSERT_NE(nullptr, ssm_);
141 ssm_->needBlockNotifyFocusStatusUntilForeground_ = false;
142 ssm_->OnSessionStateChange(1, state);
143 focusGroup->SetFocusedSessionId(0);
144 ASSERT_NE(nullptr, ssm_);
145 ssm_->OnSessionStateChange(1, state);
146 }
147
148 /**
149 * @tc.name: OnSessionStateChange01
150 * @tc.desc: OnSessionStateChange01
151 * @tc.type: FUNC
152 */
153 HWTEST_F(SceneSessionManagerLifecycleTest2, OnSessionStateChange01, TestSize.Level1)
154 {
155 SessionState state = SessionState::STATE_BACKGROUND;
156 ASSERT_NE(nullptr, ssm_);
157 ssm_->sceneSessionMap_.clear();
158 SessionInfo sessionInfo;
159 sessionInfo.bundleName_ = "SceneSessionManagerLifecycleTest22";
160 sessionInfo.abilityName_ = "DumpSessionWithId";
161 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
162 ASSERT_NE(nullptr, ssm_);
163 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
164 ASSERT_NE(nullptr, sceneSession);
165 ASSERT_NE(nullptr, sceneSession->property_);
166 sceneSession->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_END);
167 ASSERT_NE(nullptr, ssm_);
168 ssm_->OnSessionStateChange(1, state);
169 sceneSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
170 ASSERT_NE(nullptr, ssm_);
171 ssm_->OnSessionStateChange(1, state);
172 state = SessionState::STATE_END;
173 ASSERT_NE(nullptr, ssm_);
174 ssm_->OnSessionStateChange(1, state);
175 }
176
177 /**
178 * @tc.name: OnSessionStateChange02
179 * @tc.desc: OnSessionStateChange02
180 * @tc.type: FUNC
181 */
182 HWTEST_F(SceneSessionManagerLifecycleTest2, OnSessionStateChange02, TestSize.Level1)
183 {
184 SessionState state = SessionState::STATE_FOREGROUND;
185 ASSERT_NE(nullptr, ssm_);
186 ssm_->sceneSessionMap_.clear();
187 SessionInfo sessionInfo;
188 sessionInfo.bundleName_ = "SceneSessionManagerLifecycleTest22";
189 sessionInfo.abilityName_ = "DumpSessionWithId";
190 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
191 ASSERT_NE(nullptr, ssm_);
192 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
193 ASSERT_NE(nullptr, sceneSession);
194 ASSERT_NE(nullptr, sceneSession->property_);
195 sceneSession->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_END);
196 sceneSession->SetFocusedOnShow(true);
197 ASSERT_NE(nullptr, ssm_);
198 ssm_->OnSessionStateChange(1, state);
199 sceneSession->SetFocusedOnShow(false);
200 ASSERT_NE(nullptr, ssm_);
201 ssm_->OnSessionStateChange(1, state);
202 }
203
204 /**
205 * @tc.name: NotifyWindowStateErrorFromMMI
206 * @tc.desc: NotifyWindowStateErrorFromMMI
207 * @tc.type: FUNC
208 */
209 HWTEST_F(SceneSessionManagerLifecycleTest2, NotifyWindowStateErrorFromMMI, TestSize.Level1)
210 {
211 int ret = 0;
212 ssm_->sceneSessionMap_.clear();
213 SessionInfo info;
214 info.abilityName_ = "SceneSessionManagerLifecycleTest2";
215 info.bundleName_ = "NotifyWindowStateErrorFromMMI";
216 info.screenId_ = 0;
217 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
218 ASSERT_NE(nullptr, sceneSession);
219 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
220 ASSERT_NE(nullptr, property);
221 property->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
222 sceneSession->property_ = property;
223 sceneSession->SetCallingPid(100);
224
225 SessionInfo info1;
226 info1.abilityName_ = "SceneSessionManagerLifecycleTest2";
227 info1.bundleName_ = "NotifyWindowStateErrorFromMMI1";
228 info1.screenId_ = 0;
229 sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(info1, nullptr);
230 ASSERT_NE(nullptr, sceneSession1);
231 sceneSession1->SetCallingPid(200);
232
233 SessionInfo info2;
234 info2.abilityName_ = "SceneSessionManagerLifecycleTest2";
235 info2.bundleName_ = "NotifyWindowStateErrorFromMMI2";
236 info2.screenId_ = 0;
237 sptr<SceneSession> sceneSession2 = sptr<SceneSession>::MakeSptr(info2, nullptr);
238 ASSERT_NE(nullptr, sceneSession2);
239 sptr<WindowSessionProperty> property2 = sptr<WindowSessionProperty>::MakeSptr();
240 ASSERT_NE(nullptr, property2);
241 property2->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
242 sceneSession2->property_ = property2;
243 sceneSession2->SetCallingPid(100);
244
245 ssm_->sceneSessionMap_.insert({ 10086, sceneSession });
246 ssm_->sceneSessionMap_.insert({ 10087, sceneSession1 });
247 ssm_->sceneSessionMap_.insert({ 10088, sceneSession2 });
248 ssm_->sceneSessionMap_.insert({ 10089, nullptr });
249 ssm_->NotifyWindowStateErrorFromMMI(-1, 10086);
250 ssm_->NotifyWindowStateErrorFromMMI(100, 10086);
251 ASSERT_EQ(ret, 0);
252 }
253 } // namespace
254 } // namespace Rosen
255 } // namespace OHOS
256