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 <fcntl.h>
17 #include <gtest/gtest.h>
18 #include "display.h"
19 #include "display_manager.h"
20 #include "snapshot_utils.h"
21 #include "common_test_utils.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 constexpr int RGB565_PIXEL_BYTES = 2;
30 constexpr int RGB888_PIXEL_BYTES = 3;
31 constexpr int BPP = 4;
32 constexpr int RGBA8888BUF_SIZE = 10;
33 constexpr int RGB888BUF_SIZE = 10;
34 constexpr int RGB565BUF_SIZE = 10;
35 }
36 class SnapshotUtilsTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 virtual void SetUp() override;
41 virtual void TearDown() override;
42 const std::string defaultFile_ = "/data/local/tmp/snapshot_display_1.jpeg";
43 const int defaultBitDepth_ = 8;
44 };
45
SetUpTestCase()46 void SnapshotUtilsTest::SetUpTestCase()
47 {
48 CommonTestUtils::InjectTokenInfoByHapName(0, "com.ohos.systemui", 0);
49 const char** perms = new const char *[1];
50 perms[0] = "ohos.permission.CAPTURE_SCREEN";
51 CommonTestUtils::SetAceessTokenPermission("DisplayManagerServiceTest", perms, 1);
52 }
53
TearDownTestCase()54 void SnapshotUtilsTest::TearDownTestCase()
55 {
56 }
57
SetUp()58 void SnapshotUtilsTest::SetUp()
59 {
60 }
61
TearDown()62 void SnapshotUtilsTest::TearDown()
63 {
64 }
65
66 namespace {
67 /**
68 * @tc.name: Check01
69 * @tc.desc: Check if default jpeg is valid file names
70 * @tc.type: FUNC
71 */
72 HWTEST_F(SnapshotUtilsTest, Check01, Function | SmallTest | Level3)
73 {
74 ASSERT_EQ(true, SnapShotUtils::CheckFileNameValid(defaultFile_));
75 }
76
77 /**
78 * @tc.name: Check02
79 * @tc.desc: Check custom jpeg is valid file names
80 * @tc.type: FUNC
81 */
82 HWTEST_F(SnapshotUtilsTest, Check02, Function | SmallTest | Level3)
83 {
84 std::string fileName = "/data/local/tmp/test.jpeg";
85 ASSERT_EQ(true, SnapShotUtils::CheckFileNameValid(fileName));
86 }
87
88 /**
89 * @tc.name: Check03
90 * @tc.desc: Check random path is invalid file names
91 * @tc.type: FUNC
92 */
93 HWTEST_F(SnapshotUtilsTest, Check03, Function | SmallTest | Level3)
94 {
95 std::string fileName1 = "/path/to/test/1.jpeg";
96 ASSERT_EQ(false, SnapShotUtils::CheckFileNameValid(fileName1));
97 std::string fileName2 = "";
98 ASSERT_EQ(false, SnapShotUtils::CheckFileNameValid(fileName2));
99 std::string fileName3 = "/data/test.png";
100 ASSERT_EQ(false, SnapShotUtils::CheckFileNameValid(fileName3));
101 std::string fileName4 = "test.png";
102 ASSERT_EQ(false, SnapShotUtils::CheckFileNameValid(fileName4));
103 std::string fileName5 = "/data";
104 ASSERT_EQ(false, SnapShotUtils::CheckFileNameValid(fileName5));
105 std::string fileName6 = "/data/local/tmp/test.png";
106 ASSERT_EQ(false, SnapShotUtils::CheckFileNameValid(fileName6));
107 }
108
109 /**
110 * @tc.name: RGBA8888ToRGB88801
111 * @tc.desc: RGBA8888 to RGB888 using invalid params
112 * @tc.type: FUNC
113 */
114 HWTEST_F(SnapshotUtilsTest, RGBA8888ToRGB88801, Function | SmallTest | Level3)
115 {
116 uint8_t rgba8888Buf[RGBA8888BUF_SIZE];
117 uint8_t rgb888Buf[RGB888BUF_SIZE];
118 EXPECT_FALSE(SnapShotUtils::RGBA8888ToRGB888(rgba8888Buf, nullptr, RGBA8888BUF_SIZE));
119 EXPECT_FALSE(SnapShotUtils::RGBA8888ToRGB888(nullptr, rgb888Buf, RGB888BUF_SIZE));
120 EXPECT_FALSE(SnapShotUtils::RGBA8888ToRGB888(rgba8888Buf, rgb888Buf, 0));
121 EXPECT_TRUE(SnapShotUtils::RGBA8888ToRGB888(rgba8888Buf, rgb888Buf, RGBA8888BUF_SIZE));
122 }
123
124 /**
125 * @tc.name: RGB565ToRGB888
126 * @tc.desc: RGB565 to RGB888 using invalid params
127 * @tc.type: FUNC
128 */
129 HWTEST_F(SnapshotUtilsTest, RGB565ToRGB888, Function | SmallTest | Level3)
130 {
131 uint8_t rgb565Buf[RGB565BUF_SIZE];
132 uint8_t rgb888Buf[RGB888BUF_SIZE];
133 EXPECT_FALSE(SnapShotUtils::RGB565ToRGB888(rgb565Buf, nullptr, RGB565BUF_SIZE));
134 EXPECT_FALSE(SnapShotUtils::RGB565ToRGB888(nullptr, rgb888Buf, RGB888BUF_SIZE));
135 EXPECT_FALSE(SnapShotUtils::RGB565ToRGB888(rgb565Buf, rgb888Buf, 0));
136 EXPECT_TRUE(SnapShotUtils::RGB565ToRGB888(rgb565Buf, rgb888Buf, RGB565BUF_SIZE));
137 }
138
139 /**
140 * @tc.name: WriteRgb888ToJpeg01
141 * @tc.desc: write rgb888 to jpeg using invalid data
142 * @tc.type: FUNC
143 */
144 HWTEST_F(SnapshotUtilsTest, WriteRgb888ToJpeg01, Function | SmallTest | Level3)
145 {
146 uint8_t *data = nullptr;
147 FILE *file = fopen(defaultFile_.c_str(), "wb");
148 if (file == nullptr) {
149 return;
150 }
151 ASSERT_FALSE(SnapShotUtils::WriteRgb888ToJpeg(file, 100, 100, data));
152 fclose(file);
153 }
154
155 /**
156 * @tc.name: WriteRgb888ToJpeg02
157 * @tc.desc: write rgb888 to jpeg using invalid file
158 * @tc.type: FUNC
159 */
160 HWTEST_F(SnapshotUtilsTest, WriteRgb888ToJpeg02, Function | SmallTest | Level3)
161 {
162 uint8_t *data = new uint8_t;
163 FILE *file = nullptr;
164 ASSERT_FALSE(SnapShotUtils::WriteRgb888ToJpeg(file, 100, 100, data));
165 }
166
167 /**
168 * @tc.name: Write01
169 * @tc.desc: Write default jpeg using valid file names and valid PixelMap
170 * @tc.type: FUNC
171 */
172 HWTEST_F(SnapshotUtilsTest, Write01, Function | MediumTest | Level3)
173 {
174 DisplayId id = DisplayManager::GetInstance().GetDefaultDisplayId();
175 std::shared_ptr<Media::PixelMap> pixelMap = DisplayManager::GetInstance().GetScreenshot(id);
176 ASSERT_NE(nullptr, pixelMap);
177 ASSERT_EQ(true, SnapShotUtils::WriteToJpegWithPixelMap(defaultFile_, *pixelMap));
178 }
179
180 /**
181 * @tc.name: Write02
182 * @tc.desc: Write default jpeg using valid file names and valid WriteToJpegParam
183 * @tc.type: FUNC
184 */
185 HWTEST_F(SnapshotUtilsTest, Write02, Function | MediumTest | Level3)
186 {
187 DisplayId id = DisplayManager::GetInstance().GetDefaultDisplayId();
188 std::shared_ptr<Media::PixelMap> pixelMap = DisplayManager::GetInstance().GetScreenshot(id);
189 ASSERT_NE(nullptr, pixelMap);
190 WriteToJpegParam param = {
191 .width = pixelMap->GetWidth(),
192 .height = pixelMap->GetHeight(),
193 .stride = pixelMap->GetRowBytes(),
194 .format = pixelMap->GetPixelFormat(),
195 .data = pixelMap->GetPixels()
196 };
197 ASSERT_EQ(true, SnapShotUtils::WriteToJpeg(defaultFile_, param));
198 }
199
200 /**
201 * @tc.name: Write03
202 * @tc.desc: Write custom jpeg using valid file names and valid WriteToJpegParam
203 * @tc.type: FUNC
204 */
205 HWTEST_F(SnapshotUtilsTest, Write03, Function | MediumTest | Level3)
206 {
207 DisplayId id = DisplayManager::GetInstance().GetDefaultDisplayId();
208 std::shared_ptr<Media::PixelMap> pixelMap = DisplayManager::GetInstance().GetScreenshot(id);
209 ASSERT_NE(nullptr, pixelMap);
210 WriteToJpegParam param = {
211 .width = (pixelMap->GetWidth() / 2),
212 .height = (pixelMap->GetWidth() / 2),
213 .stride = pixelMap->GetRowBytes(),
214 .format = pixelMap->GetPixelFormat(),
215 .data = pixelMap->GetPixels()
216 };
217 ASSERT_EQ(false, SnapShotUtils::WriteToJpeg(defaultFile_, param));
218 }
219
220 /**
221 * @tc.name: Write04
222 * @tc.desc: Write pixel map with jpeg, using fd
223 * @tc.type: FUNC
224 */
225 HWTEST_F(SnapshotUtilsTest, Write04, Function | MediumTest | Level3)
226 {
227 DisplayId id = DisplayManager::GetInstance().GetDefaultDisplayId();
228 std::shared_ptr<Media::PixelMap> pixelMap = DisplayManager::GetInstance().GetScreenshot(id);
229 ASSERT_NE(nullptr, pixelMap);
230 int fd = open(defaultFile_.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
231 if (fd == -1) {
232 return;
233 }
234 ASSERT_EQ(true, SnapShotUtils::WriteToJpegWithPixelMap(fd, *pixelMap));
235 close(fd);
236 }
237
238 /**
239 * @tc.name: Write05
240 * @tc.desc: Write custom jpeg using invalid file names and valid WriteToJpegParam
241 * @tc.type: FUNC
242 */
243 HWTEST_F(SnapshotUtilsTest, Write05, Function | MediumTest | Level3)
244 {
245 WriteToJpegParam param = {
246 .width = 256,
247 .height = 256,
248 .stride = 256 * BPP,
249 .format = Media::PixelFormat::UNKNOWN,
250 .data = new uint8_t
251 };
252 ASSERT_FALSE(SnapShotUtils::WriteToJpeg("", param));
253 }
254
255 /**
256 * @tc.name: Write06
257 * @tc.desc: Write custom jpeg using valid file names and invalid WriteToJpegParam
258 * @tc.type: FUNC
259 */
260 HWTEST_F(SnapshotUtilsTest, Write06, Function | MediumTest | Level3)
261 {
262 WriteToJpegParam param = {
263 .width = 256,
264 .height = 256,
265 .stride = 256 * BPP,
266 .format = Media::PixelFormat::UNKNOWN,
267 .data = nullptr
268 };
269 ASSERT_FALSE(SnapShotUtils::WriteToJpeg(defaultFile_, param));
270 }
271
272 /**
273 * @tc.name: Write07
274 * @tc.desc: Write custom jpeg using valid fd and invalid WriteToJpegParam
275 * @tc.type: FUNC
276 */
277 HWTEST_F(SnapshotUtilsTest, Write07, Function | MediumTest | Level3)
278 {
279 WriteToJpegParam param = {
280 .width = 256,
281 .height = 256,
282 .stride = 256 * BPP,
283 .format = Media::PixelFormat::UNKNOWN,
284 .data = nullptr
285 };
286 ASSERT_FALSE(SnapShotUtils::WriteToJpeg(1, param));
287 }
288
289 /**
290 * @tc.name: Write08
291 * @tc.desc: Write custom jpeg using invalid file names and valid WriteToJpegParam
292 * @tc.type: FUNC
293 */
294 HWTEST_F(SnapshotUtilsTest, Write08, Function | MediumTest | Level3)
295 {
296 WriteToJpegParam param = {
297 .width = 256,
298 .height = 256,
299 .stride = 256 * RGB565_PIXEL_BYTES,
300 .format = Media::PixelFormat::RGB_565,
301 .data = new uint8_t
302 };
303 ASSERT_FALSE(SnapShotUtils::WriteToJpeg("", param));
304 }
305
306 /**
307 * @tc.name: Write09
308 * @tc.desc: Write custom jpeg using valid file names and invalid WriteToJpegParam
309 * @tc.type: FUNC
310 */
311 HWTEST_F(SnapshotUtilsTest, Write09, Function | MediumTest | Level3)
312 {
313 WriteToJpegParam param = {
314 .width = 256,
315 .height = 256,
316 .stride = 256 * RGB565_PIXEL_BYTES,
317 .format = Media::PixelFormat::RGB_565,
318 .data = nullptr
319 };
320 ASSERT_FALSE(SnapShotUtils::WriteToJpeg(defaultFile_, param));
321 }
322
323 /**
324 * @tc.name: Write10
325 * @tc.desc: Write custom jpeg using valid fd and invalid WriteToJpegParam
326 * @tc.type: FUNC
327 */
328 HWTEST_F(SnapshotUtilsTest, Write10, Function | MediumTest | Level3)
329 {
330 WriteToJpegParam param = {
331 .width = 256,
332 .height = 256,
333 .stride = 256 * RGB565_PIXEL_BYTES,
334 .format = Media::PixelFormat::RGB_565,
335 .data = nullptr
336 };
337 ASSERT_FALSE(SnapShotUtils::WriteToJpeg(1, param));
338 }
339
340 /**
341 * @tc.name: CheckWHValid
342 * @tc.desc: Check width and height whether valid
343 * @tc.type: FUNC
344 */
345 HWTEST_F(SnapshotUtilsTest, CheckWHValid, Function | SmallTest | Level3)
346 {
347 ASSERT_EQ(false, SnapShotUtils::CheckWHValid(0));
348 ASSERT_EQ(true, SnapShotUtils::CheckWHValid(DisplayManager::MAX_RESOLUTION_SIZE_SCREENSHOT));
349 ASSERT_EQ(false, SnapShotUtils::CheckWHValid(DisplayManager::MAX_RESOLUTION_SIZE_SCREENSHOT + 1));
350 }
351
352 /**
353 * @tc.name: CheckParamValid01
354 * @tc.desc: Check jpeg param whether valid width
355 * @tc.type: FUNC
356 */
357 HWTEST_F(SnapshotUtilsTest, CheckParamValid01, Function | SmallTest | Level3)
358 {
359 WriteToJpegParam paramInvalidWidth = {
360 .width = DisplayManager::MAX_RESOLUTION_SIZE_SCREENSHOT + 1,
361 .height = 0,
362 .stride = 0,
363 .format = Media::PixelFormat::UNKNOWN,
364 .data = nullptr
365 };
366 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidWidth));
367 }
368
369 /**
370 * @tc.name: CheckParamValid02
371 * @tc.desc: Check jpeg param whether valid height
372 * @tc.type: FUNC
373 */
374 HWTEST_F(SnapshotUtilsTest, CheckParamValid02, Function | SmallTest | Level3)
375 {
376 WriteToJpegParam paramInvalidHeight = {
377 .width = DisplayManager::MAX_RESOLUTION_SIZE_SCREENSHOT,
378 .height = 0,
379 .stride = 0,
380 .format = Media::PixelFormat::UNKNOWN,
381 .data = nullptr
382 };
383 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidHeight));
384 }
385
386 /**
387 * @tc.name: CheckParamValid03
388 * @tc.desc: Check jpeg param whether valid stride
389 * @tc.type: FUNC
390 */
391 HWTEST_F(SnapshotUtilsTest, CheckParamValid03, Function | SmallTest | Level3)
392 {
393 WriteToJpegParam paramInvalidStride = {
394 .width = 256,
395 .height = 256,
396 .stride = 1,
397 .format = Media::PixelFormat::UNKNOWN,
398 .data = nullptr
399 };
400 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidStride));
401 }
402
403 /**
404 * @tc.name: CheckParamValid04
405 * @tc.desc: Check jpeg param whether valid data
406 * @tc.type: FUNC
407 */
408 HWTEST_F(SnapshotUtilsTest, CheckParamValid04, Function | SmallTest | Level3)
409 {
410 WriteToJpegParam paramInvalidData = {
411 .width = 256,
412 .height = 256,
413 .stride = 256 * BPP,
414 .format = Media::PixelFormat::UNKNOWN,
415 .data = nullptr
416 };
417 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidData));
418 }
419
420 /**
421 * @tc.name: CheckParamValid05
422 * @tc.desc: Check jpeg param whether valid data
423 * @tc.type: FUNC
424 */
425 HWTEST_F(SnapshotUtilsTest, CheckParamValid05, Function | SmallTest | Level3)
426 {
427 WriteToJpegParam paramInvalidData = {
428 .width = 256,
429 .height = 256,
430 .stride = 256 * RGB565_PIXEL_BYTES,
431 .format = Media::PixelFormat::RGB_565,
432 .data = nullptr
433 };
434 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidData));
435 }
436
437 /**
438 * @tc.name: CheckParamValid06
439 * @tc.desc: Check jpeg param whether valid data
440 * @tc.type: FUNC
441 */
442 HWTEST_F(SnapshotUtilsTest, CheckParamValid06, Function | SmallTest | Level3)
443 {
444 WriteToJpegParam paramInvalidData = {
445 .width = 256,
446 .height = 256,
447 .stride = 1,
448 .format = Media::PixelFormat::RGB_565,
449 .data = nullptr
450 };
451 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidData));
452 }
453
454 /**
455 * @tc.name: CheckParamValid07
456 * @tc.desc: Check jpeg param whether valid data
457 * @tc.type: FUNC
458 */
459 HWTEST_F(SnapshotUtilsTest, CheckParamValid07, Function | SmallTest | Level3)
460 {
461 WriteToJpegParam paramInvalidData = {
462 .width = 256,
463 .height = 256,
464 .stride = 256 * RGB888_PIXEL_BYTES,
465 .format = Media::PixelFormat::RGB_888,
466 .data = nullptr
467 };
468 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidData));
469 }
470
471 /**
472 * @tc.name: CheckParamValid08
473 * @tc.desc: Check jpeg param whether valid data
474 * @tc.type: FUNC
475 */
476 HWTEST_F(SnapshotUtilsTest, CheckParamValid08, Function | SmallTest | Level3)
477 {
478 WriteToJpegParam paramInvalidData = {
479 .width = 256,
480 .height = 256,
481 .stride = 1,
482 .format = Media::PixelFormat::RGB_888,
483 .data = nullptr
484 };
485 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidData));
486 }
487
488 /**
489 * @tc.name: CheckParamValid09
490 * @tc.desc: Check jpeg param whether valid width and height
491 * @tc.type: FUNC
492 */
493 HWTEST_F(SnapshotUtilsTest, CheckParamValid09, Function | SmallTest | Level3)
494 {
495 WriteToJpegParam paramInvalidWidthAndHeight = {
496 .width = 0,
497 .height = 0,
498 .stride = 0,
499 .format = Media::PixelFormat::RGBA_8888,
500 .data = nullptr
501 };
502 ASSERT_EQ(false, SnapShotUtils::CheckParamValid(paramInvalidWidthAndHeight));
503 }
504
505 /**
506 * @tc.name: ProcessDisplayId01
507 * @tc.desc: Check RGBA8888ToRGB888
508 * @tc.type: FUNC
509 */
510 HWTEST_F(SnapshotUtilsTest, ProcessDisplayId01, Function | SmallTest | Level3)
511 {
512 Rosen::DisplayId displayId = 1;
513 bool isDisplayIdSet = false;
514 ASSERT_EQ(true, SnapShotUtils::ProcessDisplayId(displayId, isDisplayIdSet));
515 isDisplayIdSet = true;
516 ASSERT_EQ(true, SnapShotUtils::ProcessDisplayId(displayId, isDisplayIdSet));
517 displayId = DisplayManager::GetInstance().GetDefaultDisplayId();
518 ASSERT_EQ(true, SnapShotUtils::ProcessDisplayId(displayId, isDisplayIdSet));
519 }
520 }
521 } // namespace Rosen
522 } // namespace OHOS