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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #if defined(TOOLKIT_GTK) | 7 #if defined(TOOLKIT_GTK) |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include "chrome/browser/extensions/browser_action_test_util.h" | 11 #include "chrome/browser/extensions/browser_action_test_util.h" |
| 12 #include "chrome/browser/extensions/extension_apitest.h" | 12 #include "chrome/browser/extensions/extension_apitest.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/extension_tab_util.h" | 14 #include "chrome/browser/extensions/extension_tab_util.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_commands.h" | 17 #include "chrome/browser/ui/browser_commands.h" |
| 18 #include "chrome/browser/ui/browser_tabstrip.h" | 18 #include "chrome/browser/ui/browser_tabstrip.h" |
| 19 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
| 20 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
| 21 #include "chrome/common/extensions/extension_action.h" | 21 #include "chrome/common/extensions/extension_action.h" |
| 22 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
| 23 #include "chrome/test/base/ui_test_utils.h" | 23 #include "chrome/test/base/ui_test_utils.h" |
| 24 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 25 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 26 #include "content/public/test/browser_test_utils.h" | 26 #include "content/public/test/browser_test_utils.h" |
| 27 #include "grit/theme_resources.h" | |
| 28 #include "ui/base/resource/resource_bundle.h" | |
| 27 #include "ui/gfx/rect.h" | 29 #include "ui/gfx/rect.h" |
| 28 #include "ui/gfx/size.h" | 30 #include "ui/gfx/size.h" |
| 31 #include "ui/gfx/image/image_skia.h" | |
| 32 #include "ui/gfx/image/image_skia_operations.h" | |
| 33 #include "ui/gfx/skia_util.h" | |
| 29 | 34 |
| 30 using content::WebContents; | 35 using content::WebContents; |
| 31 using extensions::Extension; | 36 using extensions::Extension; |
| 32 | 37 |
| 38 namespace { | |
| 39 | |
| 40 const char kEmptyImageDataError[] = | |
| 41 "The imageData property must contain an ImageData object or dictionary " | |
|
Jeffrey Yasskin
2012/08/22 04:42:21
I would usually stick the expected error message d
| |
| 42 "of ImageData objects."; | |
| 43 const char kEmptyPathError[] = "The path property must not be empty."; | |
| 44 | |
| 45 // Views implementation of browser action button will return icon with set | |
| 46 // background. | |
| 47 gfx::ImageSkia AddBackgroundForViews(const gfx::ImageSkia& icon) { | |
| 48 #if defined(TOOLKIT_VIEWS) | |
| 49 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 50 gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION); | |
| 51 return gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon); | |
| 52 #endif | |
| 53 | |
| 54 return icon; | |
| 55 } | |
| 56 | |
| 57 bool ImagesAreEqual(const gfx::ImageSkia& i1, const gfx::ImageSkia& i2) { | |
| 58 SkBitmap bitmap1 = | |
| 59 i1.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap(); | |
| 60 SkBitmap bitmap2 = | |
| 61 i2.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap(); | |
| 62 return gfx::BitmapsAreEqual(bitmap1, bitmap2); | |
| 63 } | |
| 64 | |
| 33 class BrowserActionApiTest : public ExtensionApiTest { | 65 class BrowserActionApiTest : public ExtensionApiTest { |
| 34 public: | 66 public: |
| 35 BrowserActionApiTest() {} | 67 BrowserActionApiTest() {} |
| 36 virtual ~BrowserActionApiTest() {} | 68 virtual ~BrowserActionApiTest() {} |
| 37 | 69 |
| 38 protected: | 70 protected: |
| 39 BrowserActionTestUtil GetBrowserActionsBar() { | 71 BrowserActionTestUtil GetBrowserActionsBar() { |
| 40 return BrowserActionTestUtil(browser()); | 72 return BrowserActionTestUtil(browser()); |
| 41 } | 73 } |
| 42 | 74 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 | 127 |
| 96 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) { | 128 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) { |
| 97 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_; | 129 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_; |
| 98 const Extension* extension = GetSingleLoadedExtension(); | 130 const Extension* extension = GetSingleLoadedExtension(); |
| 99 ASSERT_TRUE(extension) << message_; | 131 ASSERT_TRUE(extension) << message_; |
| 100 | 132 |
| 101 // Test that there is a browser action in the toolbar. | 133 // Test that there is a browser action in the toolbar. |
| 102 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); | 134 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); |
| 103 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); | 135 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); |
| 104 | 136 |
| 105 // Set prev_id which holds the id of the previous image, and use it in the | 137 gfx::Image action_icon = extension->browser_action()->GetIcon(0); |
| 106 // next test to see if the image changes. | 138 uint32_t action_icon_last_id = action_icon.ToSkBitmap()->getGenerationID(); |
| 107 uint32_t prev_id = extension->browser_action()->GetIcon(0). | |
| 108 ToSkBitmap()->getGenerationID(); | |
| 109 | 139 |
| 110 // Tell the extension to update the icon using setIcon({imageData:...}). | 140 // Let's check that |GetIcon| doesn't always return bitmap wiht new id. |
|
Jeffrey Yasskin
2012/08/22 04:42:21
s/wiht/with/
tbarzic
2012/08/22 18:43:17
Done.
| |
| 141 ASSERT_EQ(action_icon_last_id, | |
| 142 extension->browser_action()->GetIcon(0).ToSkBitmap()-> | |
| 143 getGenerationID()); | |
| 144 | |
| 145 uint32_t action_icon_current_id = 0; | |
| 146 | |
| 147 | |
| 111 ResultCatcher catcher; | 148 ResultCatcher catcher; |
| 112 ui_test_utils::NavigateToURL(browser(), | 149 |
| 113 GURL(extension->GetResourceURL("update.html"))); | 150 // Tell the extension to update the icon using ImageData object. |
| 151 GetBrowserActionsBar().Press(0); | |
| 114 ASSERT_TRUE(catcher.GetNextResult()); | 152 ASSERT_TRUE(catcher.GetNextResult()); |
| 115 | 153 |
| 116 // Test that we received the changes. | 154 action_icon = extension->browser_action()->GetIcon(0); |
| 117 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); | |
| 118 EXPECT_NE(prev_id, | |
| 119 extension->browser_action()->GetIcon(0). | |
| 120 ToSkBitmap()->getGenerationID()); | |
| 121 prev_id = extension->browser_action()->GetIcon(0). | |
| 122 ToSkBitmap()->getGenerationID(); | |
| 123 | 155 |
| 124 // Tell the extension to update the icon using setIcon({path:...}). | 156 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
| 125 ui_test_utils::NavigateToURL(browser(), | 157 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| 126 GURL(extension->GetResourceURL("update2.html"))); | 158 action_icon_last_id = action_icon_current_id; |
| 159 | |
| 160 EXPECT_FALSE( | |
| 161 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | |
| 162 | |
| 163 EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(*action_icon.ToImageSkia()), | |
| 164 *GetBrowserActionsBar().GetIcon(0).ToImageSkia())); | |
| 165 | |
| 166 // Tell the extension to update the icon using path. | |
| 167 GetBrowserActionsBar().Press(0); | |
| 127 ASSERT_TRUE(catcher.GetNextResult()); | 168 ASSERT_TRUE(catcher.GetNextResult()); |
| 128 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); | 169 |
| 129 EXPECT_NE(prev_id, | 170 action_icon = extension->browser_action()->GetIcon(0); |
| 130 extension->browser_action()->GetIcon(0). | 171 |
| 131 ToSkBitmap()->getGenerationID()); | 172 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
| 173 EXPECT_GT(action_icon_current_id, action_icon_last_id); | |
| 174 action_icon_last_id = action_icon_current_id; | |
| 175 | |
| 176 EXPECT_FALSE( | |
| 177 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | |
| 178 | |
| 179 EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(*action_icon.ToImageSkia()), | |
| 180 *GetBrowserActionsBar().GetIcon(0).ToImageSkia())); | |
| 181 | |
| 182 // Tell the extension to update the icon using dictionary of ImageData | |
| 183 // objects. | |
| 184 GetBrowserActionsBar().Press(0); | |
| 185 ASSERT_TRUE(catcher.GetNextResult()); | |
| 186 | |
| 187 action_icon = extension->browser_action()->GetIcon(0); | |
| 188 | |
| 189 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | |
| 190 EXPECT_GT(action_icon_current_id, action_icon_last_id); | |
| 191 action_icon_last_id = action_icon_current_id; | |
| 192 | |
| 193 EXPECT_TRUE( | |
| 194 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | |
| 195 | |
| 196 EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(*action_icon.ToImageSkia()), | |
| 197 *GetBrowserActionsBar().GetIcon(0).ToImageSkia())); | |
| 198 | |
| 199 // Tell the extension to update the icon using dictionary of paths. | |
| 200 GetBrowserActionsBar().Press(0); | |
| 201 ASSERT_TRUE(catcher.GetNextResult()); | |
| 202 | |
| 203 action_icon = extension->browser_action()->GetIcon(0); | |
| 204 | |
| 205 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | |
| 206 EXPECT_GT(action_icon_current_id, action_icon_last_id); | |
| 207 action_icon_last_id = action_icon_current_id; | |
| 208 | |
| 209 EXPECT_TRUE( | |
| 210 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | |
| 211 | |
| 212 EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(*action_icon.ToImageSkia()), | |
| 213 *GetBrowserActionsBar().GetIcon(0).ToImageSkia())); | |
| 214 | |
| 215 // Tell the extension to update the icon using dictionary of ImageData | |
| 216 // objects, but setting only size 19. | |
| 217 GetBrowserActionsBar().Press(0); | |
| 218 ASSERT_TRUE(catcher.GetNextResult()); | |
| 219 | |
| 220 action_icon = extension->browser_action()->GetIcon(0); | |
| 221 | |
| 222 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | |
| 223 EXPECT_GT(action_icon_current_id, action_icon_last_id); | |
| 224 action_icon_last_id = action_icon_current_id; | |
| 225 | |
| 226 EXPECT_FALSE( | |
| 227 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | |
| 228 | |
| 229 EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(*action_icon.ToImageSkia()), | |
| 230 *GetBrowserActionsBar().GetIcon(0).ToImageSkia())); | |
| 231 | |
| 232 // Tell the extension to update the icon using dictionary of paths, but | |
| 233 // setting only size 19. | |
| 234 GetBrowserActionsBar().Press(0); | |
| 235 ASSERT_TRUE(catcher.GetNextResult()); | |
| 236 | |
| 237 action_icon = extension->browser_action()->GetIcon(0); | |
| 238 | |
| 239 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | |
| 240 EXPECT_GT(action_icon_current_id, action_icon_last_id); | |
| 241 action_icon_last_id = action_icon_current_id; | |
| 242 | |
| 243 EXPECT_FALSE( | |
| 244 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | |
| 245 | |
| 246 EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(*action_icon.ToImageSkia()), | |
| 247 *GetBrowserActionsBar().GetIcon(0).ToImageSkia())); | |
| 248 | |
| 249 // Tell the extension to update the icon using dictionary of ImageData | |
| 250 // objects, but setting only size 38. | |
| 251 GetBrowserActionsBar().Press(0); | |
| 252 ASSERT_TRUE(catcher.GetNextResult()); | |
| 253 | |
| 254 action_icon = extension->browser_action()->GetIcon(0); | |
| 255 | |
| 256 const gfx::ImageSkia* action_icon_skia = action_icon.ToImageSkia(); | |
| 257 | |
| 258 EXPECT_FALSE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_100P)); | |
| 259 EXPECT_TRUE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_200P)); | |
| 260 | |
| 261 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | |
| 262 EXPECT_GT(action_icon_current_id, action_icon_last_id); | |
| 263 action_icon_last_id = action_icon_current_id; | |
| 264 | |
| 265 EXPECT_TRUE(gfx::BitmapsAreEqual( | |
| 266 *action_icon.ToSkBitmap(), | |
| 267 action_icon_skia->GetRepresentation(ui::SCALE_FACTOR_200P).sk_bitmap())); | |
| 268 | |
| 269 EXPECT_TRUE(ImagesAreEqual(AddBackgroundForViews(*action_icon.ToImageSkia()), | |
| 270 *GetBrowserActionsBar().GetIcon(0).ToImageSkia())); | |
| 271 | |
| 272 // Try setting icon with empty dictionary of ImageData objects. | |
| 273 GetBrowserActionsBar().Press(0); | |
| 274 ASSERT_FALSE(catcher.GetNextResult()); | |
| 275 EXPECT_EQ(kEmptyImageDataError, catcher.message()); | |
| 276 | |
| 277 // Try setting icon with empty dictionary of path objects. | |
| 278 GetBrowserActionsBar().Press(0); | |
| 279 ASSERT_FALSE(catcher.GetNextResult()); | |
| 280 EXPECT_EQ(kEmptyPathError, catcher.message()); | |
| 132 } | 281 } |
| 133 | 282 |
| 134 // This test is flaky as per http://crbug.com/74557. | 283 // This test is flaky as per http://crbug.com/74557. |
| 135 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, | 284 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, |
| 136 DISABLED_TabSpecificBrowserActionState) { | 285 DISABLED_TabSpecificBrowserActionState) { |
| 137 ASSERT_TRUE(RunExtensionTest("browser_action/tab_specific_state")) << | 286 ASSERT_TRUE(RunExtensionTest("browser_action/tab_specific_state")) << |
| 138 message_; | 287 message_; |
| 139 const Extension* extension = GetSingleLoadedExtension(); | 288 const Extension* extension = GetSingleLoadedExtension(); |
| 140 ASSERT_TRUE(extension) << message_; | 289 ASSERT_TRUE(extension) << message_; |
| 141 | 290 |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 470 ResultCatcher catcher; | 619 ResultCatcher catcher; |
| 471 ui_test_utils::NavigateToURL(browser(), | 620 ui_test_utils::NavigateToURL(browser(), |
| 472 GURL(extension->GetResourceURL("update.html"))); | 621 GURL(extension->GetResourceURL("update.html"))); |
| 473 ASSERT_TRUE(catcher.GetNextResult()); | 622 ASSERT_TRUE(catcher.GetNextResult()); |
| 474 | 623 |
| 475 // Test the getters for a specific tab. | 624 // Test the getters for a specific tab. |
| 476 ui_test_utils::NavigateToURL(browser(), | 625 ui_test_utils::NavigateToURL(browser(), |
| 477 GURL(extension->GetResourceURL("update2.html"))); | 626 GURL(extension->GetResourceURL("update2.html"))); |
| 478 ASSERT_TRUE(catcher.GetNextResult()); | 627 ASSERT_TRUE(catcher.GetNextResult()); |
| 479 } | 628 } |
| 629 | |
| 630 } // namespace | |
| OLD | NEW |