1 /*
2 * Copyright (c) 2022 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 "display.h"
18 #include "display_manager.h"
19 #include "snapshot_utils.h"
20 #include "common_test_utils.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28 constexpr int BPP = 4;
29 }
30 class SnapshotUtilsTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 virtual void SetUp() override;
35 virtual void TearDown() override;
36 const std::string defaultFile_ = "/data/snapshot_display_1.jpeg";
37 const int defaultBitDepth_ = 8;
38 };
39
SetUpTestCase()40 void SnapshotUtilsTest::SetUpTestCase()
41 {
42 CommonTestUtils::InjectTokenInfoByHapName(0, "com.ohos.systemui", 0);
43 }
44
TearDownTestCase()45 void SnapshotUtilsTest::TearDownTestCase()
46 {
47 }
48
SetUp()49 void SnapshotUtilsTest::SetUp()
50 {
51 }
52
TearDown()53 void SnapshotUtilsTest::TearDown()
54 {
55 }
56
57 namespace {
58 /**
59 * @tc.name: Check01
60 * @tc.desc: Check if default jpeg is valid file names
61 * @tc.type: FUNC
62 */
63 HWTEST_F(SnapshotUtilsTest, Check01, Function | SmallTest | Level3)
64 {
65 ASSERT_EQ(true, SnapShotUtils::CheckFileNameValid(defaultFile_));
66 }
67
68 /**
69 * @tc.name: Check02
70 * @tc.desc: Check custom jpeg is valid file names
71 * @tc.type: FUNC
72 */
73 HWTEST_F(SnapshotUtilsTest, Check02, Function | SmallTest | Level3)
74 {
75 std::string fileName = "/data/test.jpeg";
76 ASSERT_EQ(true, SnapShotUtils::CheckFileNameValid(fileName));
77 }
78
79 /**
80 * @tc.name: Check03
81 * @tc.desc: Check random path is invalid file names
82 * @tc.type: FUNC
83 */
84 HWTEST_F(SnapshotUtilsTest, Check03, Function | SmallTest | Level3)
85 {
86 std::string fileName1 = "/path/to/test/1.jpeg";
87 ASSERT_EQ(false, SnapShotUtils::CheckFileNameValid(fileName1));
88 std::string fileName2 = "";
89 ASSERT_EQ(false, SnapShotUtils::CheckFileNameValid(fileName2));
90 std::string fileName3 = "/data/test.png";
91 ASSERT_EQ(false, SnapShotUtils::CheckFileNameValid(fileName3));
92 }
93
94 /**
95 * @tc.name: RGBA8888ToRGB88801
96 * @tc.desc: RGBA8888 to RGB888 using invalid params
97 * @tc.type: FUNC
98 */
99 HWTEST_F(SnapshotUtilsTest, RGBA8888ToRGB88801, Function | SmallTest | Level3)
100 {
101 ASSERT_FALSE(SnapShotUtils::RGBA8888ToRGB888(nullptr, nullptr, -1));
102 }
103
104 /**
105 * @tc.name: WriteRgb888ToJpeg01
106 * @tc.desc: write rgb888 to jpeg using invalid data
107 * @tc.type: FUNC
108 */
109 HWTEST_F(SnapshotUtilsTest, WriteRgb888ToJpeg01, Function | SmallTest | Level3)
110 {
111 uint8_t *data = nullptr;
112 FILE *file = fopen(defaultFile_.c_str(), "wb");
113 ASSERT_FALSE(SnapShotUtils::WriteRgb888ToJpeg(file, 100, 100, data));
114 }
115
116 /**
117 * @tc.name: WriteRgb888ToJpeg02
118 * @tc.desc: write rgb888 to jpeg using invalid file
119 * @tc.type: FUNC
120 */
121 HWTEST_F(SnapshotUtilsTest, WriteRgb888ToJpeg02, Function | SmallTest | Level3)
122 {
123 uint8_t *data = new uint8_t;
124 FILE *file = nullptr;
125 ASSERT_FALSE(SnapShotUtils::WriteRgb888ToJpeg(file, 100, 100, data));
126 }
127
128 /**
129 * @tc.name: Write01
130 * @tc.desc: Write default jpeg using valid file names and valid PixelMap
131 * @tc.type: FUNC
132 */
133 HWTEST_F(SnapshotUtilsTest, Write01, Function | MediumTest | Level3)
134 {
135 DisplayId id = DisplayManager::GetInstance().GetDefaultDisplayId();
136 std::shared_ptr<Media::PixelMap> pixelMap = DisplayManager::GetInstance().GetScreenshot(id);
137 ASSERT_NE(nullptr, pixelMap);
138 ASSERT_EQ(true, SnapShotUtils::WriteToJpegWithPixelMap(defaultFile_, *pixelMap));
139 }
140
141 /**
142 * @tc.name: Write02
143 * @tc.desc: Write default jpeg using valid file names and valid WriteToJpegParam
144 * @tc.type: FUNC
145 */
146 HWTEST_F(SnapshotUtilsTest, Write02, Function | MediumTest | Level3)
147 {
148 DisplayId id = DisplayManager::GetInstance().GetDefaultDisplayId();
149 std::shared_ptr<Media::PixelMap> pixelMap = DisplayManager::GetInstance().GetScreenshot(id);
150 ASSERT_NE(nullptr, pixelMap);
151 WriteToJpegParam param = {
152 .width = pixelMap->GetWidth(),
153 .height = pixelMap->GetHeight(),
154 .stride = pixelMap->GetRowBytes(),
155 .format = pixelMap->GetPixelFormat(),
156 .data = pixelMap->GetPixels()
157 };
158 ASSERT_EQ(true, SnapShotUtils::WriteToJpeg(defaultFile_, param));
159 }
160
161 /**
162 * @tc.name: Write03
163 * @tc.desc: Write custom jpeg using valid file names and valid WriteToJpegParam
164 * @tc.type: FUNC
165 */
166 HWTEST_F(SnapshotUtilsTest, Write03, Function | MediumTest | Level3)
167 {
168 DisplayId id = DisplayManager::GetInstance().GetDefaultDisplayId();
169 std::shared_ptr<Media::PixelMap> pixelMap = DisplayManager::GetInstance().GetScreenshot(id);
170 ASSERT_NE(nullptr, pixelMap);
171 WriteToJpegParam param = {
172 .width = (pixelMap->GetWidth() / 2),
173 .height = (pixelMap->GetWidth() / 2),
174 .stride = pixelMap->GetRowBytes(),
175 .format = pixelMap->GetPixelFormat(),
176 .data = pixelMap->GetPixels()
177 };
178 ASSERT_EQ(false, SnapShotUtils::WriteToJpeg(defaultFile_, param));
179 }
180
181 /**
182 * @tc.name: Write04
183 * @tc.desc: Write pixel map with jpeg, using fd
184 * @tc.type: FUNC
185 */
186 HWTEST_F(SnapshotUtilsTest, Write04, Function | MediumTest | Level3)
187 {
188 DisplayId id = DisplayManager::GetInstance().GetDefaultDisplayId();
189 std::shared_ptr<Media::PixelMap> pixelMap = DisplayManager::GetInstance().GetScreenshot(id);
190 ASSERT_EQ(true, SnapShotUtils::WriteToJpegWithPixelMap(0, *pixelMap));
191 }
192
193 /**
194 * @tc.name: Write05
195 * @tc.desc: Write custom jpeg using invalid file names and valid WriteToJpegParam
196 * @tc.type: FUNC
197 */
198 HWTEST_F(SnapshotUtilsTest, Write05, Function | MediumTest | Level3)
199 {
200 WriteToJpegParam param = {
201 .width = 256,
202 .height = 256,
203 .stride = 256 * BPP,
204 .format = Media::PixelFormat::UNKNOWN,
205 .data = new uint8_t
206 };
207 ASSERT_FALSE(SnapShotUtils::WriteToJpeg("", param));
208 }
209
210 /**
211 * @tc.name: Write06
212 * @tc.desc: Write custom jpeg using valid file names and invalid WriteToJpegParam
213 * @tc.type: FUNC
214 */
215 HWTEST_F(SnapshotUtilsTest, Write06, Function | MediumTest | Level3)
216 {
217 WriteToJpegParam param = {
218 .width = 256,
219 .height = 256,
220 .stride = 256 * BPP,
221 .format = Media::PixelFormat::UNKNOWN,
222 .data = nullptr
223 };
224 ASSERT_FALSE(SnapShotUtils::WriteToJpeg(defaultFile_, param));
225 }
226
227 /**
228 * @tc.name: Write07
229 * @tc.desc: Write custom jpeg using valid fd and invalid WriteToJpegParam
230 * @tc.type: FUNC
231 */
232 HWTEST_F(SnapshotUtilsTest, Write07, Function | MediumTest | Level3)
233 {
234 WriteToJpegParam param = {
235 .width = 256,
236 .height = 256,
237 .stride = 256 * BPP,
238 .format = Media::PixelFormat::UNKNOWN,
239 .data = nullptr
240 };
241 ASSERT_FALSE(SnapShotUtils::WriteToJpeg(1, param));
242 }
243
244 /**
245 * @tc.name: CheckWHValid
246 * @tc.desc: Check width and height whether valid
247 * @tc.type: FUNC
248 */
249 HWTEST_F(SnapshotUtilsTest, CheckWHValid, Function | SmallTest | Level3)
250 {
251 ASSERT_EQ(false, SnapShotUtils::CheckWHValid(0));
252 ASSERT_EQ(true, SnapShotUtils::CheckWHValid(DisplayManager::MAX_RESOLUTION_SIZE_SCREENSHOT));
253 ASSERT_EQ(false, SnapShotUtils::CheckWHValid(DisplayManager::MAX_RESOLUTION_SIZE_SCREENSHOT + 1));
254 }
255
256 /**
257 * @tc.name: CheckParamValid01
258 * @tc.desc: Check jpeg param whether valid width
259 * @tc.type: FUNC
260 */
261 HWTEST_F(SnapshotUtilsTest, CheckParamValid01, Function | SmallTest | Level3)
262 {
263 WriteToJpegParam paramInvalidWidth = {
264 .width = DisplayManager::MAX_RESOLUTION_SIZE_SCREENSHOT + 1,
265 .height = 0,
266 .stride = 0,
267 .format = Media::PixelFormat::UNKNOWN,
268 .data = nullptr
269 };
270 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidWidth));
271 }
272
273 /**
274 * @tc.name: CheckParamValid02
275 * @tc.desc: Check jpeg param whether valid height
276 * @tc.type: FUNC
277 */
278 HWTEST_F(SnapshotUtilsTest, CheckParamValid02, Function | SmallTest | Level3)
279 {
280 WriteToJpegParam paramInvalidHeight = {
281 .width = DisplayManager::MAX_RESOLUTION_SIZE_SCREENSHOT,
282 .height = 0,
283 .stride = 0,
284 .format = Media::PixelFormat::UNKNOWN,
285 .data = nullptr
286 };
287 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidHeight));
288 }
289
290 /**
291 * @tc.name: CheckParamValid03
292 * @tc.desc: Check jpeg param whether valid stride
293 * @tc.type: FUNC
294 */
295 HWTEST_F(SnapshotUtilsTest, CheckParamValid03, Function | SmallTest | Level3)
296 {
297 WriteToJpegParam paramInvalidStride = {
298 .width = 256,
299 .height = 256,
300 .stride = 1,
301 .format = Media::PixelFormat::UNKNOWN,
302 .data = nullptr
303 };
304 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidStride));
305 }
306
307 /**
308 * @tc.name: CheckParamValid04
309 * @tc.desc: Check jpeg param whether valid data
310 * @tc.type: FUNC
311 */
312 HWTEST_F(SnapshotUtilsTest, CheckParamValid04, Function | SmallTest | Level3)
313 {
314 WriteToJpegParam paramInvalidData = {
315 .width = 256,
316 .height = 256,
317 .stride = 256 * BPP,
318 .format = Media::PixelFormat::UNKNOWN,
319 .data = nullptr
320 };
321 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidData));
322 }
323 }
324 } // namespace Rosen
325 } // namespace OHOS