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  */
HWTEST_F(SnapshotUtilsTest, Check01, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Check02, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Check03, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, RGBA8888ToRGB88801, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, RGB565ToRGB888, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, WriteRgb888ToJpeg01, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, WriteRgb888ToJpeg02, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Write01, Function | MediumTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Write02, Function | MediumTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Write03, Function | MediumTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Write04, Function | MediumTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Write05, Function | MediumTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Write06, Function | MediumTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Write07, Function | MediumTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Write08, Function | MediumTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Write09, Function | MediumTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, Write10, Function | MediumTest | Level3)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 /**
341  * @tc.name: CheckWHValid
342  * @tc.desc: Check width and height whether valid
343  * @tc.type: FUNC
344  */
HWTEST_F(SnapshotUtilsTest, CheckWHValid, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, CheckParamValid01, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, CheckParamValid02, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, CheckParamValid03, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, CheckParamValid04, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, CheckParamValid05, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, CheckParamValid06, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, CheckParamValid07, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, CheckParamValid08, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, CheckParamValid09, Function | SmallTest | Level3)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  */
HWTEST_F(SnapshotUtilsTest, ProcessDisplayId01, Function | SmallTest | Level3)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