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/active_tab_permission_granter.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_manager.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/extensions/tab_helper.h"
11 #include "chrome/browser/sessions/session_tab_helper.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/extensions/extension.h"
16 #include "chrome/common/extensions/permissions/permissions_data.h"
17 #include "chrome/test/base/interactive_test_utils.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/test/browser_test_utils.h"
23 using content::WebContents;
25 namespace extensions {
27 class CommandsApiTest : public ExtensionApiTest {
30 virtual ~CommandsApiTest() {}
33 BrowserActionTestUtil GetBrowserActionsBar() {
34 return BrowserActionTestUtil(browser());
37 bool IsGrantedForTab(const Extension* extension,
38 const content::WebContents* web_contents) {
39 return PermissionsData::HasAPIPermissionForTab(
41 SessionID::IdForTab(web_contents),
46 class ScriptBadgesCommandsApiTest : public ExtensionApiTest {
48 ScriptBadgesCommandsApiTest() {
49 // We cannot add this to CommandsApiTest because then PageActions get
50 // treated like BrowserActions and the PageAction test starts failing.
51 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
52 switches::kScriptBadges, "1");
54 virtual ~ScriptBadgesCommandsApiTest() {}
57 // Test the basic functionality of the Keybinding API:
58 // - That pressing the shortcut keys should perform actions (activate the
59 // browser action or send an event).
60 // - Note: Page action keybindings are tested in PageAction test below.
61 // - The shortcut keys taken by one extension are not overwritten by the last
62 // installed extension.
63 IN_PROC_BROWSER_TEST_F(CommandsApiTest, Basic) {
64 ASSERT_TRUE(test_server()->Start());
65 ASSERT_TRUE(RunExtensionTest("keybinding/basics")) << message_;
66 const Extension* extension = GetSingleLoadedExtension();
67 ASSERT_TRUE(extension) << message_;
69 // Load this extension, which uses the same keybindings but sets the page
70 // to different colors. This is so we can see that it doesn't interfere. We
71 // don't test this extension in any other way (it should otherwise be
72 // immaterial to this test).
73 ASSERT_TRUE(RunExtensionTest("keybinding/conflicting")) << message_;
75 // Test that there are two browser actions in the toolbar.
76 ASSERT_EQ(2, GetBrowserActionsBar().NumberOfBrowserActions());
78 ui_test_utils::NavigateToURL(browser(),
79 test_server()->GetURL("files/extensions/test_file.txt"));
81 // activeTab shouldn't have been granted yet.
82 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
85 EXPECT_FALSE(IsGrantedForTab(extension, tab));
87 // Activate the shortcut (Ctrl+Shift+F).
88 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
89 browser(), ui::VKEY_F, true, true, false, false));
91 // activeTab should now be granted.
92 EXPECT_TRUE(IsGrantedForTab(extension, tab));
94 // Verify the command worked.
96 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
98 "setInterval(function(){"
99 " if(document.body.bgColor == 'red'){"
100 " window.domAutomationController.send(true)}}, 100)",
104 // Activate the shortcut (Ctrl+Shift+Y).
105 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
106 browser(), ui::VKEY_Y, true, true, false, false));
109 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
111 "setInterval(function(){"
112 " if(document.body.bgColor == 'blue'){"
113 " window.domAutomationController.send(true)}}, 100)",
118 // Flaky on linux and chromeos, http://crbug.com/165825
119 #if defined(OS_MACOSX) || defined(OS_WIN)
120 #define MAYBE_PageAction PageAction
122 #define MAYBE_PageAction DISABLED_PageAction
124 IN_PROC_BROWSER_TEST_F(CommandsApiTest, MAYBE_PageAction) {
125 ASSERT_TRUE(test_server()->Start());
126 ASSERT_TRUE(RunExtensionTest("keybinding/page_action")) << message_;
127 const Extension* extension = GetSingleLoadedExtension();
128 ASSERT_TRUE(extension) << message_;
131 // Load a page, the extension will detect the navigation and request to show
132 // the page action icon.
133 ResultCatcher catcher;
134 ui_test_utils::NavigateToURL(browser(),
135 test_server()->GetURL("files/extensions/test_file.txt"));
136 ASSERT_TRUE(catcher.GetNextResult());
139 // Make sure it appears and is the right one.
140 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
141 int tab_id = SessionTabHelper::FromWebContents(
142 browser()->tab_strip_model()->GetActiveWebContents())->session_id().id();
143 ExtensionAction* action =
144 ExtensionActionManager::Get(browser()->profile())->
145 GetPageAction(*extension);
147 EXPECT_EQ("Make this page red", action->GetTitle(tab_id));
149 // Activate the shortcut (Alt+Shift+F).
150 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
151 browser(), ui::VKEY_F, false, true, true, false));
153 // Verify the command worked (the page action turns the page red).
154 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
156 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
158 "setInterval(function(){"
159 " if(document.body.bgColor == 'red'){"
160 " window.domAutomationController.send(true)}}, 100)",
165 // Checked-in in a disabled state, because the necessary functionality to
166 // automatically verify that the test works hasn't been implemented for the
167 // script badges yet (see http://crbug.com/140016). The test results, can be
168 // verified manually by running the test and verifying that the synthesized
169 // popup for script badges appear. When bug 140016 has been fixed, the popup
170 // code can signal to the test that the test passed.
171 // TODO(finnur): Enable this test once the bug is fixed.
172 IN_PROC_BROWSER_TEST_F(ScriptBadgesCommandsApiTest, DISABLED_ScriptBadge) {
173 ASSERT_TRUE(test_server()->Start());
174 ASSERT_TRUE(RunExtensionTest("keybinding/script_badge")) << message_;
175 const Extension* extension = GetSingleLoadedExtension();
176 ASSERT_TRUE(extension) << message_;
179 ResultCatcher catcher;
180 // Tell the extension to update the script badge state.
181 ui_test_utils::NavigateToURL(
182 browser(), GURL(extension->GetResourceURL("show.html")));
183 ASSERT_TRUE(catcher.GetNextResult());
187 ResultCatcher catcher;
188 // Activate the shortcut (Ctrl+Shift+F).
189 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
190 browser(), ui::VKEY_F, true, true, false, false));
191 ASSERT_TRUE(catcher.GetNextResult());
195 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
196 // TODO(erg): linux_aura bringup: http://crbug.com/163931
197 #define MAYBE_SynthesizedCommand DISABLED_SynthesizedCommand
199 #define MAYBE_SynthesizedCommand SynthesizedCommand
202 // This test validates that the getAll query API function returns registered
203 // commands as well as synthesized ones and that inactive commands (like the
204 // synthesized ones are in nature) have no shortcuts.
205 IN_PROC_BROWSER_TEST_F(CommandsApiTest, MAYBE_SynthesizedCommand) {
206 ASSERT_TRUE(test_server()->Start());
207 ASSERT_TRUE(RunExtensionTest("keybinding/synthesized")) << message_;
210 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
211 // TODO(erg): linux_aura bringup: http://crbug.com/163931
212 #define MAYBE_DontOverwriteSystemShortcuts DISABLED_DontOverwriteSystemShortcuts
214 #define MAYBE_DontOverwriteSystemShortcuts DontOverwriteSystemShortcuts
217 // This test validates that an extension cannot request a shortcut that is
218 // already in use by Chrome.
219 IN_PROC_BROWSER_TEST_F(CommandsApiTest, MAYBE_DontOverwriteSystemShortcuts) {
220 ASSERT_TRUE(test_server()->Start());
222 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
224 ASSERT_TRUE(RunExtensionTest("keybinding/dont_overwrite_system")) << message_;
226 ui_test_utils::NavigateToURL(browser(),
227 test_server()->GetURL("files/extensions/test_file.txt"));
229 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
232 // Activate the shortcut (Alt+Shift+F) to make page blue.
234 ResultCatcher catcher;
235 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
236 browser(), ui::VKEY_F, false, true, true, false));
237 ASSERT_TRUE(catcher.GetNextResult());
241 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
243 "setInterval(function() {"
244 " if (document.body.bgColor == 'blue') {"
245 " window.domAutomationController.send(true)}}, 100)",
249 // Activate the shortcut (Ctrl+F) to make page red (should not work).
250 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
251 browser(), ui::VKEY_F, true, false, false, false));
253 // The page should still be blue.
255 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
257 "setInterval(function() {"
258 " if (document.body.bgColor == 'blue') {"
259 " window.domAutomationController.send(true)}}, 100)",
264 } // namespace extensions