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