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 "scene_persistence.h"
17 #include "session/host/include/scene_session.h"
18 #include "session.h"
19 #include <gtest/gtest.h>
20 #include "session_info.h"
21 #include "ws_common.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Rosen {
28 class ScenePersistenceTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34 private:
35 std::shared_ptr<Media::PixelMap> mPixelMap = std::make_shared<Media::PixelMap>();
36 };
37
38 constexpr const char* UNDERLINE_SEPARATOR = "_";
39 constexpr const char* IMAGE_SUFFIX = ".jpg";
40
41 static sptr<ScenePersistence> scenePersistence = new ScenePersistence("testBundleName", 1423);
42
SetUpTestCase()43 void ScenePersistenceTest::SetUpTestCase()
44 {
45 }
46
TearDownTestCase()47 void ScenePersistenceTest::TearDownTestCase()
48 {
49 }
50
SetUp()51 void ScenePersistenceTest::SetUp()
52 {
53 }
54
TearDown()55 void ScenePersistenceTest::TearDown()
56 {
57 }
58
59 namespace {
60 /**
61 * @tc.name: CreateSnapshotDir
62 * @tc.desc: test function : CreateSnapshotDir
63 * @tc.type: FUNC
64 */
65 HWTEST_F(ScenePersistenceTest, CreateSnapshotDir, Function | SmallTest | Level1)
66 {
67 std::string directory = "0/Storage";
68 ASSERT_NE(nullptr, scenePersistence);
69 bool result = scenePersistence->CreateSnapshotDir(directory);
70 ASSERT_EQ(result, false);
71 }
72
73 /**
74 * @tc.name: CreateUpdatedIconDir
75 * @tc.desc: test function : CreateUpdatedIconDir
76 * @tc.type: FUNC
77 */
78 HWTEST_F(ScenePersistenceTest, CreateUpdatedIconDir, Function | SmallTest | Level1)
79 {
80 std::string directory = "0/Storage";
81 ASSERT_NE(nullptr, scenePersistence);
82 bool result = scenePersistence->CreateUpdatedIconDir(directory);
83 ASSERT_EQ(result, false);
84 }
85
86 /**
87 * @tc.name: SaveSnapshot
88 * @tc.desc: test function : SaveSnapshot
89 * @tc.type: FUNC
90 */
91 HWTEST_F(ScenePersistenceTest, SaveSnapshot, Function | SmallTest | Level1)
92 {
93 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
94 std::string directory = "0/Storage";
95 std::string bundleName = "testBundleName";
96
97 SessionInfo info;
98 info.abilityName_ = bundleName;
99 info.bundleName_ = bundleName;
100 sptr<SceneSession> sceneSession = new (std::nothrow) SceneSession(info, nullptr);
101
102 int32_t persistentId = 1423;
103 ASSERT_NE(nullptr, scenePersistence);
104 scenePersistence->SaveSnapshot(pixelMap);
105
106 sptr<Session> session = new Session(info);
107 ASSERT_NE(nullptr, session);
108 scenePersistence->snapshotPath_ = "/data/1.png";
109
110 scenePersistence->SaveSnapshot(mPixelMap);
111 uint32_t fileID = static_cast<uint32_t>(persistentId) & 0x3fffffff;
112 std::string test = ScenePersistence::snapshotDirectory_ +
113 bundleName + UNDERLINE_SEPARATOR + std::to_string(fileID) + IMAGE_SUFFIX;
114 std::pair<uint32_t, uint32_t> sizeResult = scenePersistence->GetSnapshotSize();
115 EXPECT_EQ(sizeResult.first, 0);
116 EXPECT_EQ(sizeResult.second, 0);
117 }
118
119 /**
120 * @tc.name: RenameSnapshotFromOldPersistentId
121 * @tc.desc: test function : RenameSnapshotFromOldPersistentId
122 * @tc.type: FUNC
123 */
124 HWTEST_F(ScenePersistenceTest, RenameSnapshotFromOldPersistentId, Function | SmallTest | Level3)
125 {
126 int ret = 0;
127 int32_t persistentId = 1424;
128 std::string bundleName = "testBundleName";
129 sptr<ScenePersistence> scenePersistence2 = new ScenePersistence(bundleName, persistentId);
130 scenePersistence2->RenameSnapshotFromOldPersistentId(persistentId);
131 ASSERT_EQ(ret, 0);
132
133 sptr<ScenePersistence> scenePersistence3 = new ScenePersistence(bundleName, persistentId);
134 ASSERT_NE(nullptr, scenePersistence3);
135 scenePersistence3->snapshotPath_ = "/data/1.png";
136 scenePersistence3->RenameSnapshotFromOldPersistentId(persistentId);
137 }
138
139 /**
140 * @tc.name: IsSnapshotExisted
141 * @tc.desc: test function : IsSnapshotExisted
142 * @tc.type: FUNC
143 */
144 HWTEST_F(ScenePersistenceTest, IsSnapshotExisted, Function | SmallTest | Level1)
145 {
146 std::string bundleName = "testBundleName";
147 int32_t persistentId = 1423;
148 sptr<ScenePersistence> scenePersistence = new ScenePersistence(bundleName, persistentId);
149 ASSERT_NE(nullptr, scenePersistence);
150 bool result = scenePersistence->IsSnapshotExisted();
151 ASSERT_EQ(result, false);
152 }
153
154 /**
155 * @tc.name: GetLocalSnapshotPixelMap
156 * @tc.desc: test function : get local snapshot pixelmap
157 * @tc.type: FUNC
158 */
159 HWTEST_F(ScenePersistenceTest, GetLocalSnapshotPixelMap, Function | SmallTest | Level1)
160 {
161 SessionInfo info;
162 info.abilityName_ = "GetPixelMap";
163 info.bundleName_ = "GetPixelMap1";
164 sptr<Session> session = new Session(info);
165 ASSERT_NE(nullptr, session);
166 auto abilityInfo = session->GetSessionInfo();
167 auto persistentId = abilityInfo.persistentId_;
168 ScenePersistence::CreateSnapshotDir("storage");
169 sptr<ScenePersistence> scenePersistence =
170 new ScenePersistence(abilityInfo.bundleName_, persistentId);
171 ASSERT_NE(nullptr, scenePersistence);
172 auto result = scenePersistence->GetLocalSnapshotPixelMap(0.5, 0.5);
173 EXPECT_EQ(result, nullptr);
174
175 bool result2 = scenePersistence->IsSnapshotExisted();
176 EXPECT_EQ(result2, false);
177
178 // create pixelMap
179 const uint32_t colors[1] = { 0x6f0000ff };
180 constexpr uint32_t COMMON_SIZE = 1;
181 uint32_t colorsLength = sizeof(colors) / sizeof(colors[0]);
182 const int32_t offset = 0;
183 Media::InitializationOptions opts;
184 opts.size.width = COMMON_SIZE;
185 opts.size.height = COMMON_SIZE;
186 opts.pixelFormat = Media::PixelFormat::RGBA_8888;
187 opts.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
188 int32_t stride = opts.size.width;
189 std::shared_ptr<Media::PixelMap> pixelMap1 = Media::PixelMap::Create(colors, colorsLength, offset, stride, opts);
190
191 scenePersistence->SaveSnapshot(pixelMap1);
192 int maxScenePersistencePollNum = 100;
193 scenePersistence->snapshotPath_ = "/data/1.png";
194 for (int i = 0; i < maxScenePersistencePollNum; i++) {
195 result = scenePersistence->GetLocalSnapshotPixelMap(0.8, 0.2);
196 result2 = scenePersistence->IsSnapshotExisted();
197 }
198 EXPECT_NE(result, nullptr);
199 ASSERT_EQ(result2, true);
200
201 result = scenePersistence->GetLocalSnapshotPixelMap(0.0, 0.2);
202 EXPECT_NE(result, nullptr);
203 }
204
205 /**
206 * @tc.name: IsSavingSnapshot
207 * @tc.desc: test function : IsSavingSnapshot
208 * @tc.type: FUNC
209 */
210 HWTEST_F(ScenePersistenceTest, IsSavingSnapshot, Function | SmallTest | Level1)
211 {
212 std::string bundleName = "testBundleName";
213 int32_t persistentId = 1423;
214 sptr<ScenePersistence> scenePersistence = new ScenePersistence(bundleName, persistentId);
215 ASSERT_NE(nullptr, scenePersistence);
216 bool result = scenePersistence->IsSavingSnapshot();
217 ASSERT_EQ(result, false);
218 }
219
220 /**
221 * @tc.name: GetSnapshotFilePath
222 * @tc.desc: test function : GetSnapshotFilePath
223 * @tc.type: FUNC
224 */
225 HWTEST_F(ScenePersistenceTest, GetSnapshotFilePath, Function | SmallTest | Level1)
226 {
227 std::string bundleName = "testBundleName";
228 int32_t persistentId = 1423;
229 sptr<ScenePersistence> scenePersistence = new ScenePersistence(bundleName, persistentId);
230 ASSERT_NE(nullptr, scenePersistence);
231 scenePersistence->RenameSnapshotFromOldPersistentId(0);
232 auto result = scenePersistence->GetSnapshotFilePath();
233 ASSERT_EQ(result, scenePersistence->snapshotPath_);
234 }
235
236 /**
237 * @tc.name: HasSnapshot
238 * @tc.desc: test function: HasSnapshot
239 * @tc.type: FUNC
240 */
241 HWTEST_F(ScenePersistenceTest, HasSnapshot, Function | SmallTest | Level1)
242 {
243 std::string bundleName = "testBundleName";
244 int32_t persistentId = 1423;
245 sptr<ScenePersistence> scenePersistence = new ScenePersistence(bundleName, persistentId);
246 ASSERT_NE(nullptr, scenePersistence);
247 scenePersistence->SetHasSnapshot(true);
248 ASSERT_EQ(scenePersistence->HasSnapshot(), true);
249 scenePersistence->SetHasSnapshot(false);
250 ASSERT_EQ(scenePersistence->HasSnapshot(), false);
251 }
252 }
253 }
254 }