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