OLD | NEW |
---|---|
(Empty) | |
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 "chrome/browser/favicon/select_favicon_frames.h" | |
6 | |
7 #include "ui/base/layout.h" | |
8 #include "ui/gfx/image/image_skia.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 #include "third_party/skia/include/core/SkBitmap.h" | |
11 #include "third_party/skia/include/core/SkColor.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 SkBitmap MakeBitmap(SkColor color, int w, int h) { | |
29 SkBitmap bitmap; | |
30 bitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h); | |
31 bitmap.allocPixels(); | |
32 bitmap.eraseColor(color); | |
33 return bitmap; | |
34 } | |
35 | |
36 SkColor GetColor(const gfx::ImageSkia& image, ui::ScaleFactor factor, | |
sky
2012/08/06 15:05:08
nit: each param on its own line and no default val
| |
37 int x = -1, int y = -1) { | |
38 const SkBitmap& bitmap = image.GetRepresentation(factor).sk_bitmap(); | |
39 if (x == -1) | |
40 x = bitmap.width() / 2; | |
41 if (y == -1) | |
42 y = bitmap.width() / 2; | |
43 bitmap.lockPixels(); | |
44 SkColor color = bitmap.getColor(x, y); | |
45 bitmap.unlockPixels(); | |
46 return color; | |
47 } | |
48 | |
49 SkColor GetColor1x(const gfx::ImageSkia& image) { | |
50 return GetColor(image, ui::SCALE_FACTOR_100P); | |
51 } | |
52 | |
53 SkColor GetColor2x(const gfx::ImageSkia& image) { | |
54 return GetColor(image, ui::SCALE_FACTOR_200P); | |
55 } | |
56 | |
57 TEST(SelectFaviconFramesTest, ZeroSizePicksLargest) { | |
58 vector<SkBitmap> bitmaps; | |
59 bitmaps.push_back(MakeBitmap(SK_ColorRED, 16, 16)); | |
60 bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 48, 48)); | |
61 bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 32, 32)); | |
62 | |
63 gfx::ImageSkia image = SelectFaviconFrames(bitmaps, Scale1x(), 0); | |
64 EXPECT_EQ(1u, image.image_reps().size()); | |
65 ASSERT_TRUE(image.HasRepresentation(ui::SCALE_FACTOR_100P)); | |
66 EXPECT_EQ(48, image.width()); | |
67 EXPECT_EQ(48, image.height()); | |
68 | |
69 EXPECT_EQ(SK_ColorGREEN, GetColor1x(image)); | |
70 } | |
71 | |
72 TEST(SelectFaviconFramesTest, _16From16) { | |
73 vector<SkBitmap> bitmaps; | |
74 bitmaps.push_back(MakeBitmap(SK_ColorRED, 15, 15)); | |
75 bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 16, 16)); | |
76 bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 17, 17)); | |
77 | |
78 gfx::ImageSkia image = SelectFaviconFrames(bitmaps, Scale1x(), 16); | |
79 EXPECT_EQ(1u, image.image_reps().size()); | |
80 ASSERT_TRUE(image.HasRepresentation(ui::SCALE_FACTOR_100P)); | |
81 EXPECT_EQ(16, image.width()); | |
82 EXPECT_EQ(16, image.height()); | |
83 EXPECT_EQ(SK_ColorGREEN, GetColor1x(image)); | |
84 } | |
85 | |
86 TEST(SelectFaviconFramesTest, _16From17) { | |
87 vector<SkBitmap> bitmaps; | |
88 bitmaps.push_back(MakeBitmap(SK_ColorRED, 15, 15)); | |
89 bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 17, 17)); | |
90 | |
91 // Should resample from the bigger candidate. | |
92 gfx::ImageSkia image = SelectFaviconFrames(bitmaps, Scale1x(), 16); | |
93 EXPECT_EQ(1u, image.image_reps().size()); | |
94 ASSERT_TRUE(image.HasRepresentation(ui::SCALE_FACTOR_100P)); | |
95 EXPECT_EQ(16, image.width()); | |
96 EXPECT_EQ(16, image.height()); | |
97 EXPECT_EQ(SK_ColorGREEN, GetColor1x(image)); | |
98 } | |
99 | |
100 TEST(SelectFaviconFramesTest, _16From15) { | |
101 vector<SkBitmap> bitmaps; | |
102 bitmaps.push_back(MakeBitmap(SK_ColorRED, 14, 14)); | |
103 bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 15, 15)); | |
104 | |
105 // If nothing else is available, should resample form the next smaller | |
106 // candidate. | |
107 gfx::ImageSkia image = SelectFaviconFrames(bitmaps, Scale1x(), 16); | |
108 EXPECT_EQ(1u, image.image_reps().size()); | |
109 ASSERT_TRUE(image.HasRepresentation(ui::SCALE_FACTOR_100P)); | |
110 EXPECT_EQ(16, image.width()); | |
111 EXPECT_EQ(16, image.height()); | |
112 EXPECT_EQ(SK_ColorGREEN, GetColor1x(image)); | |
113 } | |
114 | |
115 TEST(SelectFaviconFramesTest, _16From16_Scale2x_32_From_16) { | |
116 vector<SkBitmap> bitmaps; | |
117 bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 16, 16)); | |
118 | |
119 gfx::ImageSkia image = SelectFaviconFrames(bitmaps, Scale1x2x(), 16); | |
120 EXPECT_EQ(2u, image.image_reps().size()); | |
121 ASSERT_TRUE(image.HasRepresentation(ui::SCALE_FACTOR_100P)); | |
122 ASSERT_TRUE(image.HasRepresentation(ui::SCALE_FACTOR_200P)); | |
123 EXPECT_EQ(16, image.width()); | |
124 EXPECT_EQ(16, image.height()); | |
125 EXPECT_EQ(SK_ColorGREEN, GetColor1x(image)); | |
126 EXPECT_EQ(SK_ColorGREEN, GetColor2x(image)); | |
127 } | |
128 | |
129 TEST(SelectFaviconFramesTest, _16From16_Scale2x_32_From_32) { | |
130 vector<SkBitmap> bitmaps; | |
131 bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 16, 16)); | |
132 bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 32, 32)); | |
133 | |
134 gfx::ImageSkia image = SelectFaviconFrames(bitmaps, Scale1x2x(), 16); | |
135 EXPECT_EQ(2u, image.image_reps().size()); | |
136 ASSERT_TRUE(image.HasRepresentation(ui::SCALE_FACTOR_100P)); | |
137 ASSERT_TRUE(image.HasRepresentation(ui::SCALE_FACTOR_200P)); | |
138 EXPECT_EQ(16, image.width()); | |
139 EXPECT_EQ(16, image.height()); | |
140 EXPECT_EQ(SK_ColorGREEN, GetColor1x(image)); | |
141 EXPECT_EQ(SK_ColorBLUE, GetColor2x(image)); | |
142 } | |
143 | |
144 TEST(SelectFaviconFramesTest, _24_48) { | |
145 vector<SkBitmap> bitmaps; | |
146 bitmaps.push_back(MakeBitmap(SK_ColorRED, 25, 25)); | |
147 bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 16, 16)); | |
148 bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 32, 32)); | |
149 bitmaps.push_back(MakeBitmap(SK_ColorYELLOW, 49, 49)); | |
150 | |
151 gfx::ImageSkia image = SelectFaviconFrames(bitmaps, Scale1x2x(), 24); | |
152 EXPECT_EQ(2u, image.image_reps().size()); | |
153 ASSERT_TRUE(image.HasRepresentation(ui::SCALE_FACTOR_100P)); | |
154 ASSERT_TRUE(image.HasRepresentation(ui::SCALE_FACTOR_200P)); | |
155 EXPECT_EQ(24, image.width()); | |
156 EXPECT_EQ(24, image.height()); | |
157 EXPECT_EQ(SK_ColorGREEN, GetColor1x(image)); | |
158 EXPECT_EQ(0u, SkColorGetA(GetColor(image, ui::SCALE_FACTOR_100P, 0, 0))); | |
159 EXPECT_EQ(0u, SkColorGetA(GetColor(image, ui::SCALE_FACTOR_100P, 3, 4))); | |
160 EXPECT_EQ(255u, SkColorGetA(GetColor(image, ui::SCALE_FACTOR_100P, 4, 4))); | |
161 | |
162 EXPECT_EQ(SK_ColorBLUE, GetColor2x(image)); | |
163 EXPECT_EQ(0u, SkColorGetA(GetColor(image, ui::SCALE_FACTOR_200P, 0, 0))); | |
164 EXPECT_EQ(0u, SkColorGetA(GetColor(image, ui::SCALE_FACTOR_200P, 7, 8))); | |
165 EXPECT_EQ(255u, SkColorGetA(GetColor(image, ui::SCALE_FACTOR_200P, 8, 8))); | |
166 } | |
167 | |
168 } | |
OLD | NEW |