Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / extension_action / browser_action_apitest.cc
blob0dfc4a8314c530160133197b7c606603d1562998
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_util.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_registry.h"
25 #include "extensions/browser/extension_system.h"
26 #include "extensions/browser/notification_types.h"
27 #include "extensions/browser/process_manager.h"
28 #include "extensions/browser/test_extension_registry_observer.h"
29 #include "extensions/common/feature_switch.h"
30 #include "extensions/test/result_catcher.h"
31 #include "grit/theme_resources.h"
32 #include "ui/base/resource/resource_bundle.h"
33 #include "ui/gfx/geometry/rect.h"
34 #include "ui/gfx/geometry/size.h"
35 #include "ui/gfx/image/canvas_image_source.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 {
43 namespace {
45 // An ImageSkia source that will do nothing (i.e., have a blank skia). We need
46 // this because we need a blank canvas at a certain size, and that can't be done
47 // by just using a null ImageSkia.
48 class BlankImageSource : public gfx::CanvasImageSource {
49 public:
50 explicit BlankImageSource(const gfx::Size& size)
51 : gfx::CanvasImageSource(size, false) {}
52 ~BlankImageSource() override {}
54 void Draw(gfx::Canvas* canvas) override {}
56 private:
57 DISALLOW_COPY_AND_ASSIGN(BlankImageSource);
60 const char kEmptyImageDataError[] =
61 "The imageData property must contain an ImageData object or dictionary "
62 "of ImageData objects.";
63 const char kEmptyPathError[] = "The path property must not be empty.";
65 // Views platforms have the icon superimposed over a button's background.
66 // Macs don't, but still need a 29x29-sized image (and the easiest way to do
67 // that is to superimpose the icon over a blank background).
68 gfx::ImageSkia AddBackground(const gfx::ImageSkia& icon) {
69 #if !defined(OS_MACOSX)
70 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
71 gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION);
72 #else
73 const gfx::Size size(29, 29); // Size of browser actions buttons.
74 gfx::ImageSkia bg(new BlankImageSource(size), size);
75 #endif
76 return gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon);
79 bool ImagesAreEqualAtScale(const gfx::ImageSkia& i1,
80 const gfx::ImageSkia& i2,
81 float scale) {
82 SkBitmap bitmap1 = i1.GetRepresentation(scale).sk_bitmap();
83 SkBitmap bitmap2 = i2.GetRepresentation(scale).sk_bitmap();
84 return gfx::BitmapsAreEqual(bitmap1, bitmap2);
87 class BrowserActionApiTest : public ExtensionApiTest {
88 public:
89 BrowserActionApiTest() {}
90 ~BrowserActionApiTest() override {}
92 protected:
93 BrowserActionTestUtil* GetBrowserActionsBar() {
94 if (!browser_action_test_util_)
95 browser_action_test_util_.reset(new BrowserActionTestUtil(browser()));
96 return browser_action_test_util_.get();
99 bool OpenPopup(int index) {
100 ResultCatcher catcher;
101 content::WindowedNotificationObserver popup_observer(
102 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
103 content::NotificationService::AllSources());
104 GetBrowserActionsBar()->Press(index);
105 popup_observer.Wait();
106 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
107 return GetBrowserActionsBar()->HasPopup();
110 ExtensionAction* GetBrowserAction(const Extension& extension) {
111 return ExtensionActionManager::Get(browser()->profile())->
112 GetBrowserAction(extension);
115 private:
116 scoped_ptr<BrowserActionTestUtil> browser_action_test_util_;
118 DISALLOW_COPY_AND_ASSIGN(BrowserActionApiTest);
121 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Basic) {
122 ASSERT_TRUE(test_server()->Start());
123 ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_;
124 const Extension* extension = GetSingleLoadedExtension();
125 ASSERT_TRUE(extension) << message_;
127 // Test that there is a browser action in the toolbar.
128 ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
130 // Tell the extension to update the browser action state.
131 ResultCatcher catcher;
132 ui_test_utils::NavigateToURL(browser(),
133 GURL(extension->GetResourceURL("update.html")));
134 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
136 // Test that we received the changes.
137 ExtensionAction* action = GetBrowserAction(*extension);
138 ASSERT_EQ("Modified", action->GetTitle(ExtensionAction::kDefaultTabId));
139 ASSERT_EQ("badge", action->GetBadgeText(ExtensionAction::kDefaultTabId));
140 ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255),
141 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
143 // Simulate the browser action being clicked.
144 ui_test_utils::NavigateToURL(browser(),
145 test_server()->GetURL("files/extensions/test_file.txt"));
147 ExtensionActionAPI::Get(browser()->profile())->ExecuteExtensionAction(
148 extension, browser(), true);
150 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
153 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
154 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_;
155 const Extension* extension = GetSingleLoadedExtension();
156 ASSERT_TRUE(extension) << message_;
158 #if defined (OS_MACOSX)
159 // We need this on mac so we don't loose 2x representations from browser icon
160 // in transformations gfx::ImageSkia -> NSImage -> gfx::ImageSkia.
161 std::vector<ui::ScaleFactor> supported_scale_factors;
162 supported_scale_factors.push_back(ui::SCALE_FACTOR_100P);
163 supported_scale_factors.push_back(ui::SCALE_FACTOR_200P);
164 ui::SetSupportedScaleFactors(supported_scale_factors);
165 #endif
167 // We should not be creating icons asynchronously, so we don't need an
168 // observer.
169 ExtensionActionIconFactory icon_factory(
170 profile(),
171 extension,
172 GetBrowserAction(*extension),
173 NULL);
174 // Test that there is a browser action in the toolbar.
175 ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
176 EXPECT_TRUE(GetBrowserActionsBar()->HasIcon(0));
178 gfx::Image action_icon = icon_factory.GetIcon(0);
179 uint32_t action_icon_last_id = action_icon.ToSkBitmap()->getGenerationID();
181 // Let's check that |GetIcon| doesn't always return bitmap with new id.
182 ASSERT_EQ(action_icon_last_id,
183 icon_factory.GetIcon(0).ToSkBitmap()->getGenerationID());
185 uint32_t action_icon_current_id = 0;
187 ResultCatcher catcher;
189 // Tell the extension to update the icon using ImageData object.
190 GetBrowserActionsBar()->Press(0);
191 ASSERT_TRUE(catcher.GetNextResult());
193 action_icon = icon_factory.GetIcon(0);
195 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
196 EXPECT_GT(action_icon_current_id, action_icon_last_id);
197 action_icon_last_id = action_icon_current_id;
199 EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
201 EXPECT_TRUE(
202 ImagesAreEqualAtScale(AddBackground(*action_icon.ToImageSkia()),
203 *GetBrowserActionsBar()->GetIcon(0).ToImageSkia(),
204 1.0f));
206 // Tell the extension to update the icon using path.
207 GetBrowserActionsBar()->Press(0);
208 ASSERT_TRUE(catcher.GetNextResult());
210 action_icon = icon_factory.GetIcon(0);
212 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
213 EXPECT_GT(action_icon_current_id, action_icon_last_id);
214 action_icon_last_id = action_icon_current_id;
216 EXPECT_FALSE(
217 action_icon.ToImageSkia()->HasRepresentation(2.0f));
219 EXPECT_TRUE(
220 ImagesAreEqualAtScale(AddBackground(*action_icon.ToImageSkia()),
221 *GetBrowserActionsBar()->GetIcon(0).ToImageSkia(),
222 1.0f));
224 // Tell the extension to update the icon using dictionary of ImageData
225 // objects.
226 GetBrowserActionsBar()->Press(0);
227 ASSERT_TRUE(catcher.GetNextResult());
229 action_icon = icon_factory.GetIcon(0);
231 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
232 EXPECT_GT(action_icon_current_id, action_icon_last_id);
233 action_icon_last_id = action_icon_current_id;
235 EXPECT_TRUE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
237 EXPECT_TRUE(
238 ImagesAreEqualAtScale(AddBackground(*action_icon.ToImageSkia()),
239 *GetBrowserActionsBar()->GetIcon(0).ToImageSkia(),
240 1.0f));
242 // Tell the extension to update the icon using dictionary of paths.
243 GetBrowserActionsBar()->Press(0);
244 ASSERT_TRUE(catcher.GetNextResult());
246 action_icon = icon_factory.GetIcon(0);
248 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
249 EXPECT_GT(action_icon_current_id, action_icon_last_id);
250 action_icon_last_id = action_icon_current_id;
252 EXPECT_TRUE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
254 EXPECT_TRUE(
255 ImagesAreEqualAtScale(AddBackground(*action_icon.ToImageSkia()),
256 *GetBrowserActionsBar()->GetIcon(0).ToImageSkia(),
257 1.0f));
259 // Tell the extension to update the icon using dictionary of ImageData
260 // objects, but setting only size 19.
261 GetBrowserActionsBar()->Press(0);
262 ASSERT_TRUE(catcher.GetNextResult());
264 action_icon = icon_factory.GetIcon(0);
266 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
267 EXPECT_GT(action_icon_current_id, action_icon_last_id);
268 action_icon_last_id = action_icon_current_id;
270 EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
272 EXPECT_TRUE(
273 ImagesAreEqualAtScale(AddBackground(*action_icon.ToImageSkia()),
274 *GetBrowserActionsBar()->GetIcon(0).ToImageSkia(),
275 1.0f));
277 // Tell the extension to update the icon using dictionary of paths, but
278 // setting only size 19.
279 GetBrowserActionsBar()->Press(0);
280 ASSERT_TRUE(catcher.GetNextResult());
282 action_icon = icon_factory.GetIcon(0);
284 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
285 EXPECT_GT(action_icon_current_id, action_icon_last_id);
286 action_icon_last_id = action_icon_current_id;
288 EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
290 EXPECT_TRUE(
291 ImagesAreEqualAtScale(AddBackground(*action_icon.ToImageSkia()),
292 *GetBrowserActionsBar()->GetIcon(0).ToImageSkia(),
293 1.0f));
295 // Tell the extension to update the icon using dictionary of ImageData
296 // objects, but setting only size 38.
297 GetBrowserActionsBar()->Press(0);
298 ASSERT_TRUE(catcher.GetNextResult());
300 action_icon = icon_factory.GetIcon(0);
302 const gfx::ImageSkia* action_icon_skia = action_icon.ToImageSkia();
304 EXPECT_FALSE(action_icon_skia->HasRepresentation(1.0f));
305 EXPECT_TRUE(action_icon_skia->HasRepresentation(2.0f));
307 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
308 EXPECT_GT(action_icon_current_id, action_icon_last_id);
309 action_icon_last_id = action_icon_current_id;
311 EXPECT_TRUE(gfx::BitmapsAreEqual(
312 *action_icon.ToSkBitmap(),
313 action_icon_skia->GetRepresentation(2.0f).sk_bitmap()));
315 EXPECT_TRUE(
316 ImagesAreEqualAtScale(AddBackground(*action_icon_skia),
317 *GetBrowserActionsBar()->GetIcon(0).ToImageSkia(),
318 2.0f));
320 // Try setting icon with empty dictionary of ImageData objects.
321 GetBrowserActionsBar()->Press(0);
322 ASSERT_FALSE(catcher.GetNextResult());
323 EXPECT_EQ(kEmptyImageDataError, catcher.message());
325 // Try setting icon with empty dictionary of path objects.
326 GetBrowserActionsBar()->Press(0);
327 ASSERT_FALSE(catcher.GetNextResult());
328 EXPECT_EQ(kEmptyPathError, catcher.message());
331 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, TabSpecificBrowserActionState) {
332 ASSERT_TRUE(RunExtensionTest("browser_action/tab_specific_state")) <<
333 message_;
334 const Extension* extension = GetSingleLoadedExtension();
335 ASSERT_TRUE(extension) << message_;
337 // Test that there is a browser action in the toolbar and that it has an icon.
338 ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
339 EXPECT_TRUE(GetBrowserActionsBar()->HasIcon(0));
341 // Execute the action, its title should change.
342 ResultCatcher catcher;
343 GetBrowserActionsBar()->Press(0);
344 ASSERT_TRUE(catcher.GetNextResult());
345 EXPECT_EQ("Showing icon 2", GetBrowserActionsBar()->GetTooltip(0));
347 // Open a new tab, the title should go back.
348 chrome::NewTab(browser());
349 EXPECT_EQ("hi!", GetBrowserActionsBar()->GetTooltip(0));
351 // Go back to first tab, changed title should reappear.
352 browser()->tab_strip_model()->ActivateTabAt(0, true);
353 EXPECT_EQ("Showing icon 2", GetBrowserActionsBar()->GetTooltip(0));
355 // Reload that tab, default title should come back.
356 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
357 EXPECT_EQ("hi!", GetBrowserActionsBar()->GetTooltip(0));
360 // http://code.google.com/p/chromium/issues/detail?id=70829
361 // Mac used to be ok, but then mac 10.5 started failing too. =(
362 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DISABLED_BrowserActionPopup) {
363 ASSERT_TRUE(
364 LoadExtension(test_data_dir_.AppendASCII("browser_action/popup")));
365 BrowserActionTestUtil* actions_bar = GetBrowserActionsBar();
366 const Extension* extension = GetSingleLoadedExtension();
367 ASSERT_TRUE(extension) << message_;
369 // The extension's popup's size grows by |growFactor| each click.
370 const int growFactor = 500;
371 gfx::Size minSize = BrowserActionTestUtil::GetMinPopupSize();
372 gfx::Size middleSize = gfx::Size(growFactor, growFactor);
373 gfx::Size maxSize = BrowserActionTestUtil::GetMaxPopupSize();
375 // Ensure that two clicks will exceed the maximum allowed size.
376 ASSERT_GT(minSize.height() + growFactor * 2, maxSize.height());
377 ASSERT_GT(minSize.width() + growFactor * 2, maxSize.width());
379 // Simulate a click on the browser action and verify the size of the resulting
380 // popup. The first one tries to be 0x0, so it should be the min values.
381 ASSERT_TRUE(OpenPopup(0));
382 EXPECT_EQ(minSize, actions_bar->GetPopupSize());
383 EXPECT_TRUE(actions_bar->HidePopup());
385 ASSERT_TRUE(OpenPopup(0));
386 EXPECT_EQ(middleSize, actions_bar->GetPopupSize());
387 EXPECT_TRUE(actions_bar->HidePopup());
389 // One more time, but this time it should be constrained by the max values.
390 ASSERT_TRUE(OpenPopup(0));
391 EXPECT_EQ(maxSize, actions_bar->GetPopupSize());
392 EXPECT_TRUE(actions_bar->HidePopup());
395 // Test that calling chrome.browserAction.setPopup() can enable and change
396 // a popup.
397 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionAddPopup) {
398 ASSERT_TRUE(RunExtensionTest("browser_action/add_popup")) << message_;
399 const Extension* extension = GetSingleLoadedExtension();
400 ASSERT_TRUE(extension) << message_;
402 int tab_id = ExtensionTabUtil::GetTabId(
403 browser()->tab_strip_model()->GetActiveWebContents());
405 ExtensionAction* browser_action = GetBrowserAction(*extension);
406 ASSERT_TRUE(browser_action)
407 << "Browser action test extension should have a browser action.";
409 ASSERT_FALSE(browser_action->HasPopup(tab_id));
410 ASSERT_FALSE(browser_action->HasPopup(ExtensionAction::kDefaultTabId));
412 // Simulate a click on the browser action icon. The onClicked handler
413 // will add a popup.
415 ResultCatcher catcher;
416 GetBrowserActionsBar()->Press(0);
417 ASSERT_TRUE(catcher.GetNextResult());
420 // The call to setPopup in background.html set a tab id, so the
421 // current tab's setting should have changed, but the default setting
422 // should not have changed.
423 ASSERT_TRUE(browser_action->HasPopup(tab_id))
424 << "Clicking on the browser action should have caused a popup to "
425 << "be added.";
426 ASSERT_FALSE(browser_action->HasPopup(ExtensionAction::kDefaultTabId))
427 << "Clicking on the browser action should not have set a default "
428 << "popup.";
430 ASSERT_STREQ("/a_popup.html",
431 browser_action->GetPopupUrl(tab_id).path().c_str());
433 // Now change the popup from a_popup.html to another_popup.html by loading
434 // a page which removes the popup using chrome.browserAction.setPopup().
436 ResultCatcher catcher;
437 ui_test_utils::NavigateToURL(
438 browser(),
439 GURL(extension->GetResourceURL("change_popup.html")));
440 ASSERT_TRUE(catcher.GetNextResult());
443 // The call to setPopup in change_popup.html did not use a tab id,
444 // so the default setting should have changed as well as the current tab.
445 ASSERT_TRUE(browser_action->HasPopup(tab_id));
446 ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId));
447 ASSERT_STREQ("/another_popup.html",
448 browser_action->GetPopupUrl(tab_id).path().c_str());
451 // Test that calling chrome.browserAction.setPopup() can remove a popup.
452 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionRemovePopup) {
453 // Load the extension, which has a browser action with a default popup.
454 ASSERT_TRUE(RunExtensionTest("browser_action/remove_popup")) << message_;
455 const Extension* extension = GetSingleLoadedExtension();
456 ASSERT_TRUE(extension) << message_;
458 int tab_id = ExtensionTabUtil::GetTabId(
459 browser()->tab_strip_model()->GetActiveWebContents());
461 ExtensionAction* browser_action = GetBrowserAction(*extension);
462 ASSERT_TRUE(browser_action)
463 << "Browser action test extension should have a browser action.";
465 ASSERT_TRUE(browser_action->HasPopup(tab_id))
466 << "Expect a browser action popup before the test removes it.";
467 ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId))
468 << "Expect a browser action popup is the default for all tabs.";
470 // Load a page which removes the popup using chrome.browserAction.setPopup().
472 ResultCatcher catcher;
473 ui_test_utils::NavigateToURL(
474 browser(),
475 GURL(extension->GetResourceURL("remove_popup.html")));
476 ASSERT_TRUE(catcher.GetNextResult());
479 ASSERT_FALSE(browser_action->HasPopup(tab_id))
480 << "Browser action popup should have been removed.";
481 ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId))
482 << "Browser action popup default should not be changed by setting "
483 << "a specific tab id.";
486 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoBasic) {
487 ASSERT_TRUE(test_server()->Start());
489 ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_;
490 const Extension* extension = GetSingleLoadedExtension();
491 ASSERT_TRUE(extension) << message_;
493 // Test that there is a browser action in the toolbar.
494 ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
496 // Open an incognito window and test that the browser action isn't there by
497 // default.
498 Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
499 base::RunLoop().RunUntilIdle(); // Wait for profile initialization.
500 Browser* incognito_browser =
501 new Browser(Browser::CreateParams(incognito_profile,
502 browser()->host_desktop_type()));
504 ASSERT_EQ(0,
505 BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
507 // Now enable the extension in incognito mode, and test that the browser
508 // action shows up.
509 // SetIsIncognitoEnabled() requires a reload of the extension, so we have to
510 // wait for it.
511 TestExtensionRegistryObserver registry_observer(
512 ExtensionRegistry::Get(profile()), extension->id());
513 extensions::util::SetIsIncognitoEnabled(
514 extension->id(), browser()->profile(), true);
515 registry_observer.WaitForExtensionLoaded();
517 ASSERT_EQ(1,
518 BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
520 // TODO(mpcomplete): simulate a click and have it do the right thing in
521 // incognito.
524 // Tests that events are dispatched to the correct profile for split mode
525 // extensions.
526 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoSplit) {
527 ResultCatcher catcher;
528 const Extension* extension = LoadExtensionWithFlags(
529 test_data_dir_.AppendASCII("browser_action/split_mode"),
530 kFlagEnableIncognito);
531 ASSERT_TRUE(extension) << message_;
533 // Open an incognito window.
534 Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
535 Browser* incognito_browser =
536 new Browser(Browser::CreateParams(incognito_profile,
537 browser()->host_desktop_type()));
538 base::RunLoop().RunUntilIdle(); // Wait for profile initialization.
539 // Navigate just to have a tab in this window, otherwise wonky things happen.
540 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
541 ASSERT_EQ(1,
542 BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
544 // A click in the regular profile should open a tab in the regular profile.
545 ExtensionActionAPI* extension_action_api =
546 ExtensionActionAPI::Get(browser()->profile());
547 extension_action_api->ExecuteExtensionAction(
548 extension, browser(), true);
549 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
551 // A click in the incognito profile should open a tab in the
552 // incognito profile.
553 extension_action_api->ExecuteExtensionAction(
554 extension, incognito_browser, true);
555 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
558 // Disabled because of failures (crashes) on ASAN bot.
559 // See http://crbug.com/98861.
560 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DISABLED_CloseBackgroundPage) {
561 ASSERT_TRUE(LoadExtension(
562 test_data_dir_.AppendASCII("browser_action/close_background")));
563 const Extension* extension = GetSingleLoadedExtension();
565 // There is a background page and a browser action with no badge text.
566 extensions::ProcessManager* manager =
567 extensions::ProcessManager::Get(browser()->profile());
568 ASSERT_TRUE(manager->GetBackgroundHostForExtension(extension->id()));
569 ExtensionAction* action = GetBrowserAction(*extension);
570 ASSERT_EQ("", action->GetBadgeText(ExtensionAction::kDefaultTabId));
572 content::WindowedNotificationObserver host_destroyed_observer(
573 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
574 content::NotificationService::AllSources());
576 // Click the browser action.
577 ExtensionActionAPI::Get(browser()->profile())->ExecuteExtensionAction(
578 extension, browser(), true);
580 // It can take a moment for the background page to actually get destroyed
581 // so we wait for the notification before checking that it's really gone
582 // and the badge text has been set.
583 host_destroyed_observer.Wait();
584 ASSERT_FALSE(manager->GetBackgroundHostForExtension(extension->id()));
585 ASSERT_EQ("X", action->GetBadgeText(ExtensionAction::kDefaultTabId));
588 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BadgeBackgroundColor) {
589 ASSERT_TRUE(test_server()->Start());
590 ASSERT_TRUE(RunExtensionTest("browser_action/color")) << message_;
591 const Extension* extension = GetSingleLoadedExtension();
592 ASSERT_TRUE(extension) << message_;
594 // Test that there is a browser action in the toolbar.
595 ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
597 // Test that CSS values (#FF0000) set color correctly.
598 ExtensionAction* action = GetBrowserAction(*extension);
599 ASSERT_EQ(SkColorSetARGB(255, 255, 0, 0),
600 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
602 // Tell the extension to update the browser action state.
603 ResultCatcher catcher;
604 ui_test_utils::NavigateToURL(browser(),
605 GURL(extension->GetResourceURL("update.html")));
606 ASSERT_TRUE(catcher.GetNextResult());
608 // Test that CSS values (#0F0) set color correctly.
609 ASSERT_EQ(SkColorSetARGB(255, 0, 255, 0),
610 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
612 ui_test_utils::NavigateToURL(browser(),
613 GURL(extension->GetResourceURL("update2.html")));
614 ASSERT_TRUE(catcher.GetNextResult());
616 // Test that array values set color correctly.
617 ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255),
618 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
621 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Getters) {
622 ASSERT_TRUE(RunExtensionTest("browser_action/getters")) << message_;
623 const Extension* extension = GetSingleLoadedExtension();
624 ASSERT_TRUE(extension) << message_;
626 // Test that there is a browser action in the toolbar.
627 ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
629 // Test the getters for defaults.
630 ResultCatcher catcher;
631 ui_test_utils::NavigateToURL(browser(),
632 GURL(extension->GetResourceURL("update.html")));
633 ASSERT_TRUE(catcher.GetNextResult());
635 // Test the getters for a specific tab.
636 ui_test_utils::NavigateToURL(browser(),
637 GURL(extension->GetResourceURL("update2.html")));
638 ASSERT_TRUE(catcher.GetNextResult());
641 // Verify triggering browser action.
642 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, TestTriggerBrowserAction) {
643 ASSERT_TRUE(test_server()->Start());
645 ASSERT_TRUE(RunExtensionTest("trigger_actions/browser_action")) << message_;
646 const Extension* extension = GetSingleLoadedExtension();
647 ASSERT_TRUE(extension) << message_;
649 // Test that there is a browser action in the toolbar.
650 ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
652 ui_test_utils::NavigateToURL(
653 browser(),
654 test_server()->GetURL("files/simple.html"));
656 ExtensionAction* browser_action = GetBrowserAction(*extension);
657 EXPECT_TRUE(browser_action != NULL);
659 // Simulate a click on the browser action icon.
661 ResultCatcher catcher;
662 GetBrowserActionsBar()->Press(0);
663 EXPECT_TRUE(catcher.GetNextResult());
666 WebContents* tab =
667 browser()->tab_strip_model()->GetActiveWebContents();
668 EXPECT_TRUE(tab != NULL);
670 // Verify that the browser action turned the background color red.
671 const std::string script =
672 "window.domAutomationController.send(document.body.style."
673 "backgroundColor);";
674 std::string result;
675 EXPECT_TRUE(content::ExecuteScriptAndExtractString(tab, script, &result));
676 EXPECT_EQ(result, "red");
679 } // namespace
680 } // namespace extensions