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/browser_action_test_util.h"
7 #include "chrome/browser/extensions/extension_action.h"
8 #include "chrome/browser/extensions/extension_action_icon_factory.h"
9 #include "chrome/browser/extensions/extension_action_manager.h"
10 #include "chrome/browser/extensions/extension_apitest.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_tab_util.h"
13 #include "chrome/browser/extensions/extension_toolbar_model.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_commands.h"
17 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/url_constants.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/test/browser_test_utils.h"
24 #include "extensions/browser/extension_system.h"
25 #include "extensions/browser/notification_types.h"
26 #include "extensions/common/feature_switch.h"
27 #include "grit/theme_resources.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/gfx/image/image_skia.h"
30 #include "ui/gfx/image/image_skia_operations.h"
31 #include "ui/gfx/rect.h"
32 #include "ui/gfx/size.h"
33 #include "ui/gfx/skia_util.h"
35 using content::WebContents;
37 namespace extensions {
40 const char kEmptyImageDataError[] =
41 "The imageData property must contain an ImageData object or dictionary "
42 "of ImageData objects.";
43 const char kEmptyPathError[] = "The path property must not be empty.";
45 // Views implementation of browser action button will return icon whose
46 // background will be set.
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);
57 bool ImagesAreEqualAtScale(const gfx::ImageSkia& i1,
58 const gfx::ImageSkia& i2,
60 SkBitmap bitmap1 = i1.GetRepresentation(scale).sk_bitmap();
61 SkBitmap bitmap2 = i2.GetRepresentation(scale).sk_bitmap();
62 return gfx::BitmapsAreEqual(bitmap1, bitmap2);
65 class BrowserActionApiTest : public ExtensionApiTest {
67 BrowserActionApiTest() {}
68 virtual ~BrowserActionApiTest() {}
71 BrowserActionTestUtil GetBrowserActionsBar() {
72 return BrowserActionTestUtil(browser());
75 bool OpenPopup(int index) {
76 ResultCatcher catcher;
77 content::WindowedNotificationObserver popup_observer(
78 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
79 content::NotificationService::AllSources());
80 GetBrowserActionsBar().Press(index);
81 popup_observer.Wait();
82 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
83 return GetBrowserActionsBar().HasPopup();
86 ExtensionAction* GetBrowserAction(const Extension& extension) {
87 return ExtensionActionManager::Get(browser()->profile())->
88 GetBrowserAction(extension);
92 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Basic) {
93 ASSERT_TRUE(test_server()->Start());
94 ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_;
95 const Extension* extension = GetSingleLoadedExtension();
96 ASSERT_TRUE(extension) << message_;
98 // Test that there is a browser action in the toolbar.
99 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
101 // Tell the extension to update the browser action state.
102 ResultCatcher catcher;
103 ui_test_utils::NavigateToURL(browser(),
104 GURL(extension->GetResourceURL("update.html")));
105 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
107 // Test that we received the changes.
108 ExtensionAction* action = GetBrowserAction(*extension);
109 ASSERT_EQ("Modified", action->GetTitle(ExtensionAction::kDefaultTabId));
110 ASSERT_EQ("badge", action->GetBadgeText(ExtensionAction::kDefaultTabId));
111 ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255),
112 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
114 // Simulate the browser action being clicked.
115 ui_test_utils::NavigateToURL(browser(),
116 test_server()->GetURL("files/extensions/test_file.txt"));
118 ExtensionToolbarModel* toolbar_model = ExtensionToolbarModel::Get(
119 browser()->profile());
120 toolbar_model->ExecuteBrowserAction(extension, browser(), NULL, true);
122 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
125 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
126 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_;
127 const Extension* extension = GetSingleLoadedExtension();
128 ASSERT_TRUE(extension) << message_;
130 #if defined (OS_MACOSX)
131 // We need this on mac so we don't loose 2x representations from browser icon
132 // in transformations gfx::ImageSkia -> NSImage -> gfx::ImageSkia.
133 std::vector<ui::ScaleFactor> supported_scale_factors;
134 supported_scale_factors.push_back(ui::SCALE_FACTOR_100P);
135 supported_scale_factors.push_back(ui::SCALE_FACTOR_200P);
136 ui::SetSupportedScaleFactors(supported_scale_factors);
139 // We should not be creating icons asynchronously, so we don't need an
141 ExtensionActionIconFactory icon_factory(
144 GetBrowserAction(*extension),
146 // Test that there is a browser action in the toolbar.
147 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
148 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0));
150 gfx::Image action_icon = icon_factory.GetIcon(0);
151 uint32_t action_icon_last_id = action_icon.ToSkBitmap()->getGenerationID();
153 // Let's check that |GetIcon| doesn't always return bitmap with new id.
154 ASSERT_EQ(action_icon_last_id,
155 icon_factory.GetIcon(0).ToSkBitmap()->getGenerationID());
157 uint32_t action_icon_current_id = 0;
159 ResultCatcher catcher;
161 // Tell the extension to update the icon using ImageData object.
162 GetBrowserActionsBar().Press(0);
163 ASSERT_TRUE(catcher.GetNextResult());
165 action_icon = icon_factory.GetIcon(0);
167 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
168 EXPECT_GT(action_icon_current_id, action_icon_last_id);
169 action_icon_last_id = action_icon_current_id;
171 EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
174 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
175 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
178 // Tell the extension to update the icon using path.
179 GetBrowserActionsBar().Press(0);
180 ASSERT_TRUE(catcher.GetNextResult());
182 action_icon = icon_factory.GetIcon(0);
184 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
185 EXPECT_GT(action_icon_current_id, action_icon_last_id);
186 action_icon_last_id = action_icon_current_id;
189 action_icon.ToImageSkia()->HasRepresentation(2.0f));
192 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
193 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
196 // Tell the extension to update the icon using dictionary of ImageData
198 GetBrowserActionsBar().Press(0);
199 ASSERT_TRUE(catcher.GetNextResult());
201 action_icon = icon_factory.GetIcon(0);
203 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
204 EXPECT_GT(action_icon_current_id, action_icon_last_id);
205 action_icon_last_id = action_icon_current_id;
207 EXPECT_TRUE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
210 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
211 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
214 // Tell the extension to update the icon using dictionary of paths.
215 GetBrowserActionsBar().Press(0);
216 ASSERT_TRUE(catcher.GetNextResult());
218 action_icon = icon_factory.GetIcon(0);
220 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
221 EXPECT_GT(action_icon_current_id, action_icon_last_id);
222 action_icon_last_id = action_icon_current_id;
224 EXPECT_TRUE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
227 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
228 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
231 // Tell the extension to update the icon using dictionary of ImageData
232 // objects, but setting only size 19.
233 GetBrowserActionsBar().Press(0);
234 ASSERT_TRUE(catcher.GetNextResult());
236 action_icon = icon_factory.GetIcon(0);
238 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
239 EXPECT_GT(action_icon_current_id, action_icon_last_id);
240 action_icon_last_id = action_icon_current_id;
242 EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
245 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
246 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
249 // Tell the extension to update the icon using dictionary of paths, but
250 // setting only size 19.
251 GetBrowserActionsBar().Press(0);
252 ASSERT_TRUE(catcher.GetNextResult());
254 action_icon = icon_factory.GetIcon(0);
256 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
257 EXPECT_GT(action_icon_current_id, action_icon_last_id);
258 action_icon_last_id = action_icon_current_id;
260 EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
263 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
264 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
267 // Tell the extension to update the icon using dictionary of ImageData
268 // objects, but setting only size 38.
269 GetBrowserActionsBar().Press(0);
270 ASSERT_TRUE(catcher.GetNextResult());
272 action_icon = icon_factory.GetIcon(0);
274 const gfx::ImageSkia* action_icon_skia = action_icon.ToImageSkia();
276 EXPECT_FALSE(action_icon_skia->HasRepresentation(1.0f));
277 EXPECT_TRUE(action_icon_skia->HasRepresentation(2.0f));
279 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
280 EXPECT_GT(action_icon_current_id, action_icon_last_id);
281 action_icon_last_id = action_icon_current_id;
283 EXPECT_TRUE(gfx::BitmapsAreEqual(
284 *action_icon.ToSkBitmap(),
285 action_icon_skia->GetRepresentation(2.0f).sk_bitmap()));
288 ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon_skia),
289 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
292 // Try setting icon with empty dictionary of ImageData objects.
293 GetBrowserActionsBar().Press(0);
294 ASSERT_FALSE(catcher.GetNextResult());
295 EXPECT_EQ(kEmptyImageDataError, catcher.message());
297 // Try setting icon with empty dictionary of path objects.
298 GetBrowserActionsBar().Press(0);
299 ASSERT_FALSE(catcher.GetNextResult());
300 EXPECT_EQ(kEmptyPathError, catcher.message());
303 // This test is flaky as per http://crbug.com/74557.
304 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest,
305 DISABLED_TabSpecificBrowserActionState) {
306 ASSERT_TRUE(RunExtensionTest("browser_action/tab_specific_state")) <<
308 const Extension* extension = GetSingleLoadedExtension();
309 ASSERT_TRUE(extension) << message_;
311 // Test that there is a browser action in the toolbar and that it has an icon.
312 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
313 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0));
315 // Execute the action, its title should change.
316 ResultCatcher catcher;
317 GetBrowserActionsBar().Press(0);
318 ASSERT_TRUE(catcher.GetNextResult());
319 EXPECT_EQ("Showing icon 2", GetBrowserActionsBar().GetTooltip(0));
321 // Open a new tab, the title should go back.
322 chrome::NewTab(browser());
323 EXPECT_EQ("hi!", GetBrowserActionsBar().GetTooltip(0));
325 // Go back to first tab, changed title should reappear.
326 browser()->tab_strip_model()->ActivateTabAt(0, true);
327 EXPECT_EQ("Showing icon 2", GetBrowserActionsBar().GetTooltip(0));
329 // Reload that tab, default title should come back.
330 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
331 EXPECT_EQ("hi!", GetBrowserActionsBar().GetTooltip(0));
334 // http://code.google.com/p/chromium/issues/detail?id=70829
335 // Mac used to be ok, but then mac 10.5 started failing too. =(
336 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DISABLED_BrowserActionPopup) {
338 LoadExtension(test_data_dir_.AppendASCII("browser_action/popup")));
339 BrowserActionTestUtil actions_bar = GetBrowserActionsBar();
340 const Extension* extension = GetSingleLoadedExtension();
341 ASSERT_TRUE(extension) << message_;
343 // The extension's popup's size grows by |growFactor| each click.
344 const int growFactor = 500;
345 gfx::Size minSize = BrowserActionTestUtil::GetMinPopupSize();
346 gfx::Size middleSize = gfx::Size(growFactor, growFactor);
347 gfx::Size maxSize = BrowserActionTestUtil::GetMaxPopupSize();
349 // Ensure that two clicks will exceed the maximum allowed size.
350 ASSERT_GT(minSize.height() + growFactor * 2, maxSize.height());
351 ASSERT_GT(minSize.width() + growFactor * 2, maxSize.width());
353 // Simulate a click on the browser action and verify the size of the resulting
354 // popup. The first one tries to be 0x0, so it should be the min values.
355 ASSERT_TRUE(OpenPopup(0));
356 EXPECT_EQ(minSize, actions_bar.GetPopupBounds().size());
357 EXPECT_TRUE(actions_bar.HidePopup());
359 ASSERT_TRUE(OpenPopup(0));
360 EXPECT_EQ(middleSize, actions_bar.GetPopupBounds().size());
361 EXPECT_TRUE(actions_bar.HidePopup());
363 // One more time, but this time it should be constrained by the max values.
364 ASSERT_TRUE(OpenPopup(0));
365 EXPECT_EQ(maxSize, actions_bar.GetPopupBounds().size());
366 EXPECT_TRUE(actions_bar.HidePopup());
369 // Test that calling chrome.browserAction.setPopup() can enable and change
371 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionAddPopup) {
372 ASSERT_TRUE(RunExtensionTest("browser_action/add_popup")) << message_;
373 const Extension* extension = GetSingleLoadedExtension();
374 ASSERT_TRUE(extension) << message_;
376 int tab_id = ExtensionTabUtil::GetTabId(
377 browser()->tab_strip_model()->GetActiveWebContents());
379 ExtensionAction* browser_action = GetBrowserAction(*extension);
380 ASSERT_TRUE(browser_action)
381 << "Browser action test extension should have a browser action.";
383 ASSERT_FALSE(browser_action->HasPopup(tab_id));
384 ASSERT_FALSE(browser_action->HasPopup(ExtensionAction::kDefaultTabId));
386 // Simulate a click on the browser action icon. The onClicked handler
389 ResultCatcher catcher;
390 GetBrowserActionsBar().Press(0);
391 ASSERT_TRUE(catcher.GetNextResult());
394 // The call to setPopup in background.html set a tab id, so the
395 // current tab's setting should have changed, but the default setting
396 // should not have changed.
397 ASSERT_TRUE(browser_action->HasPopup(tab_id))
398 << "Clicking on the browser action should have caused a popup to "
400 ASSERT_FALSE(browser_action->HasPopup(ExtensionAction::kDefaultTabId))
401 << "Clicking on the browser action should not have set a default "
404 ASSERT_STREQ("/a_popup.html",
405 browser_action->GetPopupUrl(tab_id).path().c_str());
407 // Now change the popup from a_popup.html to another_popup.html by loading
408 // a page which removes the popup using chrome.browserAction.setPopup().
410 ResultCatcher catcher;
411 ui_test_utils::NavigateToURL(
413 GURL(extension->GetResourceURL("change_popup.html")));
414 ASSERT_TRUE(catcher.GetNextResult());
417 // The call to setPopup in change_popup.html did not use a tab id,
418 // so the default setting should have changed as well as the current tab.
419 ASSERT_TRUE(browser_action->HasPopup(tab_id));
420 ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId));
421 ASSERT_STREQ("/another_popup.html",
422 browser_action->GetPopupUrl(tab_id).path().c_str());
425 // Test that calling chrome.browserAction.setPopup() can remove a popup.
426 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionRemovePopup) {
427 // Load the extension, which has a browser action with a default popup.
428 ASSERT_TRUE(RunExtensionTest("browser_action/remove_popup")) << message_;
429 const Extension* extension = GetSingleLoadedExtension();
430 ASSERT_TRUE(extension) << message_;
432 int tab_id = ExtensionTabUtil::GetTabId(
433 browser()->tab_strip_model()->GetActiveWebContents());
435 ExtensionAction* browser_action = GetBrowserAction(*extension);
436 ASSERT_TRUE(browser_action)
437 << "Browser action test extension should have a browser action.";
439 ASSERT_TRUE(browser_action->HasPopup(tab_id))
440 << "Expect a browser action popup before the test removes it.";
441 ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId))
442 << "Expect a browser action popup is the default for all tabs.";
444 // Load a page which removes the popup using chrome.browserAction.setPopup().
446 ResultCatcher catcher;
447 ui_test_utils::NavigateToURL(
449 GURL(extension->GetResourceURL("remove_popup.html")));
450 ASSERT_TRUE(catcher.GetNextResult());
453 ASSERT_FALSE(browser_action->HasPopup(tab_id))
454 << "Browser action popup should have been removed.";
455 ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId))
456 << "Browser action popup default should not be changed by setting "
457 << "a specific tab id.";
460 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoBasic) {
461 ASSERT_TRUE(test_server()->Start());
463 ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_;
464 const Extension* extension = GetSingleLoadedExtension();
465 ASSERT_TRUE(extension) << message_;
467 // Test that there is a browser action in the toolbar.
468 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
470 // Open an incognito window and test that the browser action isn't there by
472 Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
473 Browser* incognito_browser =
474 new Browser(Browser::CreateParams(incognito_profile,
475 browser()->host_desktop_type()));
478 BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
480 // Now enable the extension in incognito mode, and test that the browser
481 // action shows up. Note that we don't update the existing window at the
482 // moment, so we just create a new one.
483 extensions::ExtensionPrefs::Get(browser()->profile())
484 ->SetIsIncognitoEnabled(extension->id(), true);
486 chrome::CloseWindow(incognito_browser);
488 new Browser(Browser::CreateParams(incognito_profile,
489 browser()->host_desktop_type()));
491 BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
493 // TODO(mpcomplete): simulate a click and have it do the right thing in
497 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoDragging) {
498 ExtensionService* service = extensions::ExtensionSystem::Get(
499 browser()->profile())->extension_service();
501 // The tooltips for each respective browser action.
502 const char kTooltipA[] = "Alpha";
503 const char kTooltipB[] = "Beta";
504 const char kTooltipC[] = "Gamma";
506 const size_t size_before = service->extensions()->size();
508 base::FilePath test_dir = test_data_dir_.AppendASCII("browser_action");
509 const Extension* extension_a = InstallExtension(
510 test_dir.AppendASCII("empty_browser_action_alpha.crx"), 1);
511 const Extension* extension_b = InstallExtension(
512 test_dir.AppendASCII("empty_browser_action_beta.crx"), 1);
513 const Extension* extension_c = InstallExtension(
514 test_dir.AppendASCII("empty_browser_action_gamma.crx"), 1);
515 ASSERT_TRUE(extension_a);
516 ASSERT_TRUE(extension_b);
517 ASSERT_TRUE(extension_c);
519 // Test that there are 3 browser actions in the toolbar.
520 ASSERT_EQ(size_before + 3, service->extensions()->size());
521 ASSERT_EQ(3, GetBrowserActionsBar().NumberOfBrowserActions());
523 // Now enable 2 of the extensions in incognito mode, and test that the browser
525 extensions::ExtensionPrefs* prefs =
526 extensions::ExtensionPrefs::Get(browser()->profile());
527 prefs->SetIsIncognitoEnabled(extension_a->id(), true);
528 prefs->SetIsIncognitoEnabled(extension_c->id(), true);
530 Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
531 Browser* incognito_browser =
532 new Browser(Browser::CreateParams(incognito_profile,
533 browser()->host_desktop_type()));
534 BrowserActionTestUtil incognito_bar(incognito_browser);
536 // Navigate just to have a tab in this window, otherwise wonky things happen.
537 ui_test_utils::OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
539 ASSERT_EQ(2, incognito_bar.NumberOfBrowserActions());
541 // Ensure that the browser actions are in the right order (ABC).
542 EXPECT_EQ(kTooltipA, GetBrowserActionsBar().GetTooltip(0));
543 EXPECT_EQ(kTooltipB, GetBrowserActionsBar().GetTooltip(1));
544 EXPECT_EQ(kTooltipC, GetBrowserActionsBar().GetTooltip(2));
546 EXPECT_EQ(kTooltipA, incognito_bar.GetTooltip(0));
547 EXPECT_EQ(kTooltipC, incognito_bar.GetTooltip(1));
549 // Now rearrange them and ensure that they are rearranged correctly in both
550 // regular and incognito mode.
553 ExtensionToolbarModel* toolbar_model = ExtensionToolbarModel::Get(
554 browser()->profile());
555 toolbar_model->MoveBrowserAction(extension_c, 0);
557 EXPECT_EQ(kTooltipC, GetBrowserActionsBar().GetTooltip(0));
558 EXPECT_EQ(kTooltipA, GetBrowserActionsBar().GetTooltip(1));
559 EXPECT_EQ(kTooltipB, GetBrowserActionsBar().GetTooltip(2));
561 EXPECT_EQ(kTooltipC, incognito_bar.GetTooltip(0));
562 EXPECT_EQ(kTooltipA, incognito_bar.GetTooltip(1));
565 toolbar_model->MoveBrowserAction(extension_b, 1);
567 EXPECT_EQ(kTooltipC, GetBrowserActionsBar().GetTooltip(0));
568 EXPECT_EQ(kTooltipB, GetBrowserActionsBar().GetTooltip(1));
569 EXPECT_EQ(kTooltipA, GetBrowserActionsBar().GetTooltip(2));
571 EXPECT_EQ(kTooltipC, incognito_bar.GetTooltip(0));
572 EXPECT_EQ(kTooltipA, incognito_bar.GetTooltip(1));
575 // Tests that events are dispatched to the correct profile for split mode
577 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoSplit) {
578 ResultCatcher catcher;
579 const Extension* extension = LoadExtensionWithFlags(
580 test_data_dir_.AppendASCII("browser_action/split_mode"),
581 kFlagEnableIncognito);
582 ASSERT_TRUE(extension) << message_;
584 // Open an incognito window.
585 Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
586 Browser* incognito_browser =
587 new Browser(Browser::CreateParams(incognito_profile,
588 browser()->host_desktop_type()));
589 // Navigate just to have a tab in this window, otherwise wonky things happen.
590 ui_test_utils::OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
592 BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
594 // A click in the regular profile should open a tab in the regular profile.
595 ExtensionToolbarModel* toolbar_model = ExtensionToolbarModel::Get(
596 browser()->profile());
597 toolbar_model->ExecuteBrowserAction(extension, browser(), NULL, true);
598 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
600 // A click in the incognito profile should open a tab in the
601 // incognito profile.
602 toolbar_model->ExecuteBrowserAction(extension, incognito_browser, NULL, true);
603 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
606 // Disabled because of failures (crashes) on ASAN bot.
607 // See http://crbug.com/98861.
608 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DISABLED_CloseBackgroundPage) {
609 ASSERT_TRUE(LoadExtension(
610 test_data_dir_.AppendASCII("browser_action/close_background")));
611 const Extension* extension = GetSingleLoadedExtension();
613 // There is a background page and a browser action with no badge text.
614 extensions::ProcessManager* manager =
615 extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
616 ASSERT_TRUE(manager->GetBackgroundHostForExtension(extension->id()));
617 ExtensionAction* action = GetBrowserAction(*extension);
618 ASSERT_EQ("", action->GetBadgeText(ExtensionAction::kDefaultTabId));
620 content::WindowedNotificationObserver host_destroyed_observer(
621 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
622 content::NotificationService::AllSources());
624 // Click the browser action.
625 ExtensionToolbarModel* toolbar_model = ExtensionToolbarModel::Get(
626 browser()->profile());
627 toolbar_model->ExecuteBrowserAction(extension, browser(), NULL, true);
629 // It can take a moment for the background page to actually get destroyed
630 // so we wait for the notification before checking that it's really gone
631 // and the badge text has been set.
632 host_destroyed_observer.Wait();
633 ASSERT_FALSE(manager->GetBackgroundHostForExtension(extension->id()));
634 ASSERT_EQ("X", action->GetBadgeText(ExtensionAction::kDefaultTabId));
637 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BadgeBackgroundColor) {
638 ASSERT_TRUE(test_server()->Start());
639 ASSERT_TRUE(RunExtensionTest("browser_action/color")) << message_;
640 const Extension* extension = GetSingleLoadedExtension();
641 ASSERT_TRUE(extension) << message_;
643 // Test that there is a browser action in the toolbar.
644 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
646 // Test that CSS values (#FF0000) set color correctly.
647 ExtensionAction* action = GetBrowserAction(*extension);
648 ASSERT_EQ(SkColorSetARGB(255, 255, 0, 0),
649 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
651 // Tell the extension to update the browser action state.
652 ResultCatcher catcher;
653 ui_test_utils::NavigateToURL(browser(),
654 GURL(extension->GetResourceURL("update.html")));
655 ASSERT_TRUE(catcher.GetNextResult());
657 // Test that CSS values (#0F0) set color correctly.
658 ASSERT_EQ(SkColorSetARGB(255, 0, 255, 0),
659 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
661 ui_test_utils::NavigateToURL(browser(),
662 GURL(extension->GetResourceURL("update2.html")));
663 ASSERT_TRUE(catcher.GetNextResult());
665 // Test that array values set color correctly.
666 ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255),
667 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
670 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Getters) {
671 ASSERT_TRUE(RunExtensionTest("browser_action/getters")) << message_;
672 const Extension* extension = GetSingleLoadedExtension();
673 ASSERT_TRUE(extension) << message_;
675 // Test that there is a browser action in the toolbar.
676 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
678 // Test the getters for defaults.
679 ResultCatcher catcher;
680 ui_test_utils::NavigateToURL(browser(),
681 GURL(extension->GetResourceURL("update.html")));
682 ASSERT_TRUE(catcher.GetNextResult());
684 // Test the getters for a specific tab.
685 ui_test_utils::NavigateToURL(browser(),
686 GURL(extension->GetResourceURL("update2.html")));
687 ASSERT_TRUE(catcher.GetNextResult());
690 // Verify triggering browser action.
691 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, TestTriggerBrowserAction) {
692 ASSERT_TRUE(test_server()->Start());
694 ASSERT_TRUE(RunExtensionTest("trigger_actions/browser_action")) << message_;
695 const Extension* extension = GetSingleLoadedExtension();
696 ASSERT_TRUE(extension) << message_;
698 // Test that there is a browser action in the toolbar.
699 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
701 ui_test_utils::NavigateToURL(
703 test_server()->GetURL("files/simple.html"));
705 ExtensionAction* browser_action = GetBrowserAction(*extension);
706 EXPECT_TRUE(browser_action != NULL);
708 // Simulate a click on the browser action icon.
710 ResultCatcher catcher;
711 GetBrowserActionsBar().Press(0);
712 EXPECT_TRUE(catcher.GetNextResult());
716 browser()->tab_strip_model()->GetActiveWebContents();
717 EXPECT_TRUE(tab != NULL);
719 // Verify that the browser action turned the background color red.
720 const std::string script =
721 "window.domAutomationController.send(document.body.style."
724 EXPECT_TRUE(content::ExecuteScriptAndExtractString(tab, script, &result));
725 EXPECT_EQ(result, "red");
729 } // namespace extensions