| 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/extensions/extension_icon_factory_delegate.h" |
| 22 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 23 #include "chrome/test/base/ui_test_utils.h" | 24 #include "chrome/test/base/ui_test_utils.h" |
| 24 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
| 25 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 26 #include "content/public/test/browser_test_utils.h" | 27 #include "content/public/test/browser_test_utils.h" |
| 27 #include "grit/theme_resources.h" | 28 #include "grit/theme_resources.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" |
| 28 #include "ui/base/resource/resource_bundle.h" | 30 #include "ui/base/resource/resource_bundle.h" |
| 29 #include "ui/gfx/rect.h" | 31 #include "ui/gfx/rect.h" |
| 30 #include "ui/gfx/size.h" | 32 #include "ui/gfx/size.h" |
| 31 #include "ui/gfx/image/image_skia.h" | 33 #include "ui/gfx/image/image_skia.h" |
| 32 #include "ui/gfx/image/image_skia_operations.h" | 34 #include "ui/gfx/image/image_skia_operations.h" |
| 33 #include "ui/gfx/skia_util.h" | 35 #include "ui/gfx/skia_util.h" |
| 34 | 36 |
| 35 #if defined (OS_MACOSX) | 37 #if defined (OS_MACOSX) |
| 36 #include "ui/gfx/image/image_unittest_util.h" | 38 #include "ui/gfx/image/image_unittest_util.h" |
| 37 #endif | 39 #endif |
| 38 | 40 |
| 41 using content::BrowserThread; |
| 39 using content::WebContents; | 42 using content::WebContents; |
| 40 using extensions::Extension; | 43 using extensions::Extension; |
| 44 using ::testing::_; |
| 41 | 45 |
| 42 namespace { | 46 namespace { |
| 43 | 47 |
| 48 class MockActionIconFactory : public ExtensionIconFactoryDelegate { |
| 49 public: |
| 50 virtual ~MockActionIconFactory() {} |
| 51 |
| 52 MOCK_METHOD2(GetIcon, gfx::ImageSkia(const ExtensionIconSet* icon_set, |
| 53 int desired_size)); |
| 54 }; |
| 55 |
| 56 } // namespace |
| 57 |
| 58 namespace { |
| 59 |
| 44 const char kEmptyImageDataError[] = | 60 const char kEmptyImageDataError[] = |
| 45 "The imageData property must contain an ImageData object or dictionary " | 61 "The imageData property must contain an ImageData object or dictionary " |
| 46 "of ImageData objects."; | 62 "of ImageData objects."; |
| 47 const char kEmptyPathError[] = "The path property must not be empty."; | 63 const char kEmptyPathError[] = "The path property must not be empty."; |
| 48 | 64 |
| 49 // Views implementation of browser action button will return icon whose | 65 // Views implementation of browser action button will return icon whose |
| 50 // background will be set. | 66 // background will be set. |
| 51 gfx::ImageSkia AddBackgroundForViews(const gfx::ImageSkia& icon) { | 67 gfx::ImageSkia AddBackgroundForViews(const gfx::ImageSkia& icon) { |
| 52 #if defined(TOOLKIT_VIEWS) | 68 #if defined(TOOLKIT_VIEWS) |
| 53 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 69 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_; | 149 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_; |
| 134 const Extension* extension = GetSingleLoadedExtension(); | 150 const Extension* extension = GetSingleLoadedExtension(); |
| 135 ASSERT_TRUE(extension) << message_; | 151 ASSERT_TRUE(extension) << message_; |
| 136 | 152 |
| 137 #if defined (OS_MACOSX) | 153 #if defined (OS_MACOSX) |
| 138 // We need this on mac so we don't loose 2x representations from browser icon | 154 // We need this on mac so we don't loose 2x representations from browser icon |
| 139 // in transformations gfx::ImageSkia -> NSImage -> gfx::ImageSkia. | 155 // in transformations gfx::ImageSkia -> NSImage -> gfx::ImageSkia. |
| 140 gfx::test::SetSupportedScaleFactorsTo1xAnd2x(); | 156 gfx::test::SetSupportedScaleFactorsTo1xAnd2x(); |
| 141 #endif | 157 #endif |
| 142 | 158 |
| 159 MockActionIconFactory mock_icon_factory; |
| 160 EXPECT_CALL(mock_icon_factory, GetIcon(_, _)).Times(0); |
| 161 |
| 143 // Test that there is a browser action in the toolbar. | 162 // Test that there is a browser action in the toolbar. |
| 144 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); | 163 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); |
| 145 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); | 164 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); |
| 146 | 165 |
| 147 gfx::Image action_icon = extension->browser_action()->GetIcon(0); | 166 gfx::Image action_icon = |
| 167 extension->browser_action()->GetIcon(0, &mock_icon_factory); |
| 148 uint32_t action_icon_last_id = action_icon.ToSkBitmap()->getGenerationID(); | 168 uint32_t action_icon_last_id = action_icon.ToSkBitmap()->getGenerationID(); |
| 149 | 169 |
| 150 // Let's check that |GetIcon| doesn't always return bitmap with new id. | 170 // Let's check that |GetIcon| doesn't always return bitmap with new id. |
| 151 ASSERT_EQ(action_icon_last_id, | 171 ASSERT_EQ(action_icon_last_id, |
| 152 extension->browser_action()->GetIcon(0).ToSkBitmap()-> | 172 extension->browser_action()->GetIcon(0, &mock_icon_factory). |
| 153 getGenerationID()); | 173 ToSkBitmap()->getGenerationID()); |
| 154 | 174 |
| 155 uint32_t action_icon_current_id = 0; | 175 uint32_t action_icon_current_id = 0; |
| 156 | 176 |
| 157 ResultCatcher catcher; | 177 ResultCatcher catcher; |
| 158 | 178 |
| 159 // Tell the extension to update the icon using ImageData object. | 179 // Tell the extension to update the icon using ImageData object. |
| 160 GetBrowserActionsBar().Press(0); | 180 GetBrowserActionsBar().Press(0); |
| 161 ASSERT_TRUE(catcher.GetNextResult()); | 181 ASSERT_TRUE(catcher.GetNextResult()); |
| 162 | 182 |
| 163 action_icon = extension->browser_action()->GetIcon(0); | 183 action_icon = extension->browser_action()->GetIcon(0, &mock_icon_factory); |
| 164 | 184 |
| 165 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 185 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
| 166 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 186 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| 167 action_icon_last_id = action_icon_current_id; | 187 action_icon_last_id = action_icon_current_id; |
| 168 | 188 |
| 169 EXPECT_FALSE( | 189 EXPECT_FALSE( |
| 170 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 190 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
| 171 | 191 |
| 172 EXPECT_TRUE(ImagesAreEqualAtScale( | 192 EXPECT_TRUE(ImagesAreEqualAtScale( |
| 173 AddBackgroundForViews(*action_icon.ToImageSkia()), | 193 AddBackgroundForViews(*action_icon.ToImageSkia()), |
| 174 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 194 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
| 175 ui::SCALE_FACTOR_100P)); | 195 ui::SCALE_FACTOR_100P)); |
| 176 | 196 |
| 177 // Tell the extension to update the icon using path. | 197 // Tell the extension to update the icon using path. |
| 178 GetBrowserActionsBar().Press(0); | 198 GetBrowserActionsBar().Press(0); |
| 179 ASSERT_TRUE(catcher.GetNextResult()); | 199 ASSERT_TRUE(catcher.GetNextResult()); |
| 180 | 200 |
| 181 action_icon = extension->browser_action()->GetIcon(0); | 201 action_icon = extension->browser_action()->GetIcon(0, &mock_icon_factory); |
| 182 | 202 |
| 183 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 203 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
| 184 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 204 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| 185 action_icon_last_id = action_icon_current_id; | 205 action_icon_last_id = action_icon_current_id; |
| 186 | 206 |
| 187 EXPECT_FALSE( | 207 EXPECT_FALSE( |
| 188 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 208 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
| 189 | 209 |
| 190 EXPECT_TRUE(ImagesAreEqualAtScale( | 210 EXPECT_TRUE(ImagesAreEqualAtScale( |
| 191 AddBackgroundForViews(*action_icon.ToImageSkia()), | 211 AddBackgroundForViews(*action_icon.ToImageSkia()), |
| 192 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 212 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
| 193 ui::SCALE_FACTOR_100P)); | 213 ui::SCALE_FACTOR_100P)); |
| 194 | 214 |
| 195 // Tell the extension to update the icon using dictionary of ImageData | 215 // Tell the extension to update the icon using dictionary of ImageData |
| 196 // objects. | 216 // objects. |
| 197 GetBrowserActionsBar().Press(0); | 217 GetBrowserActionsBar().Press(0); |
| 198 ASSERT_TRUE(catcher.GetNextResult()); | 218 ASSERT_TRUE(catcher.GetNextResult()); |
| 199 | 219 |
| 200 action_icon = extension->browser_action()->GetIcon(0); | 220 action_icon = extension->browser_action()->GetIcon(0, &mock_icon_factory); |
| 201 | 221 |
| 202 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 222 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
| 203 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 223 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| 204 action_icon_last_id = action_icon_current_id; | 224 action_icon_last_id = action_icon_current_id; |
| 205 | 225 |
| 206 EXPECT_TRUE( | 226 EXPECT_TRUE( |
| 207 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 227 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
| 208 | 228 |
| 209 EXPECT_TRUE(ImagesAreEqualAtScale( | 229 EXPECT_TRUE(ImagesAreEqualAtScale( |
| 210 AddBackgroundForViews(*action_icon.ToImageSkia()), | 230 AddBackgroundForViews(*action_icon.ToImageSkia()), |
| 211 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 231 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
| 212 ui::SCALE_FACTOR_100P)); | 232 ui::SCALE_FACTOR_100P)); |
| 213 | 233 |
| 214 // Tell the extension to update the icon using dictionary of paths. | 234 // Tell the extension to update the icon using dictionary of paths. |
| 215 GetBrowserActionsBar().Press(0); | 235 GetBrowserActionsBar().Press(0); |
| 216 ASSERT_TRUE(catcher.GetNextResult()); | 236 ASSERT_TRUE(catcher.GetNextResult()); |
| 217 | 237 |
| 218 action_icon = extension->browser_action()->GetIcon(0); | 238 action_icon = extension->browser_action()->GetIcon(0, &mock_icon_factory); |
| 219 | 239 |
| 220 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 240 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
| 221 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 241 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| 222 action_icon_last_id = action_icon_current_id; | 242 action_icon_last_id = action_icon_current_id; |
| 223 | 243 |
| 224 EXPECT_TRUE( | 244 EXPECT_TRUE( |
| 225 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 245 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
| 226 | 246 |
| 227 EXPECT_TRUE(ImagesAreEqualAtScale( | 247 EXPECT_TRUE(ImagesAreEqualAtScale( |
| 228 AddBackgroundForViews(*action_icon.ToImageSkia()), | 248 AddBackgroundForViews(*action_icon.ToImageSkia()), |
| 229 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 249 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
| 230 ui::SCALE_FACTOR_100P)); | 250 ui::SCALE_FACTOR_100P)); |
| 231 | 251 |
| 232 // Tell the extension to update the icon using dictionary of ImageData | 252 // Tell the extension to update the icon using dictionary of ImageData |
| 233 // objects, but setting only size 19. | 253 // objects, but setting only size 19. |
| 234 GetBrowserActionsBar().Press(0); | 254 GetBrowserActionsBar().Press(0); |
| 235 ASSERT_TRUE(catcher.GetNextResult()); | 255 ASSERT_TRUE(catcher.GetNextResult()); |
| 236 | 256 |
| 237 action_icon = extension->browser_action()->GetIcon(0); | 257 action_icon = extension->browser_action()->GetIcon(0, &mock_icon_factory); |
| 238 | 258 |
| 239 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 259 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
| 240 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 260 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| 241 action_icon_last_id = action_icon_current_id; | 261 action_icon_last_id = action_icon_current_id; |
| 242 | 262 |
| 243 EXPECT_FALSE( | 263 EXPECT_FALSE( |
| 244 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 264 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
| 245 | 265 |
| 246 EXPECT_TRUE(ImagesAreEqualAtScale( | 266 EXPECT_TRUE(ImagesAreEqualAtScale( |
| 247 AddBackgroundForViews(*action_icon.ToImageSkia()), | 267 AddBackgroundForViews(*action_icon.ToImageSkia()), |
| 248 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 268 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
| 249 ui::SCALE_FACTOR_100P)); | 269 ui::SCALE_FACTOR_100P)); |
| 250 | 270 |
| 251 // Tell the extension to update the icon using dictionary of paths, but | 271 // Tell the extension to update the icon using dictionary of paths, but |
| 252 // setting only size 19. | 272 // setting only size 19. |
| 253 GetBrowserActionsBar().Press(0); | 273 GetBrowserActionsBar().Press(0); |
| 254 ASSERT_TRUE(catcher.GetNextResult()); | 274 ASSERT_TRUE(catcher.GetNextResult()); |
| 255 | 275 |
| 256 action_icon = extension->browser_action()->GetIcon(0); | 276 action_icon = extension->browser_action()->GetIcon(0, &mock_icon_factory); |
| 257 | 277 |
| 258 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 278 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
| 259 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 279 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| 260 action_icon_last_id = action_icon_current_id; | 280 action_icon_last_id = action_icon_current_id; |
| 261 | 281 |
| 262 EXPECT_FALSE( | 282 EXPECT_FALSE( |
| 263 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 283 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
| 264 | 284 |
| 265 EXPECT_TRUE(ImagesAreEqualAtScale( | 285 EXPECT_TRUE(ImagesAreEqualAtScale( |
| 266 AddBackgroundForViews(*action_icon.ToImageSkia()), | 286 AddBackgroundForViews(*action_icon.ToImageSkia()), |
| 267 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 287 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
| 268 ui::SCALE_FACTOR_100P)); | 288 ui::SCALE_FACTOR_100P)); |
| 269 | 289 |
| 270 // Tell the extension to update the icon using dictionary of ImageData | 290 // Tell the extension to update the icon using dictionary of ImageData |
| 271 // objects, but setting only size 38. | 291 // objects, but setting only size 38. |
| 272 GetBrowserActionsBar().Press(0); | 292 GetBrowserActionsBar().Press(0); |
| 273 ASSERT_TRUE(catcher.GetNextResult()); | 293 ASSERT_TRUE(catcher.GetNextResult()); |
| 274 | 294 |
| 275 action_icon = extension->browser_action()->GetIcon(0); | 295 action_icon = extension->browser_action()->GetIcon(0, &mock_icon_factory); |
| 276 | 296 |
| 277 const gfx::ImageSkia* action_icon_skia = action_icon.ToImageSkia(); | 297 const gfx::ImageSkia* action_icon_skia = action_icon.ToImageSkia(); |
| 278 | 298 |
| 279 EXPECT_FALSE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_100P)); | 299 EXPECT_FALSE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_100P)); |
| 280 EXPECT_TRUE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_200P)); | 300 EXPECT_TRUE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_200P)); |
| 281 | 301 |
| 282 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 302 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
| 283 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 303 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
| 284 action_icon_last_id = action_icon_current_id; | 304 action_icon_last_id = action_icon_current_id; |
| 285 | 305 |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 644 GURL(extension->GetResourceURL("update.html"))); | 664 GURL(extension->GetResourceURL("update.html"))); |
| 645 ASSERT_TRUE(catcher.GetNextResult()); | 665 ASSERT_TRUE(catcher.GetNextResult()); |
| 646 | 666 |
| 647 // Test the getters for a specific tab. | 667 // Test the getters for a specific tab. |
| 648 ui_test_utils::NavigateToURL(browser(), | 668 ui_test_utils::NavigateToURL(browser(), |
| 649 GURL(extension->GetResourceURL("update2.html"))); | 669 GURL(extension->GetResourceURL("update2.html"))); |
| 650 ASSERT_TRUE(catcher.GetNextResult()); | 670 ASSERT_TRUE(catcher.GetNextResult()); |
| 651 } | 671 } |
| 652 | 672 |
| 653 } // namespace | 673 } // namespace |
| OLD | NEW |