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