• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include "include/cef_image.h"
6 #include "tests/ceftests/image_util.h"
7 #include "tests/gtest/include/gtest/gtest.h"
8 
9 namespace {
10 
11 // The expected image size in device independent pixels (DIPs).
12 const int kExpectedDIPSize = 16;
13 
LoadImage(CefRefPtr<CefImage> image,double scale_factor)14 void LoadImage(CefRefPtr<CefImage> image, double scale_factor) {
15   image_util::LoadIconImage(image, scale_factor);
16 }
17 
VerifyScaleEmpty(CefRefPtr<CefImage> image,float scale_factor)18 void VerifyScaleEmpty(CefRefPtr<CefImage> image, float scale_factor) {
19   float actual_scale_factor = 0.0f;
20   int pixel_width = 0;
21   int pixel_height = 0;
22 
23   EXPECT_FALSE(image->HasRepresentation(scale_factor));
24   EXPECT_FALSE(image->GetRepresentationInfo(scale_factor, actual_scale_factor,
25                                             pixel_width, pixel_height));
26   EXPECT_EQ(0.0f, actual_scale_factor);
27   EXPECT_EQ(0, pixel_width);
28   EXPECT_EQ(0, pixel_height);
29   EXPECT_FALSE(image->RemoveRepresentation(scale_factor));
30 }
31 
VerifyScaleExists(CefRefPtr<CefImage> image,float scale_factor,float expected_scale_factor)32 void VerifyScaleExists(CefRefPtr<CefImage> image,
33                        float scale_factor,
34                        float expected_scale_factor) {
35   float actual_scale_factor = 0.0f;
36   int pixel_width = 0;
37   int pixel_height = 0;
38   int expected_pixel_size = kExpectedDIPSize * expected_scale_factor;
39 
40   // Only returns true for exact matches.
41   if (scale_factor == expected_scale_factor)
42     EXPECT_TRUE(image->HasRepresentation(scale_factor));
43   else
44     EXPECT_FALSE(image->HasRepresentation(scale_factor));
45 
46   // Returns the closest match.
47   EXPECT_TRUE(image->GetRepresentationInfo(scale_factor, actual_scale_factor,
48                                            pixel_width, pixel_height));
49   EXPECT_EQ(expected_scale_factor, actual_scale_factor);
50   EXPECT_EQ(expected_pixel_size, pixel_width);
51   EXPECT_EQ(expected_pixel_size, pixel_height);
52 
53   // Only returns true for exact matches.
54   if (scale_factor == expected_scale_factor) {
55     EXPECT_TRUE(image->RemoveRepresentation(scale_factor));
56     EXPECT_FALSE(image->HasRepresentation(scale_factor));
57   } else {
58     EXPECT_FALSE(image->RemoveRepresentation(scale_factor));
59   }
60 }
61 
VerifySaveAsBitmap(CefRefPtr<CefImage> image,float scale_factor,float expected_scale_factor)62 void VerifySaveAsBitmap(CefRefPtr<CefImage> image,
63                         float scale_factor,
64                         float expected_scale_factor) {
65   int pixel_width = 0;
66   int pixel_height = 0;
67   int expected_pixel_size = kExpectedDIPSize * expected_scale_factor;
68   size_t expected_data_size = expected_pixel_size * expected_pixel_size * 4U;
69 
70   CefRefPtr<CefBinaryValue> value = image->GetAsBitmap(
71       scale_factor, CEF_COLOR_TYPE_RGBA_8888, CEF_ALPHA_TYPE_PREMULTIPLIED,
72       pixel_width, pixel_height);
73   EXPECT_TRUE(value.get());
74   size_t data_size = value->GetSize();
75   EXPECT_EQ(expected_data_size, data_size);
76   EXPECT_EQ(expected_pixel_size, pixel_width);
77   EXPECT_EQ(expected_pixel_size, pixel_height);
78 
79   std::vector<unsigned char> data(data_size);
80   value->GetData(&data[0], data_size, 0U);
81 
82   CefRefPtr<CefImage> image2 = CefImage::CreateImage();
83   EXPECT_TRUE(image2.get());
84   EXPECT_TRUE(image2->AddBitmap(expected_scale_factor, pixel_width,
85                                 pixel_height, CEF_COLOR_TYPE_RGBA_8888,
86                                 CEF_ALPHA_TYPE_PREMULTIPLIED, &data[0],
87                                 data_size));
88   VerifyScaleExists(image2, expected_scale_factor, expected_scale_factor);
89 }
90 
VerifySaveAsPNG(CefRefPtr<CefImage> image,float scale_factor,float expected_scale_factor)91 void VerifySaveAsPNG(CefRefPtr<CefImage> image,
92                      float scale_factor,
93                      float expected_scale_factor) {
94   int pixel_width = 0;
95   int pixel_height = 0;
96   int expected_pixel_size = kExpectedDIPSize * expected_scale_factor;
97 
98   CefRefPtr<CefBinaryValue> value =
99       image->GetAsPNG(scale_factor, true, pixel_width, pixel_height);
100   EXPECT_TRUE(value.get());
101   size_t data_size = value->GetSize();
102   EXPECT_GT(data_size, 0U);
103   EXPECT_EQ(expected_pixel_size, pixel_width);
104   EXPECT_EQ(expected_pixel_size, pixel_height);
105 
106   std::vector<unsigned char> data(data_size);
107   value->GetData(&data[0], data_size, 0U);
108 
109   CefRefPtr<CefImage> image2 = CefImage::CreateImage();
110   EXPECT_TRUE(image2.get());
111   EXPECT_TRUE(image2->AddPNG(expected_scale_factor, &data[0], data_size));
112   VerifyScaleExists(image2, expected_scale_factor, expected_scale_factor);
113 }
114 
VerifySaveAsJPEG(CefRefPtr<CefImage> image,float scale_factor,float expected_scale_factor)115 void VerifySaveAsJPEG(CefRefPtr<CefImage> image,
116                       float scale_factor,
117                       float expected_scale_factor) {
118   int pixel_width = 0;
119   int pixel_height = 0;
120   int expected_pixel_size = kExpectedDIPSize * expected_scale_factor;
121 
122   CefRefPtr<CefBinaryValue> value =
123       image->GetAsJPEG(scale_factor, 80, pixel_width, pixel_height);
124   EXPECT_TRUE(value.get());
125   size_t data_size = value->GetSize();
126   EXPECT_GT(data_size, 0U);
127   EXPECT_EQ(expected_pixel_size, pixel_width);
128   EXPECT_EQ(expected_pixel_size, pixel_height);
129 
130   std::vector<unsigned char> data(data_size);
131   value->GetData(&data[0], data_size, 0U);
132 
133   CefRefPtr<CefImage> image2 = CefImage::CreateImage();
134   EXPECT_TRUE(image2.get());
135   EXPECT_TRUE(image2->AddJPEG(expected_scale_factor, &data[0], data_size));
136   VerifyScaleExists(image2, expected_scale_factor, expected_scale_factor);
137 }
138 
139 }  // namespace
140 
TEST(ImageTest,Empty)141 TEST(ImageTest, Empty) {
142   CefRefPtr<CefImage> image = CefImage::CreateImage();
143   EXPECT_TRUE(image.get());
144 
145   // An image is the same as itself.
146   EXPECT_TRUE(image->IsSame(image));
147 
148   EXPECT_TRUE(image->IsEmpty());
149   EXPECT_EQ(0U, image->GetWidth());
150   EXPECT_EQ(0U, image->GetHeight());
151 
152   // Empty images are the same.
153   CefRefPtr<CefImage> image2 = CefImage::CreateImage();
154   EXPECT_TRUE(image->IsSame(image2));
155   EXPECT_TRUE(image2->IsSame(image));
156 
157   // 1x scale does not exist.
158   VerifyScaleEmpty(image, 1.0f);
159 
160   // 2x scale does not exist.
161   VerifyScaleEmpty(image, 2.0f);
162 }
163 
TEST(ImageTest,Scale1x)164 TEST(ImageTest, Scale1x) {
165   CefRefPtr<CefImage> image = CefImage::CreateImage();
166   EXPECT_TRUE(image.get());
167 
168   LoadImage(image, 1.0f);
169 
170   // 1x scale should exist.
171   VerifyScaleExists(image, 1.0f, 1.0f);
172 
173   // 2x scale should not exist.
174   VerifyScaleEmpty(image, 2.0f);
175 }
176 
TEST(ImageTest,Scale2x)177 TEST(ImageTest, Scale2x) {
178   CefRefPtr<CefImage> image = CefImage::CreateImage();
179   EXPECT_TRUE(image.get());
180 
181   LoadImage(image, 2.0f);
182 
183   // 1x scale should return the 2x image.
184   VerifyScaleExists(image, 1.0f, 2.0f);
185 
186   // 2x scale should exist.
187   VerifyScaleExists(image, 2.0f, 2.0f);
188 }
189 
TEST(ImageTest,ScaleMulti)190 TEST(ImageTest, ScaleMulti) {
191   CefRefPtr<CefImage> image = CefImage::CreateImage();
192   EXPECT_TRUE(image.get());
193 
194   LoadImage(image, 1.0f);
195   LoadImage(image, 2.0f);
196 
197   // 1x scale should exist.
198   VerifyScaleExists(image, 1.0f, 1.0f);
199 
200   // 2x scale should exist.
201   VerifyScaleExists(image, 2.0f, 2.0f);
202 }
203 
TEST(ImageTest,SaveBitmap1x)204 TEST(ImageTest, SaveBitmap1x) {
205   CefRefPtr<CefImage> image = CefImage::CreateImage();
206   EXPECT_TRUE(image.get());
207 
208   LoadImage(image, 1.0f);
209 
210   VerifySaveAsBitmap(image, 1.0f, 1.0f);
211 }
212 
TEST(ImageTest,SaveBitmap2x)213 TEST(ImageTest, SaveBitmap2x) {
214   CefRefPtr<CefImage> image = CefImage::CreateImage();
215   EXPECT_TRUE(image.get());
216 
217   LoadImage(image, 2.0f);
218 
219   VerifySaveAsBitmap(image, 2.0f, 2.0f);
220 }
221 
TEST(ImageTest,SaveBitmapMulti)222 TEST(ImageTest, SaveBitmapMulti) {
223   CefRefPtr<CefImage> image = CefImage::CreateImage();
224   EXPECT_TRUE(image.get());
225 
226   LoadImage(image, 2.0f);
227 
228   VerifySaveAsBitmap(image, 1.0f, 2.0f);
229 }
230 
TEST(ImageTest,SavePNG1x)231 TEST(ImageTest, SavePNG1x) {
232   CefRefPtr<CefImage> image = CefImage::CreateImage();
233   EXPECT_TRUE(image.get());
234 
235   LoadImage(image, 1.0f);
236 
237   VerifySaveAsPNG(image, 1.0f, 1.0f);
238 }
239 
TEST(ImageTest,SavePNG2x)240 TEST(ImageTest, SavePNG2x) {
241   CefRefPtr<CefImage> image = CefImage::CreateImage();
242   EXPECT_TRUE(image.get());
243 
244   LoadImage(image, 2.0f);
245 
246   VerifySaveAsPNG(image, 2.0f, 2.0f);
247 }
248 
TEST(ImageTest,SavePNGMulti)249 TEST(ImageTest, SavePNGMulti) {
250   CefRefPtr<CefImage> image = CefImage::CreateImage();
251   EXPECT_TRUE(image.get());
252 
253   LoadImage(image, 2.0f);
254 
255   VerifySaveAsPNG(image, 1.0f, 2.0f);
256 }
257 
TEST(ImageTest,SaveJPEG1x)258 TEST(ImageTest, SaveJPEG1x) {
259   CefRefPtr<CefImage> image = CefImage::CreateImage();
260   EXPECT_TRUE(image.get());
261 
262   LoadImage(image, 1.0f);
263 
264   VerifySaveAsJPEG(image, 1.0f, 1.0f);
265 }
266 
TEST(ImageTest,SaveJPEG2x)267 TEST(ImageTest, SaveJPEG2x) {
268   CefRefPtr<CefImage> image = CefImage::CreateImage();
269   EXPECT_TRUE(image.get());
270 
271   LoadImage(image, 2.0f);
272 
273   VerifySaveAsJPEG(image, 2.0f, 2.0f);
274 }
275 
TEST(ImageTest,SaveJPEGMulti)276 TEST(ImageTest, SaveJPEGMulti) {
277   CefRefPtr<CefImage> image = CefImage::CreateImage();
278   EXPECT_TRUE(image.get());
279 
280   LoadImage(image, 2.0f);
281 
282   VerifySaveAsJPEG(image, 1.0f, 2.0f);
283 }
284