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