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 ssm_->focusedSessionId_ = 1;
141 ssm_->OnSessionStateChange(1, state);
142 sceneSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
143 ASSERT_NE(nullptr, ssm_);
144 ssm_->needBlockNotifyFocusStatusUntilForeground_ = true;
145 ssm_->OnSessionStateChange(1, state);
146 ASSERT_NE(nullptr, ssm_);
147 ssm_->needBlockNotifyFocusStatusUntilForeground_ = false;
148 ssm_->OnSessionStateChange(1, state);
149 ssm_->focusedSessionId_ = 0;
150 ASSERT_NE(nullptr, ssm_);
151 ssm_->OnSessionStateChange(1, state);
152 }
153
154 /**
155 * @tc.name: OnSessionStateChange01
156 * @tc.desc: OnSessionStateChange01
157 * @tc.type: FUNC
158 */
159 HWTEST_F(SceneSessionManagerLifecycleTest2, OnSessionStateChange01, Function | SmallTest | Level3)
160 {
161 SessionState state = SessionState::STATE_BACKGROUND;
162 ASSERT_NE(nullptr, ssm_);
163 ssm_->sceneSessionMap_.clear();
164 SessionInfo sessionInfo;
165 sessionInfo.bundleName_ = "SceneSessionManagerLifecycleTest22";
166 sessionInfo.abilityName_ = "DumpSessionWithId";
167 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
168 ASSERT_NE(nullptr, ssm_);
169 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
170 ASSERT_NE(nullptr, sceneSession);
171 ASSERT_NE(nullptr, sceneSession->property_);
172 sceneSession->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_END);
173 ASSERT_NE(nullptr, ssm_);
174 ssm_->OnSessionStateChange(1, state);
175 sceneSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
176 ASSERT_NE(nullptr, ssm_);
177 ssm_->OnSessionStateChange(1, state);
178 state = SessionState::STATE_END;
179 ASSERT_NE(nullptr, ssm_);
180 ssm_->OnSessionStateChange(1, state);
181 }
182
183 /**
184 * @tc.name: OnSessionStateChange02
185 * @tc.desc: OnSessionStateChange02
186 * @tc.type: FUNC
187 */
188 HWTEST_F(SceneSessionManagerLifecycleTest2, OnSessionStateChange02, Function | SmallTest | Level3)
189 {
190 SessionState state = SessionState::STATE_FOREGROUND;
191 ASSERT_NE(nullptr, ssm_);
192 ssm_->sceneSessionMap_.clear();
193 SessionInfo sessionInfo;
194 sessionInfo.bundleName_ = "SceneSessionManagerLifecycleTest22";
195 sessionInfo.abilityName_ = "DumpSessionWithId";
196 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
197 ASSERT_NE(nullptr, ssm_);
198 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
199 ASSERT_NE(nullptr, sceneSession);
200 ASSERT_NE(nullptr, sceneSession->property_);
201 sceneSession->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_END);
202 sceneSession->SetFocusedOnShow(true);
203 ASSERT_NE(nullptr, ssm_);
204 ssm_->OnSessionStateChange(1, state);
205 sceneSession->SetFocusedOnShow(false);
206 ASSERT_NE(nullptr, ssm_);
207 ssm_->OnSessionStateChange(1, state);
208 }
209
210 /**
211 * @tc.name: NotifyWindowStateErrorFromMMI
212 * @tc.desc: NotifyWindowStateErrorFromMMI
213 * @tc.type: FUNC
214 */
215 HWTEST_F(SceneSessionManagerLifecycleTest2, NotifyWindowStateErrorFromMMI, Function | SmallTest | Level3)
216 {
217 int ret = 0;
218 ssm_->sceneSessionMap_.clear();
219 SessionInfo info;
220 info.abilityName_ = "SceneSessionManagerLifecycleTest2";
221 info.bundleName_ = "NotifyWindowStateErrorFromMMI";
222 info.screenId_ = 0;
223 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
224 ASSERT_NE(nullptr, sceneSession);
225 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
226 ASSERT_NE(nullptr, property);
227 property->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
228 sceneSession->property_ = property;
229 sceneSession->SetCallingPid(100);
230
231 SessionInfo info1;
232 info1.abilityName_ = "SceneSessionManagerLifecycleTest2";
233 info1.bundleName_ = "NotifyWindowStateErrorFromMMI1";
234 info1.screenId_ = 0;
235 sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(info1, nullptr);
236 ASSERT_NE(nullptr, sceneSession1);
237 sceneSession1->SetCallingPid(200);
238
239 ssm_->sceneSessionMap_.insert({10086, sceneSession});
240 ssm_->sceneSessionMap_.insert({10087, sceneSession1});
241 ssm_->NotifyWindowStateErrorFromMMI(100, 10086);
242 ASSERT_EQ(ret, 0);
243 }
244 }
245 } // namespace Rosen
246 } // namespace OHOS
247