Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / history / 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 "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "third_party/skia/include/core/SkColor.h"
10 #include "ui/base/layout.h"
11 #include "ui/gfx/image/image_skia.h"
12
13 using std::vector;
14
15 namespace {
16
17 vector<ui::ScaleFactor> Scale1x() {
18   return vector<ui::ScaleFactor>(1, ui::SCALE_FACTOR_100P);
19 }
20
21 vector<ui::ScaleFactor> Scale1x2x() {
22   vector<ui::ScaleFactor> scales;
23   scales.push_back(ui::SCALE_FACTOR_100P);
24   scales.push_back(ui::SCALE_FACTOR_200P);
25   return scales;
26 }
27
28 // Return gfx::Size vector with the pixel sizes of |bitmaps|.
29 vector<gfx::Size> SizesFromBitmaps(const vector<SkBitmap>& bitmaps) {
30   vector<gfx::Size> sizes;
31   for (size_t i = 0; i < bitmaps.size(); ++i)
32     sizes.push_back(gfx::Size(bitmaps[i].width(), bitmaps[i].height()));
33   return sizes;
34 }
35
36 SkBitmap MakeBitmap(SkColor color, int w, int h) {
37   SkBitmap bitmap;
38   bitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
39   bitmap.allocPixels();
40   bitmap.eraseColor(color);
41   return bitmap;
42 }
43
44 SkColor GetColor(const gfx::ImageSkia& image, ui::ScaleFactor factor,
45                  int x = -1, int y = -1) {
46   const SkBitmap& bitmap =
47       image.GetRepresentation(ui::GetImageScale(factor)).sk_bitmap();
48   if (x == -1)
49     x = bitmap.width() / 2;
50   if (y == -1)
51     y = bitmap.width() / 2;
52   bitmap.lockPixels();
53   SkColor color = bitmap.getColor(x, y);
54   bitmap.unlockPixels();
55   return color;
56 }
57
58 SkColor GetColor1x(const gfx::ImageSkia& image) {
59   return GetColor(image, ui::SCALE_FACTOR_100P);
60 }
61
62 SkColor GetColor2x(const gfx::ImageSkia& image) {
63   return GetColor(image, ui::SCALE_FACTOR_200P);
64 }
65
66 TEST(SelectFaviconFramesTest, ZeroSizePicksLargest) {
67   vector<SkBitmap> bitmaps;
68   bitmaps.push_back(MakeBitmap(SK_ColorRED, 16, 16));
69   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 48, 48));
70   bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 32, 32));
71
72   gfx::ImageSkia image = SelectFaviconFrames(bitmaps,
73       SizesFromBitmaps(bitmaps), Scale1x(), 0, NULL);
74   EXPECT_EQ(1u, image.image_reps().size());
75   ASSERT_TRUE(image.HasRepresentation(1.0f));
76   EXPECT_EQ(48, image.width());
77   EXPECT_EQ(48, image.height());
78
79   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
80 }
81
82 TEST(SelectFaviconFramesTest, _16From16) {
83   vector<SkBitmap> bitmaps;
84   bitmaps.push_back(MakeBitmap(SK_ColorRED, 15, 15));
85   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
86   bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 17, 17));
87
88   gfx::ImageSkia image = SelectFaviconFrames(bitmaps,
89       SizesFromBitmaps(bitmaps), Scale1x(), 16, NULL);
90   EXPECT_EQ(1u, image.image_reps().size());
91   ASSERT_TRUE(image.HasRepresentation(1.0f));
92   EXPECT_EQ(16, image.width());
93   EXPECT_EQ(16, image.height());
94   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
95 }
96
97 TEST(SelectFaviconFramesTest, _16From17) {
98   vector<SkBitmap> bitmaps;
99   bitmaps.push_back(MakeBitmap(SK_ColorRED, 15, 15));
100   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 17, 17));
101
102   // Should resample from the bigger candidate.
103   gfx::ImageSkia image = SelectFaviconFrames(bitmaps,
104       SizesFromBitmaps(bitmaps), Scale1x(), 16, NULL);
105   EXPECT_EQ(1u, image.image_reps().size());
106   ASSERT_TRUE(image.HasRepresentation(1.0f));
107   EXPECT_EQ(16, image.width());
108   EXPECT_EQ(16, image.height());
109   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
110 }
111
112 TEST(SelectFaviconFramesTest, _16From15) {
113   vector<SkBitmap> bitmaps;
114   bitmaps.push_back(MakeBitmap(SK_ColorRED, 14, 14));
115   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 15, 15));
116
117   // If nothing else is available, should resample from the next smaller
118   // candidate.
119   gfx::ImageSkia image = SelectFaviconFrames(bitmaps,
120       SizesFromBitmaps(bitmaps), Scale1x(), 16, NULL);
121   EXPECT_EQ(1u, image.image_reps().size());
122   ASSERT_TRUE(image.HasRepresentation(1.0f));
123   EXPECT_EQ(16, image.width());
124   EXPECT_EQ(16, image.height());
125   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
126 }
127
128 TEST(SelectFaviconFramesTest, _16From16_Scale2x_32_From_16) {
129   vector<SkBitmap> bitmaps;
130   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
131
132   gfx::ImageSkia image = SelectFaviconFrames(bitmaps,
133       SizesFromBitmaps(bitmaps), Scale1x2x(), 16, NULL);
134   EXPECT_EQ(2u, image.image_reps().size());
135   ASSERT_TRUE(image.HasRepresentation(1.0f));
136   ASSERT_TRUE(image.HasRepresentation(2.0f));
137   EXPECT_EQ(16, image.width());
138   EXPECT_EQ(16, image.height());
139   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
140   EXPECT_EQ(SK_ColorGREEN, GetColor2x(image));
141 }
142
143 TEST(SelectFaviconFramesTest, _16From16_Scale2x_32_From_32) {
144   vector<SkBitmap> bitmaps;
145   bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
146   bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 32, 32));
147
148   gfx::ImageSkia image = SelectFaviconFrames(bitmaps,
149       SizesFromBitmaps(bitmaps), Scale1x2x(), 16, NULL);
150   EXPECT_EQ(2u, image.image_reps().size());
151   ASSERT_TRUE(image.HasRepresentation(1.0f));
152   ASSERT_TRUE(image.HasRepresentation(2.0f));
153   EXPECT_EQ(16, image.width());
154   EXPECT_EQ(16, image.height());
155   EXPECT_EQ(SK_ColorGREEN, GetColor1x(image));
156   EXPECT_EQ(SK_ColorBLUE, GetColor2x(image));
157 }
158
159 TEST(SelectFaviconFramesTest, ExactMatchBetterThanLargeBitmap) {
160   float score1;
161   vector<SkBitmap> bitmaps1;
162   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 48, 48));
163   SelectFaviconFrames(bitmaps1,
164       SizesFromBitmaps(bitmaps1), Scale1x2x(), 16, &score1);
165
166   float score2;
167   vector<SkBitmap> bitmaps2;
168   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
169   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 32, 32));
170   SelectFaviconFrames(bitmaps2,
171       SizesFromBitmaps(bitmaps2), Scale1x2x(), 16, &score2);
172
173   EXPECT_GT(score2, score1);
174 }
175
176 TEST(SelectFaviconFramesTest, UpsampleABitBetterThanHugeBitmap) {
177   float score1;
178   vector<SkBitmap> bitmaps1;
179   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 128, 128));
180   SelectFaviconFrames(bitmaps1,
181       SizesFromBitmaps(bitmaps1), Scale1x2x(), 16, &score1);
182
183   float score2;
184   vector<SkBitmap> bitmaps2;
185   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 24, 24));
186   SelectFaviconFrames(bitmaps2,
187       SizesFromBitmaps(bitmaps2), Scale1x2x(), 16, &score2);
188
189   float score3;
190   vector<SkBitmap> bitmaps3;
191   bitmaps3.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
192   SelectFaviconFrames(bitmaps3,
193       SizesFromBitmaps(bitmaps3), Scale1x2x(), 16, &score3);
194
195   float score4;
196   vector<SkBitmap> bitmaps4;
197   bitmaps4.push_back(MakeBitmap(SK_ColorGREEN, 15, 15));
198   SelectFaviconFrames(bitmaps4,
199       SizesFromBitmaps(bitmaps4), Scale1x2x(), 16, &score4);
200
201   EXPECT_GT(score2, score1);
202   EXPECT_GT(score3, score1);
203   EXPECT_GT(score4, score1);
204 }
205
206 TEST(SelectFaviconFramesTest, DownsamplingBetterThanUpsampling) {
207   float score1;
208   vector<SkBitmap> bitmaps1;
209   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 8, 8));
210   SelectFaviconFrames(bitmaps1,
211       SizesFromBitmaps(bitmaps1), Scale1x(), 16, &score1);
212
213   float score2;
214   vector<SkBitmap> bitmaps2;
215   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 24, 24));
216   SelectFaviconFrames(bitmaps2,
217       SizesFromBitmaps(bitmaps2), Scale1x(), 16, &score2);
218
219   EXPECT_GT(score2, score1);
220 }
221
222 TEST(SelectFaviconFramesTest, DownsamplingLessIsBetter) {
223   float score1;
224   vector<SkBitmap> bitmaps1;
225   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 34, 34));
226   SelectFaviconFrames(bitmaps1,
227       SizesFromBitmaps(bitmaps1), Scale1x2x(), 16, &score1);
228
229   float score2;
230   vector<SkBitmap> bitmaps2;
231   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 33, 33));
232   SelectFaviconFrames(bitmaps2,
233       SizesFromBitmaps(bitmaps2), Scale1x2x(), 16, &score2);
234
235   EXPECT_GT(score2, score1);
236 }
237
238 TEST(SelectFaviconFramesTest, UpsamplingLessIsBetter) {
239   float score1;
240   vector<SkBitmap> bitmaps1;
241   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 8, 8));
242   SelectFaviconFrames(bitmaps1,
243       SizesFromBitmaps(bitmaps1), Scale1x2x(), 16, &score1);
244
245   float score2;
246   vector<SkBitmap> bitmaps2;
247   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 9, 9));
248   SelectFaviconFrames(bitmaps2,
249       SizesFromBitmaps(bitmaps2), Scale1x2x(), 16, &score2);
250
251   EXPECT_GT(score2, score1);
252 }
253
254 // Test that the score is determined by the |original_sizes| parameter, not the
255 // |bitmaps| parameter to SelectFaviconFrames().
256 TEST(SelectFaviconFramesTest, ScoreDeterminedByOriginalSizes) {
257   vector<SkBitmap> bitmaps1;
258   bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 16, 16));
259   vector<gfx::Size> sizes1;
260   sizes1.push_back(gfx::Size(256, 256));
261   float score1;
262   SelectFaviconFrames(bitmaps1, sizes1, Scale1x(), 16, &score1);
263
264   vector<SkBitmap> bitmaps2;
265   bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 15, 15));
266   vector<gfx::Size> sizes2;
267   sizes2.push_back(gfx::Size(15, 15));
268   float score2;
269   SelectFaviconFrames(bitmaps2, sizes2, Scale1x(), 16, &score2);
270
271   EXPECT_GT(score2, score1);
272 }
273
274 }  // namespace