[M73 Dev][EFL] Disable VizDisplayCompositor for EFL port
[platform/framework/web/chromium-efl.git] / components / favicon_base / select_favicon_frames_unittest.cc
1 // Copyright (c) 2012 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/favicon_base/select_favicon_frames.h"
6
7 #include <stddef.h>
8
9 #include "base/stl_util.h"
10 #include "build/build_config.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "third_party/skia/include/core/SkColor.h"
14 #include "ui/base/layout.h"
15 #include "ui/gfx/image/image_skia.h"
16
17 using std::vector;
18
19 namespace {
20
21 const ui::ScaleFactor FaviconScaleFactor1x[] = {
22   ui::SCALE_FACTOR_100P,
23 };
24
25 const ui::ScaleFactor FaviconScaleFactor1xAnd2x[] = {
26   ui::SCALE_FACTOR_100P,
27   ui::SCALE_FACTOR_200P,
28 };
29
30 #define SCOPED_FAVICON_SCALE_FACTOR(list)          \
31   ui::test::ScopedSetSupportedScaleFactors scoped( \
32       std::vector<ui::ScaleFactor>(list, list + base::size(list)))
33
34 #define SCOPED_FAVICON_SCALE_FACTOR_1X \
35   SCOPED_FAVICON_SCALE_FACTOR(FaviconScaleFactor1x)
36 #define SCOPED_FAVICON_SCALE_FACTOR_1XAND2X \
37   SCOPED_FAVICON_SCALE_FACTOR(FaviconScaleFactor1xAnd2x)
38
39 // Return gfx::Size vector with the pixel sizes of |bitmaps|.
40 vector<gfx::Size> SizesFromBitmaps(const vector<SkBitmap>& bitmaps) {
41   vector<gfx::Size> sizes;
42   for (size_t i = 0; i < bitmaps.size(); ++i)
43     sizes.push_back(gfx::Size(bitmaps[i].width(), bitmaps[i].height()));
44   return sizes;
45 }
46
47 SkBitmap MakeBitmap(SkColor color, int w, int h) {
48   SkBitmap bitmap;
49   bitmap.allocN32Pixels(w, h);
50   bitmap.eraseColor(color);
51   return bitmap;
52 }
53
54 SkColor GetColor(const gfx::ImageSkia& image, float scale,
55                  int x = -1, int y = -1) {
56   const SkBitmap& bitmap = image.GetRepresentation(scale).GetBitmap();
57   if (x == -1)
58     x = bitmap.width() / 2;
59   if (y == -1)
60     y = bitmap.width() / 2;
61   SkColor color = bitmap.getColor(x, y);
62   return color;
63 }
64
65 SkColor GetColor1x(const gfx::ImageSkia& image) {
66   return GetColor(image, 1.0f);
67 }
68
69 SkColor GetColor2x(const gfx::ImageSkia& image) {
70   return GetColor(image, 2.0f);
71 }
72
73 TEST(SelectFaviconFramesTest, ZeroSizePicksLargest) {
74   vector<SkBitmap> bitmaps;
75   bitmaps.push_back(MakeBitmap(SK_ColorRED, 16, 16));
76   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 48, 48));
77   bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 32, 32));
78
79   gfx::ImageSkia image =
80       CreateFaviconImageSkia(bitmaps, SizesFromBitmaps(bitmaps), 0, nullptr);
81   EXPECT_EQ(1u, image.image_reps().size());
82   ASSERT_TRUE(image.HasRepresentation(1.0f));
83   EXPECT_EQ(48, image.width());
84   EXPECT_EQ(48, image.height());
85
86   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
87 }
88
89 TEST(SelectFaviconFramesTest, _16From16) {
90   SCOPED_FAVICON_SCALE_FACTOR_1X;
91
92   vector<SkBitmap> bitmaps;
93   bitmaps.push_back(MakeBitmap(SK_ColorRED, 15, 15));
94   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
95   bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 17, 17));
96
97   gfx::ImageSkia image =
98       CreateFaviconImageSkia(bitmaps, SizesFromBitmaps(bitmaps), 16, nullptr);
99   image.EnsureRepsForSupportedScales();
100   EXPECT_EQ(1u, image.image_reps().size());
101   ASSERT_TRUE(image.HasRepresentation(1.0f));
102   EXPECT_EQ(16, image.width());
103   EXPECT_EQ(16, image.height());
104   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
105
106 #if !defined(OS_IOS)
107   const gfx::ImageSkiaRep& rep = image.GetRepresentation(1.5f);
108   EXPECT_EQ(1.5f, rep.scale());
109   EXPECT_EQ(16, rep.GetWidth());
110   EXPECT_EQ(16, rep.GetHeight());
111   EXPECT_EQ(24, rep.pixel_width());
112   EXPECT_EQ(24, rep.pixel_height());
113   EXPECT_EQ(2u, image.image_reps().size());
114 #endif
115 }
116
117 TEST(SelectFaviconFramesTest, _16From17) {
118   SCOPED_FAVICON_SCALE_FACTOR_1X;
119
120   vector<SkBitmap> bitmaps;
121   bitmaps.push_back(MakeBitmap(SK_ColorRED, 15, 15));
122   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 17, 17));
123
124   // Should resample from the bigger candidate.
125   gfx::ImageSkia image =
126       CreateFaviconImageSkia(bitmaps, SizesFromBitmaps(bitmaps), 16, nullptr);
127   image.EnsureRepsForSupportedScales();
128   EXPECT_EQ(1u, image.image_reps().size());
129   ASSERT_TRUE(image.HasRepresentation(1.0f));
130   EXPECT_EQ(16, image.width());
131   EXPECT_EQ(16, image.height());
132   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
133 }
134
135 TEST(SelectFaviconFramesTest, _16From15) {
136   SCOPED_FAVICON_SCALE_FACTOR_1X;
137
138   vector<SkBitmap> bitmaps;
139   bitmaps.push_back(MakeBitmap(SK_ColorRED, 14, 14));
140   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 15, 15));
141
142   // If nothing else is available, should resample from the next smaller
143   // candidate.
144   gfx::ImageSkia image =
145       CreateFaviconImageSkia(bitmaps, SizesFromBitmaps(bitmaps), 16, nullptr);
146   image.EnsureRepsForSupportedScales();
147   EXPECT_EQ(1u, image.image_reps().size());
148   ASSERT_TRUE(image.HasRepresentation(1.0f));
149   EXPECT_EQ(16, image.width());
150   EXPECT_EQ(16, image.height());
151   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
152 }
153
154 TEST(SelectFaviconFramesTest, _16From16_Scale2x_32_From_16) {
155   SCOPED_FAVICON_SCALE_FACTOR_1XAND2X;
156   vector<SkBitmap> bitmaps;
157   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
158
159   gfx::ImageSkia image =
160       CreateFaviconImageSkia(bitmaps, SizesFromBitmaps(bitmaps), 16, nullptr);
161   image.EnsureRepsForSupportedScales();
162   EXPECT_EQ(2u, image.image_reps().size());
163   ASSERT_TRUE(image.HasRepresentation(1.0f));
164   ASSERT_TRUE(image.HasRepresentation(2.0f));
165   EXPECT_EQ(16, image.width());
166   EXPECT_EQ(16, image.height());
167   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
168   EXPECT_EQ(SK_ColorGREEN, GetColor2x(image));
169 }
170
171 TEST(SelectFaviconFramesTest, _16From16_Scale2x_32_From_32) {
172   SCOPED_FAVICON_SCALE_FACTOR_1XAND2X;
173
174   vector<SkBitmap> bitmaps;
175   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
176   bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 32, 32));
177
178   gfx::ImageSkia image =
179       CreateFaviconImageSkia(bitmaps, SizesFromBitmaps(bitmaps), 16, nullptr);
180   image.EnsureRepsForSupportedScales();
181   EXPECT_EQ(2u, image.image_reps().size());
182   ASSERT_TRUE(image.HasRepresentation(1.0f));
183   ASSERT_TRUE(image.HasRepresentation(2.0f));
184   EXPECT_EQ(16, image.width());
185   EXPECT_EQ(16, image.height());
186   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
187   EXPECT_EQ(SK_ColorBLUE, GetColor2x(image));
188
189 #if !defined(OS_IOS)
190   const gfx::ImageSkiaRep& rep = image.GetRepresentation(1.5f);
191   EXPECT_EQ(1.5f, rep.scale());
192   EXPECT_EQ(16, rep.GetWidth());
193   EXPECT_EQ(16, rep.GetHeight());
194   EXPECT_EQ(24, rep.pixel_width());
195   EXPECT_EQ(24, rep.pixel_height());
196   EXPECT_EQ(3u, image.image_reps().size());
197 #endif
198 }
199
200 TEST(SelectFaviconFramesTest, ExactMatchBetterThanLargeBitmap) {
201   SCOPED_FAVICON_SCALE_FACTOR_1XAND2X;
202
203   float score1;
204   vector<SkBitmap> bitmaps1;
205   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 48, 48));
206   CreateFaviconImageSkia(
207       bitmaps1,
208       SizesFromBitmaps(bitmaps1), 16, &score1);
209
210   float score2;
211   vector<SkBitmap> bitmaps2;
212   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
213   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 32, 32));
214   CreateFaviconImageSkia(bitmaps2,
215       SizesFromBitmaps(bitmaps2), 16, &score2);
216
217   EXPECT_GT(score2, score1);
218 }
219
220 TEST(SelectFaviconFramesTest, UpsampleABitBetterThanHugeBitmap) {
221   SCOPED_FAVICON_SCALE_FACTOR_1XAND2X;
222
223   float score1;
224   vector<SkBitmap> bitmaps1;
225   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 128, 128));
226   CreateFaviconImageSkia(bitmaps1,
227       SizesFromBitmaps(bitmaps1), 16, &score1);
228
229   float score2;
230   vector<SkBitmap> bitmaps2;
231   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 24, 24));
232   CreateFaviconImageSkia(bitmaps2,
233       SizesFromBitmaps(bitmaps2), 16, &score2);
234
235   float score3;
236   vector<SkBitmap> bitmaps3;
237   bitmaps3.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
238   CreateFaviconImageSkia(bitmaps3,
239       SizesFromBitmaps(bitmaps3), 16, &score3);
240
241   float score4;
242   vector<SkBitmap> bitmaps4;
243   bitmaps4.push_back(MakeBitmap(SK_ColorGREEN, 15, 15));
244   CreateFaviconImageSkia(bitmaps4,
245       SizesFromBitmaps(bitmaps4), 16, &score4);
246
247   EXPECT_GT(score2, score1);
248   EXPECT_GT(score3, score1);
249   EXPECT_GT(score4, score1);
250 }
251
252 TEST(SelectFaviconFramesTest, DownsamplingBetterThanUpsampling) {
253   SCOPED_FAVICON_SCALE_FACTOR_1XAND2X;
254
255   float score1;
256   vector<SkBitmap> bitmaps1;
257   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 8, 8));
258   CreateFaviconImageSkia(bitmaps1,
259                          SizesFromBitmaps(bitmaps1), 16, &score1);
260
261   float score2;
262   vector<SkBitmap> bitmaps2;
263   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 24, 24));
264   CreateFaviconImageSkia(bitmaps2,
265                          SizesFromBitmaps(bitmaps2), 16, &score2);
266
267   EXPECT_GT(score2, score1);
268 }
269
270 TEST(SelectFaviconFramesTest, DownsamplingLessIsBetter) {
271   SCOPED_FAVICON_SCALE_FACTOR_1XAND2X;
272
273   float score1;
274   vector<SkBitmap> bitmaps1;
275   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 34, 34));
276   CreateFaviconImageSkia(bitmaps1,
277       SizesFromBitmaps(bitmaps1), 16, &score1);
278
279   float score2;
280   vector<SkBitmap> bitmaps2;
281   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 33, 33));
282   CreateFaviconImageSkia(bitmaps2,
283       SizesFromBitmaps(bitmaps2), 16, &score2);
284
285   EXPECT_GT(score2, score1);
286 }
287
288 TEST(SelectFaviconFramesTest, UpsamplingLessIsBetter) {
289   SCOPED_FAVICON_SCALE_FACTOR_1XAND2X;
290
291   float score1;
292   vector<SkBitmap> bitmaps1;
293   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 8, 8));
294   CreateFaviconImageSkia(bitmaps1,
295       SizesFromBitmaps(bitmaps1), 16, &score1);
296
297   float score2;
298   vector<SkBitmap> bitmaps2;
299   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 9, 9));
300   CreateFaviconImageSkia(bitmaps2,
301       SizesFromBitmaps(bitmaps2), 16, &score2);
302
303   EXPECT_GT(score2, score1);
304 }
305
306 // Test that the score is determined by the |original_sizes| parameter, not the
307 // |bitmaps| parameter to SelectFaviconFrames().
308 TEST(SelectFaviconFramesTest, ScoreDeterminedByOriginalSizes) {
309   SCOPED_FAVICON_SCALE_FACTOR_1XAND2X;
310
311   vector<SkBitmap> bitmaps1;
312   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
313   vector<gfx::Size> sizes1;
314   sizes1.push_back(gfx::Size(256, 256));
315   float score1;
316   CreateFaviconImageSkia(bitmaps1, sizes1, 16, &score1);
317
318   vector<SkBitmap> bitmaps2;
319   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 15, 15));
320   vector<gfx::Size> sizes2;
321   sizes2.push_back(gfx::Size(15, 15));
322   float score2;
323   CreateFaviconImageSkia(bitmaps2, sizes2, 16, &score2);
324
325   EXPECT_GT(score2, score1);
326 }
327
328 }  // namespace