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 "base/command_line.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_prefs.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_system.h"
15 #include "chrome/browser/extensions/extension_tab_util.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/omnibox/location_bar.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/extensions/extension.h"
23 #include "chrome/test/base/ui_test_utils.h"
24 #include "content/public/browser/web_contents.h"
26 // These are a mash-up of the tests from from page_actions_apitest.cc and
27 // browser_actions_apitest.cc.
29 namespace extensions {
32 class PageAsBrowserActionApiTest : public ExtensionApiTest {
34 PageAsBrowserActionApiTest() {}
35 virtual ~PageAsBrowserActionApiTest() {}
37 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
38 ExtensionApiTest::SetUpCommandLine(command_line);
39 command_line->AppendSwitchASCII(switches::kScriptBadges, "1");
43 BrowserActionTestUtil GetBrowserActionsBar() {
44 return BrowserActionTestUtil(browser());
47 ExtensionActionManager* extension_action_manager() {
48 return ExtensionActionManager::Get(browser()->profile());
52 IN_PROC_BROWSER_TEST_F(PageAsBrowserActionApiTest, Basic) {
53 ASSERT_TRUE(test_server()->Start());
54 ASSERT_TRUE(RunExtensionTest("page_action/basics")) << message_;
55 const Extension* extension = GetSingleLoadedExtension();
56 ASSERT_TRUE(extension) << message_;
58 // The extension declares a page action, but it should have gotten a browser
60 ASSERT_TRUE(extension_action_manager()->GetBrowserAction(*extension));
61 ASSERT_FALSE(extension_action_manager()->GetPageAction(*extension));
63 // With the "action box" there won't be browser actions unless they're pinned.
64 ExtensionActionAPI::SetBrowserActionVisibility(
65 extensions::ExtensionSystem::Get(browser()->profile())->
66 extension_service()->extension_prefs(),
70 // Test that there is a browser action in the toolbar.
71 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
74 // Tell the extension to update the page action state.
75 ResultCatcher catcher;
76 ui_test_utils::NavigateToURL(browser(),
77 GURL(extension->GetResourceURL("update.html")));
78 ASSERT_TRUE(catcher.GetNextResult());
81 // Test that we received the changes.
82 int tab_id = ExtensionTabUtil::GetTabId(
83 browser()->tab_strip_model()->GetActiveWebContents());
84 ExtensionAction* action =
85 extension_action_manager()->GetBrowserAction(*extension);
87 EXPECT_EQ("Modified", action->GetTitle(tab_id));
90 // Simulate the page action being clicked.
91 ResultCatcher catcher;
92 ExtensionService* service = extensions::ExtensionSystem::Get(
93 browser()->profile())->extension_service();
94 service->toolbar_model()->ExecuteBrowserAction(
95 extension, browser(), NULL, true);
96 EXPECT_TRUE(catcher.GetNextResult());
100 // Tell the extension to update the page action state again.
101 ResultCatcher catcher;
102 ui_test_utils::NavigateToURL(browser(),
103 GURL(extension->GetResourceURL("update2.html")));
104 ASSERT_TRUE(catcher.GetNextResult());
107 // We should not be creating icons asynchronously, so we don't need an
109 ExtensionActionIconFactory icon_factory(
110 profile(), extension, action, NULL);
112 // Test that we received the changes.
113 EXPECT_FALSE(icon_factory.GetIcon(tab_id).IsEmpty());
116 // Test that calling chrome.pageAction.setPopup() can enable a popup.
117 IN_PROC_BROWSER_TEST_F(PageAsBrowserActionApiTest, AddPopup) {
118 // Load the extension, which has no default popup.
119 ASSERT_TRUE(RunExtensionTest("page_action/add_popup")) << message_;
120 const Extension* extension = GetSingleLoadedExtension();
121 ASSERT_TRUE(extension) << message_;
123 int tab_id = ExtensionTabUtil::GetTabId(
124 browser()->tab_strip_model()->GetActiveWebContents());
126 ExtensionAction* page_action =
127 extension_action_manager()->GetBrowserAction(*extension);
128 ASSERT_TRUE(page_action)
129 << "Page action test extension should have a page action.";
131 ASSERT_FALSE(page_action->HasPopup(tab_id));
133 // Simulate the page action being clicked. The resulting event should
134 // install a page action popup.
136 ResultCatcher catcher;
137 ExtensionService* service = extensions::ExtensionSystem::Get(
138 browser()->profile())->extension_service();
139 service->toolbar_model()->ExecuteBrowserAction(
140 extension, browser(), NULL, true);
141 ASSERT_TRUE(catcher.GetNextResult());
144 ASSERT_TRUE(page_action->HasPopup(tab_id))
145 << "Clicking on the page action should have caused a popup to be added.";
147 ASSERT_STREQ("/a_popup.html",
148 page_action->GetPopupUrl(tab_id).path().c_str());
150 // Now change the popup from a_popup.html to a_second_popup.html .
151 // Load a page which removes the popup using chrome.pageAction.setPopup().
153 ResultCatcher catcher;
154 ui_test_utils::NavigateToURL(
156 GURL(extension->GetResourceURL("change_popup.html")));
157 ASSERT_TRUE(catcher.GetNextResult());
160 ASSERT_TRUE(page_action->HasPopup(tab_id));
161 ASSERT_STREQ("/another_popup.html",
162 page_action->GetPopupUrl(tab_id).path().c_str());
165 // Test that calling chrome.pageAction.setPopup() can remove a popup.
166 IN_PROC_BROWSER_TEST_F(PageAsBrowserActionApiTest, RemovePopup) {
167 // Load the extension, which has a page action with a default popup.
168 ASSERT_TRUE(RunExtensionTest("page_action/remove_popup")) << message_;
169 const Extension* extension = GetSingleLoadedExtension();
170 ASSERT_TRUE(extension) << message_;
172 int tab_id = ExtensionTabUtil::GetTabId(
173 browser()->tab_strip_model()->GetActiveWebContents());
175 ExtensionAction* page_action =
176 extension_action_manager()->GetBrowserAction(*extension);
177 ASSERT_TRUE(page_action)
178 << "Page action test extension should have a page action.";
180 ASSERT_TRUE(page_action->HasPopup(tab_id))
181 << "Expect a page action popup before the test removes it.";
183 // Load a page which removes the popup using chrome.pageAction.setPopup().
185 ResultCatcher catcher;
186 ui_test_utils::NavigateToURL(
188 GURL(extension->GetResourceURL("remove_popup.html")));
189 ASSERT_TRUE(catcher.GetNextResult());
192 ASSERT_FALSE(page_action->HasPopup(tab_id))
193 << "Page action popup should have been removed.";
196 IN_PROC_BROWSER_TEST_F(PageAsBrowserActionApiTest, Getters) {
197 ASSERT_TRUE(RunExtensionTest("page_action/getters")) << message_;
198 const Extension* extension = GetSingleLoadedExtension();
199 ASSERT_TRUE(extension) << message_;
201 ResultCatcher catcher;
202 ui_test_utils::NavigateToURL(browser(),
203 GURL(extension->GetResourceURL("update.html")));
204 ASSERT_TRUE(catcher.GetNextResult());
208 } // namespace extensions