• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 <gtest/gtest.h>
17 #include "svg_decoder.h"
18 #include "buffer_source_stream.h"
19 
20 using namespace testing::ext;
21 using namespace OHOS::Media;
22 
23 namespace OHOS {
24 namespace ImagePlugin {
25 static constexpr size_t NUMBER_ONE = 1;
26 static constexpr size_t NUMBER_TWO = 2;
27 class SvgDecoderTest : public testing::Test {};
28 
29 class MockInputDataStream : public SourceStream {
30 public:
31     MockInputDataStream() = default;
~MockInputDataStream()32     ~MockInputDataStream() {}
33 
UpdateData(const uint8_t * data,uint32_t size,bool isCompleted)34     uint32_t UpdateData(const uint8_t *data, uint32_t size, bool isCompleted) override
35     {
36         return ERR_IMAGE_DATA_UNSUPPORT;
37     }
38 
Read(uint32_t desiredSize,DataStreamBuffer & outData)39     bool Read(uint32_t desiredSize, DataStreamBuffer &outData) override
40     {
41         if (streamSize_ == NUMBER_ONE) {
42             streamBuffer_ = std::make_shared<uint8_t>(streamSize_);
43             outData.inputStreamBuffer = streamBuffer_.get();
44         } else if (streamSize_ == NUMBER_TWO) {
45             outData.dataSize = streamSize_;
46         }
47         return returnValue_;
48     }
49 
Read(uint32_t desiredSize,uint8_t * outBuffer,uint32_t bufferSize,uint32_t & readSize)50     bool Read(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) override
51     {
52         return returnValue_;
53     }
54 
Peek(uint32_t desiredSize,DataStreamBuffer & outData)55     bool Peek(uint32_t desiredSize, DataStreamBuffer &outData) override
56     {
57         return returnValue_;
58     }
59 
Peek(uint32_t desiredSize,uint8_t * outBuffer,uint32_t bufferSize,uint32_t & readSize)60     bool Peek(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) override
61     {
62         return returnValue_;
63     }
64 
Tell()65     uint32_t Tell() override
66     {
67         return 0;
68     }
69 
Seek(uint32_t position)70     bool Seek(uint32_t position) override
71     {
72         return returnValue_;
73     }
74 
GetStreamType()75     uint32_t GetStreamType()
76     {
77         return -1;
78     }
79 
GetDataPtr()80     uint8_t *GetDataPtr()
81     {
82         return nullptr;
83     }
84 
IsStreamCompleted()85     bool IsStreamCompleted()
86     {
87         return returnValue_;
88     }
89 
GetStreamSize()90     size_t GetStreamSize()
91     {
92         return streamSize_;
93     }
94 
SetReturn(bool returnValue)95     void SetReturn(bool returnValue)
96     {
97         returnValue_ = returnValue;
98     }
99 
SetStreamSize(size_t size)100     void SetStreamSize(size_t size)
101     {
102         streamSize_ = size;
103     }
104 
105 private:
106     bool returnValue_ = false;
107     size_t streamSize_ = 0;
108     std::shared_ptr<uint8_t> streamBuffer_ = nullptr;
109 };
110 
111 /**
112  * @tc.name: GetImageSizeTest001
113  * @tc.desc: Test of GetImageSize
114  * @tc.type: FUNC
115  */
116 HWTEST_F(SvgDecoderTest, GetImageSizeTest001, TestSize.Level3)
117 {
118     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest001 start";
119     auto svgDecoder = std::make_shared<SvgDecoder>();
120     int size = 1000;
121     auto data = std::make_unique<uint8_t[]>(size);
122     auto streamPtr = BufferSourceStream::CreateSourceStream(data.get(), size);
123     ImagePlugin::PlSize plSize;
124     svgDecoder->SetSource(*streamPtr.release());
125     svgDecoder->GetImageSize(2, plSize);
126     bool result = (svgDecoder != nullptr);
127     ASSERT_EQ(result, true);
128     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest001 end";
129 }
130 
131 /**
132  * @tc.name: GetImageSizeTest002
133  * @tc.desc: Test of GetImageSize
134  * @tc.type: FUNC
135  */
136 HWTEST_F(SvgDecoderTest, GetImageSizeTest002, TestSize.Level3)
137 {
138     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest002 start";
139     auto svgDecoder = std::make_shared<SvgDecoder>();
140     ImagePlugin::PlSize plSize;
141     svgDecoder->GetImageSize(0, plSize);
142     int size = 1000;
143     auto data = std::make_unique<uint8_t[]>(size);
144     auto streamPtr = BufferSourceStream::CreateSourceStream(data.get(), size);
145     svgDecoder->SetSource(*streamPtr.release());
146     bool result = (svgDecoder != nullptr);
147     ASSERT_EQ(result, true);
148     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest002 end";
149 }
150 
151 /**
152  * @tc.name: GetImageSizeTest003
153  * @tc.desc: Test of GetImageSize
154  * @tc.type: FUNC
155  */
156 HWTEST_F(SvgDecoderTest, GetImageSizeTest003, TestSize.Level3)
157 {
158     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest003 start";
159     auto svgDecoder = std::make_shared<SvgDecoder>();
160     auto mock = std::make_shared<MockInputDataStream>();
161     svgDecoder->SetSource(*mock.get());
162     ImagePlugin::PlSize plSize;
163     svgDecoder->GetImageSize(0, plSize);
164     bool result = (svgDecoder != nullptr);
165     ASSERT_EQ(result, true);
166     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest003 end";
167 }
168 
169 /**
170  * @tc.name: GetImageSizeTest004
171  * @tc.desc: Test of GetImageSize
172  * @tc.type: FUNC
173  */
174 HWTEST_F(SvgDecoderTest, GetImageSizeTest004, TestSize.Level3)
175 {
176     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest004 start";
177     auto svgDecoder = std::make_shared<SvgDecoder>();
178     auto mock = std::make_shared<MockInputDataStream>();
179     mock->SetReturn(true);
180     svgDecoder->SetSource(*mock.get());
181     ImagePlugin::PlSize plSize;
182     svgDecoder->GetImageSize(0, plSize);
183     bool result = (svgDecoder != nullptr);
184     ASSERT_EQ(result, true);
185     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest004 end";
186 }
187 
188 /**
189  * @tc.name: GetImageSizeTest005
190  * @tc.desc: Test of GetImageSize
191  * @tc.type: FUNC
192  */
193 HWTEST_F(SvgDecoderTest, GetImageSizeTest005, TestSize.Level3)
194 {
195     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest005 start";
196     auto svgDecoder = std::make_shared<SvgDecoder>();
197     auto mock = std::make_shared<MockInputDataStream>();
198     mock->SetStreamSize(1);
199     mock->SetReturn(true);
200     svgDecoder->SetSource(*mock.get());
201     ImagePlugin::PlSize plSize;
202     svgDecoder->GetImageSize(0, plSize);
203     bool result = (svgDecoder != nullptr);
204     ASSERT_EQ(result, true);
205     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest005 end";
206 }
207 
208 /**
209  * @tc.name: GetImageSizeTest006
210  * @tc.desc: Test of GetImageSize
211  * @tc.type: FUNC
212  */
213 HWTEST_F(SvgDecoderTest, GetImageSizeTest006, TestSize.Level3)
214 {
215     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest006 start";
216     auto svgDecoder = std::make_shared<SvgDecoder>();
217     auto mock = std::make_shared<MockInputDataStream>();
218     mock->SetStreamSize(2);
219     mock->SetReturn(true);
220     svgDecoder->SetSource(*mock.get());
221     ImagePlugin::PlSize plSize;
222     svgDecoder->GetImageSize(0, plSize);
223     bool result = (svgDecoder != nullptr);
224     ASSERT_EQ(result, true);
225     GTEST_LOG_(INFO) << "SvgDecoderTest: GetImageSizeTest006 end";
226 }
227 
228 /**
229  * @tc.name: SetDecodeOptionsTest001
230  * @tc.desc: Test of SetDecodeOptions
231  * @tc.type: FUNC
232  */
233 HWTEST_F(SvgDecoderTest, SetDecodeOptionsTest001, TestSize.Level3)
234 {
235     GTEST_LOG_(INFO) << "SvgDecoderTest: SetDecodeOptionsTest001 start";
236     auto svgDecoder = std::make_shared<SvgDecoder>();
237     int size = 1000;
238     auto data = std::make_unique<uint8_t[]>(size);
239     auto streamPtr = BufferSourceStream::CreateSourceStream(data.get(), size);
240     svgDecoder->SetSource(*streamPtr.release());
241     PixelDecodeOptions opts;
242     PlImageInfo info;
243     svgDecoder->SetDecodeOptions(2, opts, info);
244     bool result = (svgDecoder != nullptr);
245     ASSERT_EQ(result, true);
246     GTEST_LOG_(INFO) << "SvgDecoderTest: SetDecodeOptionsTest001 end";
247 }
248 
249 /**
250  * @tc.name: SetDecodeOptionsTest002
251  * @tc.desc: Test of SetDecodeOptions
252  * @tc.type: FUNC
253  */
254 HWTEST_F(SvgDecoderTest, SetDecodeOptionsTest002, TestSize.Level3)
255 {
256     GTEST_LOG_(INFO) << "SvgDecoderTest: SetDecodeOptionsTest002 start";
257     auto svgDecoder = std::make_shared<SvgDecoder>();
258     PixelDecodeOptions opts;
259     PlImageInfo info;
260     svgDecoder->SetDecodeOptions(0, opts, info);
261     int size = 1000;
262     auto data = std::make_unique<uint8_t[]>(size);
263     auto streamPtr = BufferSourceStream::CreateSourceStream(data.get(), size);
264     svgDecoder->SetSource(*streamPtr.release());
265     bool result = (svgDecoder != nullptr);
266     ASSERT_EQ(result, true);
267     GTEST_LOG_(INFO) << "SvgDecoderTest: SetDecodeOptionsTest002 end";
268 }
269 
270 /**
271  * @tc.name: SetDecodeOptionsTest003
272  * @tc.desc: Test of SetDecodeOptions
273  * @tc.type: FUNC
274  */
275 HWTEST_F(SvgDecoderTest, SetDecodeOptionsTest003, TestSize.Level3)
276 {
277     GTEST_LOG_(INFO) << "SvgDecoderTest: SetDecodeOptionsTest003 start";
278     auto svgDecoder = std::make_shared<SvgDecoder>();
279     int size = 1000;
280     auto data = std::make_unique<uint8_t[]>(size);
281     auto streamPtr = BufferSourceStream::CreateSourceStream(data.get(), size);
282     svgDecoder->SetSource(*streamPtr.release());
283     PixelDecodeOptions opts;
284     opts.desiredPixelFormat = PlPixelFormat::RGB_565;
285     PlImageInfo info;
286     svgDecoder->SetDecodeOptions(0, opts, info);
287     bool result = (svgDecoder != nullptr);
288     ASSERT_EQ(result, true);
289     GTEST_LOG_(INFO) << "SvgDecoderTest: SetDecodeOptionsTest003 end";
290 }
291 
292 /**
293  * @tc.name: SetDecodeOptionsTest004
294  * @tc.desc: Test of SetDecodeOptions
295  * @tc.type: FUNC
296  */
297 HWTEST_F(SvgDecoderTest, SetDecodeOptionsTest004, TestSize.Level3)
298 {
299     GTEST_LOG_(INFO) << "SvgDecoderTest: SetDecodeOptionsTest004 start";
300     auto svgDecoder = std::make_shared<SvgDecoder>();
301     auto mock = std::make_shared<MockInputDataStream>();
302     mock->SetReturn(false);
303     svgDecoder->SetSource(*mock.get());
304     PixelDecodeOptions opts;
305     PlImageInfo info;
306     svgDecoder->SetDecodeOptions(0, opts, info);
307     bool result = (svgDecoder != nullptr);
308     ASSERT_EQ(result, true);
309     GTEST_LOG_(INFO) << "SvgDecoderTest: SetDecodeOptionsTest004 end";
310 }
311 
312 /**
313  * @tc.name: DecodeTest001
314  * @tc.desc: Test of Decode
315  * @tc.type: FUNC
316  */
317 HWTEST_F(SvgDecoderTest, DecodeTest001, TestSize.Level3)
318 {
319     GTEST_LOG_(INFO) << "SvgDecoderTest: DecodeTest001 start";
320     auto svgDecoder = std::make_shared<SvgDecoder>();
321     int size = 1000;
322     auto data = std::make_unique<uint8_t[]>(size);
323     auto streamPtr = BufferSourceStream::CreateSourceStream(data.get(), size);
324     svgDecoder->SetSource(*streamPtr.release());
325     DecodeContext context;
326     svgDecoder->Decode(2, context);
327     bool result = (svgDecoder != nullptr);
328     ASSERT_EQ(result, true);
329     GTEST_LOG_(INFO) << "SvgDecoderTest: DecodeTest001 end";
330 }
331 
332 /**
333  * @tc.name: DecodeTest002
334  * @tc.desc: Test of Decode
335  * @tc.type: FUNC
336  */
337 HWTEST_F(SvgDecoderTest, DecodeTest002, TestSize.Level3)
338 {
339     GTEST_LOG_(INFO) << "SvgDecoderTest: DecodeTest002 start";
340     auto svgDecoder = std::make_shared<SvgDecoder>();
341     DecodeContext context;
342     svgDecoder->Decode(0, context);
343     int size = 1000;
344     auto data = std::make_unique<uint8_t[]>(size);
345     auto streamPtr = BufferSourceStream::CreateSourceStream(data.get(), size);
346     svgDecoder->SetSource(*streamPtr.release());
347     bool result = (svgDecoder != nullptr);
348     ASSERT_EQ(result, true);
349     GTEST_LOG_(INFO) << "SvgDecoderTest: DecodeTest002 end";
350 }
351 
352 /**
353  * @tc.name: PromoteIncrementalDecodeTest001
354  * @tc.desc: Test of Decode
355  * @tc.type: FUNC
356  */
357 HWTEST_F(SvgDecoderTest, PromoteIncrementalDecodeTest001, TestSize.Level3)
358 {
359     GTEST_LOG_(INFO) << "SvgDecoderTest: PromoteIncrementalDecodeTest001 start";
360     auto svgDecoder = std::make_shared<SvgDecoder>();
361     int size = 1000;
362     auto data = std::make_unique<uint8_t[]>(size);
363     auto streamPtr = BufferSourceStream::CreateSourceStream(data.get(), size);
364     svgDecoder->SetSource(*streamPtr.release());
365     ProgDecodeContext context;
366     svgDecoder->PromoteIncrementalDecode(2, context);
367     bool result = (svgDecoder != nullptr);
368     ASSERT_EQ(result, true);
369     GTEST_LOG_(INFO) << "SvgDecoderTest: PromoteIncrementalDecodeTest001 end";
370 }
371 
372 /**
373  * @tc.name: PromoteIncrementalDecodeTest002
374  * @tc.desc: Test of Decode
375  * @tc.type: FUNC
376  */
377 HWTEST_F(SvgDecoderTest, PromoteIncrementalDecodeTest002, TestSize.Level3)
378 {
379     GTEST_LOG_(INFO) << "SvgDecoderTest: PromoteIncrementalDecodeTest002 start";
380     auto svgDecoder = std::make_shared<SvgDecoder>();
381     ProgDecodeContext context;
382     svgDecoder->PromoteIncrementalDecode(0, context);
383     int size = 1000;
384     auto data = std::make_unique<uint8_t[]>(size);
385     auto streamPtr = BufferSourceStream::CreateSourceStream(data.get(), size);
386     svgDecoder->SetSource(*streamPtr.release());
387     bool result = (svgDecoder != nullptr);
388     ASSERT_EQ(result, true);
389     GTEST_LOG_(INFO) << "SvgDecoderTest: PromoteIncrementalDecodeTest002 end";
390 }
391 
392 /**
393  * @tc.name: PromoteIncrementalDecodeTest003
394  * @tc.desc: Test of Decode
395  * @tc.type: FUNC
396  */
397 HWTEST_F(SvgDecoderTest, PromoteIncrementalDecodeTest003, TestSize.Level3)
398 {
399     GTEST_LOG_(INFO) << "SvgDecoderTest: PromoteIncrementalDecodeTest003 start";
400     auto svgDecoder = std::make_shared<SvgDecoder>();
401     auto mock = std::make_shared<MockInputDataStream>();
402     mock->SetReturn(false);
403     svgDecoder->SetSource(*mock.get());
404     ProgDecodeContext context;
405     svgDecoder->PromoteIncrementalDecode(0, context);
406     bool result = (svgDecoder != nullptr);
407     ASSERT_EQ(result, true);
408     GTEST_LOG_(INFO) << "SvgDecoderTest: PromoteIncrementalDecodeTest003 end";
409 }
410 }
411 }