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