1// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/image_fetcher/ios/ios_image_decoder_impl.h"
6
7#import <UIKit/UIKit.h>
8
9#include "base/bind.h"
10#include "base/macros.h"
11#include "base/test/task_environment.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "testing/platform_test.h"
14#include "ui/gfx/geometry/size.h"
15#include "ui/gfx/image/image.h"
16
17#if !defined(__has_feature) || !__has_feature(objc_arc)
18#error "This file requires ARC support."
19#endif
20
21namespace {
22
23static unsigned char kJPGImage[] = {
24    255, 216, 255, 224, 0,   16,  74,  70, 73, 70, 0,   1,   1,   1,   0,
25    72,  0,   72,  0,   0,   255, 254, 0,  19, 67, 114, 101, 97,  116, 101,
26    100, 32,  119, 105, 116, 104, 32,  71, 73, 77, 80,  255, 219, 0,   67,
27    0,   5,   3,   4,   4,   4,   3,   5,  4,  4,  4,   5,   5,   5,   6,
28    7,   12,  8,   7,   7,   7,   7,   15, 11, 11, 9,   12,  17,  15,  18,
29    18,  17,  15,  17,  17,  19,  22,  28, 23, 19, 20,  26,  21,  17,  17,
30    24,  33,  24,  26,  29,  29,  31,  31, 31, 19, 23,  34,  36,  34,  30,
31    36,  28,  30,  31,  30,  255, 219, 0,  67, 1,  5,   5,   5,   7,   6,
32    7,   14,  8,   8,   14,  30,  20,  17, 20, 30, 30,  30,  30,  30,  30,
33    30,  30,  30,  30,  30,  30,  30,  30, 30, 30, 30,  30,  30,  30,  30,
34    30,  30,  30,  30,  30,  30,  30,  30, 30, 30, 30,  30,  30,  30,  30,
35    30,  30,  30,  30,  30,  30,  30,  30, 30, 30, 30,  30,  30,  30,  255,
36    192, 0,   17,  8,   0,   1,   0,   1,  3,  1,  34,  0,   2,   17,  1,
37    3,   17,  1,   255, 196, 0,   21,  0,  1,  1,  0,   0,   0,   0,   0,
38    0,   0,   0,   0,   0,   0,   0,   0,  0,  0,  8,   255, 196, 0,   20,
39    16,  1,   0,   0,   0,   0,   0,   0,  0,  0,  0,   0,   0,   0,   0,
40    0,   0,   0,   255, 196, 0,   20,  1,  1,  0,  0,   0,   0,   0,   0,
41    0,   0,   0,   0,   0,   0,   0,   0,  0,  0,  255, 196, 0,   20,  17,
42    1,   0,   0,   0,   0,   0,   0,   0,  0,  0,  0,   0,   0,   0,   0,
43    0,   0,   255, 218, 0,   12,  3,   1,  0,  2,  17,  3,   17,  0,   63,
44    0,   178, 192, 7,   255, 217};
45
46static unsigned char kWEBPImage[] = {
47    82, 73, 70, 70, 74,  0, 0,  0,   87,  69,  66,  80,  86,  80, 56, 88, 10,
48    0,  0,  0,  16, 0,   0, 0,  0,   0,   0,   0,   0,   0,   65, 76, 80, 72,
49    12, 0,  0,  0,  1,   7, 16, 17,  253, 15,  68,  68,  255, 3,  0,  0,  86,
50    80, 56, 32, 24, 0,   0, 0,  48,  1,   0,   157, 1,   42,  1,  0,  1,  0,
51    3,  0,  52, 37, 164, 0, 3,  112, 0,   254, 251, 253, 80,  0};
52
53}  // namespace
54
55namespace image_fetcher {
56
57class IOSImageDecoderImplTest : public PlatformTest {
58 public:
59  void OnImageDecoded(const gfx::Image& image) { decoded_image_ = image; }
60
61 protected:
62  IOSImageDecoderImplTest() {
63    ios_image_decoder_impl_ = CreateIOSImageDecoder();
64  }
65
66  ~IOSImageDecoderImplTest() override {}
67
68  base::test::TaskEnvironment scoped_task_evironment_;
69  std::unique_ptr<ImageDecoder> ios_image_decoder_impl_;
70
71  gfx::Image decoded_image_;
72};
73
74TEST_F(IOSImageDecoderImplTest, JPGImage) {
75  ASSERT_TRUE(decoded_image_.IsEmpty());
76
77  std::string image_data =
78      std::string(reinterpret_cast<char*>(kJPGImage), sizeof(kJPGImage));
79  ios_image_decoder_impl_->DecodeImage(
80      image_data, gfx::Size(),
81      base::BindOnce(&IOSImageDecoderImplTest::OnImageDecoded,
82                     base::Unretained(this)));
83
84  scoped_task_evironment_.RunUntilIdle();
85
86  EXPECT_FALSE(decoded_image_.IsEmpty());
87}
88
89TEST_F(IOSImageDecoderImplTest, WebpImage) {
90  ASSERT_TRUE(decoded_image_.IsEmpty());
91
92  std::string image_data =
93      std::string(reinterpret_cast<char*>(kWEBPImage), sizeof(kWEBPImage));
94  ios_image_decoder_impl_->DecodeImage(
95      image_data, gfx::Size(),
96      base::BindOnce(&IOSImageDecoderImplTest::OnImageDecoded,
97                     base::Unretained(this)));
98
99  scoped_task_evironment_.RunUntilIdle();
100
101  EXPECT_FALSE(decoded_image_.IsEmpty());
102}
103
104}  // namespace image_fetcher
105