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