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 "chrome/browser/extensions/api/extension_action/extension_action_api.h"
6 #include "chrome/browser/extensions/extension_action.h"
7 #include "chrome/browser/extensions/extension_action_icon_factory.h"
8 #include "chrome/browser/extensions/extension_action_manager.h"
9 #include "chrome/browser/extensions/extension_action_test_util.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/profiles/profile.h"
14 #include "chrome/browser/sessions/session_tab_helper.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/test/base/ui_test_utils.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/test/browser_test_utils.h"
22 #include "extensions/browser/extension_system.h"
23 #include "extensions/common/extension.h"
24 #include "extensions/test/result_catcher.h"
26 using content::WebContents
;
28 namespace extensions
{
31 class PageActionApiTest
: public ExtensionApiTest
{
33 ExtensionAction
* GetPageAction(const Extension
& extension
) {
34 return ExtensionActionManager::Get(browser()->profile())->
35 GetPageAction(extension
);
39 IN_PROC_BROWSER_TEST_F(PageActionApiTest
, Basic
) {
40 ASSERT_TRUE(test_server()->Start());
41 ASSERT_TRUE(RunExtensionTest("page_action/basics")) << message_
;
42 const Extension
* extension
= GetSingleLoadedExtension();
43 ASSERT_TRUE(extension
) << message_
;
45 // Tell the extension to update the page action state.
46 ResultCatcher catcher
;
47 ui_test_utils::NavigateToURL(browser(),
48 GURL(extension
->GetResourceURL("update.html")));
49 ASSERT_TRUE(catcher
.GetNextResult());
52 // Test that we received the changes.
53 int tab_id
= SessionTabHelper::FromWebContents(
54 browser()->tab_strip_model()->GetActiveWebContents())->session_id().id();
55 ExtensionAction
* action
= GetPageAction(*extension
);
57 EXPECT_EQ("Modified", action
->GetTitle(tab_id
));
60 // Simulate the page action being clicked.
61 ResultCatcher catcher
;
62 ExtensionActionAPI::Get(browser()->profile())->ExecuteExtensionAction(
63 extension
, browser(), true);
64 EXPECT_TRUE(catcher
.GetNextResult());
68 // Tell the extension to update the page action state again.
69 ResultCatcher catcher
;
70 ui_test_utils::NavigateToURL(browser(),
71 GURL(extension
->GetResourceURL("update2.html")));
72 ASSERT_TRUE(catcher
.GetNextResult());
75 // We should not be creating icons asynchronously, so we don't need an
77 ExtensionActionIconFactory
icon_factory(profile(), extension
, action
, NULL
);
79 // Test that we received the changes.
80 tab_id
= SessionTabHelper::FromWebContents(
81 browser()->tab_strip_model()->GetActiveWebContents())->session_id().id();
82 EXPECT_FALSE(icon_factory
.GetIcon(tab_id
).IsEmpty());
85 // Test that calling chrome.pageAction.setPopup() can enable a popup.
86 IN_PROC_BROWSER_TEST_F(PageActionApiTest
, AddPopup
) {
87 // Load the extension, which has no default popup.
88 ASSERT_TRUE(RunExtensionTest("page_action/add_popup")) << message_
;
89 const Extension
* extension
= GetSingleLoadedExtension();
90 ASSERT_TRUE(extension
) << message_
;
92 int tab_id
= ExtensionTabUtil::GetTabId(
93 browser()->tab_strip_model()->GetActiveWebContents());
95 ExtensionAction
* page_action
= GetPageAction(*extension
);
96 ASSERT_TRUE(page_action
)
97 << "Page action test extension should have a page action.";
99 ASSERT_FALSE(page_action
->HasPopup(tab_id
));
101 // Simulate the page action being clicked. The resulting event should
102 // install a page action popup.
104 ResultCatcher catcher
;
105 ExtensionActionAPI::Get(browser()->profile())->ExecuteExtensionAction(
106 extension
, browser(), true);
107 ASSERT_TRUE(catcher
.GetNextResult());
110 ASSERT_TRUE(page_action
->HasPopup(tab_id
))
111 << "Clicking on the page action should have caused a popup to be added.";
113 ASSERT_STREQ("/a_popup.html",
114 page_action
->GetPopupUrl(tab_id
).path().c_str());
116 // Now change the popup from a_popup.html to a_second_popup.html .
117 // Load a page which removes the popup using chrome.pageAction.setPopup().
119 ResultCatcher catcher
;
120 ui_test_utils::NavigateToURL(
122 GURL(extension
->GetResourceURL("change_popup.html")));
123 ASSERT_TRUE(catcher
.GetNextResult());
126 ASSERT_TRUE(page_action
->HasPopup(tab_id
));
127 ASSERT_STREQ("/another_popup.html",
128 page_action
->GetPopupUrl(tab_id
).path().c_str());
131 // Test that calling chrome.pageAction.setPopup() can remove a popup.
132 IN_PROC_BROWSER_TEST_F(PageActionApiTest
, RemovePopup
) {
133 // Load the extension, which has a page action with a default popup.
134 ASSERT_TRUE(RunExtensionTest("page_action/remove_popup")) << message_
;
135 const Extension
* extension
= GetSingleLoadedExtension();
136 ASSERT_TRUE(extension
) << message_
;
138 int tab_id
= ExtensionTabUtil::GetTabId(
139 browser()->tab_strip_model()->GetActiveWebContents());
141 ExtensionAction
* page_action
= GetPageAction(*extension
);
142 ASSERT_TRUE(page_action
)
143 << "Page action test extension should have a page action.";
145 ASSERT_TRUE(page_action
->HasPopup(tab_id
))
146 << "Expect a page action popup before the test removes it.";
148 // Load a page which removes the popup using chrome.pageAction.setPopup().
150 ResultCatcher catcher
;
151 ui_test_utils::NavigateToURL(
153 GURL(extension
->GetResourceURL("remove_popup.html")));
154 ASSERT_TRUE(catcher
.GetNextResult());
157 ASSERT_FALSE(page_action
->HasPopup(tab_id
))
158 << "Page action popup should have been removed.";
161 // Tests popups in page actions.
162 // Flaky on the trybots. See http://crbug.com/96725.
163 IN_PROC_BROWSER_TEST_F(PageActionApiTest
, DISABLED_ShowPageActionPopup
) {
164 ASSERT_TRUE(RunExtensionTest("page_action/popup")) << message_
;
165 const Extension
* extension
= GetSingleLoadedExtension();
166 ASSERT_TRUE(extension
) << message_
;
168 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
171 ResultCatcher catcher
;
172 ExtensionActionAPI::Get(browser()->profile())->ShowExtensionActionPopup(
173 extension
, browser(), true);
174 ASSERT_TRUE(catcher
.GetNextResult());
178 // Test http://crbug.com/57333: that two page action extensions using the same
179 // icon for the page action icon and the extension icon do not crash.
180 IN_PROC_BROWSER_TEST_F(PageActionApiTest
, TestCrash57333
) {
182 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("page_action")
183 .AppendASCII("crash_57333")
184 .AppendASCII("Extension1")));
186 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("page_action")
187 .AppendASCII("crash_57333")
188 .AppendASCII("Extension2")));
191 IN_PROC_BROWSER_TEST_F(PageActionApiTest
, Getters
) {
192 ASSERT_TRUE(RunExtensionTest("page_action/getters")) << message_
;
193 const Extension
* extension
= GetSingleLoadedExtension();
194 ASSERT_TRUE(extension
) << message_
;
196 ResultCatcher catcher
;
197 ui_test_utils::NavigateToURL(browser(),
198 GURL(extension
->GetResourceURL("update.html")));
199 ASSERT_TRUE(catcher
.GetNextResult());
202 // Verify triggering page action.
203 IN_PROC_BROWSER_TEST_F(PageActionApiTest
, TestTriggerPageAction
) {
204 ASSERT_TRUE(test_server()->Start());
206 ASSERT_TRUE(RunExtensionTest("trigger_actions/page_action")) << message_
;
207 const Extension
* extension
= GetSingleLoadedExtension();
208 ASSERT_TRUE(extension
) << message_
;
210 // Page action icon is displayed when a tab is created.
211 ui_test_utils::NavigateToURL(browser(),
212 test_server()->GetURL("files/simple.html"));
213 chrome::NewTab(browser());
214 browser()->tab_strip_model()->ActivateTabAt(0, true);
216 // Give the extension time to show the page action on the tab.
217 WaitForPageActionVisibilityChangeTo(1);
219 ExtensionAction
* page_action
= GetPageAction(*extension
);
220 ASSERT_TRUE(page_action
);
223 browser()->tab_strip_model()->GetActiveWebContents();
226 EXPECT_TRUE(page_action
->GetIsVisible(ExtensionTabUtil::GetTabId(tab
)));
229 // Simulate the page action being clicked.
230 ResultCatcher catcher
;
231 ExtensionActionAPI::Get(browser()->profile())->ExecuteExtensionAction(
232 extension
, browser(), true);
233 EXPECT_TRUE(catcher
.GetNextResult());
236 // Verify that the browser action turned the background color red.
237 const std::string script
=
238 "window.domAutomationController.send(document.body.style."
241 EXPECT_TRUE(content::ExecuteScriptAndExtractString(tab
, script
, &result
));
242 EXPECT_EQ(result
, "red");
246 } // namespace extensions