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 <bundle_mgr_interface.h>
17 #include <bundlemgr/launcher_service.h>
18 #include <gtest/gtest.h>
19 #include <regex>
20
21 #include "application_info.h"
22 #include "context.h"
23 #include "interfaces/include/ws_common.h"
24 #include "iremote_object_mocker.h"
25 #include "screen_fold_data.h"
26 #include "screen_session_manager_client/include/screen_session_manager_client.h"
27 #include "session_info.h"
28 #include "session/host/include/scene_persistence.h"
29 #include "session/host/include/scene_persistent_storage.h"
30 #include "session/host/include/scene_session.h"
31 #include "session/host/include/main_session.h"
32 #include "session_manager.h"
33 #include "session_manager/include/scene_session_manager.h"
34 #include "mock/mock_session_stage.h"
35 #include "mock/mock_window_event_channel.h"
36 #include "window_manager_agent.h"
37 #include "zidl/window_manager_agent_interface.h"
38
39 using namespace testing;
40 using namespace testing::ext;
41
42 namespace OHOS {
43 namespace Rosen {
44 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
45 constexpr uint32_t COMMON_SIZE = 1;
46 namespace {
47 std::string g_logMsg;
MyLogCallback(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * msg)48 void MyLogCallback(const LogType type, const LogLevel level, const unsigned int domain, const char* tag,
49 const char* msg)
50 {
51 g_logMsg = msg;
52 }
53 }
54
55 class WindowPatternSnapshotTest : public testing::Test {
56 public:
57 static void SetUpTestCase();
58 static void TearDownTestCase();
59 void SetUp() override;
60 void TearDown() override;
61 static sptr<SceneSessionManager> ssm_;
62
63 private:
64 RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
65 sptr<Session> session_ = nullptr;
66 std::shared_ptr<Media::PixelMap> mPixelMap = std::make_shared<Media::PixelMap>();
67 };
68
69 constexpr const char* UNDERLINE_SEPARATOR = "_";
70 constexpr const char* IMAGE_SUFFIX = ".jpg";
71
72 sptr<SceneSessionManager> WindowPatternSnapshotTest::ssm_ = nullptr;
73 static sptr<ScenePersistence> scenePersistence = sptr<ScenePersistence>::MakeSptr("WindowPatternSnapshotTest", 1423);
74
SetUpTestCase()75 void WindowPatternSnapshotTest::SetUpTestCase()
76 {
77 ssm_ = &SceneSessionManager::GetInstance();
78 }
79
TearDownTestCase()80 void WindowPatternSnapshotTest::TearDownTestCase()
81 {
82 ssm_ = nullptr;
83 }
84
SetUp()85 void WindowPatternSnapshotTest::SetUp()
86 {
87 SessionInfo info;
88 info.abilityName_ = "testSession1";
89 info.moduleName_ = "testSession2";
90 info.bundleName_ = "testSession3";
91 session_ = sptr<Session>::MakeSptr(info);
92 ASSERT_NE(nullptr, session_);
93 session_->surfaceNode_ = CreateRSSurfaceNode();
94 ssm_->sceneSessionMap_.clear();
95 }
96
TearDown()97 void WindowPatternSnapshotTest::TearDown()
98 {
99 session_ = nullptr;
100 ssm_->sceneSessionMap_.clear();
101 usleep(WAIT_SYNC_IN_NS);
102 }
103
CreateRSSurfaceNode()104 RSSurfaceNode::SharedPtr WindowPatternSnapshotTest::CreateRSSurfaceNode()
105 {
106 struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
107 rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTest2SurfaceNode";
108 auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
109 if (surfaceNode == nullptr) {
110 GTEST_LOG_(INFO) << "WindowSessionTest2::CreateRSSurfaceNode surfaceNode is nullptr";
111 }
112 return surfaceNode;
113 }
114
115 namespace {
116 /**
117 * @tc.name: CreateSnapshotDir
118 * @tc.desc: test function : CreateSnapshotDir
119 * @tc.type: FUNC
120 */
121 HWTEST_F(WindowPatternSnapshotTest, CreateSnapshotDir, TestSize.Level1)
122 {
123 std::string directory = "0/Storage";
124 ASSERT_NE(nullptr, scenePersistence);
125 bool result = scenePersistence->CreateSnapshotDir(directory);
126 ASSERT_EQ(result, false);
127 }
128
129 /**
130 * @tc.name: CreateUpdatedIconDir
131 * @tc.desc: test function : CreateUpdatedIconDir
132 * @tc.type: FUNC
133 */
134 HWTEST_F(WindowPatternSnapshotTest, CreateUpdatedIconDir, TestSize.Level1)
135 {
136 std::string directory = "0/Storage";
137 ASSERT_NE(nullptr, scenePersistence);
138 bool result = scenePersistence->CreateUpdatedIconDir(directory);
139 ASSERT_EQ(result, false);
140 }
141
142 /**
143 * @tc.name: scenePersistence::SaveSnapshot
144 * @tc.desc: test function : scenePersistence::SaveSnapshot
145 * @tc.type: FUNC
146 */
147 HWTEST_F(WindowPatternSnapshotTest, SaveSnapshot01, TestSize.Level1)
148 {
149 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
150 std::string directory = "0/Storage";
151 std::string bundleName = "testBundleName";
152
153 auto key = defaultStatus;
154 int32_t persistentId = 1423;
155 ASSERT_NE(nullptr, scenePersistence);
__anoncf2b46c30302() 156 scenePersistence->SaveSnapshot(pixelMap, []() {}, key);
157
158 scenePersistence->snapshotPath_[key.first][key.second] = "/data/1.png";
__anoncf2b46c30402() 159 scenePersistence->SaveSnapshot(mPixelMap, []() {}, key);
160 uint32_t fileID = static_cast<uint32_t>(persistentId) & 0x3fffffff;
161 std::string test =
162 ScenePersistence::snapshotDirectory_ + bundleName + UNDERLINE_SEPARATOR + std::to_string(fileID) + IMAGE_SUFFIX;
163 std::pair<uint32_t, uint32_t> sizeResult = scenePersistence->GetSnapshotSize(key);
164 EXPECT_EQ(sizeResult.first, 0);
165 EXPECT_EQ(sizeResult.second, 0);
166 }
167
168 /**
169 * @tc.name: RenameSnapshotFromOldPersistentId
170 * @tc.desc: test function : RenameSnapshotFromOldPersistentId
171 * @tc.type: FUNC
172 */
173 HWTEST_F(WindowPatternSnapshotTest, RenameSnapshotFromOldPersistentId, TestSize.Level1)
174 {
175 g_logMsg.clear();
176 LOG_SetCallback(MyLogCallback);
177 int32_t persistentId = 1424;
178 std::string bundleName = "testBundleName";
179 ScenePersistence::InitAstcEnabled();
180 EXPECT_EQ(ScenePersistence::IsAstcEnabled(), true);
181 sptr<ScenePersistence> scenePersistence = sptr<ScenePersistence>::MakeSptr(bundleName, persistentId);
182 scenePersistence->RenameSnapshotFromOldPersistentId(persistentId);
183
184 scenePersistence->isAstcEnabled_ = false;
185 scenePersistence->RenameSnapshotFromOldPersistentId(persistentId);
186 usleep(WAIT_SYNC_IN_NS);
187 EXPECT_TRUE(g_logMsg.find("snapshot from") != std::string::npos);
188 }
189
190 /**
191 * @tc.name: IsSnapshotExisted
192 * @tc.desc: test function : IsSnapshotExisted
193 * @tc.type: FUNC
194 */
195 HWTEST_F(WindowPatternSnapshotTest, IsSnapshotExisted, TestSize.Level1)
196 {
197 std::string bundleName = "testBundleName";
198 int32_t persistentId = 1423;
199 sptr<ScenePersistence> scenePersistence = sptr<ScenePersistence>::MakeSptr(bundleName, persistentId);
200 ASSERT_NE(nullptr, scenePersistence);
201 bool result = scenePersistence->IsSnapshotExisted();
202 ASSERT_EQ(result, false);
203 }
204
205 /**
206 * @tc.name: GetLocalSnapshotPixelMap
207 * @tc.desc: test function : get local snapshot pixelmap
208 * @tc.type: FUNC
209 */
210 HWTEST_F(WindowPatternSnapshotTest, GetLocalSnapshotPixelMap, TestSize.Level1)
211 {
212 SessionInfo info;
213 info.abilityName_ = "GetPixelMap";
214 info.bundleName_ = "GetPixelMap1";
215 sptr<Session> session = sptr<Session>::MakeSptr(info);
216 ASSERT_NE(nullptr, session);
217 auto abilityInfo = session->GetSessionInfo();
218 auto persistentId = abilityInfo.persistentId_;
219 ScenePersistence::CreateSnapshotDir("storage");
220 sptr<ScenePersistence> scenePersistence = sptr<ScenePersistence>::MakeSptr(abilityInfo.bundleName_, persistentId);
221 ASSERT_NE(nullptr, scenePersistence);
222 auto result = scenePersistence->GetLocalSnapshotPixelMap(0.5, 0.5);
223 ASSERT_EQ(result, nullptr);
224
225 bool result2 = scenePersistence->IsSnapshotExisted();
226 ASSERT_EQ(result2, false);
227
228 // create pixelMap
229 const uint32_t colors[1] = { 0x6f0000ff };
230 uint32_t colorsLength = sizeof(colors) / sizeof(colors[0]);
231 const int32_t offset = 0;
232 Media::InitializationOptions opts;
233 opts.size.width = COMMON_SIZE;
234 opts.size.height = COMMON_SIZE;
235 opts.pixelFormat = Media::PixelFormat::RGBA_8888;
236 opts.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
237 int32_t stride = opts.size.width;
238 std::shared_ptr<Media::PixelMap> pixelMap1 = Media::PixelMap::Create(colors, colorsLength, offset, stride, opts);
239
240 auto key = defaultStatus;
241 scenePersistence->snapshotPath_[key.first][key.second] = "/data/1.png";
242 scenePersistence->SaveSnapshot(pixelMap1);
243 int maxScenePersistencePollNum = 100;
244 for (int i = 0; i < maxScenePersistencePollNum; i++) {
245 result = scenePersistence->GetLocalSnapshotPixelMap(0.8, 0.2);
246 result2 = scenePersistence->IsSnapshotExisted();
247 }
248 ASSERT_NE(result, nullptr);
249 EXPECT_EQ(result2, true);
250
251 result = scenePersistence->GetLocalSnapshotPixelMap(0.0, 0.2);
252 ASSERT_NE(result, nullptr);
253 }
254
255 /**
256 * @tc.name: IsSavingSnapshot
257 * @tc.desc: test function : IsSavingSnapshot
258 * @tc.type: FUNC
259 */
260 HWTEST_F(WindowPatternSnapshotTest, IsSavingSnapshot, TestSize.Level1)
261 {
262 std::string bundleName = "testBundleName";
263 int32_t persistentId = 1423;
264 sptr<ScenePersistence> scenePersistence = sptr<ScenePersistence>::MakeSptr(bundleName, persistentId);
265 ASSERT_NE(nullptr, scenePersistence);
266 bool result = scenePersistence->IsSavingSnapshot();
267 EXPECT_EQ(result, false);
268
269 auto key = defaultStatus;
270 result = scenePersistence->IsSavingSnapshot(key, true);
271 EXPECT_EQ(result, false);
272 }
273
274 /**
275 * @tc.name: GetSnapshotFilePath
276 * @tc.desc: test function : GetSnapshotFilePath
277 * @tc.type: FUNC
278 */
279 HWTEST_F(WindowPatternSnapshotTest, GetSnapshotFilePath, TestSize.Level1)
280 {
281 std::string bundleName = "testBundleName";
282 int32_t persistentId = 1423;
283 sptr<ScenePersistence> scenePersistence = sptr<ScenePersistence>::MakeSptr(bundleName, persistentId);
284
285 auto key = defaultStatus;
286 std::string path = "/data/1.png";
287 scenePersistence->snapshotPath_[key.first][key.second] = path;
288 auto ret = scenePersistence->GetSnapshotFilePath(key, false, true);
289 EXPECT_NE(ret, path);
290
291 ret = scenePersistence->GetSnapshotFilePath(key, true);
292 EXPECT_EQ(ret, path);
293
294 ret = scenePersistence->GetSnapshotFilePath(key);
295 EXPECT_EQ(ret, path);
296
297 scenePersistence->hasSnapshot_[key.first][key.second] = true;
298 ret = scenePersistence->GetSnapshotFilePath(key);
299 EXPECT_EQ(ret, path);
300
301 SnapshotStatus status = { SCREEN_UNKNOWN, SNAPSHOT_LANDSCAPE };
302 ret = scenePersistence->GetSnapshotFilePath(status);
303 EXPECT_EQ(ret, path);
304
305 SnapshotStatus status1 = { SCREEN_EXPAND, SNAPSHOT_LANDSCAPE };
306 ret = scenePersistence->GetSnapshotFilePath(status1);
307 EXPECT_EQ(ret, path);
308 }
309
310 /**
311 * @tc.name: HasSnapshot
312 * @tc.desc: test function: HasSnapshot
313 * @tc.type: FUNC
314 */
315 HWTEST_F(WindowPatternSnapshotTest, HasSnapshot, TestSize.Level1)
316 {
317 std::string bundleName = "testBundleName";
318 int32_t persistentId = 1423;
319 sptr<ScenePersistence> scenePersistence = sptr<ScenePersistence>::MakeSptr(bundleName, persistentId);
320 auto key = defaultStatus;
321 scenePersistence->SetHasSnapshot(true, key);
322 EXPECT_EQ(scenePersistence->HasSnapshot(), true);
323 scenePersistence->SetHasSnapshot(false, key);
324 EXPECT_EQ(scenePersistence->HasSnapshot(key, false), false);
325
326 EXPECT_EQ(scenePersistence->HasSnapshot(key, true), false);
327 EXPECT_EQ(scenePersistence->HasSnapshot(key, false), false);
328 scenePersistence->SetHasSnapshotFreeMultiWindow(true);
329 EXPECT_EQ(scenePersistence->HasSnapshot(key, true), true);
330 }
331
332 /**
333 * @tc.name: SetSaveSnapshotCallback
334 * @tc.desc: test function: SetSaveSnapshotCallback
335 * @tc.type: FUNC
336 */
337 HWTEST_F(WindowPatternSnapshotTest, SetSaveSnapshotCallback, TestSize.Level1)
338 {
339 std::string bundleName = "testBundleName";
340 SessionInfo info;
341 info.abilityName_ = bundleName;
342 info.bundleName_ = bundleName;
343 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
344 ASSERT_NE(nullptr, sceneSession->saveSnapshotCallback_);
345 sceneSession->SetSaveSnapshotCallback(nullptr);
346 ASSERT_NE(nullptr, sceneSession->saveSnapshotCallback_);
347 }
348
349 /**
350 * @tc.name: SetRemoveSnapshotCallback
351 * @tc.desc: test function: SetRemoveSnapshotCallback
352 * @tc.type: FUNC
353 */
354 HWTEST_F(WindowPatternSnapshotTest, SetRemoveSnapshotCallback, TestSize.Level1)
355 {
356 std::string bundleName = "testBundleName";
357 SessionInfo info;
358 info.abilityName_ = bundleName;
359 info.bundleName_ = bundleName;
360 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
361 ASSERT_NE(nullptr, sceneSession->removeSnapshotCallback_);
362 sceneSession->SetRemoveSnapshotCallback(nullptr);
363 ASSERT_NE(nullptr, sceneSession->removeSnapshotCallback_);
364 }
365
366 /**
367 * @tc.name: SetAddSnapshotCallback
368 * @tc.desc: test function: SetAddSnapshotCallback
369 * @tc.type: FUNC
370 */
371 HWTEST_F(WindowPatternSnapshotTest, SetAddSnapshotCallback, TestSize.Level1)
372 {
373 std::string bundleName = "testBundleName";
374 SessionInfo info;
375 info.abilityName_ = bundleName;
376 info.bundleName_ = bundleName;
377 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
378 ASSERT_NE(nullptr, sceneSession->addSnapshotCallback_);
379 sceneSession->SetAddSnapshotCallback(nullptr);
380 ASSERT_NE(nullptr, sceneSession->addSnapshotCallback_);
381 }
382
383 /**
384 * @tc.name: NotifyAddSnapshot
385 * @tc.desc: test function: NotifyAddSnapshot
386 * @tc.type: FUNC
387 */
388 HWTEST_F(WindowPatternSnapshotTest, NotifyAddSnapshot, TestSize.Level1)
389 {
390 std::string bundleName = "testBundleName";
391 SessionInfo info;
392 info.abilityName_ = bundleName;
393 info.bundleName_ = bundleName;
394 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
395 ASSERT_NE(nullptr, sceneSession->addSnapshotCallback_);
396 sceneSession->NotifyAddSnapshot(true);
397 ASSERT_NE(nullptr, sceneSession->addSnapshotCallback_);
398 }
399
400 /**
401 * @tc.name: ResetSnapshotCache
402 * @tc.desc: test function: ResetSnapshotCache
403 * @tc.type: FUNC
404 */
405 HWTEST_F(WindowPatternSnapshotTest, ResetSnapshotCache, TestSize.Level1)
406 {
407 std::string bundleName = "testBundleName";
408 int32_t persistentId = 1423;
409 sptr<ScenePersistence> scenePersistence = sptr<ScenePersistence>::MakeSptr(bundleName, persistentId);
410 scenePersistence->ResetSnapshotCache();
411 auto key = defaultStatus;
412 ASSERT_EQ(scenePersistence->isSavingSnapshot_[key.first][key.second], false);
413 }
414
415 /**
416 * @tc.name: GetSessionSnapshotPixelMap01
417 * @tc.desc: SceneSesionManager get session snapshot pixelmap
418 * @tc.type: FUNC
419 */
420 HWTEST_F(WindowPatternSnapshotTest, GetSessionSnapshotPixelMap01, TestSize.Level1)
421 {
422 SessionInfo info;
423 info.abilityName_ = "GetPixelMap";
424 info.bundleName_ = "GetPixelMap1";
425 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
426 sceneSession->SetSessionState(SessionState::STATE_ACTIVE);
427
428 int32_t persistentId = 65535;
429 float scaleValue = 0.5f;
430 auto pixelMap = ssm_->GetSessionSnapshotPixelMap(persistentId, scaleValue);
431 ASSERT_EQ(pixelMap, nullptr);
432 }
433
434 /**
435 * @tc.name: GetSessionSnapshotPixelMap
436 * @tc.desc: GetSessionSnapshotPixelMap
437 * @tc.type: FUNC
438 */
439 HWTEST_F(WindowPatternSnapshotTest, GetSessionSnapshotPixelMap02, TestSize.Level1)
440 {
441 ASSERT_NE(ssm_, nullptr);
442 SessionInfo info;
443 info.abilityName_ = "GetPixelMap";
444 info.bundleName_ = "GetPixelMap1";
445 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
446 ASSERT_NE(sceneSession, nullptr);
447 sceneSession->SetSessionState(SessionState::STATE_ACTIVE);
448 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
449 int32_t persistentId = 1;
450 float scaleParam = 0.5f;
451 auto result = ssm_->GetSessionSnapshotPixelMap(persistentId, scaleParam);
452 ASSERT_EQ(result, nullptr);
453
454 sceneSession->SetSessionState(SessionState::STATE_FOREGROUND);
455 std::string bundleName = "testBundleName";
456 int32_t testpersistentId = 1;
457 sceneSession->scenePersistence_ = sptr<ScenePersistence>::MakeSptr(bundleName, testpersistentId);
458 ASSERT_NE(sceneSession->scenePersistence_, nullptr);
459 struct RSSurfaceNodeConfig config;
460 sceneSession->surfaceNode_ = RSSurfaceNode::Create(config);
461 ASSERT_NE(sceneSession->surfaceNode_, nullptr);
462 sceneSession->bufferAvailable_ = true;
463 result = ssm_->GetSessionSnapshotPixelMap(persistentId, scaleParam);
464 ASSERT_EQ(result, nullptr);
465 }
466
467 /**
468 * @tc.name: GetSnapshot
469 * @tc.desc: GetSnapshot Test
470 * @tc.type: FUNC
471 */
472 HWTEST_F(WindowPatternSnapshotTest, GetSnapshot, TestSize.Level1)
473 {
474 ASSERT_NE(session_, nullptr);
475 session_->state_ = SessionState::STATE_DISCONNECT;
476 std::shared_ptr<Media::PixelMap> snapshot = session_->Snapshot();
477 ASSERT_EQ(snapshot, session_->GetSnapshot());
478 }
479
480 /**
481 * @tc.name: NotifyAddSnapshot
482 * @tc.desc: NotifyAddSnapshot Test
483 * @tc.type: FUNC
484 */
485 HWTEST_F(WindowPatternSnapshotTest, NotifyAddSnapshot02, TestSize.Level1)
486 {
487 ASSERT_NE(session_, nullptr);
488 session_->state_ = SessionState::STATE_DISCONNECT;
489 session_->NotifyAddSnapshot();
490 ASSERT_EQ(session_->GetSnapshot(), nullptr);
491 }
492
493 /**
494 * @tc.name: NotifyRemoveSnapshot
495 * @tc.desc: NotifyRemoveSnapshot Test
496 * @tc.type: FUNC
497 */
498 HWTEST_F(WindowPatternSnapshotTest, NotifyRemoveSnapshot, TestSize.Level1)
499 {
500 ASSERT_NE(session_, nullptr);
501 session_->scenePersistence_ = sptr<ScenePersistence>::MakeSptr("bundleName", 1);
502 session_->state_ = SessionState::STATE_DISCONNECT;
503 session_->NotifyRemoveSnapshot();
504 ASSERT_EQ(session_->GetScenePersistence()->HasSnapshot(), false);
505 }
506
507 /**
508 * @tc.name: Snapshot01
509 * @tc.desc: ret is false
510 * @tc.type: FUNC
511 */
512 HWTEST_F(WindowPatternSnapshotTest, Snapshot01, TestSize.Level1)
513 {
514 ASSERT_NE(session_, nullptr);
515
516 session_->surfaceNode_ = nullptr;
517
518 ASSERT_EQ(nullptr, session_->Snapshot());
519 }
520
521 /**
522 * @tc.name: Snapshot
523 * @tc.desc: Snapshot Test
524 * @tc.type: FUNC
525 */
526 HWTEST_F(WindowPatternSnapshotTest, Snapshot02, TestSize.Level1)
527 {
528 ASSERT_NE(session_, nullptr);
529 int32_t persistentId = 1424;
530 std::string bundleName = "testBundleName";
531 session_->scenePersistence_ = sptr<ScenePersistence>::MakeSptr(bundleName, persistentId);
532 ASSERT_NE(session_->scenePersistence_, nullptr);
533 struct RSSurfaceNodeConfig config;
534 session_->surfaceNode_ = RSSurfaceNode::Create(config);
535 ASSERT_NE(session_->surfaceNode_, nullptr);
536 ASSERT_EQ(nullptr, session_->Snapshot(false, 0.0f));
537
538 session_->bufferAvailable_ = true;
539 ASSERT_EQ(nullptr, session_->Snapshot(false, 0.0f));
540
541 session_->surfaceNode_->bufferAvailable_ = true;
542 ASSERT_EQ(nullptr, session_->Snapshot(false, 0.0f));
543
544 session_->surfaceNode_ = nullptr;
545 ASSERT_EQ(nullptr, session_->Snapshot(false, 0.0f));
546 }
547
548 /**
549 * @tc.name: ResetSnapshot
550 * @tc.desc: ResetSnapshot Test
551 * @tc.type: FUNC
552 */
553 HWTEST_F(WindowPatternSnapshotTest, ResetSnapshot, TestSize.Level1)
554 {
555 ASSERT_NE(session_, nullptr);
556 std::string bundleName = "testBundleName";
557 int32_t persistentId = 1423;
558 session_->scenePersistence_ = sptr<ScenePersistence>::MakeSptr(bundleName, persistentId);
559 session_->snapshot_ = std::make_shared<Media::PixelMap>();
560
561 session_->ResetSnapshot();
562 ASSERT_EQ(nullptr, session_->snapshot_);
563 }
564
565 /**
566 * @tc.name: Session::SaveSnapshot
567 * @tc.desc: Session::SaveSnapshot Test
568 * @tc.type: FUNC
569 */
570 HWTEST_F(WindowPatternSnapshotTest, SaveSnapshot02, TestSize.Level1)
571 {
572 ASSERT_NE(session_, nullptr);
573
574 session_->scenePersistence_ = nullptr;
575 session_->snapshot_ = nullptr;
576 session_->SaveSnapshot(true);
577 ASSERT_EQ(session_->snapshot_, nullptr);
578
579 session_->scenePersistence_ =
580 sptr<ScenePersistence>::MakeSptr(session_->sessionInfo_.bundleName_, session_->persistentId_);
581
582 session_->SaveSnapshot(false);
583 ASSERT_EQ(session_->snapshot_, nullptr);
584
585 session_->SaveSnapshot(true);
586 ASSERT_EQ(session_->snapshot_, nullptr);
587
588 auto pixelMap = std::make_shared<Media::PixelMap>();
589 session_->SaveSnapshot(false, true, pixelMap);
590 ASSERT_NE(session_->snapshot_, nullptr);
591
592 session_->freeMultiWindow_.store(true);
593 session_->SaveSnapshot(false, true, pixelMap);
594 ASSERT_NE(session_->snapshot_, nullptr);
595 }
596
597 /**
598 * @tc.name: GetSnapshotPixelMap
599 * @tc.desc: GetSnapshotPixelMap Test
600 * @tc.type: FUNC
601 */
602 HWTEST_F(WindowPatternSnapshotTest, GetSnapshotPixelMap, TestSize.Level1)
603 {
604 ASSERT_NE(session_, nullptr);
605 session_->scenePersistence_ = nullptr;
606 ASSERT_EQ(nullptr, session_->GetSnapshotPixelMap(6.6f, 8.8f));
607 session_->scenePersistence_ = sptr<ScenePersistence>::MakeSptr("GetSnapshotPixelMap", 2024);
608 auto key = defaultStatus;
609 session_->scenePersistence_->isSavingSnapshot_[key.first][key.second].store(true);
610 session_->snapshot_ = nullptr;
611 ASSERT_EQ(nullptr, session_->GetSnapshotPixelMap(6.6f, 8.8f));
612 }
613
614 /**
615 * @tc.name: GetEnableAddSnapshot
616 * @tc.desc: GetEnableAddSnapshot Test
617 * @tc.type: FUNC
618 */
619 HWTEST_F(WindowPatternSnapshotTest, GetEnableAddSnapshot, TestSize.Level1)
620 {
621 ASSERT_NE(session_, nullptr);
622 bool res = session_->GetEnableAddSnapshot();
623 EXPECT_EQ(res, true);
624 }
625
626 /**
627 * @tc.name: SetEnableAddSnapshot
628 * @tc.desc: SetEnableAddSnapshot Test
629 * @tc.type: FUNC
630 */
631 HWTEST_F(WindowPatternSnapshotTest, SetEnableAddSnapshot, TestSize.Level1)
632 {
633 ASSERT_NE(session_, nullptr);
634 session_->SetEnableAddSnapshot(false);
635 bool res = session_->GetEnableAddSnapshot();
636 EXPECT_EQ(res, false);
637 }
638
639 /**
640 * @tc.name: NotifyUpdateSnapshotWindow
641 * @tc.desc: NotifyUpdateSnapshotWindow Test
642 * @tc.type: FUNC
643 */
644 HWTEST_F(WindowPatternSnapshotTest, NotifyUpdateSnapshotWindow, TestSize.Level1)
645 {
646 ASSERT_NE(session_, nullptr);
647 session_->state_ = SessionState::STATE_DISCONNECT;
648 session_->NotifyUpdateSnapshotWindow();
649 ASSERT_EQ(session_->GetSnapshot(), nullptr);
650 }
651
652 /**
653 * @tc.name: NotifySnapshotUpdate
654 * @tc.desc: NotifySnapshotUpdate Test
655 * @tc.type: FUNC
656 */
657 HWTEST_F(WindowPatternSnapshotTest, NotifySnapshotUpdate, TestSize.Level1)
658 {
659 SessionInfo info;
660 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
661 WMError ret = sceneSession->NotifySnapshotUpdate();
662 EXPECT_EQ(ret, WMError::WM_OK);
663 }
664
665 /**
666 * @tc.name: SetSnapshotCapacity
667 * @tc.desc: SetSnapshotCapacity Test
668 * @tc.type: FUNC
669 */
670 HWTEST_F(WindowPatternSnapshotTest, SetSnapshotCapacity, TestSize.Level1)
671 {
672 std::string bundleName = "testBundleName";
673 int32_t persistentId = 1423;
674 sptr<ScenePersistence> scenePersistence = sptr<ScenePersistence>::MakeSptr(bundleName, persistentId);
675
676 scenePersistence->SetSnapshotCapacity(defaultCapacity);
677 EXPECT_EQ(scenePersistence->capacity_, defaultCapacity);
678 }
679
680 /**
681 * @tc.name: InitSnapshotCapacity
682 * @tc.desc: InitSnapshotCapacity Test
683 * @tc.type: FUNC
684 */
685 HWTEST_F(WindowPatternSnapshotTest, InitSnapshotCapacity, TestSize.Level1)
686 {
687 SessionInfo info;
688 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
689 sceneSession->systemConfig_.supportSnapshotAllSessionStatus_ = false;
690 sceneSession->InitSnapshotCapacity();
691 EXPECT_EQ(sceneSession->capacity_, defaultCapacity);
692
693 sceneSession->systemConfig_.supportSnapshotAllSessionStatus_ = true;
694 sceneSession->scenePersistence_ = sptr<ScenePersistence>::MakeSptr("bundleName", 1);
695 sceneSession->InitSnapshotCapacity();
696 EXPECT_EQ(sceneSession->scenePersistence_->capacity_, maxCapacity);
697 }
698
699 /**
700 * @tc.name: GetWindowStatus
701 * @tc.desc: GetWindowStatus Test
702 * @tc.type: FUNC
703 */
704 HWTEST_F(WindowPatternSnapshotTest, GetWindowStatus, TestSize.Level1)
705 {
706 SessionInfo info;
707 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
708 sceneSession->capacity_ = defaultCapacity;
709 auto ret = sceneSession->GetWindowStatus();
710 EXPECT_EQ(ret, defaultStatus);
711
712 sceneSession->capacity_ = maxCapacity;
713 ret = sceneSession->GetWindowStatus();
714 EXPECT_NE(ret.second, 3);
715 }
716
717 /**
718 * @tc.name: GetSessionStatus
719 * @tc.desc: GetSessionStatus Test
720 * @tc.type: FUNC
721 */
722 HWTEST_F(WindowPatternSnapshotTest, GetSessionStatus, TestSize.Level1)
723 {
724 SessionInfo info;
725 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
726 sceneSession->capacity_ = defaultCapacity;
727 auto ret = sceneSession->GetSessionStatus();
728 EXPECT_EQ(ret, defaultStatus);
729
730 sceneSession->capacity_ = maxCapacity;
731 sceneSession->state_ = SessionState::STATE_DISCONNECT;
732 sceneSession->currentRotation_ = 0;
733 sceneSession->GetSessionStatus();
734
735 sceneSession->state_ = SessionState::STATE_ACTIVE;
736 ret = sceneSession->GetSessionStatus();
737 EXPECT_EQ(ret.second, 0);
738
739 ScreenLockReason reason = ScreenLockReason::EXPAND_TO_FOLD_SINGLE_POCKET;
740 ret = sceneSession->GetSessionStatus(reason);
741 EXPECT_EQ(ret.first, 1);
742 }
743
744 /**
745 * @tc.name: GetWindowOrientation
746 * @tc.desc: GetWindowOrientation Test
747 * @tc.type: FUNC
748 */
749 HWTEST_F(WindowPatternSnapshotTest, GetWindowOrientation, TestSize.Level1)
750 {
751 SessionInfo info;
752 info.screenId_ = 0;
753 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
754 auto ret = sceneSession->GetWindowOrientation();
755 EXPECT_EQ(ret, DisplayOrientation::PORTRAIT);
756
757 sceneSession->capacity_ = maxCapacity;
758 ScreenId screenId = 0;
759 sptr<ScreenSession> screenSession = nullptr;
760 screenSession = new ScreenSession(0, ScreenProperty(), 0);
761 ASSERT_NE(screenSession, nullptr);
762 ScreenSessionManagerClient::GetInstance().screenSessionMap_.emplace(screenId, screenSession);
763
764 sceneSession->GetWindowOrientation();
765 ScreenSessionManagerClient::GetInstance().screenSessionMap_.clear();
766
767 session_->capacity_ = maxCapacity;
768 ret = session_->GetWindowOrientation();
769 EXPECT_EQ(ret, DisplayOrientation::PORTRAIT);
770 }
771
772 /**
773 * @tc.name: GetLastOrientation
774 * @tc.desc: GetLastOrientation Test
775 * @tc.type: FUNC
776 */
777 HWTEST_F(WindowPatternSnapshotTest, GetLastOrientation, TestSize.Level1)
778 {
779 SessionInfo info;
780 info.screenId_ = 0;
781 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
782 auto ret = sceneSession->GetLastOrientation();
783 EXPECT_EQ(ret, 0);
784
785 sceneSession->capacity_ = maxCapacity;
786 sceneSession->currentRotation_ = 90;
787 ret = sceneSession->GetLastOrientation();
788 EXPECT_EQ(ret, 1);
789 }
790
791 /**
792 * @tc.name: GetScreenStatus
793 * @tc.desc: GetScreenStatus Test
794 * @tc.type: FUNC
795 */
796 HWTEST_F(WindowPatternSnapshotTest, GetScreenStatus, TestSize.Level1)
797 {
798 FoldStatus foldStatus = FoldStatus::FOLDED;
799 auto ret = WSSnapshotHelper::GetScreenStatus(foldStatus);
800 EXPECT_EQ(ret, SCREEN_FOLDED);
801
802 foldStatus = FoldStatus::EXPAND;
803 ret = WSSnapshotHelper::GetScreenStatus(foldStatus);
804 EXPECT_EQ(ret, SCREEN_EXPAND);
805
806 foldStatus = FoldStatus::HALF_FOLD;
807 ret = WSSnapshotHelper::GetScreenStatus(foldStatus);
808 EXPECT_EQ(ret, SCREEN_EXPAND);
809
810 foldStatus = FoldStatus::FOLD_STATE_EXPAND_WITH_SECOND_EXPAND;
811 ret = WSSnapshotHelper::GetScreenStatus(foldStatus);
812 EXPECT_EQ(ret, SCREEN_UNKNOWN);
813 }
814
815 /**
816 * @tc.name: GetDisplayOrientation
817 * @tc.desc: GetDisplayOrientation Test
818 * @tc.type: FUNC
819 */
820 HWTEST_F(WindowPatternSnapshotTest, GetDisplayOrientation, TestSize.Level1)
821 {
822 int32_t rotation = PORTRAIT_ANGLE;
823 auto ret = WSSnapshotHelper::GetDisplayOrientation(rotation);
824 EXPECT_EQ(ret, DisplayOrientation::PORTRAIT);
825
826 rotation = LANDSCAPE_ANGLE;
827 ret = WSSnapshotHelper::GetDisplayOrientation(rotation);
828 EXPECT_EQ(ret, DisplayOrientation::LANDSCAPE);
829
830 rotation = PORTRAIT_INVERTED_ANGLE;
831 ret = WSSnapshotHelper::GetDisplayOrientation(rotation);
832 EXPECT_EQ(ret, DisplayOrientation::PORTRAIT_INVERTED);
833
834 rotation = LANDSCAPE_INVERTED_ANGLE;
835 ret = WSSnapshotHelper::GetDisplayOrientation(rotation);
836 EXPECT_EQ(ret, DisplayOrientation::LANDSCAPE_INVERTED);
837
838 rotation = 1;
839 ret = WSSnapshotHelper::GetDisplayOrientation(rotation);
840 EXPECT_EQ(ret, DisplayOrientation::PORTRAIT);
841 }
842
843 /**
844 * @tc.name: ConfigSupportSnapshotAllSessionStatus
845 * @tc.desc: ConfigSupportSnapshotAllSessionStatus Test
846 * @tc.type: FUNC
847 */
848 HWTEST_F(WindowPatternSnapshotTest, ConfigSupportSnapshotAllSessionStatus, TestSize.Level1)
849 {
850 ASSERT_NE(ssm_, nullptr);
851 ssm_->ConfigSupportSnapshotAllSessionStatus();
852 usleep(WAIT_SYNC_IN_NS);
853 EXPECT_EQ(ssm_->systemConfig_.supportSnapshotAllSessionStatus_, true);
854 }
855
856 /**
857 * @tc.name: SetIsSavingSnapshot
858 * @tc.desc: SetIsSavingSnapshot Test
859 * @tc.type: FUNC
860 */
861 HWTEST_F(WindowPatternSnapshotTest, SetIsSavingSnapshot, TestSize.Level1)
862 {
863 ASSERT_NE(scenePersistence, nullptr);
864 auto key = defaultStatus;
865 scenePersistence->SetIsSavingSnapshot(key, true, true);
866 EXPECT_EQ(scenePersistence->isSavingSnapshotFreeMultiWindow_, true);
867
868 scenePersistence->SetIsSavingSnapshot(key, false, true);
869 EXPECT_EQ(scenePersistence->isSavingSnapshot_[key.first][key.second], true);
870 }
871
872 /**
873 * @tc.name: SetSnapshotSize
874 * @tc.desc: SetSnapshotSize Test
875 * @tc.type: FUNC
876 */
877 HWTEST_F(WindowPatternSnapshotTest, SetSnapshotSize, TestSize.Level1)
878 {
879 ASSERT_NE(scenePersistence, nullptr);
880 auto key = defaultStatus;
881 std::pair<uint32_t, uint32_t> size = { 1440, 2580 };
882 scenePersistence->SetSnapshotSize(key, true, size);
883 EXPECT_EQ(scenePersistence->GetSnapshotSize(key, true), size);
884
885 scenePersistence->SetSnapshotSize(key, false, size);
886 EXPECT_EQ(scenePersistence->GetSnapshotSize(key, false), size);
887 }
888
889 /**
890 * @tc.name: ClearSnapshot
891 * @tc.desc: ClearSnapshot Test
892 * @tc.type: FUNC
893 */
894 HWTEST_F(WindowPatternSnapshotTest, ClearSnapshot, TestSize.Level1)
895 {
896 ASSERT_NE(scenePersistence, nullptr);
897 auto key = defaultStatus;
898 scenePersistence->ClearSnapshot(key);
899 EXPECT_EQ(scenePersistence->hasSnapshot_[key.first][key.second], true);
900 }
901
902 /**
903 * @tc.name: DeleteHasSnapshot
904 * @tc.desc: DeleteHasSnapshot Test
905 * @tc.type: FUNC
906 */
907 HWTEST_F(WindowPatternSnapshotTest, DeleteHasSnapshot, TestSize.Level1)
908 {
909 ASSERT_NE(session_, nullptr);
910 ASSERT_NE(scenePersistence, nullptr);
911 session_->scenePersistence_ = scenePersistence;
912 session_->freeMultiWindow_.store(false);
913 auto pixelMap = std::make_shared<Media::PixelMap>();
914 ScenePersistentStorage::InitDir("/data/Snapshot");
915 session_->SaveSnapshot(false, true, pixelMap);
916
917 auto key = defaultStatus;
918 ScenePersistentStorage::Insert("Snapshot_" + std::to_string(session_->persistentId_) +
919 "_" + std::to_string(key.first) + std::to_string(key.second), true,
920 ScenePersistentStorageType::MAXIMIZE_STATE);
921 EXPECT_EQ(session_->HasSnapshot(key), true);
922 session_->DeleteHasSnapshot(key);
923 session_->scenePersistence_ = nullptr;
924 EXPECT_EQ(session_->HasSnapshot(key), false);
925
926 ScenePersistentStorage::Insert("Snapshot_" + std::to_string(session_->persistentId_),
927 true, ScenePersistentStorageType::MAXIMIZE_STATE);
928 session_->freeMultiWindow_.store(true);
929 session_->SaveSnapshot(false, true, pixelMap);
930 EXPECT_EQ(session_->HasSnapshot(), true);
931 session_->DeleteHasSnapshotFreeMultiWindow();
932 session_->scenePersistence_ = scenePersistence;
933 EXPECT_EQ(session_->HasSnapshot(), false);
934 }
935
936 /**
937 * @tc.name: SetFreeMultiWindow
938 * @tc.desc: SetfreeMultiWindow Test
939 * @tc.type: FUNC
940 */
941 HWTEST_F(WindowPatternSnapshotTest, SetFreeMultiWindow, TestSize.Level1)
942 {
943 session_->capacity_ = maxCapacity;
944 ASSERT_NE(session_, nullptr);
945 session_->property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
946 session_->SetFreeMultiWindow();
947 EXPECT_EQ(session_->freeMultiWindow_, true);
948
949 session_->property_->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
950 session_->SetFreeMultiWindow();
951 EXPECT_EQ(session_->freeMultiWindow_, false);
952
953 session_->capacity_ = defaultCapacity;
954 session_->SetFreeMultiWindow();
955 EXPECT_EQ(session_->freeMultiWindow_, false);
956 }
957
958 /**
959 * @tc.name: FindClosestFormSnapshot
960 * @tc.desc: FindClosestFormSnapshot Test
961 * @tc.type: FUNC
962 */
963 HWTEST_F(WindowPatternSnapshotTest, FindClosestFormSnapshot, TestSize.Level1)
964 {
965 std::string bundleName = "testBundleName";
966 int32_t persistentId = 1423;
967 sptr<ScenePersistence> scenePersistence = sptr<ScenePersistence>::MakeSptr(bundleName, persistentId);
968 scenePersistence->capacity_ = maxCapacity;
969 SnapshotStatus key = defaultStatus;
970 auto ret = scenePersistence->FindClosestFormSnapshot(key);
971 EXPECT_EQ(ret, false);
972
973 scenePersistence->hasSnapshot_[SCREEN_EXPAND][SNAPSHOT_PORTRAIT] = true;
974 ret = scenePersistence->FindClosestFormSnapshot(key);
975 EXPECT_EQ(ret, true);
976
977 key.first = SCREEN_FOLDED;
978 ret = scenePersistence->FindClosestFormSnapshot(key);
979 EXPECT_EQ(ret, true);
980
981 key.first = SCREEN_FOLDED;
982 scenePersistence->hasSnapshot_[SCREEN_EXPAND][SNAPSHOT_PORTRAIT] = false;
983 scenePersistence->hasSnapshot_[SCREEN_EXPAND][SNAPSHOT_LANDSCAPE] = true;
984 ret = scenePersistence->FindClosestFormSnapshot(key);
985 EXPECT_EQ(ret, true);
986
987 key.first = SCREEN_FOLDED;
988 scenePersistence->hasSnapshot_[SCREEN_EXPAND][SNAPSHOT_LANDSCAPE] = false;
989 ret = scenePersistence->FindClosestFormSnapshot(key);
990 EXPECT_EQ(ret, false);
991 }
992 } // namespace
993 } // namespace Rosen
994 } // namespace OHOS