Chromium Code Reviews| Index: chrome/browser/extensions/api/extension_action/browser_action_apitest.cc |
| diff --git a/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc b/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc |
| index 737b94c248cf4ec7c8a9a8c3f8a1f55ce760b212..08c9727ebaab6816b47265db32eea9f99dc375b6 100644 |
| --- a/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc |
| +++ b/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc |
| @@ -24,12 +24,39 @@ |
| #include "content/public/browser/notification_service.h" |
| #include "content/public/browser/web_contents.h" |
| #include "content/public/test/browser_test_utils.h" |
| +#include "grit/theme_resources.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| #include "ui/gfx/rect.h" |
| #include "ui/gfx/size.h" |
| +#include "ui/gfx/image/image_skia.h" |
| +#include "ui/gfx/image/image_skia_operations.h" |
| +#include "ui/gfx/skia_util.h" |
| using content::WebContents; |
| using extensions::Extension; |
| +namespace { |
| + |
| +// Views implementation of browser action button will return icon with set |
| +// background. |
| +gfx::ImageSkia AddBackgroundForViews(const gfx::ImageSkia& icon) { |
| +#if defined(TOOLKIT_VIEWS) |
| + ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| + gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION); |
| + return gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon); |
| +#endif |
| + |
| + return icon; |
| +} |
| + |
| +bool ImagesAreEqual(const gfx::ImageSkia& i1, const gfx::ImageSkia& i2) { |
| + SkBitmap bitmap1 = |
| + i1.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap(); |
| + SkBitmap bitmap2 = |
| + i2.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap(); |
| + return gfx::BitmapsAreEqual(bitmap1, bitmap2); |
| +} |
| + |
| class BrowserActionApiTest : public ExtensionApiTest { |
| public: |
| BrowserActionApiTest() {} |
| @@ -102,33 +129,110 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) { |
| ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); |
| EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); |
| - // Set prev_id which holds the id of the previous image, and use it in the |
| - // next test to see if the image changes. |
| - uint32_t prev_id = extension->browser_action()->GetIcon(0). |
| - ToSkBitmap()->getGenerationID(); |
| + gfx::Image action_icon = extension->browser_action()->GetIcon(0); |
| + uint32_t action_icon_last_id = action_icon.AsBitmap().getGenerationID(); |
|
Jeffrey Yasskin
2012/08/21 20:10:38
It might be nice to have one sanity-check here, of
tbarzic
2012/08/22 00:12:49
Done.
|
| + uint32_t action_icon_current_id = 0; |
| + |
| - // Tell the extension to update the icon using setIcon({imageData:...}). |
| ResultCatcher catcher; |
| - ui_test_utils::NavigateToURL(browser(), |
| - GURL(extension->GetResourceURL("update.html"))); |
| + |
| + // Tell the extension to update the icon using setIcon({imageData:...}). |
| + GetBrowserActionsBar().Press(0); |
| ASSERT_TRUE(catcher.GetNextResult()); |
| - // Test that we received the changes. |
| - EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); |
| - EXPECT_NE(prev_id, |
| - extension->browser_action()->GetIcon(0). |
| - ToSkBitmap()->getGenerationID()); |
| - prev_id = extension->browser_action()->GetIcon(0). |
| - ToSkBitmap()->getGenerationID(); |
| + action_icon = extension->browser_action()->GetIcon(0); |
| + |
| + action_icon_current_id = action_icon.AsBitmap().getGenerationID(); |
| + EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| + action_icon_last_id = action_icon_current_id; |
| + |
| + EXPECT_FALSE( |
| + action_icon.AsImageSkia().HasRepresentation(ui::SCALE_FACTOR_200P)); |
| + |
| + EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(action_icon.AsImageSkia()), |
| + GetBrowserActionsBar().GetIcon(0).AsImageSkia())); |
| // Tell the extension to update the icon using setIcon({path:...}). |
| - ui_test_utils::NavigateToURL(browser(), |
| - GURL(extension->GetResourceURL("update2.html"))); |
| + GetBrowserActionsBar().Press(0); |
| ASSERT_TRUE(catcher.GetNextResult()); |
| - EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); |
| - EXPECT_NE(prev_id, |
| - extension->browser_action()->GetIcon(0). |
| - ToSkBitmap()->getGenerationID()); |
| + |
| + action_icon = extension->browser_action()->GetIcon(0); |
| + |
| + action_icon_current_id = action_icon.AsBitmap().getGenerationID(); |
| + EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| + action_icon_last_id = action_icon_current_id; |
| + |
| + EXPECT_FALSE( |
| + action_icon.AsImageSkia().HasRepresentation(ui::SCALE_FACTOR_200P)); |
| + |
| + EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(action_icon.AsImageSkia()), |
| + GetBrowserActionsBar().GetIcon(0).AsImageSkia())); |
| + |
| + // Tell the extension to update the icon using setIcon({imageDataSet:...}). |
| + GetBrowserActionsBar().Press(0); |
| + ASSERT_TRUE(catcher.GetNextResult()); |
| + |
| + action_icon = extension->browser_action()->GetIcon(0); |
| + |
| + action_icon_current_id = action_icon.AsBitmap().getGenerationID(); |
| + EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| + action_icon_last_id = action_icon_current_id; |
| + |
| + EXPECT_TRUE( |
| + action_icon.AsImageSkia().HasRepresentation(ui::SCALE_FACTOR_200P)); |
| + |
| + EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(action_icon.AsImageSkia()), |
| + GetBrowserActionsBar().GetIcon(0).AsImageSkia())); |
| + |
| + // Tell the extension to update the icon using setIcon({pathSet:...}). |
| + GetBrowserActionsBar().Press(0); |
| + ASSERT_TRUE(catcher.GetNextResult()); |
| + |
| + action_icon = extension->browser_action()->GetIcon(0); |
| + |
| + action_icon_current_id = action_icon.AsBitmap().getGenerationID(); |
| + EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| + action_icon_last_id = action_icon_current_id; |
| + |
| + EXPECT_TRUE( |
| + action_icon.AsImageSkia().HasRepresentation(ui::SCALE_FACTOR_200P)); |
| + |
| + EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(action_icon.AsImageSkia()), |
| + GetBrowserActionsBar().GetIcon(0).AsImageSkia())); |
| + |
| + // Tell the extension to update the icon using setIcon({imageDataSet:...}), |
| + // but setting only size 19. |
| + GetBrowserActionsBar().Press(0); |
| + ASSERT_TRUE(catcher.GetNextResult()); |
| + |
| + action_icon = extension->browser_action()->GetIcon(0); |
| + |
| + action_icon_current_id = action_icon.AsBitmap().getGenerationID(); |
| + EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| + action_icon_last_id = action_icon_current_id; |
| + |
| + EXPECT_FALSE( |
| + action_icon.AsImageSkia().HasRepresentation(ui::SCALE_FACTOR_200P)); |
| + |
| + EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(action_icon.AsImageSkia()), |
| + GetBrowserActionsBar().GetIcon(0).AsImageSkia())); |
| + |
| + // Tell the extension to update the icon using setIcon({pathSet:...}), but |
| + // setting only size 19. |
| + GetBrowserActionsBar().Press(0); |
| + ASSERT_TRUE(catcher.GetNextResult()); |
| + |
| + action_icon = extension->browser_action()->GetIcon(0); |
| + |
| + action_icon_current_id = action_icon.AsBitmap().getGenerationID(); |
| + EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| + action_icon_last_id = action_icon_current_id; |
| + |
| + EXPECT_FALSE( |
| + action_icon.AsImageSkia().HasRepresentation(ui::SCALE_FACTOR_200P)); |
| + |
| + EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(action_icon.AsImageSkia()), |
| + GetBrowserActionsBar().GetIcon(0).AsImageSkia())); |
| } |
| // This test is flaky as per http://crbug.com/74557. |
| @@ -477,3 +581,5 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Getters) { |
| GURL(extension->GetResourceURL("update2.html"))); |
| ASSERT_TRUE(catcher.GetNextResult()); |
| } |
| + |
| +} // namespace |