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
3 // found in the LICENSE file.
5 #include "build/build_config.h"
6 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
7 #include "chrome/browser/extensions/browser_action_test_util.h"
8 #include "chrome/browser/extensions/extension_action.h"
9 #include "chrome/browser/extensions/extension_action_icon_factory.h"
10 #include "chrome/browser/extensions/extension_action_manager.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "chrome/browser/extensions/extension_tab_util.h"
13 #include "chrome/browser/extensions/extension_toolbar_model.h"
14 #include "chrome/browser/extensions/extension_util.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_commands.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/test/base/ui_test_utils.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "extensions/browser/extension_registry.h"
26 #include "extensions/browser/extension_system.h"
27 #include "extensions/browser/notification_types.h"
28 #include "extensions/browser/process_manager.h"
29 #include "extensions/browser/test_extension_registry_observer.h"
30 #include "extensions/common/feature_switch.h"
31 #include "extensions/test/result_catcher.h"
32 #include "grit/theme_resources.h"
33 #include "ui/base/resource/resource_bundle.h"
34 #include "ui/gfx/geometry/rect.h"
35 #include "ui/gfx/geometry/size.h"
36 #include "ui/gfx/image/image_skia.h"
37 #include "ui/gfx/image/image_skia_operations.h"
38 #include "ui/gfx/skia_util.h"
40 using content::WebContents
;
42 namespace extensions
{
45 const char kEmptyImageDataError
[] =
46 "The imageData property must contain an ImageData object or dictionary "
47 "of ImageData objects.";
48 const char kEmptyPathError
[] = "The path property must not be empty.";
50 // Views implementation of browser action button will return icon whose
51 // background will be set.
52 gfx::ImageSkia
AddBackgroundForViews(const gfx::ImageSkia
& icon
) {
53 #if !defined(OS_MACOSX)
54 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
55 gfx::ImageSkia bg
= *rb
.GetImageSkiaNamed(IDR_BROWSER_ACTION
);
56 return gfx::ImageSkiaOperations::CreateSuperimposedImage(bg
, icon
);
62 bool ImagesAreEqualAtScale(const gfx::ImageSkia
& i1
,
63 const gfx::ImageSkia
& i2
,
65 SkBitmap bitmap1
= i1
.GetRepresentation(scale
).sk_bitmap();
66 SkBitmap bitmap2
= i2
.GetRepresentation(scale
).sk_bitmap();
67 return gfx::BitmapsAreEqual(bitmap1
, bitmap2
);
70 class BrowserActionApiTest
: public ExtensionApiTest
{
72 BrowserActionApiTest() {}
73 ~BrowserActionApiTest() override
{}
76 BrowserActionTestUtil
GetBrowserActionsBar() {
77 return BrowserActionTestUtil(browser());
80 bool OpenPopup(int index
) {
81 ResultCatcher catcher
;
82 content::WindowedNotificationObserver
popup_observer(
83 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
84 content::NotificationService::AllSources());
85 GetBrowserActionsBar().Press(index
);
86 popup_observer
.Wait();
87 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
88 return GetBrowserActionsBar().HasPopup();
91 ExtensionAction
* GetBrowserAction(const Extension
& extension
) {
92 return ExtensionActionManager::Get(browser()->profile())->
93 GetBrowserAction(extension
);
97 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, Basic
) {
98 ASSERT_TRUE(test_server()->Start());
99 ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_
;
100 const Extension
* extension
= GetSingleLoadedExtension();
101 ASSERT_TRUE(extension
) << message_
;
103 // Test that there is a browser action in the toolbar.
104 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
106 // Tell the extension to update the browser action state.
107 ResultCatcher catcher
;
108 ui_test_utils::NavigateToURL(browser(),
109 GURL(extension
->GetResourceURL("update.html")));
110 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
112 // Test that we received the changes.
113 ExtensionAction
* action
= GetBrowserAction(*extension
);
114 ASSERT_EQ("Modified", action
->GetTitle(ExtensionAction::kDefaultTabId
));
115 ASSERT_EQ("badge", action
->GetBadgeText(ExtensionAction::kDefaultTabId
));
116 ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255),
117 action
->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId
));
119 // Simulate the browser action being clicked.
120 ui_test_utils::NavigateToURL(browser(),
121 test_server()->GetURL("files/extensions/test_file.txt"));
123 ExtensionActionAPI::Get(browser()->profile())->ExecuteExtensionAction(
124 extension
, browser(), true);
126 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
129 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, DynamicBrowserAction
) {
130 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_
;
131 const Extension
* extension
= GetSingleLoadedExtension();
132 ASSERT_TRUE(extension
) << message_
;
134 #if defined (OS_MACOSX)
135 // We need this on mac so we don't loose 2x representations from browser icon
136 // in transformations gfx::ImageSkia -> NSImage -> gfx::ImageSkia.
137 std::vector
<ui::ScaleFactor
> supported_scale_factors
;
138 supported_scale_factors
.push_back(ui::SCALE_FACTOR_100P
);
139 supported_scale_factors
.push_back(ui::SCALE_FACTOR_200P
);
140 ui::SetSupportedScaleFactors(supported_scale_factors
);
143 // We should not be creating icons asynchronously, so we don't need an
145 ExtensionActionIconFactory
icon_factory(
148 GetBrowserAction(*extension
),
150 // Test that there is a browser action in the toolbar.
151 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
152 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0));
154 gfx::Image action_icon
= icon_factory
.GetIcon(0);
155 uint32_t action_icon_last_id
= action_icon
.ToSkBitmap()->getGenerationID();
157 // Let's check that |GetIcon| doesn't always return bitmap with new id.
158 ASSERT_EQ(action_icon_last_id
,
159 icon_factory
.GetIcon(0).ToSkBitmap()->getGenerationID());
161 uint32_t action_icon_current_id
= 0;
163 ResultCatcher catcher
;
165 // Tell the extension to update the icon using ImageData object.
166 GetBrowserActionsBar().Press(0);
167 ASSERT_TRUE(catcher
.GetNextResult());
169 action_icon
= icon_factory
.GetIcon(0);
171 action_icon_current_id
= action_icon
.ToSkBitmap()->getGenerationID();
172 EXPECT_GT(action_icon_current_id
, action_icon_last_id
);
173 action_icon_last_id
= action_icon_current_id
;
175 EXPECT_FALSE(action_icon
.ToImageSkia()->HasRepresentation(2.0f
));
178 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon
.ToImageSkia()),
179 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
182 // Tell the extension to update the icon using path.
183 GetBrowserActionsBar().Press(0);
184 ASSERT_TRUE(catcher
.GetNextResult());
186 action_icon
= icon_factory
.GetIcon(0);
188 action_icon_current_id
= action_icon
.ToSkBitmap()->getGenerationID();
189 EXPECT_GT(action_icon_current_id
, action_icon_last_id
);
190 action_icon_last_id
= action_icon_current_id
;
193 action_icon
.ToImageSkia()->HasRepresentation(2.0f
));
196 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon
.ToImageSkia()),
197 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
200 // Tell the extension to update the icon using dictionary of ImageData
202 GetBrowserActionsBar().Press(0);
203 ASSERT_TRUE(catcher
.GetNextResult());
205 action_icon
= icon_factory
.GetIcon(0);
207 action_icon_current_id
= action_icon
.ToSkBitmap()->getGenerationID();
208 EXPECT_GT(action_icon_current_id
, action_icon_last_id
);
209 action_icon_last_id
= action_icon_current_id
;
211 EXPECT_TRUE(action_icon
.ToImageSkia()->HasRepresentation(2.0f
));
214 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon
.ToImageSkia()),
215 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
218 // Tell the extension to update the icon using dictionary of paths.
219 GetBrowserActionsBar().Press(0);
220 ASSERT_TRUE(catcher
.GetNextResult());
222 action_icon
= icon_factory
.GetIcon(0);
224 action_icon_current_id
= action_icon
.ToSkBitmap()->getGenerationID();
225 EXPECT_GT(action_icon_current_id
, action_icon_last_id
);
226 action_icon_last_id
= action_icon_current_id
;
228 EXPECT_TRUE(action_icon
.ToImageSkia()->HasRepresentation(2.0f
));
231 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon
.ToImageSkia()),
232 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
235 // Tell the extension to update the icon using dictionary of ImageData
236 // objects, but setting only size 19.
237 GetBrowserActionsBar().Press(0);
238 ASSERT_TRUE(catcher
.GetNextResult());
240 action_icon
= icon_factory
.GetIcon(0);
242 action_icon_current_id
= action_icon
.ToSkBitmap()->getGenerationID();
243 EXPECT_GT(action_icon_current_id
, action_icon_last_id
);
244 action_icon_last_id
= action_icon_current_id
;
246 EXPECT_FALSE(action_icon
.ToImageSkia()->HasRepresentation(2.0f
));
249 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon
.ToImageSkia()),
250 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
253 // Tell the extension to update the icon using dictionary of paths, but
254 // setting only size 19.
255 GetBrowserActionsBar().Press(0);
256 ASSERT_TRUE(catcher
.GetNextResult());
258 action_icon
= icon_factory
.GetIcon(0);
260 action_icon_current_id
= action_icon
.ToSkBitmap()->getGenerationID();
261 EXPECT_GT(action_icon_current_id
, action_icon_last_id
);
262 action_icon_last_id
= action_icon_current_id
;
264 EXPECT_FALSE(action_icon
.ToImageSkia()->HasRepresentation(2.0f
));
267 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon
.ToImageSkia()),
268 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
271 // Tell the extension to update the icon using dictionary of ImageData
272 // objects, but setting only size 38.
273 GetBrowserActionsBar().Press(0);
274 ASSERT_TRUE(catcher
.GetNextResult());
276 action_icon
= icon_factory
.GetIcon(0);
278 const gfx::ImageSkia
* action_icon_skia
= action_icon
.ToImageSkia();
280 EXPECT_FALSE(action_icon_skia
->HasRepresentation(1.0f
));
281 EXPECT_TRUE(action_icon_skia
->HasRepresentation(2.0f
));
283 action_icon_current_id
= action_icon
.ToSkBitmap()->getGenerationID();
284 EXPECT_GT(action_icon_current_id
, action_icon_last_id
);
285 action_icon_last_id
= action_icon_current_id
;
287 EXPECT_TRUE(gfx::BitmapsAreEqual(
288 *action_icon
.ToSkBitmap(),
289 action_icon_skia
->GetRepresentation(2.0f
).sk_bitmap()));
292 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon_skia
),
293 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
296 // Try setting icon with empty dictionary of ImageData objects.
297 GetBrowserActionsBar().Press(0);
298 ASSERT_FALSE(catcher
.GetNextResult());
299 EXPECT_EQ(kEmptyImageDataError
, catcher
.message());
301 // Try setting icon with empty dictionary of path objects.
302 GetBrowserActionsBar().Press(0);
303 ASSERT_FALSE(catcher
.GetNextResult());
304 EXPECT_EQ(kEmptyPathError
, catcher
.message());
307 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, TabSpecificBrowserActionState
) {
308 ASSERT_TRUE(RunExtensionTest("browser_action/tab_specific_state")) <<
310 const Extension
* extension
= GetSingleLoadedExtension();
311 ASSERT_TRUE(extension
) << message_
;
313 // Test that there is a browser action in the toolbar and that it has an icon.
314 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
315 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0));
317 // Execute the action, its title should change.
318 ResultCatcher catcher
;
319 GetBrowserActionsBar().Press(0);
320 ASSERT_TRUE(catcher
.GetNextResult());
321 EXPECT_EQ("Showing icon 2", GetBrowserActionsBar().GetTooltip(0));
323 // Open a new tab, the title should go back.
324 chrome::NewTab(browser());
325 EXPECT_EQ("hi!", GetBrowserActionsBar().GetTooltip(0));
327 // Go back to first tab, changed title should reappear.
328 browser()->tab_strip_model()->ActivateTabAt(0, true);
329 EXPECT_EQ("Showing icon 2", GetBrowserActionsBar().GetTooltip(0));
331 // Reload that tab, default title should come back.
332 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
333 EXPECT_EQ("hi!", GetBrowserActionsBar().GetTooltip(0));
336 // http://code.google.com/p/chromium/issues/detail?id=70829
337 // Mac used to be ok, but then mac 10.5 started failing too. =(
338 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, DISABLED_BrowserActionPopup
) {
340 LoadExtension(test_data_dir_
.AppendASCII("browser_action/popup")));
341 BrowserActionTestUtil actions_bar
= GetBrowserActionsBar();
342 const Extension
* extension
= GetSingleLoadedExtension();
343 ASSERT_TRUE(extension
) << message_
;
345 // The extension's popup's size grows by |growFactor| each click.
346 const int growFactor
= 500;
347 gfx::Size minSize
= BrowserActionTestUtil::GetMinPopupSize();
348 gfx::Size middleSize
= gfx::Size(growFactor
, growFactor
);
349 gfx::Size maxSize
= BrowserActionTestUtil::GetMaxPopupSize();
351 // Ensure that two clicks will exceed the maximum allowed size.
352 ASSERT_GT(minSize
.height() + growFactor
* 2, maxSize
.height());
353 ASSERT_GT(minSize
.width() + growFactor
* 2, maxSize
.width());
355 // Simulate a click on the browser action and verify the size of the resulting
356 // popup. The first one tries to be 0x0, so it should be the min values.
357 ASSERT_TRUE(OpenPopup(0));
358 EXPECT_EQ(minSize
, actions_bar
.GetPopupSize());
359 EXPECT_TRUE(actions_bar
.HidePopup());
361 ASSERT_TRUE(OpenPopup(0));
362 EXPECT_EQ(middleSize
, actions_bar
.GetPopupSize());
363 EXPECT_TRUE(actions_bar
.HidePopup());
365 // One more time, but this time it should be constrained by the max values.
366 ASSERT_TRUE(OpenPopup(0));
367 EXPECT_EQ(maxSize
, actions_bar
.GetPopupSize());
368 EXPECT_TRUE(actions_bar
.HidePopup());
371 // Test that calling chrome.browserAction.setPopup() can enable and change
373 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, BrowserActionAddPopup
) {
374 ASSERT_TRUE(RunExtensionTest("browser_action/add_popup")) << message_
;
375 const Extension
* extension
= GetSingleLoadedExtension();
376 ASSERT_TRUE(extension
) << message_
;
378 int tab_id
= ExtensionTabUtil::GetTabId(
379 browser()->tab_strip_model()->GetActiveWebContents());
381 ExtensionAction
* browser_action
= GetBrowserAction(*extension
);
382 ASSERT_TRUE(browser_action
)
383 << "Browser action test extension should have a browser action.";
385 ASSERT_FALSE(browser_action
->HasPopup(tab_id
));
386 ASSERT_FALSE(browser_action
->HasPopup(ExtensionAction::kDefaultTabId
));
388 // Simulate a click on the browser action icon. The onClicked handler
391 ResultCatcher catcher
;
392 GetBrowserActionsBar().Press(0);
393 ASSERT_TRUE(catcher
.GetNextResult());
396 // The call to setPopup in background.html set a tab id, so the
397 // current tab's setting should have changed, but the default setting
398 // should not have changed.
399 ASSERT_TRUE(browser_action
->HasPopup(tab_id
))
400 << "Clicking on the browser action should have caused a popup to "
402 ASSERT_FALSE(browser_action
->HasPopup(ExtensionAction::kDefaultTabId
))
403 << "Clicking on the browser action should not have set a default "
406 ASSERT_STREQ("/a_popup.html",
407 browser_action
->GetPopupUrl(tab_id
).path().c_str());
409 // Now change the popup from a_popup.html to another_popup.html by loading
410 // a page which removes the popup using chrome.browserAction.setPopup().
412 ResultCatcher catcher
;
413 ui_test_utils::NavigateToURL(
415 GURL(extension
->GetResourceURL("change_popup.html")));
416 ASSERT_TRUE(catcher
.GetNextResult());
419 // The call to setPopup in change_popup.html did not use a tab id,
420 // so the default setting should have changed as well as the current tab.
421 ASSERT_TRUE(browser_action
->HasPopup(tab_id
));
422 ASSERT_TRUE(browser_action
->HasPopup(ExtensionAction::kDefaultTabId
));
423 ASSERT_STREQ("/another_popup.html",
424 browser_action
->GetPopupUrl(tab_id
).path().c_str());
427 // Test that calling chrome.browserAction.setPopup() can remove a popup.
428 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, BrowserActionRemovePopup
) {
429 // Load the extension, which has a browser action with a default popup.
430 ASSERT_TRUE(RunExtensionTest("browser_action/remove_popup")) << message_
;
431 const Extension
* extension
= GetSingleLoadedExtension();
432 ASSERT_TRUE(extension
) << message_
;
434 int tab_id
= ExtensionTabUtil::GetTabId(
435 browser()->tab_strip_model()->GetActiveWebContents());
437 ExtensionAction
* browser_action
= GetBrowserAction(*extension
);
438 ASSERT_TRUE(browser_action
)
439 << "Browser action test extension should have a browser action.";
441 ASSERT_TRUE(browser_action
->HasPopup(tab_id
))
442 << "Expect a browser action popup before the test removes it.";
443 ASSERT_TRUE(browser_action
->HasPopup(ExtensionAction::kDefaultTabId
))
444 << "Expect a browser action popup is the default for all tabs.";
446 // Load a page which removes the popup using chrome.browserAction.setPopup().
448 ResultCatcher catcher
;
449 ui_test_utils::NavigateToURL(
451 GURL(extension
->GetResourceURL("remove_popup.html")));
452 ASSERT_TRUE(catcher
.GetNextResult());
455 ASSERT_FALSE(browser_action
->HasPopup(tab_id
))
456 << "Browser action popup should have been removed.";
457 ASSERT_TRUE(browser_action
->HasPopup(ExtensionAction::kDefaultTabId
))
458 << "Browser action popup default should not be changed by setting "
459 << "a specific tab id.";
462 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, IncognitoBasic
) {
463 ASSERT_TRUE(test_server()->Start());
465 ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_
;
466 const Extension
* extension
= GetSingleLoadedExtension();
467 ASSERT_TRUE(extension
) << message_
;
469 // Test that there is a browser action in the toolbar.
470 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
472 // Open an incognito window and test that the browser action isn't there by
474 Profile
* incognito_profile
= browser()->profile()->GetOffTheRecordProfile();
475 base::RunLoop().RunUntilIdle(); // Wait for profile initialization.
476 Browser
* incognito_browser
=
477 new Browser(Browser::CreateParams(incognito_profile
,
478 browser()->host_desktop_type()));
481 BrowserActionTestUtil(incognito_browser
).NumberOfBrowserActions());
483 // Now enable the extension in incognito mode, and test that the browser
485 // SetIsIncognitoEnabled() requires a reload of the extension, so we have to
487 TestExtensionRegistryObserver
registry_observer(
488 ExtensionRegistry::Get(profile()), extension
->id());
489 extensions::util::SetIsIncognitoEnabled(
490 extension
->id(), browser()->profile(), true);
491 registry_observer
.WaitForExtensionLoaded();
494 BrowserActionTestUtil(incognito_browser
).NumberOfBrowserActions());
496 // TODO(mpcomplete): simulate a click and have it do the right thing in
500 // Tests that events are dispatched to the correct profile for split mode
502 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, IncognitoSplit
) {
503 ResultCatcher catcher
;
504 const Extension
* extension
= LoadExtensionWithFlags(
505 test_data_dir_
.AppendASCII("browser_action/split_mode"),
506 kFlagEnableIncognito
);
507 ASSERT_TRUE(extension
) << message_
;
509 // Open an incognito window.
510 Profile
* incognito_profile
= browser()->profile()->GetOffTheRecordProfile();
511 Browser
* incognito_browser
=
512 new Browser(Browser::CreateParams(incognito_profile
,
513 browser()->host_desktop_type()));
514 base::RunLoop().RunUntilIdle(); // Wait for profile initialization.
515 // Navigate just to have a tab in this window, otherwise wonky things happen.
516 ui_test_utils::OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
518 BrowserActionTestUtil(incognito_browser
).NumberOfBrowserActions());
520 // A click in the regular profile should open a tab in the regular profile.
521 ExtensionActionAPI
* extension_action_api
=
522 ExtensionActionAPI::Get(browser()->profile());
523 extension_action_api
->ExecuteExtensionAction(
524 extension
, browser(), true);
525 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
527 // A click in the incognito profile should open a tab in the
528 // incognito profile.
529 extension_action_api
->ExecuteExtensionAction(
530 extension
, incognito_browser
, true);
531 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
534 // Disabled because of failures (crashes) on ASAN bot.
535 // See http://crbug.com/98861.
536 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, DISABLED_CloseBackgroundPage
) {
537 ASSERT_TRUE(LoadExtension(
538 test_data_dir_
.AppendASCII("browser_action/close_background")));
539 const Extension
* extension
= GetSingleLoadedExtension();
541 // There is a background page and a browser action with no badge text.
542 extensions::ProcessManager
* manager
=
543 extensions::ProcessManager::Get(browser()->profile());
544 ASSERT_TRUE(manager
->GetBackgroundHostForExtension(extension
->id()));
545 ExtensionAction
* action
= GetBrowserAction(*extension
);
546 ASSERT_EQ("", action
->GetBadgeText(ExtensionAction::kDefaultTabId
));
548 content::WindowedNotificationObserver
host_destroyed_observer(
549 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
550 content::NotificationService::AllSources());
552 // Click the browser action.
553 ExtensionActionAPI::Get(browser()->profile())->ExecuteExtensionAction(
554 extension
, browser(), true);
556 // It can take a moment for the background page to actually get destroyed
557 // so we wait for the notification before checking that it's really gone
558 // and the badge text has been set.
559 host_destroyed_observer
.Wait();
560 ASSERT_FALSE(manager
->GetBackgroundHostForExtension(extension
->id()));
561 ASSERT_EQ("X", action
->GetBadgeText(ExtensionAction::kDefaultTabId
));
564 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, BadgeBackgroundColor
) {
565 ASSERT_TRUE(test_server()->Start());
566 ASSERT_TRUE(RunExtensionTest("browser_action/color")) << message_
;
567 const Extension
* extension
= GetSingleLoadedExtension();
568 ASSERT_TRUE(extension
) << message_
;
570 // Test that there is a browser action in the toolbar.
571 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
573 // Test that CSS values (#FF0000) set color correctly.
574 ExtensionAction
* action
= GetBrowserAction(*extension
);
575 ASSERT_EQ(SkColorSetARGB(255, 255, 0, 0),
576 action
->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId
));
578 // Tell the extension to update the browser action state.
579 ResultCatcher catcher
;
580 ui_test_utils::NavigateToURL(browser(),
581 GURL(extension
->GetResourceURL("update.html")));
582 ASSERT_TRUE(catcher
.GetNextResult());
584 // Test that CSS values (#0F0) set color correctly.
585 ASSERT_EQ(SkColorSetARGB(255, 0, 255, 0),
586 action
->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId
));
588 ui_test_utils::NavigateToURL(browser(),
589 GURL(extension
->GetResourceURL("update2.html")));
590 ASSERT_TRUE(catcher
.GetNextResult());
592 // Test that array values set color correctly.
593 ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255),
594 action
->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId
));
597 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, Getters
) {
598 ASSERT_TRUE(RunExtensionTest("browser_action/getters")) << message_
;
599 const Extension
* extension
= GetSingleLoadedExtension();
600 ASSERT_TRUE(extension
) << message_
;
602 // Test that there is a browser action in the toolbar.
603 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
605 // Test the getters for defaults.
606 ResultCatcher catcher
;
607 ui_test_utils::NavigateToURL(browser(),
608 GURL(extension
->GetResourceURL("update.html")));
609 ASSERT_TRUE(catcher
.GetNextResult());
611 // Test the getters for a specific tab.
612 ui_test_utils::NavigateToURL(browser(),
613 GURL(extension
->GetResourceURL("update2.html")));
614 ASSERT_TRUE(catcher
.GetNextResult());
617 // Verify triggering browser action.
618 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest
, TestTriggerBrowserAction
) {
619 ASSERT_TRUE(test_server()->Start());
621 ASSERT_TRUE(RunExtensionTest("trigger_actions/browser_action")) << message_
;
622 const Extension
* extension
= GetSingleLoadedExtension();
623 ASSERT_TRUE(extension
) << message_
;
625 // Test that there is a browser action in the toolbar.
626 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
628 ui_test_utils::NavigateToURL(
630 test_server()->GetURL("files/simple.html"));
632 ExtensionAction
* browser_action
= GetBrowserAction(*extension
);
633 EXPECT_TRUE(browser_action
!= NULL
);
635 // Simulate a click on the browser action icon.
637 ResultCatcher catcher
;
638 GetBrowserActionsBar().Press(0);
639 EXPECT_TRUE(catcher
.GetNextResult());
643 browser()->tab_strip_model()->GetActiveWebContents();
644 EXPECT_TRUE(tab
!= NULL
);
646 // Verify that the browser action turned the background color red.
647 const std::string script
=
648 "window.domAutomationController.send(document.body.style."
651 EXPECT_TRUE(content::ExecuteScriptAndExtractString(tab
, script
, &result
));
652 EXPECT_EQ(result
, "red");
656 } // namespace extensions