Chromium Code Reviews| 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/common/chrome_paths.h" | 11 #include "chrome/common/chrome_paths.h" |
| 11 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/common/extensions/extension_constants.h" | |
| 12 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
| 15 #include "grit/theme_resources.h" | |
| 16 #include "ui/base/resource/resource_bundle.h" | |
| 17 #include "ui/gfx/skia_util.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 19 |
| 15 using content::BrowserThread; | 20 using content::BrowserThread; |
| 16 using extensions::Extension; | 21 using extensions::Extension; |
| 17 using extensions::IconImage; | 22 using extensions::IconImage; |
| 18 | 23 |
| 19 namespace { | 24 namespace { |
| 20 | 25 |
| 26 SkBitmap CreateBlankBitmapForScale(int size_dip, ui::ScaleFactor scale_factor) { | |
| 27 SkBitmap bitmap; | |
| 28 const float scale = ui::GetScaleFactorScale(scale_factor); | |
| 29 bitmap.setConfig(SkBitmap::kARGB_8888_Config, | |
| 30 static_cast<int>(size_dip * scale), | |
| 31 static_cast<int>(size_dip * scale)); | |
| 32 bitmap.allocPixels(); | |
| 33 bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0)); | |
| 34 return bitmap; | |
| 35 } | |
| 36 | |
| 37 // Helper class for synchronously loading extension image resource. | |
| 38 class TestImageLoader : public ImageLoadingTracker::Observer { | |
| 39 public: | |
| 40 explicit TestImageLoader(const Extension* extension) | |
| 41 : extension_(extension), | |
| 42 waiting_(false), | |
| 43 image_loaded_(false), | |
| 44 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { | |
| 45 } | |
| 46 virtual ~TestImageLoader() {} | |
| 47 | |
| 48 // ImageLoadingTracker::Observer override. | |
| 49 virtual void OnImageLoaded(const gfx::Image& image, | |
| 50 const std::string& extension_id, | |
| 51 int index) OVERRIDE { | |
| 52 image_ = image; | |
| 53 image_loaded_ = true; | |
| 54 if (waiting_) | |
| 55 MessageLoop::current()->Quit(); | |
| 56 } | |
| 57 | |
| 58 SkBitmap LoadBitmap(const std::string& path, | |
| 59 int size, | |
| 60 ImageLoadingTracker::CacheParam cache_param) { | |
| 61 image_loaded_ = false; | |
| 62 | |
| 63 tracker_.LoadImage(extension_, | |
| 64 extension_->GetResource(path), | |
| 65 gfx::Size(size, size), | |
| 66 cache_param); | |
| 67 | |
| 68 // If |image_| still hasn't been loaded (i.e. it is being loaded | |
| 69 // asynchronously), wait for it. | |
| 70 if (!image_loaded_) { | |
| 71 waiting_ = true; | |
| 72 MessageLoop::current()->Run(); | |
| 73 waiting_ = false; | |
| 74 } | |
| 75 | |
| 76 EXPECT_TRUE(image_loaded_); | |
| 77 | |
| 78 return image_.IsEmpty() ? SkBitmap() : *image_.ToSkBitmap(); | |
| 79 } | |
| 80 | |
| 81 private: | |
| 82 const Extension* extension_; | |
| 83 bool waiting_; | |
| 84 bool image_loaded_; | |
| 85 gfx::Image image_; | |
| 86 ImageLoadingTracker tracker_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(TestImageLoader); | |
| 89 }; | |
| 90 | |
| 21 class ExtensionIconImageTest : public testing::Test, | 91 class ExtensionIconImageTest : public testing::Test, |
| 22 public IconImage::Observer { | 92 public IconImage::Observer { |
| 23 public: | 93 public: |
| 24 ExtensionIconImageTest() | 94 ExtensionIconImageTest() |
| 25 : image_loaded_count_(0), | 95 : image_loaded_count_(0), |
| 26 image_load_failure_count_(0), | |
| 27 quit_in_image_loaded_(false), | 96 quit_in_image_loaded_(false), |
| 28 ui_thread_(BrowserThread::UI, &ui_loop_), | 97 ui_thread_(BrowserThread::UI, &ui_loop_), |
| 29 file_thread_(BrowserThread::FILE), | 98 file_thread_(BrowserThread::FILE), |
| 30 io_thread_(BrowserThread::IO) { | 99 io_thread_(BrowserThread::IO) { |
| 31 } | 100 } |
| 32 | 101 |
| 33 virtual ~ExtensionIconImageTest() {} | 102 virtual ~ExtensionIconImageTest() {} |
| 34 | 103 |
| 35 void WaitForImageLoad() { | 104 void WaitForImageLoad() { |
| 36 // ExtensionIconImage may return synchronously, in which case there's | |
| 37 // nothing to wait for. | |
| 38 if (image_loaded_count_ > 0 || image_load_failure_count_ > 0) | |
| 39 return; | |
| 40 quit_in_image_loaded_ = true; | 105 quit_in_image_loaded_ = true; |
| 41 MessageLoop::current()->Run(); | 106 MessageLoop::current()->Run(); |
| 42 quit_in_image_loaded_ = false; | 107 quit_in_image_loaded_ = false; |
| 43 } | 108 } |
| 44 | 109 |
| 45 int ImageLoadedCount() { | 110 int ImageLoadedCount() { |
| 46 int result = image_loaded_count_; | 111 int result = image_loaded_count_; |
| 47 image_loaded_count_ = 0; | 112 image_loaded_count_ = 0; |
| 48 return result; | 113 return result; |
| 49 } | 114 } |
| 50 | 115 |
| 51 int ImageLoadFailureCount() { | |
| 52 int result = image_load_failure_count_; | |
| 53 image_load_failure_count_ = 0; | |
| 54 return result; | |
| 55 } | |
| 56 | |
| 57 scoped_refptr<Extension> CreateExtension(const char* name, | 116 scoped_refptr<Extension> CreateExtension(const char* name, |
| 58 Extension::Location location) { | 117 Extension::Location location) { |
| 59 // Create and load an extension. | 118 // Create and load an extension. |
| 60 FilePath test_file; | 119 FilePath test_file; |
| 61 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { | 120 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { |
| 62 EXPECT_FALSE(true); | 121 EXPECT_FALSE(true); |
| 63 return NULL; | 122 return NULL; |
| 64 } | 123 } |
| 65 test_file = test_file.AppendASCII("extensions").AppendASCII(name); | 124 test_file = test_file.AppendASCII("extensions").AppendASCII(name); |
| 66 int error_code = 0; | 125 int error_code = 0; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 87 io_thread_.Start(); | 146 io_thread_.Start(); |
| 88 } | 147 } |
| 89 | 148 |
| 90 // IconImage::Delegate overrides: | 149 // IconImage::Delegate overrides: |
| 91 virtual void OnExtensionIconImageChanged(IconImage* image) OVERRIDE { | 150 virtual void OnExtensionIconImageChanged(IconImage* image) OVERRIDE { |
| 92 image_loaded_count_++; | 151 image_loaded_count_++; |
| 93 if (quit_in_image_loaded_) | 152 if (quit_in_image_loaded_) |
| 94 MessageLoop::current()->Quit(); | 153 MessageLoop::current()->Quit(); |
| 95 } | 154 } |
| 96 | 155 |
| 97 virtual void OnIconImageLoadFailed(IconImage* image, | 156 gfx::ImageSkia GetDefaultIcon() { |
| 98 ui::ScaleFactor scale_factor) OVERRIDE { | 157 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 99 image_load_failure_count_++; | 158 IDR_EXTENSIONS_FAVICON); |
| 100 if (quit_in_image_loaded_) | 159 } |
| 101 MessageLoop::current()->Quit(); | 160 |
| 161 // Loads an image to be used in test from the extension. | |
| 162 // The image will be loaded from the relative path |path|. | |
| 163 SkBitmap GetTestBitmap(const Extension* extension, | |
| 164 const std::string& path, | |
| 165 int size, | |
| 166 ImageLoadingTracker::CacheParam cache_param) { | |
| 167 TestImageLoader image_loader(extension); | |
| 168 return image_loader.LoadBitmap(path, size, cache_param); | |
| 102 } | 169 } |
| 103 | 170 |
| 104 private: | 171 private: |
| 105 int image_loaded_count_; | 172 int image_loaded_count_; |
| 106 int image_load_failure_count_; | |
| 107 bool quit_in_image_loaded_; | 173 bool quit_in_image_loaded_; |
| 108 MessageLoop ui_loop_; | 174 MessageLoop ui_loop_; |
| 109 content::TestBrowserThread ui_thread_; | 175 content::TestBrowserThread ui_thread_; |
| 110 content::TestBrowserThread file_thread_; | 176 content::TestBrowserThread file_thread_; |
| 111 content::TestBrowserThread io_thread_; | 177 content::TestBrowserThread io_thread_; |
| 112 | 178 |
| 113 DISALLOW_COPY_AND_ASSIGN(ExtensionIconImageTest); | 179 DISALLOW_COPY_AND_ASSIGN(ExtensionIconImageTest); |
| 114 }; | 180 }; |
| 115 | 181 |
| 116 } // namespace | 182 } // namespace |
| 117 | 183 |
| 118 TEST_F(ExtensionIconImageTest, Basic) { | 184 TEST_F(ExtensionIconImageTest, Basic) { |
| 119 scoped_refptr<Extension> extension(CreateExtension( | 185 scoped_refptr<Extension> extension(CreateExtension( |
| 120 "extension_icon_image", Extension::INVALID)); | 186 "extension_icon_image", Extension::INVALID)); |
| 121 ASSERT_TRUE(extension.get() != NULL); | 187 ASSERT_TRUE(extension.get() != NULL); |
| 122 | 188 |
| 123 IconImage image( | 189 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 124 extension, | 190 |
| 125 extension->icons(), | 191 // Load images we expect to find as representations in icon_image, so we |
| 126 extension_misc::EXTENSION_ICON_BITTY, | 192 // can later use them to validate icon_image. |
| 127 this); | 193 SkBitmap bitmap_16 = |
| 194 GetTestBitmap(extension, "16.png", 16, ImageLoadingTracker::DONT_CACHE); | |
| 195 ASSERT_FALSE(bitmap_16.empty()); | |
| 196 | |
| 197 // There is no image of size 32 defined in the extension manifest, so we | |
| 198 // should expect manifest image of size 48 resized to size 32. | |
| 199 SkBitmap bitmap_48_resized_to_32 = | |
| 200 GetTestBitmap(extension, "48.png", 32, ImageLoadingTracker::DONT_CACHE); | |
| 201 ASSERT_FALSE(bitmap_48_resized_to_32.empty()); | |
| 202 | |
| 203 IconImage image(extension, extension->icons(), 16, default_icon, this); | |
| 128 | 204 |
| 129 // No representations in |image_| yet. | 205 // No representations in |image_| yet. |
| 130 gfx::ImageSkia::ImageSkiaReps image_reps = image.image_skia().image_reps(); | 206 gfx::ImageSkia::ImageSkiaReps image_reps = image.image_skia().image_reps(); |
| 131 ASSERT_EQ(0u, image_reps.size()); | 207 ASSERT_EQ(0u, image_reps.size()); |
| 132 | 208 |
| 133 // Gets representation for a scale factor. | 209 // Gets representation for a scale factor. |
| 134 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | 210 gfx::ImageSkiaRep representation = |
| 135 WaitForImageLoad(); | 211 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); |
| 136 EXPECT_EQ(1, ImageLoadedCount()); | |
| 137 EXPECT_EQ(0, ImageLoadFailureCount()); | |
| 138 | 212 |
| 139 // Gets representation for an additional scale factor. | 213 // Before the image representation is loaded, image should contain blank |
| 140 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | 214 // image representation. |
| 141 WaitForImageLoad(); | 215 EXPECT_TRUE(gfx::BitmapsAreEqual( |
| 142 EXPECT_EQ(1, ImageLoadedCount()); | 216 representation.sk_bitmap(), |
| 143 EXPECT_EQ(0, ImageLoadFailureCount()); | 217 CreateBlankBitmapForScale(16, ui::SCALE_FACTOR_100P))); |
| 144 | |
| 145 gfx::ImageSkiaRep image_rep = | |
| 146 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 147 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, | |
| 148 image_rep.pixel_width()); | |
| 149 | |
| 150 image_rep = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | |
| 151 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALL, | |
| 152 image_rep.pixel_width()); | |
| 153 } | |
| 154 | |
| 155 // If we can't load icon with the exact size, but a bigger resource is | |
| 156 // available. | |
| 157 TEST_F(ExtensionIconImageTest, FallbackToBigger) { | |
| 158 scoped_refptr<Extension> extension(CreateExtension( | |
| 159 "extension_icon_image", Extension::INVALID)); | |
| 160 ASSERT_TRUE(extension.get() != NULL); | |
| 161 | |
| 162 IconImage image( | |
| 163 extension, | |
| 164 extension->icons(), | |
| 165 extension_misc::EXTENSION_ICON_BITTY, | |
| 166 this); | |
| 167 | |
| 168 // Get representation for 2x. | |
| 169 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | |
| 170 | 218 |
| 171 WaitForImageLoad(); | 219 WaitForImageLoad(); |
| 172 EXPECT_EQ(1, ImageLoadedCount()); | 220 EXPECT_EQ(1, ImageLoadedCount()); |
| 173 EXPECT_EQ(0, ImageLoadFailureCount()); | 221 ASSERT_EQ(1u, image.image_skia().image_reps().size()); |
| 174 | 222 |
| 175 gfx::ImageSkiaRep image_rep = | 223 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); |
| 176 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | |
| 177 | 224 |
| 178 // We should have found a bigger resource and it should have been resized. | 225 // We should get the right representation now. |
| 179 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor()); | 226 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_16)); |
| 180 EXPECT_EQ(2 * extension_misc::EXTENSION_ICON_BITTY, | 227 EXPECT_EQ(16, representation.pixel_width()); |
| 181 image_rep.pixel_width()); | 228 |
| 229 // Gets representation for an additional scale factor. | |
| 230 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | |
| 231 | |
| 232 EXPECT_TRUE(gfx::BitmapsAreEqual( | |
| 233 representation.sk_bitmap(), | |
| 234 CreateBlankBitmapForScale(16, ui::SCALE_FACTOR_200P))); | |
| 235 | |
| 236 WaitForImageLoad(); | |
| 237 EXPECT_EQ(1, ImageLoadedCount()); | |
| 238 ASSERT_EQ(2u, image.image_skia().image_reps().size()); | |
| 239 | |
| 240 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | |
| 241 | |
| 242 // Image should have been resized. | |
| 243 EXPECT_EQ(32, representation.pixel_width()); | |
| 244 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), | |
| 245 bitmap_48_resized_to_32)); | |
| 182 } | 246 } |
| 183 | 247 |
| 184 // There is no resource with either exact or bigger size, but there is a smaller | 248 // There is no resource with either exact or bigger size, but there is a smaller |
| 185 // resource. | 249 // resource. |
| 186 TEST_F(ExtensionIconImageTest, FallbackToSmallerWhenNoBigger) { | 250 TEST_F(ExtensionIconImageTest, FallbackToSmallerWhenNoBigger) { |
| 187 scoped_refptr<Extension> extension(CreateExtension( | 251 scoped_refptr<Extension> extension(CreateExtension( |
| 188 "extension_icon_image", Extension::INVALID)); | 252 "extension_icon_image", Extension::INVALID)); |
| 189 ASSERT_TRUE(extension.get() != NULL); | 253 ASSERT_TRUE(extension.get() != NULL); |
| 190 | 254 |
| 191 IconImage image( | 255 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 192 extension, | 256 |
| 193 extension->icons(), | 257 // Load images we expect to find as representations in icon_image, so we |
| 194 extension_misc::EXTENSION_ICON_SMALL, | 258 // can later use them to validate icon_image. |
| 195 this); | 259 SkBitmap bitmap_48 = |
| 260 GetTestBitmap(extension, "48.png", 48, ImageLoadingTracker::DONT_CACHE); | |
| 261 ASSERT_FALSE(bitmap_48.empty()); | |
| 262 | |
| 263 IconImage image(extension, extension->icons(), 32, default_icon, this); | |
| 196 | 264 |
| 197 // Attempt to get representation for 2x. | 265 // Attempt to get representation for 2x. |
| 198 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | 266 gfx::ImageSkiaRep representation = |
| 267 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | |
| 199 | 268 |
| 200 WaitForImageLoad(); | 269 WaitForImageLoad(); |
| 201 EXPECT_EQ(1, ImageLoadedCount()); | 270 EXPECT_EQ(1, ImageLoadedCount()); |
| 202 EXPECT_EQ(0, ImageLoadFailureCount()); | 271 ASSERT_EQ(1u, image.image_skia().image_reps().size()); |
| 203 | 272 |
| 204 gfx::ImageSkiaRep image_rep = | 273 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); |
| 205 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | |
| 206 | 274 |
| 207 // We should have loaded the biggest smaller resource. In this case the | 275 // We should have loaded the biggest smaller resource. In this case the |
| 208 // loaded resource should not be resized. | 276 // loaded resource should not be resized. |
| 209 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor()); | 277 EXPECT_EQ(ui::SCALE_FACTOR_200P, representation.scale_factor()); |
| 210 EXPECT_EQ(extension_misc::EXTENSION_ICON_MEDIUM, | 278 EXPECT_EQ(48, representation.pixel_width()); |
| 211 image_rep.pixel_width()); | 279 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_48)); |
| 212 } | 280 } |
| 213 | 281 |
| 214 // There is no resource with exact size, but there is a smaller and a bigger | 282 // There is no resource with exact size, but there is a smaller and a bigger |
| 215 // one. Requested size is smaller than 32 though, so the smaller resource should | 283 // one. Requested size is smaller than 32 though, so the smaller resource should |
| 216 // be loaded. | 284 // be loaded. |
| 217 TEST_F(ExtensionIconImageTest, FallbackToSmaller) { | 285 TEST_F(ExtensionIconImageTest, FallbackToSmaller) { |
| 218 scoped_refptr<Extension> extension(CreateExtension( | 286 scoped_refptr<Extension> extension(CreateExtension( |
| 219 "extension_icon_image", Extension::INVALID)); | 287 "extension_icon_image", Extension::INVALID)); |
| 220 ASSERT_TRUE(extension.get() != NULL); | 288 ASSERT_TRUE(extension.get() != NULL); |
| 221 | 289 |
| 222 IconImage image( | 290 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 223 extension, | 291 |
| 224 extension->icons(), | 292 // Load images we expect to find as representations in icon_image, so we |
| 225 17, | 293 // can later use them to validate icon_image. |
| 226 this); | 294 SkBitmap bitmap_16 = |
| 295 GetTestBitmap(extension, "16.png", 16, ImageLoadingTracker::DONT_CACHE); | |
| 296 ASSERT_FALSE(bitmap_16.empty()); | |
| 297 | |
| 298 IconImage image(extension, extension->icons(), 17, default_icon, this); | |
| 227 | 299 |
| 228 // Attempt to get representation for 1x. | 300 // Attempt to get representation for 1x. |
| 229 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | 301 gfx::ImageSkiaRep representation = |
| 302 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 230 | 303 |
| 231 WaitForImageLoad(); | 304 WaitForImageLoad(); |
| 232 EXPECT_EQ(1, ImageLoadedCount()); | 305 EXPECT_EQ(1, ImageLoadedCount()); |
| 233 EXPECT_EQ(0, ImageLoadFailureCount()); | 306 ASSERT_EQ(1u, image.image_skia().image_reps().size()); |
| 234 | 307 |
| 235 gfx::ImageSkiaRep image_rep = | 308 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); |
| 236 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 237 | 309 |
| 238 // We should have loaded smaller (not resized) resource. | 310 // We should have loaded smaller (not resized) resource. |
| 239 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor()); | 311 EXPECT_EQ(ui::SCALE_FACTOR_100P, representation.scale_factor()); |
| 240 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, | 312 EXPECT_EQ(16, representation.pixel_width()); |
| 241 image_rep.pixel_width()); | 313 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_16)); |
| 242 } | 314 } |
| 243 | 315 |
| 244 // If resource set is empty, failure should be reported. | 316 // If resource set is empty, |GetRepresentation| should synchronously return |
| 317 // default icon, without notifying observer of image change. | |
| 245 TEST_F(ExtensionIconImageTest, NoResources) { | 318 TEST_F(ExtensionIconImageTest, NoResources) { |
| 246 scoped_refptr<Extension> extension(CreateExtension( | 319 scoped_refptr<Extension> extension(CreateExtension( |
| 247 "extension_icon_image", Extension::INVALID)); | 320 "extension_icon_image", Extension::INVALID)); |
| 248 ASSERT_TRUE(extension.get() != NULL); | 321 ASSERT_TRUE(extension.get() != NULL); |
| 249 | 322 |
| 250 ExtensionIconSet empty_icon_set; | 323 ExtensionIconSet empty_icon_set; |
| 324 gfx::ImageSkia default_icon = GetDefaultIcon(); | |
| 251 | 325 |
| 252 IconImage image( | 326 IconImage image(extension, empty_icon_set, 24, default_icon, this); |
| 253 extension, | |
| 254 empty_icon_set, | |
| 255 extension_misc::EXTENSION_ICON_SMALLISH, | |
| 256 this); | |
| 257 | 327 |
| 258 // Attempt to get representation for 2x. | 328 // Attempt to get representation for 2x. |
| 259 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); | 329 gfx::ImageSkiaRep representation = |
| 330 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 331 EXPECT_TRUE(gfx::BitmapsAreEqual( | |
| 332 representation.sk_bitmap(), | |
| 333 default_icon.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap())); | |
| 334 | |
| 335 EXPECT_EQ(0, ImageLoadedCount()); | |
| 336 // We should have a default icon representation. | |
| 337 ASSERT_EQ(1u, image.image_skia().image_reps().size()); | |
| 338 | |
| 339 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 340 EXPECT_TRUE(gfx::BitmapsAreEqual( | |
| 341 representation.sk_bitmap(), | |
| 342 default_icon.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap())); | |
| 343 } | |
| 344 | |
| 345 // If resource set is invalid, image load should be done asynchronously and | |
| 346 // the observer should be notified when it's done. |GetRepresentation| should | |
| 347 // return the default icon representation once image load is done. | |
| 348 TEST_F(ExtensionIconImageTest, InvalidResource) { | |
| 349 scoped_refptr<Extension> extension(CreateExtension( | |
| 350 "extension_icon_image", Extension::INVALID)); | |
| 351 ASSERT_TRUE(extension.get() != NULL); | |
| 352 | |
| 353 ExtensionIconSet invalid_icon_set; | |
| 354 invalid_icon_set.Add(24, "invalid.png"); | |
| 355 | |
| 356 gfx::ImageSkia default_icon = GetDefaultIcon(); | |
| 357 | |
| 358 IconImage image(extension, invalid_icon_set, 24, default_icon, this); | |
| 359 | |
| 360 // Attempt to get representation for 2x. | |
| 361 gfx::ImageSkiaRep representation = | |
| 362 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 363 EXPECT_TRUE(gfx::BitmapsAreEqual( | |
| 364 representation.sk_bitmap(), | |
| 365 CreateBlankBitmapForScale(24, ui::SCALE_FACTOR_100P))); | |
| 260 | 366 |
| 261 WaitForImageLoad(); | 367 WaitForImageLoad(); |
| 368 EXPECT_EQ(1, ImageLoadedCount()); | |
| 369 // We should have default icon representation now. | |
|
Jeffrey Yasskin
2012/09/05 23:14:49
s/should have/should have/
tbarzic
2012/09/06 00:39:53
Done.
| |
| 370 ASSERT_EQ(1u, image.image_skia().image_reps().size()); | |
| 371 | |
| 372 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 373 EXPECT_TRUE(gfx::BitmapsAreEqual( | |
| 374 representation.sk_bitmap(), | |
| 375 default_icon.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap())); | |
| 376 } | |
| 377 | |
| 378 TEST_F(ExtensionIconImageTest, LoadPrecachedImage) { | |
| 379 scoped_refptr<Extension> extension(CreateExtension( | |
| 380 "extension_icon_image", Extension::INVALID)); | |
| 381 ASSERT_TRUE(extension.get() != NULL); | |
| 382 | |
| 383 gfx::ImageSkia default_icon = GetDefaultIcon(); | |
| 384 | |
| 385 // Note the cache parameter. | |
| 386 SkBitmap bitmap_16 = | |
| 387 GetTestBitmap(extension, "16.png", 16, ImageLoadingTracker::CACHE); | |
| 388 ASSERT_FALSE(bitmap_16.empty()); | |
| 389 | |
| 390 IconImage image(extension, extension->icons(), 16, default_icon, this); | |
| 391 | |
| 392 // No representations in |image_| yet. | |
| 393 gfx::ImageSkia::ImageSkiaReps image_reps = image.image_skia().image_reps(); | |
| 394 ASSERT_EQ(0u, image_reps.size()); | |
| 395 | |
| 396 // Gets representation for a scale factor. | |
| 397 // Since the icon representation is precached, it should be returned right | |
| 398 // away. Also, we should not receive any notifications. | |
| 399 gfx::ImageSkiaRep representation = | |
| 400 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 401 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_16)); | |
| 402 | |
| 262 EXPECT_EQ(0, ImageLoadedCount()); | 403 EXPECT_EQ(0, ImageLoadedCount()); |
| 263 EXPECT_EQ(1, ImageLoadFailureCount()); | 404 ASSERT_EQ(1u, image.image_skia().image_reps().size()); |
| 264 } | 405 } |
| 406 | |
| 407 TEST_F(ExtensionIconImageTest, IconImageDestruction) { | |
| 408 scoped_refptr<Extension> extension(CreateExtension( | |
| 409 "extension_icon_image", Extension::INVALID)); | |
| 410 ASSERT_TRUE(extension.get() != NULL); | |
| 411 | |
| 412 gfx::ImageSkia default_icon = GetDefaultIcon(); | |
| 413 | |
| 414 // Load images we expect to find as representations in icon_image, so we | |
| 415 // can later use them to validate icon_image. | |
| 416 SkBitmap bitmap_16 = | |
| 417 GetTestBitmap(extension, "16.png", 16, ImageLoadingTracker::DONT_CACHE); | |
| 418 ASSERT_FALSE(bitmap_16.empty()); | |
| 419 | |
| 420 scoped_ptr<IconImage> image( | |
| 421 new IconImage(extension, extension->icons(), 16, default_icon, this)); | |
| 422 | |
| 423 // Load an image representation. | |
| 424 gfx::ImageSkiaRep representation = | |
| 425 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 426 | |
| 427 WaitForImageLoad(); | |
| 428 EXPECT_EQ(1, ImageLoadedCount()); | |
| 429 ASSERT_EQ(1u, image->image_skia().image_reps().size()); | |
| 430 | |
| 431 // Stash loaded image skia, and destroy |image|. | |
| 432 gfx::ImageSkia image_skia = image->image_skia(); | |
| 433 image.reset(); | |
| 434 extension = NULL; | |
| 435 | |
| 436 // Image skia should still be able to get previously loaded representation. | |
| 437 representation = image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 438 | |
| 439 EXPECT_EQ(ui::SCALE_FACTOR_100P, representation.scale_factor()); | |
| 440 EXPECT_EQ(16, representation.pixel_width()); | |
| 441 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_16)); | |
| 442 | |
| 443 // When requesting another representation, we should get blank image. | |
| 444 representation = image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); | |
| 445 | |
| 446 EXPECT_TRUE(gfx::BitmapsAreEqual( | |
| 447 representation.sk_bitmap(), | |
| 448 CreateBlankBitmapForScale(16, ui::SCALE_FACTOR_200P))); | |
| 449 } | |
| OLD | NEW |