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 <regex>
18 #include <bundle_mgr_interface.h>
19 #include <bundlemgr/launcher_service.h>
20
21 #include "context.h"
22 #include "interfaces/include/ws_common.h"
23 #include "iremote_object_mocker.h"
24 #include "libxml/parser.h"
25 #include "libxml/tree.h"
26 #include "mock/mock_session_stage.h"
27 #include "mock/mock_window_event_channel.h"
28 #include "session_info.h"
29 #include "session_manager.h"
30 #include "session_manager/include/scene_session_manager.h"
31 #include "session/host/include/scene_session.h"
32 #include "session/host/include/main_session.h"
33 #include "window_manager_agent.h"
34 #include "zidl/window_manager_agent_interface.h"
35
36 using namespace testing;
37 using namespace testing::ext;
38
39 namespace OHOS {
40 namespace Rosen {
41 namespace {
42 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000U;
43 using ConfigItem = WindowSceneConfig::ConfigItem;
ReadConfig(const std::string & xmlStr)44 ConfigItem ReadConfig(const std::string& xmlStr)
45 {
46 ConfigItem config;
47 xmlDocPtr docPtr = xmlParseMemory(xmlStr.c_str(), xmlStr.length());
48 if (docPtr == nullptr) {
49 return config;
50 }
51
52 xmlNodePtr rootPtr = xmlDocGetRootElement(docPtr);
53 if (rootPtr == nullptr || rootPtr->name == nullptr ||
54 xmlStrcmp(rootPtr->name, reinterpret_cast<const xmlChar*>("Configs"))) {
55 xmlFreeDoc(docPtr);
56 return config;
57 }
58
59 std::map<std::string, ConfigItem> configMap;
60 config.SetValue(configMap);
61 WindowSceneConfig::ReadConfig(rootPtr, *config.mapValue_);
62 xmlFreeDoc(docPtr);
63 return config;
64 }
65 } // namespace
66
67 class SSMgrSpecificWindowTest : public testing::Test {
68 public:
69 static void SetUpTestCase();
70
71 static void TearDownTestCase();
72
73 void SetUp() override;
74
75 void TearDown() override;
76
77 static sptr<SceneSessionManager> ssm_;
78
79 private:
80 };
81
82 sptr<SceneSessionManager> SSMgrSpecificWindowTest::ssm_ = nullptr;
83
SetUpTestCase()84 void SSMgrSpecificWindowTest::SetUpTestCase()
85 {
86 ssm_ = &SceneSessionManager::GetInstance();
87 }
88
TearDownTestCase()89 void SSMgrSpecificWindowTest::TearDownTestCase()
90 {
91 ssm_ = nullptr;
92 }
93
SetUp()94 void SSMgrSpecificWindowTest::SetUp() {}
95
TearDown()96 void SSMgrSpecificWindowTest::TearDown()
97 {
98 usleep(WAIT_SYNC_IN_NS);
99 }
100
101 namespace {
102 /**
103 * @tc.name: ConfigKeyboardAnimation01
104 * @tc.desc: call ConfigKeyboardAnimation default
105 * @tc.type: FUNC
106 */
107 HWTEST_F(SSMgrSpecificWindowTest, ConfigKeyboardAnimation01, TestSize.Level1)
108 {
109 std::string xmlStr =
110 "<?xml version='1.0' encoding=\"utf-8\"?>"
111 "<Configs>"
112 "<keyboardAnimation>"
113 "<animationIn>"
114 "<timing>"
115 "<duration>abv</duration>"
116 "<curve name=\"cubic\">0.2 0.0 0.2 1.0</curve>"
117 "</timing>"
118 "</animationIn>"
119 "<animationOut>"
120 "<timing>"
121 "<duration>abv</duration>"
122 "<curve name=\"cubic\">0.2 0.0 0.2 1.0</curve>"
123 "</timing>"
124 "</animationOut>"
125 "</keyboardAnimation>"
126 "</Configs>";
127 WindowSceneConfig::config_ = ReadConfig(xmlStr);
128 ssm_->ConfigWindowSceneXml();
129
130 std::string xmlStr1 =
131 "<?xml version='1.0' encoding=\"utf-8\"?>"
132 "<Configs>"
133 "<keyboardAnimation>"
134 "<animationIn>"
135 "<timing>"
136 "<duration>500</duration>"
137 "<curve name=\"cubic\">0.2 0.0 0.2 1.0</curve>"
138 "</timing>"
139 "</animationIn>"
140 "<animationOut>"
141 "<timing>"
142 "<duration>300</duration>"
143 "<curve name=\"cubic\">0.2 0.0 0.2 1.0</curve>"
144 "</timing>"
145 "</animationOut>"
146 "</keyboardAnimation>"
147 "</Configs>";
148 WindowSceneConfig::config_ = ReadConfig(xmlStr1);
149 ssm_->ConfigWindowSceneXml();
150 ASSERT_EQ(ssm_->systemConfig_.animationIn_.duration_, static_cast<uint32_t>(500));
151 ASSERT_EQ(ssm_->systemConfig_.animationOut_.duration_, static_cast<uint32_t>(300));
152 }
153
154 /**
155 * @tc.name: ConfigKeyboardAnimation02
156 * @tc.desc: call ConfigKeyboardAnimation default
157 * @tc.type: FUNC
158 */
159 HWTEST_F(SSMgrSpecificWindowTest, ConfigKeyboardAnimation02, TestSize.Level1)
160 {
161 std::string xmlStr =
162 "<?xml version='1.0' encoding=\"utf-8\"?>"
163 "<Configs>"
164 "<keyboardAnimation>"
165 "<animationIn>"
166 "<timing>"
167 "<duration>500</duration>"
168 "<duration>600</duration>"
169 "</timing>"
170 "</animationIn>"
171 "<animationOut>"
172 "<timing>"
173 "<duration>300</duration>"
174 "</timing>"
175 "</animationOut>"
176 "</keyboardAnimation>"
177 "</Configs>";
178 WindowSceneConfig::config_ = ReadConfig(xmlStr);
179 ssm_->ConfigWindowSceneXml();
180 ASSERT_EQ(ssm_->systemConfig_.animationOut_.duration_, static_cast<uint32_t>(300));
181 }
182
183 /**
184 * @tc.name: ConfigKeyboardAnimation03
185 * @tc.desc: call ConfigKeyboardAnimation default
186 * @tc.type: FUNC
187 */
188 HWTEST_F(SSMgrSpecificWindowTest, ConfigKeyboardAnimation03, TestSize.Level1)
189 {
190 std::string xmlStr =
191 "<?xml version='1.0' encoding=\"utf-8\"?>"
192 "<Configs>"
193 "<keyboardAnimation>"
194 "<animationIn>"
195 "<timing>"
196 "<duration>500</duration>"
197 "</timing>"
198 "</animationIn>"
199 "<animationOut>"
200 "<timing>"
201 "<duration>300</duration>"
202 "<duration>400</duration>"
203 "</timing>"
204 "</animationOut>"
205 "</keyboardAnimation>"
206 "</Configs>";
207 WindowSceneConfig::config_ = ReadConfig(xmlStr);
208 ssm_->ConfigWindowSceneXml();
209 ASSERT_EQ(ssm_->systemConfig_.animationIn_.duration_, static_cast<uint32_t>(500));
210 }
211
212 /**
213 * @tc.name: ConfigKeyboardAnimation04
214 * @tc.desc: call ConfigKeyboardAnimation default
215 * @tc.type: FUNC
216 */
217 HWTEST_F(SSMgrSpecificWindowTest, ConfigKeyboardAnimation04, TestSize.Level1)
218 {
219 std::string xmlStr =
220 "<?xml version='1.0' encoding=\"utf-8\"?>"
221 "<Configs>"
222 "<keyboardAnimation>"
223 "<animationIn>"
224 "<timing>"
225 "<duration>500</duration>"
226 "</timing>"
227 "</animationIn>"
228 "</keyboardAnimation>"
229 "</Configs>";
230 WindowSceneConfig::config_ = ReadConfig(xmlStr);
231 ssm_->ConfigWindowSceneXml();
232 ASSERT_EQ(ssm_->systemConfig_.animationIn_.duration_, static_cast<uint32_t>(500));
233 }
234
235 /**
236 * @tc.name: IsKeyboardForeground
237 * @tc.desc: IsKeyboardForeground
238 * @tc.type: FUNC
239 */
240 HWTEST_F(SSMgrSpecificWindowTest, IsKeyboardForeground, TestSize.Level1)
241 {
242 ASSERT_NE(nullptr, ssm_);
243 SessionInfo info;
244 sptr<SceneSession::SpecificSessionCallback> specificCallback = nullptr;
245 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, specificCallback);
246 ASSERT_NE(sceneSession, nullptr);
247 sptr<Session> session = sptr<Session>::MakeSptr(info);
248 ASSERT_NE(session, nullptr);
249 session->property_ = nullptr;
250 auto result = ssm_->IsKeyboardForeground();
251 ASSERT_EQ(result, false);
252
253 ssm_->sceneSessionMap_.insert({ 0, sceneSession });
254 session->property_ = sptr<WindowSessionProperty>::MakeSptr();
255 ASSERT_NE(session->property_, nullptr);
256
257 if (session->property_) {
258 auto result1 = session->GetWindowType();
259 result1 = WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT;
260 ASSERT_EQ(result1, WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT);
261 }
262 result = ssm_->IsKeyboardForeground();
263 ASSERT_EQ(result, false);
264 }
265
266 /**
267 * @tc.name: ProcessSubSessionForeground
268 * @tc.desc: ProcessSubSessionForeground
269 * @tc.type: FUNC
270 */
271 HWTEST_F(SSMgrSpecificWindowTest, ProcessSubSessionForeground, TestSize.Level1)
272 {
273 sptr<SceneSession> sceneSession = nullptr;
274 ASSERT_NE(nullptr, ssm_);
275 ssm_->ProcessSubSessionForeground(sceneSession);
276 SessionInfo sessionInfo;
277 sessionInfo.bundleName_ = "SceneSessionManagerTest2";
278 sessionInfo.abilityName_ = "DumpSessionWithId";
279 sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
280 ASSERT_NE(nullptr, sceneSession);
281 ASSERT_NE(nullptr, ssm_);
282 ssm_->ProcessSubSessionForeground(sceneSession);
283 sptr<SceneSession> subSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
284 ASSERT_NE(nullptr, sceneSession);
285 ASSERT_NE(nullptr, subSession);
286 sceneSession->AddSubSession(subSession);
287 ASSERT_NE(nullptr, ssm_);
288 ssm_->ProcessSubSessionForeground(sceneSession);
289 ASSERT_NE(nullptr, subSession);
290 subSession->SetSessionState(SessionState::STATE_FOREGROUND);
291 ASSERT_NE(nullptr, ssm_);
292 ssm_->ProcessSubSessionForeground(sceneSession);
293 ASSERT_NE(nullptr, subSession);
294 subSession->SetSessionState(SessionState::STATE_ACTIVE);
295 ASSERT_NE(nullptr, ssm_);
296 ssm_->ProcessSubSessionForeground(sceneSession);
297 ASSERT_NE(nullptr, subSession);
298 subSession->SetSessionState(SessionState::STATE_INACTIVE);
299 ASSERT_NE(nullptr, ssm_);
300 ssm_->ProcessSubSessionForeground(sceneSession);
301 }
302
303 /**
304 * @tc.name: ProcessSubSessionBackground
305 * @tc.desc: ProcessSubSessionBackground
306 * @tc.type: FUNC
307 */
308 HWTEST_F(SSMgrSpecificWindowTest, ProcessSubSessionBackground, TestSize.Level1)
309 {
310 sptr<SceneSession> sceneSession = nullptr;
311 ASSERT_NE(nullptr, ssm_);
312 ssm_->ProcessSubSessionBackground(sceneSession);
313 SessionInfo sessionInfo;
314 sessionInfo.bundleName_ = "SceneSessionManagerTest2";
315 sessionInfo.abilityName_ = "DumpSessionWithId";
316 sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
317 ASSERT_NE(nullptr, sceneSession);
318 ASSERT_NE(nullptr, ssm_);
319 ssm_->ProcessSubSessionBackground(sceneSession);
320 sptr<SceneSession> subSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
321 ASSERT_NE(nullptr, sceneSession);
322 ASSERT_NE(nullptr, subSession);
323 sceneSession->AddSubSession(subSession);
324 subSession->state_ = SessionState::STATE_FOREGROUND;
325 ASSERT_NE(nullptr, ssm_);
326 ssm_->ProcessSubSessionBackground(sceneSession);
327 ASSERT_NE(nullptr, subSession);
328 subSession->state_ = SessionState::STATE_ACTIVE;
329 ASSERT_NE(nullptr, ssm_);
330 ssm_->ProcessSubSessionBackground(sceneSession);
331 ASSERT_NE(nullptr, subSession);
332 subSession->state_ = SessionState::STATE_INACTIVE;
333 ASSERT_NE(nullptr, ssm_);
334 ssm_->ProcessSubSessionBackground(sceneSession);
335 }
336
337 /**
338 * @tc.name: ProcessSubSessionBackground01
339 * @tc.desc: ProcessSubSessionBackground01
340 * @tc.type: FUNC
341 */
342 HWTEST_F(SSMgrSpecificWindowTest, ProcessSubSessionBackground01, TestSize.Level1)
343 {
344 SessionInfo sessionInfo;
345 sessionInfo.bundleName_ = "SceneSessionManagerTest2";
346 sessionInfo.abilityName_ = "DumpSessionWithId";
347 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
348 ASSERT_NE(nullptr, sceneSession);
349 sceneSession->dialogVec_.clear();
350 ASSERT_NE(nullptr, ssm_);
351 ssm_->ProcessSubSessionBackground(sceneSession);
352 sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
353 ASSERT_NE(nullptr, sceneSession1);
354 ASSERT_NE(nullptr, sceneSession);
355 sceneSession->dialogVec_.push_back(sceneSession1);
356 ASSERT_NE(nullptr, ssm_);
357 ssm_->sceneSessionMap_.clear();
358 sceneSession1->persistentId_ = 1;
359 ssm_->sceneSessionMap_.insert(std::make_pair(0, sceneSession));
360 ssm_->ProcessSubSessionBackground(sceneSession);
361 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
362 ssm_->ProcessSubSessionBackground(sceneSession);
363 sptr<SceneSession> toastSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
364 ASSERT_NE(nullptr, toastSession);
365 ASSERT_NE(nullptr, sceneSession);
366 sceneSession->AddToastSession(toastSession);
367 toastSession->state_ = SessionState::STATE_FOREGROUND;
368 ASSERT_NE(nullptr, ssm_);
369 ssm_->ProcessSubSessionBackground(sceneSession);
370 toastSession->state_ = SessionState::STATE_ACTIVE;
371 ssm_->ProcessSubSessionBackground(sceneSession);
372 toastSession->state_ = SessionState::STATE_INACTIVE;
373 ssm_->ProcessSubSessionBackground(sceneSession);
374 }
375
376 /**
377 * @tc.name: DestroyDialogWithMainWindow
378 * @tc.desc: DestroyDialogWithMainWindow
379 * @tc.type: FUNC
380 */
381 HWTEST_F(SSMgrSpecificWindowTest, DestroyDialogWithMainWindow, TestSize.Level1)
382 {
383 sptr<SceneSession> sceneSession = nullptr;
384 auto result = ssm_->DestroyDialogWithMainWindow(sceneSession);
385 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
386
387 SessionInfo info;
388 sptr<SceneSession::SpecificSessionCallback> specificCallback = nullptr;
389 sceneSession = sptr<SceneSession>::MakeSptr(info, specificCallback);
390 ASSERT_NE(sceneSession, nullptr);
391
392 sptr<Session> session = sptr<Session>::MakeSptr(info);
393 ASSERT_NE(session, nullptr);
394 session->GetDialogVector().clear();
395 result = ssm_->DestroyDialogWithMainWindow(sceneSession);
396 ASSERT_EQ(result, WSError::WS_OK);
397
398 ssm_->sceneSessionMap_.insert({ 0, sceneSession });
399 ssm_->GetSceneSession(1);
400 result = ssm_->DestroyDialogWithMainWindow(sceneSession);
401 ASSERT_EQ(result, WSError::WS_OK);
402 }
403
404 /**
405 * @tc.name: DestroyDialogWithMainWindow02
406 * @tc.desc: DestroyDialogWithMainWindow02
407 * @tc.type: FUNC
408 */
409 HWTEST_F(SSMgrSpecificWindowTest, DestroyDialogWithMainWindow02, TestSize.Level1)
410 {
411 SessionInfo info;
412 sptr<SceneSession::SpecificSessionCallback> specificCallback = nullptr;
413 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, specificCallback);
414 ASSERT_NE(sceneSession, nullptr);
415
416 sptr<Session> dialogSession1 = sptr<Session>::MakeSptr(info);
417 sptr<Session> dialogSession2 = sptr<Session>::MakeSptr(info);
418 ASSERT_NE(dialogSession1, nullptr);
419 ASSERT_NE(dialogSession2, nullptr);
420 dialogSession1->persistentId_ = 0;
421 dialogSession2->persistentId_ = 1;
422 sceneSession->dialogVec_.push_back(dialogSession1);
423 sceneSession->dialogVec_.push_back(dialogSession2);
424
425 ASSERT_NE(ssm_, nullptr);
426 ssm_->sceneSessionMap_.clear();
427 ssm_->sceneSessionMap_.insert({ 0, nullptr });
428 ssm_->sceneSessionMap_.insert({ 0, sceneSession });
429
430 auto ret = ssm_->DestroyDialogWithMainWindow(sceneSession);
431 ASSERT_EQ(ret, WSError::WS_ERROR_INVALID_SESSION);
432 ssm_->sceneSessionMap_.clear();
433 }
434
435 /**
436 * @tc.name: ConfigKeyboardAnimation
437 * @tc.desc: SceneSesionManager config keyboard animation
438 * @tc.type: FUNC
439 */
440 HWTEST_F(SSMgrSpecificWindowTest, ConfigKeyboardAnimation, TestSize.Level1)
441 {
442 WindowSceneConfig::ConfigItem animationConfig;
443 WindowSceneConfig::ConfigItem itemCurve;
444 WindowSceneConfig::ConfigItem itemDuration;
445 // prepare ItemName
446 WindowSceneConfig::ConfigItem nameProp;
447 std::string name = "cubic";
448 nameProp.SetValue(name);
449 itemCurve.SetProperty({ { "name", nameProp } });
450
451 // prepare duration
452 std::vector<int> durationVec = { 39 };
453 itemDuration.SetValue(durationVec);
454
455 // prepare timing
456 WindowSceneConfig::ConfigItem timing;
457 WindowSceneConfig::ConfigItem timingObj;
458 timingObj.SetValue({ { "curve", itemCurve }, { "duration", itemDuration } });
459 timing.SetValue({ { "timing", timingObj } });
460
461 WindowSceneConfig::ConfigItem timing2(timing);
462 // prepare animationConfig
463 animationConfig.SetValue({ { "animationIn", timing }, { "animationOut", timing2 } });
464
465 ssm_->ConfigKeyboardAnimation(animationConfig);
466 ASSERT_EQ(ssm_->systemConfig_.animationIn_.curveType_, "cubic");
467 ASSERT_EQ(ssm_->systemConfig_.animationIn_.duration_, durationVec.at(0));
468
469 uint32_t result = 150;
470 KeyboardSceneAnimationConfig animationIn;
471 KeyboardSceneAnimationConfig animationOut;
472 ssm_->systemConfig_.animationIn_.curveType_ = "";
473 ssm_->ConfigDefaultKeyboardAnimation(animationIn, animationOut);
474 ASSERT_EQ(animationIn.duration_, result);
475 }
476
477 /**
478 * @tc.name: UpdateParentSessionForDialog
479 * @tc.desc: SceneSesionManager UpdateParentSessionForDialog
480 * @tc.type: FUNC
481 */
482 HWTEST_F(SSMgrSpecificWindowTest, UpdateParentSessionForDialog, TestSize.Level1)
483 {
484 WSError result = ssm_->UpdateParentSessionForDialog(nullptr, nullptr);
485 EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
486
487 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
488 ASSERT_NE(nullptr, property);
489 result = ssm_->UpdateParentSessionForDialog(nullptr, property);
490 EXPECT_EQ(result, WSError::WS_ERROR_NULLPTR);
491
492 SessionInfo info;
493 info.abilityName_ = "test1";
494 info.bundleName_ = "test2";
495 info.moduleName_ = "test3";
496 info.appIndex_ = 10;
497 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
498 ASSERT_NE(nullptr, sceneSession);
499 result = ssm_->UpdateParentSessionForDialog(sceneSession, property);
500 EXPECT_EQ(result, WSError::WS_OK);
501
502 property->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
503 result = ssm_->UpdateParentSessionForDialog(sceneSession, property);
504 EXPECT_EQ(result, WSError::WS_OK);
505
506 SessionInfo info1;
507 info1.abilityName_ = "test2";
508 info1.bundleName_ = "test3";
509 sptr<SceneSession> sceneSession2 = sptr<SceneSession>::MakeSptr(info1, nullptr);
510 ASSERT_NE(nullptr, sceneSession2);
511 ssm_->sceneSessionMap_.insert({ 2, sceneSession2 });
512 property->SetParentPersistentId(2);
513 result = ssm_->UpdateParentSessionForDialog(sceneSession, property);
514 EXPECT_EQ(result, WSError::WS_OK);
515 }
516
517 /**
518 * @tc.name: DestroyAndDisconnectSpecificSession
519 * @tc.desc: SceneSesionManager destroy and disconnect specific session
520 * @tc.type: FUNC
521 */
522 HWTEST_F(SSMgrSpecificWindowTest, DestroyAndDisconnectSpecificSession, TestSize.Level1)
523 {
524 int32_t persistentId = 0;
525 WSError result = ssm_->DestroyAndDisconnectSpecificSession(persistentId);
526 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
527 }
528
529 /**
530 * @tc.name: DestroyAndDisconnectSpecificSessionWithDetachCallback
531 * @tc.desc: SceneSesionManager destroy and disconnect specific session with detach callback
532 * @tc.type: FUNC
533 */
534 HWTEST_F(SSMgrSpecificWindowTest, DestroyAndDisconnectSpecificSessionWithDetachCallback, TestSize.Level1)
535 {
536 int32_t persistentId = 0;
537 WSError result = ssm_->DestroyAndDisconnectSpecificSessionWithDetachCallback(persistentId, nullptr);
538 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
539 }
540 } // namespace
541 } // namespace Rosen
542 } // namespace OHOS
543