| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/extension_icon_image.h" | 5 #include "extensions/browser/extension_icon_image.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 image_loaded_count_ = 0; | 138 image_loaded_count_ = 0; |
| 139 return result; | 139 return result; |
| 140 } | 140 } |
| 141 | 141 |
| 142 scoped_refptr<Extension> CreateExtension(const char* name, | 142 scoped_refptr<Extension> CreateExtension(const char* name, |
| 143 Manifest::Location location) { | 143 Manifest::Location location) { |
| 144 // Create and load an extension. | 144 // Create and load an extension. |
| 145 base::FilePath test_file; | 145 base::FilePath test_file; |
| 146 if (!PathService::Get(DIR_TEST_DATA, &test_file)) { | 146 if (!PathService::Get(DIR_TEST_DATA, &test_file)) { |
| 147 EXPECT_FALSE(true); | 147 EXPECT_FALSE(true); |
| 148 return NULL; | 148 return nullptr; |
| 149 } | 149 } |
| 150 test_file = test_file.AppendASCII(name); | 150 test_file = test_file.AppendASCII(name); |
| 151 int error_code = 0; | 151 int error_code = 0; |
| 152 std::string error; | 152 std::string error; |
| 153 JSONFileValueSerializer serializer(test_file.AppendASCII("manifest.json")); | 153 JSONFileValueSerializer serializer(test_file.AppendASCII("manifest.json")); |
| 154 scoped_ptr<base::DictionaryValue> valid_value( | 154 scoped_ptr<base::DictionaryValue> valid_value( |
| 155 static_cast<base::DictionaryValue*>(serializer.Deserialize(&error_code, | 155 static_cast<base::DictionaryValue*>(serializer.Deserialize(&error_code, |
| 156 &error))); | 156 &error))); |
| 157 EXPECT_EQ(0, error_code) << error; | 157 EXPECT_EQ(0, error_code) << error; |
| 158 if (error_code != 0) | 158 if (error_code != 0) |
| 159 return NULL; | 159 return nullptr; |
| 160 | 160 |
| 161 EXPECT_TRUE(valid_value.get()); | 161 EXPECT_TRUE(valid_value.get()); |
| 162 if (!valid_value) | 162 if (!valid_value) |
| 163 return NULL; | 163 return nullptr; |
| 164 | 164 |
| 165 return Extension::Create(test_file, location, *valid_value, | 165 return Extension::Create(test_file, location, *valid_value, |
| 166 Extension::NO_FLAGS, &error); | 166 Extension::NO_FLAGS, &error); |
| 167 } | 167 } |
| 168 | 168 |
| 169 // testing::Test overrides: | 169 // testing::Test overrides: |
| 170 virtual void SetUp() OVERRIDE { | 170 virtual void SetUp() OVERRIDE { |
| 171 file_thread_.Start(); | 171 file_thread_.Start(); |
| 172 io_thread_.Start(); | 172 io_thread_.Start(); |
| 173 } | 173 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 206 |
| 207 } // namespace | 207 } // namespace |
| 208 | 208 |
| 209 TEST_F(ExtensionIconImageTest, Basic) { | 209 TEST_F(ExtensionIconImageTest, Basic) { |
| 210 std::vector<ui::ScaleFactor> supported_factors; | 210 std::vector<ui::ScaleFactor> supported_factors; |
| 211 supported_factors.push_back(ui::SCALE_FACTOR_100P); | 211 supported_factors.push_back(ui::SCALE_FACTOR_100P); |
| 212 supported_factors.push_back(ui::SCALE_FACTOR_200P); | 212 supported_factors.push_back(ui::SCALE_FACTOR_200P); |
| 213 ui::test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors); | 213 ui::test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors); |
| 214 scoped_refptr<Extension> extension(CreateExtension( | 214 scoped_refptr<Extension> extension(CreateExtension( |
| 215 "extension_icon_image", Manifest::INVALID_LOCATION)); | 215 "extension_icon_image", Manifest::INVALID_LOCATION)); |
| 216 ASSERT_TRUE(extension.get() != NULL); | 216 ASSERT_TRUE(extension.get() != nullptr); |
| 217 | 217 |
| 218 gfx::ImageSkia default_icon = GetDefaultIcon(); | 218 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 219 | 219 |
| 220 // Load images we expect to find as representations in icon_image, so we | 220 // Load images we expect to find as representations in icon_image, so we |
| 221 // can later use them to validate icon_image. | 221 // can later use them to validate icon_image. |
| 222 SkBitmap bitmap_16 = GetTestBitmap(extension.get(), "16.png", 16); | 222 SkBitmap bitmap_16 = GetTestBitmap(extension.get(), "16.png", 16); |
| 223 ASSERT_FALSE(bitmap_16.empty()); | 223 ASSERT_FALSE(bitmap_16.empty()); |
| 224 | 224 |
| 225 // There is no image of size 32 defined in the extension manifest, so we | 225 // There is no image of size 32 defined in the extension manifest, so we |
| 226 // should expect manifest image of size 48 resized to size 32. | 226 // should expect manifest image of size 48 resized to size 32. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 | 279 |
| 280 // There is no resource with either exact or bigger size, but there is a smaller | 280 // There is no resource with either exact or bigger size, but there is a smaller |
| 281 // resource. | 281 // resource. |
| 282 TEST_F(ExtensionIconImageTest, FallbackToSmallerWhenNoBigger) { | 282 TEST_F(ExtensionIconImageTest, FallbackToSmallerWhenNoBigger) { |
| 283 std::vector<ui::ScaleFactor> supported_factors; | 283 std::vector<ui::ScaleFactor> supported_factors; |
| 284 supported_factors.push_back(ui::SCALE_FACTOR_100P); | 284 supported_factors.push_back(ui::SCALE_FACTOR_100P); |
| 285 supported_factors.push_back(ui::SCALE_FACTOR_200P); | 285 supported_factors.push_back(ui::SCALE_FACTOR_200P); |
| 286 ui::test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors); | 286 ui::test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors); |
| 287 scoped_refptr<Extension> extension(CreateExtension( | 287 scoped_refptr<Extension> extension(CreateExtension( |
| 288 "extension_icon_image", Manifest::INVALID_LOCATION)); | 288 "extension_icon_image", Manifest::INVALID_LOCATION)); |
| 289 ASSERT_TRUE(extension.get() != NULL); | 289 ASSERT_TRUE(extension.get() != nullptr); |
| 290 | 290 |
| 291 gfx::ImageSkia default_icon = GetDefaultIcon(); | 291 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 292 | 292 |
| 293 // Load images we expect to find as representations in icon_image, so we | 293 // Load images we expect to find as representations in icon_image, so we |
| 294 // can later use them to validate icon_image. | 294 // can later use them to validate icon_image. |
| 295 SkBitmap bitmap_48 = GetTestBitmap(extension.get(), "48.png", 48); | 295 SkBitmap bitmap_48 = GetTestBitmap(extension.get(), "48.png", 48); |
| 296 ASSERT_FALSE(bitmap_48.empty()); | 296 ASSERT_FALSE(bitmap_48.empty()); |
| 297 | 297 |
| 298 IconImage image(browser_context(), | 298 IconImage image(browser_context(), |
| 299 extension.get(), | 299 extension.get(), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 316 EXPECT_EQ(64, representation.pixel_width()); | 316 EXPECT_EQ(64, representation.pixel_width()); |
| 317 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), | 317 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), |
| 318 EnsureBitmapSize(bitmap_48, 64))); | 318 EnsureBitmapSize(bitmap_48, 64))); |
| 319 } | 319 } |
| 320 | 320 |
| 321 // There is no resource with exact size, but there is a smaller and a bigger | 321 // There is no resource with exact size, but there is a smaller and a bigger |
| 322 // one. The bigger resource should be loaded. | 322 // one. The bigger resource should be loaded. |
| 323 TEST_F(ExtensionIconImageTest, FallbackToBigger) { | 323 TEST_F(ExtensionIconImageTest, FallbackToBigger) { |
| 324 scoped_refptr<Extension> extension(CreateExtension( | 324 scoped_refptr<Extension> extension(CreateExtension( |
| 325 "extension_icon_image", Manifest::INVALID_LOCATION)); | 325 "extension_icon_image", Manifest::INVALID_LOCATION)); |
| 326 ASSERT_TRUE(extension.get() != NULL); | 326 ASSERT_TRUE(extension.get() != nullptr); |
| 327 | 327 |
| 328 gfx::ImageSkia default_icon = GetDefaultIcon(); | 328 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 329 | 329 |
| 330 // Load images we expect to find as representations in icon_image, so we | 330 // Load images we expect to find as representations in icon_image, so we |
| 331 // can later use them to validate icon_image. | 331 // can later use them to validate icon_image. |
| 332 SkBitmap bitmap_24 = GetTestBitmap(extension.get(), "24.png", 24); | 332 SkBitmap bitmap_24 = GetTestBitmap(extension.get(), "24.png", 24); |
| 333 ASSERT_FALSE(bitmap_24.empty()); | 333 ASSERT_FALSE(bitmap_24.empty()); |
| 334 | 334 |
| 335 IconImage image(browser_context(), | 335 IconImage image(browser_context(), |
| 336 extension.get(), | 336 extension.get(), |
| (...skipping 15 matching lines...) Expand all Loading... |
| 352 EXPECT_EQ(17, representation.pixel_width()); | 352 EXPECT_EQ(17, representation.pixel_width()); |
| 353 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), | 353 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), |
| 354 EnsureBitmapSize(bitmap_24, 17))); | 354 EnsureBitmapSize(bitmap_24, 17))); |
| 355 } | 355 } |
| 356 | 356 |
| 357 // If resource set is empty, |GetRepresentation| should synchronously return | 357 // If resource set is empty, |GetRepresentation| should synchronously return |
| 358 // default icon, without notifying observer of image change. | 358 // default icon, without notifying observer of image change. |
| 359 TEST_F(ExtensionIconImageTest, NoResources) { | 359 TEST_F(ExtensionIconImageTest, NoResources) { |
| 360 scoped_refptr<Extension> extension(CreateExtension( | 360 scoped_refptr<Extension> extension(CreateExtension( |
| 361 "extension_icon_image", Manifest::INVALID_LOCATION)); | 361 "extension_icon_image", Manifest::INVALID_LOCATION)); |
| 362 ASSERT_TRUE(extension.get() != NULL); | 362 ASSERT_TRUE(extension.get() != nullptr); |
| 363 | 363 |
| 364 ExtensionIconSet empty_icon_set; | 364 ExtensionIconSet empty_icon_set; |
| 365 gfx::ImageSkia default_icon = GetDefaultIcon(); | 365 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 366 | 366 |
| 367 const int kRequestedSize = 24; | 367 const int kRequestedSize = 24; |
| 368 IconImage image(browser_context(), | 368 IconImage image(browser_context(), |
| 369 extension.get(), | 369 extension.get(), |
| 370 empty_icon_set, | 370 empty_icon_set, |
| 371 kRequestedSize, | 371 kRequestedSize, |
| 372 default_icon, | 372 default_icon, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 390 default_icon.GetRepresentation(1.0f).sk_bitmap(), | 390 default_icon.GetRepresentation(1.0f).sk_bitmap(), |
| 391 kRequestedSize))); | 391 kRequestedSize))); |
| 392 } | 392 } |
| 393 | 393 |
| 394 // If resource set is invalid, image load should be done asynchronously and | 394 // If resource set is invalid, image load should be done asynchronously and |
| 395 // the observer should be notified when it's done. |GetRepresentation| should | 395 // the observer should be notified when it's done. |GetRepresentation| should |
| 396 // return the default icon representation once image load is done. | 396 // return the default icon representation once image load is done. |
| 397 TEST_F(ExtensionIconImageTest, InvalidResource) { | 397 TEST_F(ExtensionIconImageTest, InvalidResource) { |
| 398 scoped_refptr<Extension> extension(CreateExtension( | 398 scoped_refptr<Extension> extension(CreateExtension( |
| 399 "extension_icon_image", Manifest::INVALID_LOCATION)); | 399 "extension_icon_image", Manifest::INVALID_LOCATION)); |
| 400 ASSERT_TRUE(extension.get() != NULL); | 400 ASSERT_TRUE(extension.get() != nullptr); |
| 401 | 401 |
| 402 const int kInvalidIconSize = 24; | 402 const int kInvalidIconSize = 24; |
| 403 ExtensionIconSet invalid_icon_set; | 403 ExtensionIconSet invalid_icon_set; |
| 404 invalid_icon_set.Add(kInvalidIconSize, "invalid.png"); | 404 invalid_icon_set.Add(kInvalidIconSize, "invalid.png"); |
| 405 | 405 |
| 406 gfx::ImageSkia default_icon = GetDefaultIcon(); | 406 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 407 | 407 |
| 408 IconImage image(browser_context(), | 408 IconImage image(browser_context(), |
| 409 extension.get(), | 409 extension.get(), |
| 410 invalid_icon_set, | 410 invalid_icon_set, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 428 EnsureBitmapSize( | 428 EnsureBitmapSize( |
| 429 default_icon.GetRepresentation(1.0f).sk_bitmap(), | 429 default_icon.GetRepresentation(1.0f).sk_bitmap(), |
| 430 kInvalidIconSize))); | 430 kInvalidIconSize))); |
| 431 } | 431 } |
| 432 | 432 |
| 433 // Test that IconImage works with lazily (but synchronously) created default | 433 // Test that IconImage works with lazily (but synchronously) created default |
| 434 // icon when IconImage returns synchronously. | 434 // icon when IconImage returns synchronously. |
| 435 TEST_F(ExtensionIconImageTest, LazyDefaultIcon) { | 435 TEST_F(ExtensionIconImageTest, LazyDefaultIcon) { |
| 436 scoped_refptr<Extension> extension(CreateExtension( | 436 scoped_refptr<Extension> extension(CreateExtension( |
| 437 "extension_icon_image", Manifest::INVALID_LOCATION)); | 437 "extension_icon_image", Manifest::INVALID_LOCATION)); |
| 438 ASSERT_TRUE(extension.get() != NULL); | 438 ASSERT_TRUE(extension.get() != nullptr); |
| 439 | 439 |
| 440 gfx::ImageSkia default_icon = GetDefaultIcon(); | 440 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 441 gfx::ImageSkia lazy_default_icon(new MockImageSkiaSource(default_icon), | 441 gfx::ImageSkia lazy_default_icon(new MockImageSkiaSource(default_icon), |
| 442 default_icon.size()); | 442 default_icon.size()); |
| 443 | 443 |
| 444 ExtensionIconSet empty_icon_set; | 444 ExtensionIconSet empty_icon_set; |
| 445 | 445 |
| 446 const int kRequestedSize = 128; | 446 const int kRequestedSize = 128; |
| 447 IconImage image(browser_context(), | 447 IconImage image(browser_context(), |
| 448 extension.get(), | 448 extension.get(), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 465 | 465 |
| 466 // We should have a default icon representation. | 466 // We should have a default icon representation. |
| 467 ASSERT_EQ(1u, image.image_skia().image_reps().size()); | 467 ASSERT_EQ(1u, image.image_skia().image_reps().size()); |
| 468 } | 468 } |
| 469 | 469 |
| 470 // Test that IconImage works with lazily (but synchronously) created default | 470 // Test that IconImage works with lazily (but synchronously) created default |
| 471 // icon when IconImage returns asynchronously. | 471 // icon when IconImage returns asynchronously. |
| 472 TEST_F(ExtensionIconImageTest, LazyDefaultIcon_AsyncIconImage) { | 472 TEST_F(ExtensionIconImageTest, LazyDefaultIcon_AsyncIconImage) { |
| 473 scoped_refptr<Extension> extension(CreateExtension( | 473 scoped_refptr<Extension> extension(CreateExtension( |
| 474 "extension_icon_image", Manifest::INVALID_LOCATION)); | 474 "extension_icon_image", Manifest::INVALID_LOCATION)); |
| 475 ASSERT_TRUE(extension.get() != NULL); | 475 ASSERT_TRUE(extension.get() != nullptr); |
| 476 | 476 |
| 477 gfx::ImageSkia default_icon = GetDefaultIcon(); | 477 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 478 gfx::ImageSkia lazy_default_icon(new MockImageSkiaSource(default_icon), | 478 gfx::ImageSkia lazy_default_icon(new MockImageSkiaSource(default_icon), |
| 479 default_icon.size()); | 479 default_icon.size()); |
| 480 | 480 |
| 481 const int kInvalidIconSize = 24; | 481 const int kInvalidIconSize = 24; |
| 482 ExtensionIconSet invalid_icon_set; | 482 ExtensionIconSet invalid_icon_set; |
| 483 invalid_icon_set.Add(kInvalidIconSize, "invalid.png"); | 483 invalid_icon_set.Add(kInvalidIconSize, "invalid.png"); |
| 484 | 484 |
| 485 IconImage image(browser_context(), | 485 IconImage image(browser_context(), |
| (...skipping 22 matching lines...) Expand all Loading... |
| 508 kInvalidIconSize))); | 508 kInvalidIconSize))); |
| 509 } | 509 } |
| 510 | 510 |
| 511 // Tests behavior of image created by IconImage after IconImage host goes | 511 // Tests behavior of image created by IconImage after IconImage host goes |
| 512 // away. The image should still return loaded representations. If requested | 512 // away. The image should still return loaded representations. If requested |
| 513 // representation was not loaded while IconImage host was around, transparent | 513 // representation was not loaded while IconImage host was around, transparent |
| 514 // representations should be returned. | 514 // representations should be returned. |
| 515 TEST_F(ExtensionIconImageTest, IconImageDestruction) { | 515 TEST_F(ExtensionIconImageTest, IconImageDestruction) { |
| 516 scoped_refptr<Extension> extension(CreateExtension( | 516 scoped_refptr<Extension> extension(CreateExtension( |
| 517 "extension_icon_image", Manifest::INVALID_LOCATION)); | 517 "extension_icon_image", Manifest::INVALID_LOCATION)); |
| 518 ASSERT_TRUE(extension.get() != NULL); | 518 ASSERT_TRUE(extension.get() != nullptr); |
| 519 | 519 |
| 520 gfx::ImageSkia default_icon = GetDefaultIcon(); | 520 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 521 | 521 |
| 522 // Load images we expect to find as representations in icon_image, so we | 522 // Load images we expect to find as representations in icon_image, so we |
| 523 // can later use them to validate icon_image. | 523 // can later use them to validate icon_image. |
| 524 SkBitmap bitmap_16 = GetTestBitmap(extension.get(), "16.png", 16); | 524 SkBitmap bitmap_16 = GetTestBitmap(extension.get(), "16.png", 16); |
| 525 ASSERT_FALSE(bitmap_16.empty()); | 525 ASSERT_FALSE(bitmap_16.empty()); |
| 526 | 526 |
| 527 scoped_ptr<IconImage> image( | 527 scoped_ptr<IconImage> image( |
| 528 new IconImage(browser_context(), | 528 new IconImage(browser_context(), |
| 529 extension.get(), | 529 extension.get(), |
| 530 IconsInfo::GetIcons(extension.get()), | 530 IconsInfo::GetIcons(extension.get()), |
| 531 16, | 531 16, |
| 532 default_icon, | 532 default_icon, |
| 533 this)); | 533 this)); |
| 534 | 534 |
| 535 // Load an image representation. | 535 // Load an image representation. |
| 536 gfx::ImageSkiaRep representation = | 536 gfx::ImageSkiaRep representation = |
| 537 image->image_skia().GetRepresentation(1.0f); | 537 image->image_skia().GetRepresentation(1.0f); |
| 538 | 538 |
| 539 WaitForImageLoad(); | 539 WaitForImageLoad(); |
| 540 EXPECT_EQ(1, ImageLoadedCount()); | 540 EXPECT_EQ(1, ImageLoadedCount()); |
| 541 ASSERT_EQ(1u, image->image_skia().image_reps().size()); | 541 ASSERT_EQ(1u, image->image_skia().image_reps().size()); |
| 542 | 542 |
| 543 // Stash loaded image skia, and destroy |image|. | 543 // Stash loaded image skia, and destroy |image|. |
| 544 gfx::ImageSkia image_skia = image->image_skia(); | 544 gfx::ImageSkia image_skia = image->image_skia(); |
| 545 image.reset(); | 545 image.reset(); |
| 546 extension = NULL; | 546 extension = nullptr; |
| 547 | 547 |
| 548 // Image skia should still be able to get previously loaded representation. | 548 // Image skia should still be able to get previously loaded representation. |
| 549 representation = image_skia.GetRepresentation(1.0f); | 549 representation = image_skia.GetRepresentation(1.0f); |
| 550 | 550 |
| 551 EXPECT_EQ(1.0f, representation.scale()); | 551 EXPECT_EQ(1.0f, representation.scale()); |
| 552 EXPECT_EQ(16, representation.pixel_width()); | 552 EXPECT_EQ(16, representation.pixel_width()); |
| 553 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_16)); | 553 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_16)); |
| 554 | 554 |
| 555 // When requesting another representation, we should not crash and return some | 555 // When requesting another representation, we should not crash and return some |
| 556 // image of the size. It could be blank or a rescale from the existing 1.0f | 556 // image of the size. It could be blank or a rescale from the existing 1.0f |
| 557 // icon. | 557 // icon. |
| 558 representation = image_skia.GetRepresentation(2.0f); | 558 representation = image_skia.GetRepresentation(2.0f); |
| 559 | 559 |
| 560 EXPECT_EQ(16, representation.GetWidth()); | 560 EXPECT_EQ(16, representation.GetWidth()); |
| 561 EXPECT_EQ(16, representation.GetHeight()); | 561 EXPECT_EQ(16, representation.GetHeight()); |
| 562 EXPECT_EQ(2.0f, representation.scale()); | 562 EXPECT_EQ(2.0f, representation.scale()); |
| 563 } | 563 } |
| 564 | 564 |
| 565 } // namespace extensions | 565 } // namespace extensions |
| OLD | NEW |