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