OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_action_icon_factory.h" | 5 #include "chrome/browser/extensions/extension_action_icon_factory.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/json/json_file_value_serializer.h" | 9 #include "base/json/json_file_value_serializer.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 31 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
32 #endif | 32 #endif |
33 | 33 |
34 using content::BrowserThread; | 34 using content::BrowserThread; |
35 | 35 |
36 namespace extensions { | 36 namespace extensions { |
37 namespace { | 37 namespace { |
38 | 38 |
39 bool ImageRepsAreEqual(const gfx::ImageSkiaRep& image_rep1, | 39 bool ImageRepsAreEqual(const gfx::ImageSkiaRep& image_rep1, |
40 const gfx::ImageSkiaRep& image_rep2) { | 40 const gfx::ImageSkiaRep& image_rep2) { |
41 return image_rep1.scale_factor() == image_rep2.scale_factor() && | 41 return image_rep1.scale() == image_rep2.scale() && |
42 gfx::BitmapsAreEqual(image_rep1.sk_bitmap(), image_rep2.sk_bitmap()); | 42 gfx::BitmapsAreEqual(image_rep1.sk_bitmap(), image_rep2.sk_bitmap()); |
43 } | 43 } |
44 | 44 |
45 gfx::Image EnsureImageSize(const gfx::Image& original, int size) { | 45 gfx::Image EnsureImageSize(const gfx::Image& original, int size) { |
46 const SkBitmap* original_bitmap = original.ToSkBitmap(); | 46 const SkBitmap* original_bitmap = original.ToSkBitmap(); |
47 if (original_bitmap->width() == size && original_bitmap->height() == size) | 47 if (original_bitmap->width() == size && original_bitmap->height() == size) |
48 return original; | 48 return original; |
49 | 49 |
50 SkBitmap resized = skia::ImageOperations::Resize( | 50 SkBitmap resized = skia::ImageOperations::Resize( |
51 *original.ToSkBitmap(), skia::ImageOperations::RESIZE_LANCZOS3, | 51 *original.ToSkBitmap(), skia::ImageOperations::RESIZE_LANCZOS3, |
52 size, size); | 52 size, size); |
53 return gfx::Image::CreateFrom1xBitmap(resized); | 53 return gfx::Image::CreateFrom1xBitmap(resized); |
54 } | 54 } |
55 | 55 |
56 gfx::ImageSkiaRep CreateBlankRep(int size_dip, ui::ScaleFactor scale_factor) { | 56 gfx::ImageSkiaRep CreateBlankRep(int size_dip, float scale) { |
57 SkBitmap bitmap; | 57 SkBitmap bitmap; |
58 const float scale = ui::GetScaleFactorScale(scale_factor); | 58 bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
59 bitmap.setConfig(SkBitmap::kARGB_8888_Config, | 59 static_cast<int>(size_dip * scale), |
60 static_cast<int>(size_dip * scale), | 60 static_cast<int>(size_dip * scale)); |
61 static_cast<int>(size_dip * scale)); | 61 bitmap.allocPixels(); |
62 bitmap.allocPixels(); | 62 bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0)); |
63 bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0)); | 63 return gfx::ImageSkiaRep(bitmap, scale); |
64 return gfx::ImageSkiaRep(bitmap, scale_factor); | |
65 } | 64 } |
66 | 65 |
67 gfx::Image LoadIcon(const std::string& filename) { | 66 gfx::Image LoadIcon(const std::string& filename) { |
68 base::FilePath path; | 67 base::FilePath path; |
69 PathService::Get(chrome::DIR_TEST_DATA, &path); | 68 PathService::Get(chrome::DIR_TEST_DATA, &path); |
70 path = path.AppendASCII("extensions/api_test").AppendASCII(filename); | 69 path = path.AppendASCII("extensions/api_test").AppendASCII(filename); |
71 | 70 |
72 std::string file_contents; | 71 std::string file_contents; |
73 base::ReadFileToString(path, &file_contents); | 72 base::ReadFileToString(path, &file_contents); |
74 const unsigned char* data = | 73 const unsigned char* data = |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 ASSERT_TRUE(browser_action->GetExplicitlySetIcon(0 /*tab id*/).isNull()); | 195 ASSERT_TRUE(browser_action->GetExplicitlySetIcon(0 /*tab id*/).isNull()); |
197 | 196 |
198 gfx::ImageSkia favicon = GetFavicon(); | 197 gfx::ImageSkia favicon = GetFavicon(); |
199 | 198 |
200 ExtensionActionIconFactory icon_factory( | 199 ExtensionActionIconFactory icon_factory( |
201 profile(), extension.get(), browser_action, this); | 200 profile(), extension.get(), browser_action, this); |
202 | 201 |
203 gfx::Image icon = icon_factory.GetIcon(0); | 202 gfx::Image icon = icon_factory.GetIcon(0); |
204 | 203 |
205 EXPECT_TRUE(ImageRepsAreEqual( | 204 EXPECT_TRUE(ImageRepsAreEqual( |
206 favicon.GetRepresentation(ui::SCALE_FACTOR_100P), | 205 favicon.GetRepresentation(1.0f), |
207 icon.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P))); | 206 icon.ToImageSkia()->GetRepresentation(1.0f))); |
208 } | 207 } |
209 | 208 |
210 // If the icon has been set using |SetIcon|, the factory should return that | 209 // If the icon has been set using |SetIcon|, the factory should return that |
211 // icon. | 210 // icon. |
212 TEST_F(ExtensionActionIconFactoryTest, AfterSetIcon) { | 211 TEST_F(ExtensionActionIconFactoryTest, AfterSetIcon) { |
213 // Load an extension that has browser action without default icon set in the | 212 // Load an extension that has browser action without default icon set in the |
214 // manifest and does not call |SetIcon| by default (but has an browser action | 213 // manifest and does not call |SetIcon| by default (but has an browser action |
215 // icon resource). | 214 // icon resource). |
216 scoped_refptr<Extension> extension(CreateExtension( | 215 scoped_refptr<Extension> extension(CreateExtension( |
217 "browser_action/no_icon", Manifest::INVALID_LOCATION)); | 216 "browser_action/no_icon", Manifest::INVALID_LOCATION)); |
218 ASSERT_TRUE(extension.get() != NULL); | 217 ASSERT_TRUE(extension.get() != NULL); |
219 ExtensionAction* browser_action = GetBrowserAction(*extension.get()); | 218 ExtensionAction* browser_action = GetBrowserAction(*extension.get()); |
220 ASSERT_TRUE(browser_action); | 219 ASSERT_TRUE(browser_action); |
221 ASSERT_FALSE(browser_action->default_icon()); | 220 ASSERT_FALSE(browser_action->default_icon()); |
222 ASSERT_TRUE(browser_action->GetExplicitlySetIcon(0 /*tab id*/).isNull()); | 221 ASSERT_TRUE(browser_action->GetExplicitlySetIcon(0 /*tab id*/).isNull()); |
223 | 222 |
224 gfx::Image set_icon = LoadIcon("browser_action/no_icon/icon.png"); | 223 gfx::Image set_icon = LoadIcon("browser_action/no_icon/icon.png"); |
225 ASSERT_FALSE(set_icon.IsEmpty()); | 224 ASSERT_FALSE(set_icon.IsEmpty()); |
226 | 225 |
227 browser_action->SetIcon(0, set_icon); | 226 browser_action->SetIcon(0, set_icon); |
228 | 227 |
229 ASSERT_FALSE(browser_action->GetExplicitlySetIcon(0 /*tab id*/).isNull()); | 228 ASSERT_FALSE(browser_action->GetExplicitlySetIcon(0 /*tab id*/).isNull()); |
230 | 229 |
231 ExtensionActionIconFactory icon_factory( | 230 ExtensionActionIconFactory icon_factory( |
232 profile(), extension.get(), browser_action, this); | 231 profile(), extension.get(), browser_action, this); |
233 | 232 |
234 gfx::Image icon = icon_factory.GetIcon(0); | 233 gfx::Image icon = icon_factory.GetIcon(0); |
235 | 234 |
236 EXPECT_TRUE(ImageRepsAreEqual( | 235 EXPECT_TRUE(ImageRepsAreEqual( |
237 set_icon.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P), | 236 set_icon.ToImageSkia()->GetRepresentation(1.0f), |
238 icon.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P))); | 237 icon.ToImageSkia()->GetRepresentation(1.0f))); |
239 | 238 |
240 // It should still return favicon for another tabs. | 239 // It should still return favicon for another tabs. |
241 icon = icon_factory.GetIcon(1); | 240 icon = icon_factory.GetIcon(1); |
242 | 241 |
243 EXPECT_TRUE(ImageRepsAreEqual( | 242 EXPECT_TRUE(ImageRepsAreEqual( |
244 GetFavicon().GetRepresentation(ui::SCALE_FACTOR_100P), | 243 GetFavicon().GetRepresentation(1.0f), |
245 icon.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P))); | 244 icon.ToImageSkia()->GetRepresentation(1.0f))); |
246 } | 245 } |
247 | 246 |
248 // If there is a default icon, and the icon has not been set using |SetIcon|, | 247 // If there is a default icon, and the icon has not been set using |SetIcon|, |
249 // the factory should return the default icon. | 248 // the factory should return the default icon. |
250 TEST_F(ExtensionActionIconFactoryTest, DefaultIcon) { | 249 TEST_F(ExtensionActionIconFactoryTest, DefaultIcon) { |
251 // Load an extension that has browser action without default icon set in the | 250 // Load an extension that has browser action without default icon set in the |
252 // manifest and does not call |SetIcon| by default (but has an browser action | 251 // manifest and does not call |SetIcon| by default (but has an browser action |
253 // icon resource). | 252 // icon resource). |
254 scoped_refptr<Extension> extension(CreateExtension( | 253 scoped_refptr<Extension> extension(CreateExtension( |
255 "browser_action/no_icon", Manifest::INVALID_LOCATION)); | 254 "browser_action/no_icon", Manifest::INVALID_LOCATION)); |
(...skipping 14 matching lines...) Expand all Loading... |
270 ASSERT_TRUE(browser_action->default_icon()); | 269 ASSERT_TRUE(browser_action->default_icon()); |
271 | 270 |
272 ExtensionActionIconFactory icon_factory( | 271 ExtensionActionIconFactory icon_factory( |
273 profile(), extension.get(), browser_action, this); | 272 profile(), extension.get(), browser_action, this); |
274 | 273 |
275 gfx::Image icon = icon_factory.GetIcon(0); | 274 gfx::Image icon = icon_factory.GetIcon(0); |
276 | 275 |
277 // The icon should be loaded asynchronously. Initially a transparent icon | 276 // The icon should be loaded asynchronously. Initially a transparent icon |
278 // should be returned. | 277 // should be returned. |
279 EXPECT_TRUE(ImageRepsAreEqual( | 278 EXPECT_TRUE(ImageRepsAreEqual( |
280 CreateBlankRep(19, ui::SCALE_FACTOR_100P), | 279 CreateBlankRep(19, 1.0f), |
281 icon.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P))); | 280 icon.ToImageSkia()->GetRepresentation(1.0f))); |
282 | 281 |
283 WaitForIconUpdate(); | 282 WaitForIconUpdate(); |
284 | 283 |
285 icon = icon_factory.GetIcon(0); | 284 icon = icon_factory.GetIcon(0); |
286 | 285 |
287 // The default icon representation should be loaded at this point. | 286 // The default icon representation should be loaded at this point. |
288 EXPECT_TRUE(ImageRepsAreEqual( | 287 EXPECT_TRUE(ImageRepsAreEqual( |
289 default_icon.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P), | 288 default_icon.ToImageSkia()->GetRepresentation(1.0f), |
290 icon.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P))); | 289 icon.ToImageSkia()->GetRepresentation(1.0f))); |
291 | 290 |
292 // The same icon should be returned for the other tabs. | 291 // The same icon should be returned for the other tabs. |
293 icon = icon_factory.GetIcon(1); | 292 icon = icon_factory.GetIcon(1); |
294 | 293 |
295 EXPECT_TRUE(ImageRepsAreEqual( | 294 EXPECT_TRUE(ImageRepsAreEqual( |
296 default_icon.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P), | 295 default_icon.ToImageSkia()->GetRepresentation(1.0f), |
297 icon.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P))); | 296 icon.ToImageSkia()->GetRepresentation(1.0f))); |
298 | 297 |
299 } | 298 } |
300 | 299 |
301 } // namespace | 300 } // namespace |
302 } // namespace extensions | 301 } // namespace extensions |
OLD | NEW |