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_icon_image.h" | 5 #include "chrome/browser/extensions/extension_icon_image.h" |
6 | 6 |
7 #include "base/json/json_file_value_serializer.h" | 7 #include "base/json/json_file_value_serializer.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "chrome/browser/extensions/image_loading_tracker.h" | 10 #include "chrome/browser/extensions/image_loading_tracker.h" |
11 #include "chrome/common/chrome_paths.h" | 11 #include "chrome/common/chrome_paths.h" |
12 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
13 #include "chrome/common/extensions/extension_constants.h" | 13 #include "chrome/common/extensions/extension_constants.h" |
14 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
15 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
| 16 #include "skia/ext/image_operations.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
18 #include "ui/gfx/image/image_skia_source.h" | 19 #include "ui/gfx/image/image_skia_source.h" |
19 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
20 | 21 |
21 using content::BrowserThread; | 22 using content::BrowserThread; |
22 using extensions::Extension; | 23 using extensions::Extension; |
23 using extensions::IconImage; | 24 using extensions::IconImage; |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 SkBitmap CreateBlankBitmapForScale(int size_dip, ui::ScaleFactor scale_factor) { | 28 SkBitmap CreateBlankBitmapForScale(int size_dip, ui::ScaleFactor scale_factor) { |
28 SkBitmap bitmap; | 29 SkBitmap bitmap; |
29 const float scale = ui::GetScaleFactorScale(scale_factor); | 30 const float scale = ui::GetScaleFactorScale(scale_factor); |
30 bitmap.setConfig(SkBitmap::kARGB_8888_Config, | 31 bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
31 static_cast<int>(size_dip * scale), | 32 static_cast<int>(size_dip * scale), |
32 static_cast<int>(size_dip * scale)); | 33 static_cast<int>(size_dip * scale)); |
33 bitmap.allocPixels(); | 34 bitmap.allocPixels(); |
34 bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0)); | 35 bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0)); |
35 return bitmap; | 36 return bitmap; |
36 } | 37 } |
37 | 38 |
| 39 SkBitmap EnsureBitmapSize(const SkBitmap& original, int size) { |
| 40 if (original.width() == size && original.height() == size) |
| 41 return original; |
| 42 |
| 43 SkBitmap resized = skia::ImageOperations::Resize( |
| 44 original, skia::ImageOperations::RESIZE_LANCZOS3, size, size); |
| 45 return resized; |
| 46 } |
| 47 |
38 // Used to test behaviour including images defined by an image skia source. | 48 // Used to test behaviour including images defined by an image skia source. |
39 // |GetImageForScale| simply returns image representation from the image given | 49 // |GetImageForScale| simply returns image representation from the image given |
40 // in the ctor. | 50 // in the ctor. |
41 class MockImageSkiaSource : public gfx::ImageSkiaSource { | 51 class MockImageSkiaSource : public gfx::ImageSkiaSource { |
42 public: | 52 public: |
43 explicit MockImageSkiaSource(const gfx::ImageSkia& image) | 53 explicit MockImageSkiaSource(const gfx::ImageSkia& image) |
44 : image_(image) { | 54 : image_(image) { |
45 } | 55 } |
46 virtual ~MockImageSkiaSource() {} | 56 virtual ~MockImageSkiaSource() {} |
47 | 57 |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 | 294 |
285 gfx::ImageSkiaRep representation = | 295 gfx::ImageSkiaRep representation = |
286 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | 296 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); |
287 | 297 |
288 WaitForImageLoad(); | 298 WaitForImageLoad(); |
289 EXPECT_EQ(1, ImageLoadedCount()); | 299 EXPECT_EQ(1, ImageLoadedCount()); |
290 ASSERT_EQ(1u, image.image_skia().image_reps().size()); | 300 ASSERT_EQ(1u, image.image_skia().image_reps().size()); |
291 | 301 |
292 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | 302 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); |
293 | 303 |
294 // We should have loaded the biggest smaller resource. In this case the | 304 // We should have loaded the biggest smaller resource resized to the actual |
295 // loaded resource should not be resized. | 305 // size. |
296 EXPECT_EQ(ui::SCALE_FACTOR_200P, representation.scale_factor()); | 306 EXPECT_EQ(ui::SCALE_FACTOR_200P, representation.scale_factor()); |
297 EXPECT_EQ(48, representation.pixel_width()); | 307 EXPECT_EQ(64, representation.pixel_width()); |
298 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_48)); | 308 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), |
| 309 EnsureBitmapSize(bitmap_48, 64))); |
299 } | 310 } |
300 | 311 |
301 // There is no resource with exact size, but there is a smaller and a bigger | 312 // There is no resource with exact size, but there is a smaller and a bigger |
302 // one. Requested size is smaller than 32 though, so the smaller resource should | 313 // one. Requested size is smaller than 32 though, so the smaller resource should |
303 // be loaded. | 314 // be loaded. |
304 TEST_F(ExtensionIconImageTest, FallbackToSmaller) { | 315 TEST_F(ExtensionIconImageTest, FallbackToSmaller) { |
305 scoped_refptr<Extension> extension(CreateExtension( | 316 scoped_refptr<Extension> extension(CreateExtension( |
306 "extension_icon_image", Extension::INVALID)); | 317 "extension_icon_image", Extension::INVALID)); |
307 ASSERT_TRUE(extension.get() != NULL); | 318 ASSERT_TRUE(extension.get() != NULL); |
308 | 319 |
309 gfx::ImageSkia default_icon = GetDefaultIcon(); | 320 gfx::ImageSkia default_icon = GetDefaultIcon(); |
310 | 321 |
311 // Load images we expect to find as representations in icon_image, so we | 322 // Load images we expect to find as representations in icon_image, so we |
312 // can later use them to validate icon_image. | 323 // can later use them to validate icon_image. |
313 SkBitmap bitmap_16 = | 324 SkBitmap bitmap_16 = |
314 GetTestBitmap(extension, "16.png", 16, ImageLoadingTracker::DONT_CACHE); | 325 GetTestBitmap(extension, "16.png", 16, ImageLoadingTracker::DONT_CACHE); |
315 ASSERT_FALSE(bitmap_16.empty()); | 326 ASSERT_FALSE(bitmap_16.empty()); |
316 | 327 |
317 IconImage image(extension, extension->icons(), 17, default_icon, this); | 328 IconImage image(extension, extension->icons(), 17, default_icon, this); |
318 | 329 |
319 gfx::ImageSkiaRep representation = | 330 gfx::ImageSkiaRep representation = |
320 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | 331 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); |
321 | 332 |
322 WaitForImageLoad(); | 333 WaitForImageLoad(); |
323 EXPECT_EQ(1, ImageLoadedCount()); | 334 EXPECT_EQ(1, ImageLoadedCount()); |
324 ASSERT_EQ(1u, image.image_skia().image_reps().size()); | 335 ASSERT_EQ(1u, image.image_skia().image_reps().size()); |
325 | 336 |
326 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | 337 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); |
327 | 338 |
328 // We should have loaded smaller (not resized) resource. | 339 // We should have loaded smaller (resized) resource. |
329 EXPECT_EQ(ui::SCALE_FACTOR_100P, representation.scale_factor()); | 340 EXPECT_EQ(ui::SCALE_FACTOR_100P, representation.scale_factor()); |
330 EXPECT_EQ(16, representation.pixel_width()); | 341 EXPECT_EQ(17, representation.pixel_width()); |
331 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_16)); | 342 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), |
| 343 EnsureBitmapSize(bitmap_16, 17))); |
332 } | 344 } |
333 | 345 |
334 // If resource set is empty, |GetRepresentation| should synchronously return | 346 // If resource set is empty, |GetRepresentation| should synchronously return |
335 // default icon, without notifying observer of image change. | 347 // default icon, without notifying observer of image change. |
336 TEST_F(ExtensionIconImageTest, NoResources) { | 348 TEST_F(ExtensionIconImageTest, NoResources) { |
337 scoped_refptr<Extension> extension(CreateExtension( | 349 scoped_refptr<Extension> extension(CreateExtension( |
338 "extension_icon_image", Extension::INVALID)); | 350 "extension_icon_image", Extension::INVALID)); |
339 ASSERT_TRUE(extension.get() != NULL); | 351 ASSERT_TRUE(extension.get() != NULL); |
340 | 352 |
341 ExtensionIconSet empty_icon_set; | 353 ExtensionIconSet empty_icon_set; |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 EXPECT_EQ(16, representation.pixel_width()); | 536 EXPECT_EQ(16, representation.pixel_width()); |
525 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_16)); | 537 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_16)); |
526 | 538 |
527 // When requesting another representation, we should get blank image. | 539 // When requesting another representation, we should get blank image. |
528 representation = image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); | 540 representation = image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
529 | 541 |
530 EXPECT_TRUE(gfx::BitmapsAreEqual( | 542 EXPECT_TRUE(gfx::BitmapsAreEqual( |
531 representation.sk_bitmap(), | 543 representation.sk_bitmap(), |
532 CreateBlankBitmapForScale(16, ui::SCALE_FACTOR_200P))); | 544 CreateBlankBitmapForScale(16, ui::SCALE_FACTOR_200P))); |
533 } | 545 } |
OLD | NEW |