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.
4 
5 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
6 #include "chrome/browser/extensions/extension_action_icon_factory.h"
7 #include "chrome/browser/extensions/extension_action_runner.h"
8 #include "chrome/browser/extensions/extension_action_test_util.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_tab_util.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_commands.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/sessions/content/session_tab_helper.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/browser_test.h"
21 #include "content/public/test/browser_test_utils.h"
22 #include "extensions/browser/extension_action.h"
23 #include "extensions/browser/extension_action_manager.h"
24 #include "extensions/browser/extension_system.h"
25 #include "extensions/common/extension.h"
26 #include "extensions/test/result_catcher.h"
27 #include "net/test/embedded_test_server/embedded_test_server.h"
28 
29 using content::WebContents;
30 
31 namespace extensions {
32 namespace {
33 
34 class PageActionApiTest : public ExtensionApiTest {
35  protected:
GetPageAction(const Extension & extension)36   ExtensionAction* GetPageAction(const Extension& extension) {
37     ExtensionAction* extension_action =
38         ExtensionActionManager::Get(browser()->profile())
39             ->GetExtensionAction(extension);
40     return extension_action->action_type() == ActionInfo::TYPE_PAGE
41                ? extension_action
42                : nullptr;
43   }
44 };
45 
IN_PROC_BROWSER_TEST_F(PageActionApiTest,Basic)46 IN_PROC_BROWSER_TEST_F(PageActionApiTest, Basic) {
47   ASSERT_TRUE(embedded_test_server()->Start());
48   ASSERT_TRUE(RunExtensionTest("page_action/basics")) << message_;
49   const Extension* extension = GetSingleLoadedExtension();
50   ASSERT_TRUE(extension) << message_;
51   {
52     // Tell the extension to update the page action state.
53     ResultCatcher catcher;
54     ui_test_utils::NavigateToURL(browser(),
55         GURL(extension->GetResourceURL("update.html")));
56     ASSERT_TRUE(catcher.GetNextResult());
57   }
58 
59   // Test that we received the changes.
60   int tab_id = sessions::SessionTabHelper::FromWebContents(
61                    browser()->tab_strip_model()->GetActiveWebContents())
62                    ->session_id()
63                    .id();
64   ExtensionAction* action = GetPageAction(*extension);
65   ASSERT_TRUE(action);
66   EXPECT_EQ("Modified", action->GetTitle(tab_id));
67 
68   {
69     // Simulate the page action being clicked.
70     ResultCatcher catcher;
71     ExtensionActionRunner::GetForWebContents(
72         browser()->tab_strip_model()->GetActiveWebContents())
73         ->RunAction(extension, true);
74     EXPECT_TRUE(catcher.GetNextResult());
75   }
76 
77   {
78     // Tell the extension to update the page action state again.
79     ResultCatcher catcher;
80     ui_test_utils::NavigateToURL(browser(),
81         GURL(extension->GetResourceURL("update2.html")));
82     ASSERT_TRUE(catcher.GetNextResult());
83   }
84 
85   // We should not be creating icons asynchronously, so we don't need an
86   // observer.
87   ExtensionActionIconFactory icon_factory(profile(), extension, action, NULL);
88 
89   // Test that we received the changes.
90   tab_id = sessions::SessionTabHelper::FromWebContents(
91                browser()->tab_strip_model()->GetActiveWebContents())
92                ->session_id()
93                .id();
94   EXPECT_FALSE(icon_factory.GetIcon(tab_id).IsEmpty());
95 }
96 
97 // Test that calling chrome.pageAction.setPopup() can enable a popup.
IN_PROC_BROWSER_TEST_F(PageActionApiTest,AddPopup)98 IN_PROC_BROWSER_TEST_F(PageActionApiTest, AddPopup) {
99   // Load the extension, which has no default popup.
100   ASSERT_TRUE(RunExtensionTest("page_action/add_popup")) << message_;
101   const Extension* extension = GetSingleLoadedExtension();
102   ASSERT_TRUE(extension) << message_;
103 
104   int tab_id = ExtensionTabUtil::GetTabId(
105       browser()->tab_strip_model()->GetActiveWebContents());
106 
107   ExtensionAction* page_action = GetPageAction(*extension);
108   ASSERT_TRUE(page_action)
109       << "Page action test extension should have a page action.";
110 
111   ASSERT_FALSE(page_action->HasPopup(tab_id));
112 
113   // Simulate the page action being clicked.  The resulting event should
114   // install a page action popup.
115   {
116     ResultCatcher catcher;
117     ExtensionActionRunner::GetForWebContents(
118         browser()->tab_strip_model()->GetActiveWebContents())
119         ->RunAction(extension, true);
120     ASSERT_TRUE(catcher.GetNextResult());
121   }
122 
123   ASSERT_TRUE(page_action->HasPopup(tab_id))
124       << "Clicking on the page action should have caused a popup to be added.";
125 
126   ASSERT_STREQ("/a_popup.html",
127                page_action->GetPopupUrl(tab_id).path().c_str());
128 
129   // Now change the popup from a_popup.html to a_second_popup.html .
130   // Load a page which removes the popup using chrome.pageAction.setPopup().
131   {
132     ResultCatcher catcher;
133     ui_test_utils::NavigateToURL(
134         browser(),
135         GURL(extension->GetResourceURL("change_popup.html")));
136     ASSERT_TRUE(catcher.GetNextResult());
137   }
138 
139   ASSERT_TRUE(page_action->HasPopup(tab_id));
140   ASSERT_STREQ("/another_popup.html",
141                page_action->GetPopupUrl(tab_id).path().c_str());
142 }
143 
144 // Test that calling chrome.pageAction.setPopup() can remove a popup.
IN_PROC_BROWSER_TEST_F(PageActionApiTest,RemovePopup)145 IN_PROC_BROWSER_TEST_F(PageActionApiTest, RemovePopup) {
146   // Load the extension, which has a page action with a default popup.
147   ASSERT_TRUE(RunExtensionTest("page_action/remove_popup")) << message_;
148   const Extension* extension = GetSingleLoadedExtension();
149   ASSERT_TRUE(extension) << message_;
150 
151   int tab_id = ExtensionTabUtil::GetTabId(
152       browser()->tab_strip_model()->GetActiveWebContents());
153 
154   ExtensionAction* page_action = GetPageAction(*extension);
155   ASSERT_TRUE(page_action)
156       << "Page action test extension should have a page action.";
157 
158   ASSERT_TRUE(page_action->HasPopup(tab_id))
159       << "Expect a page action popup before the test removes it.";
160 
161   // Load a page which removes the popup using chrome.pageAction.setPopup().
162   {
163     ResultCatcher catcher;
164     ui_test_utils::NavigateToURL(
165         browser(),
166         GURL(extension->GetResourceURL("remove_popup.html")));
167     ASSERT_TRUE(catcher.GetNextResult());
168   }
169 
170   ASSERT_FALSE(page_action->HasPopup(tab_id))
171       << "Page action popup should have been removed.";
172 }
173 
174 
175 // Test http://crbug.com/57333: that two page action extensions using the same
176 // icon for the page action icon and the extension icon do not crash.
IN_PROC_BROWSER_TEST_F(PageActionApiTest,TestCrash57333)177 IN_PROC_BROWSER_TEST_F(PageActionApiTest, TestCrash57333) {
178   // Load extension A.
179   ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("page_action")
180                                           .AppendASCII("crash_57333")
181                                           .AppendASCII("Extension1")));
182   // Load extension B.
183   ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("page_action")
184                                           .AppendASCII("crash_57333")
185                                           .AppendASCII("Extension2")));
186 }
187 
IN_PROC_BROWSER_TEST_F(PageActionApiTest,Getters)188 IN_PROC_BROWSER_TEST_F(PageActionApiTest, Getters) {
189   ASSERT_TRUE(RunExtensionTest("page_action/getters")) << message_;
190   const Extension* extension = GetSingleLoadedExtension();
191   ASSERT_TRUE(extension) << message_;
192 
193   ResultCatcher catcher;
194   ui_test_utils::NavigateToURL(browser(),
195       GURL(extension->GetResourceURL("update.html")));
196   ASSERT_TRUE(catcher.GetNextResult());
197 }
198 
199 // Verify triggering page action.
IN_PROC_BROWSER_TEST_F(PageActionApiTest,TestTriggerPageAction)200 IN_PROC_BROWSER_TEST_F(PageActionApiTest, TestTriggerPageAction) {
201   ASSERT_TRUE(embedded_test_server()->Start());
202 
203   ASSERT_TRUE(RunExtensionTest("trigger_actions/page_action")) << message_;
204   const Extension* extension = GetSingleLoadedExtension();
205   ASSERT_TRUE(extension) << message_;
206 
207   // Page action icon is displayed when a tab is created.
208   ui_test_utils::NavigateToURL(browser(),
209                                embedded_test_server()->GetURL("/simple.html"));
210   chrome::NewTab(browser());
211   browser()->tab_strip_model()->ActivateTabAt(
212       0, {TabStripModel::GestureType::kOther});
213 
214   // Give the extension time to show the page action on the tab.
215   WaitForPageActionVisibilityChangeTo(1);
216 
217   ExtensionAction* page_action = GetPageAction(*extension);
218   ASSERT_TRUE(page_action);
219 
220   WebContents* tab =
221       browser()->tab_strip_model()->GetActiveWebContents();
222   ASSERT_TRUE(tab);
223 
224   EXPECT_TRUE(page_action->GetIsVisible(ExtensionTabUtil::GetTabId(tab)));
225 
226   {
227     // Simulate the page action being clicked.
228     ResultCatcher catcher;
229     ExtensionActionRunner::GetForWebContents(
230         browser()->tab_strip_model()->GetActiveWebContents())
231         ->RunAction(extension, true);
232     EXPECT_TRUE(catcher.GetNextResult());
233   }
234 
235   // Verify that the browser action turned the background color red.
236   const std::string script =
237       "window.domAutomationController.send(document.body.style."
238       "backgroundColor);";
239   std::string result;
240   EXPECT_TRUE(content::ExecuteScriptAndExtractString(tab, script, &result));
241   EXPECT_EQ(result, "red");
242 }
243 
244 }  // namespace
245 }  // namespace extensions
246