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/active_tab_permission_granter.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_manager.h"
10 #include "chrome/browser/extensions/extension_apitest.h"
11 #include "chrome/browser/extensions/tab_helper.h"
12 #include "chrome/browser/sessions/session_tab_helper.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/test/base/interactive_test_utils.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/test/browser_test_utils.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/feature_switch.h"
22 #include "extensions/common/permissions/permissions_data.h"
24 using content::WebContents;
26 namespace extensions {
28 class CommandsApiTest : public ExtensionApiTest {
31 virtual ~CommandsApiTest() {}
34 BrowserActionTestUtil GetBrowserActionsBar() {
35 return BrowserActionTestUtil(browser());
38 bool IsGrantedForTab(const Extension* extension,
39 const content::WebContents* web_contents) {
40 return PermissionsData::HasAPIPermissionForTab(
42 SessionID::IdForTab(web_contents),
47 // Test the basic functionality of the Keybinding API:
48 // - That pressing the shortcut keys should perform actions (activate the
49 // browser action or send an event).
50 // - Note: Page action keybindings are tested in PageAction test below.
51 // - The shortcut keys taken by one extension are not overwritten by the last
52 // installed extension.
53 IN_PROC_BROWSER_TEST_F(CommandsApiTest, Basic) {
54 ASSERT_TRUE(test_server()->Start());
55 ASSERT_TRUE(RunExtensionTest("keybinding/basics")) << message_;
56 const Extension* extension = GetSingleLoadedExtension();
57 ASSERT_TRUE(extension) << message_;
59 // Load this extension, which uses the same keybindings but sets the page
60 // to different colors. This is so we can see that it doesn't interfere. We
61 // don't test this extension in any other way (it should otherwise be
62 // immaterial to this test).
63 ASSERT_TRUE(RunExtensionTest("keybinding/conflicting")) << message_;
65 // Test that there are two browser actions in the toolbar.
66 ASSERT_EQ(2, GetBrowserActionsBar().NumberOfBrowserActions());
68 ui_test_utils::NavigateToURL(browser(),
69 test_server()->GetURL("files/extensions/test_file.txt"));
71 // activeTab shouldn't have been granted yet.
72 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
75 EXPECT_FALSE(IsGrantedForTab(extension, tab));
77 // Activate the shortcut (Ctrl+Shift+F).
78 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
79 browser(), ui::VKEY_F, true, true, false, false));
81 // activeTab should now be granted.
82 EXPECT_TRUE(IsGrantedForTab(extension, tab));
84 // Verify the command worked.
86 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
88 "setInterval(function(){"
89 " if(document.body.bgColor == 'red'){"
90 " window.domAutomationController.send(true)}}, 100)",
94 // Activate the shortcut (Ctrl+Shift+Y).
95 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
96 browser(), ui::VKEY_Y, true, true, false, false));
99 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
101 "setInterval(function(){"
102 " if(document.body.bgColor == 'blue'){"
103 " window.domAutomationController.send(true)}}, 100)",
108 // Flaky on linux and chromeos, http://crbug.com/165825
109 #if defined(OS_MACOSX) || defined(OS_WIN)
110 #define MAYBE_PageAction PageAction
112 #define MAYBE_PageAction DISABLED_PageAction
114 IN_PROC_BROWSER_TEST_F(CommandsApiTest, MAYBE_PageAction) {
115 ASSERT_TRUE(test_server()->Start());
116 ASSERT_TRUE(RunExtensionTest("keybinding/page_action")) << message_;
117 const Extension* extension = GetSingleLoadedExtension();
118 ASSERT_TRUE(extension) << message_;
121 // Load a page, the extension will detect the navigation and request to show
122 // the page action icon.
123 ResultCatcher catcher;
124 ui_test_utils::NavigateToURL(browser(),
125 test_server()->GetURL("files/extensions/test_file.txt"));
126 ASSERT_TRUE(catcher.GetNextResult());
129 // Make sure it appears and is the right one.
130 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
131 int tab_id = SessionTabHelper::FromWebContents(
132 browser()->tab_strip_model()->GetActiveWebContents())->session_id().id();
133 ExtensionAction* action =
134 ExtensionActionManager::Get(browser()->profile())->
135 GetPageAction(*extension);
137 EXPECT_EQ("Make this page red", action->GetTitle(tab_id));
139 // Activate the shortcut (Alt+Shift+F).
140 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
141 browser(), ui::VKEY_F, false, true, true, false));
143 // Verify the command worked (the page action turns the page red).
144 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
146 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
148 "setInterval(function(){"
149 " if(document.body.bgColor == 'red'){"
150 " window.domAutomationController.send(true)}}, 100)",
155 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
156 // TODO(erg): linux_aura bringup: http://crbug.com/163931
157 #define MAYBE_SynthesizedCommand DISABLED_SynthesizedCommand
159 #define MAYBE_SynthesizedCommand SynthesizedCommand
162 // This test validates that the getAll query API function returns registered
163 // commands as well as synthesized ones and that inactive commands (like the
164 // synthesized ones are in nature) have no shortcuts.
165 IN_PROC_BROWSER_TEST_F(CommandsApiTest, MAYBE_SynthesizedCommand) {
166 ASSERT_TRUE(test_server()->Start());
167 ASSERT_TRUE(RunExtensionTest("keybinding/synthesized")) << message_;
170 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
171 // TODO(erg): linux_aura bringup: http://crbug.com/163931
172 #define MAYBE_DontOverwriteSystemShortcuts DISABLED_DontOverwriteSystemShortcuts
174 #define MAYBE_DontOverwriteSystemShortcuts DontOverwriteSystemShortcuts
177 // This test validates that an extension cannot request a shortcut that is
178 // already in use by Chrome.
179 IN_PROC_BROWSER_TEST_F(CommandsApiTest, MAYBE_DontOverwriteSystemShortcuts) {
180 ASSERT_TRUE(test_server()->Start());
182 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
184 ASSERT_TRUE(RunExtensionTest("keybinding/dont_overwrite_system")) << message_;
186 ui_test_utils::NavigateToURL(browser(),
187 test_server()->GetURL("files/extensions/test_file.txt"));
189 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
192 // Activate the shortcut (Alt+Shift+F) to make the page blue.
194 ResultCatcher catcher;
195 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
196 browser(), ui::VKEY_F, false, true, true, false));
197 ASSERT_TRUE(catcher.GetNextResult());
201 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
203 "setInterval(function() {"
204 " if (document.body.bgColor == 'blue') {"
205 " window.domAutomationController.send(true)}}, 100)",
209 // Activate the bookmark shortcut (Ctrl+D) to make the page green (should not
210 // work without requesting via chrome_settings_overrides).
211 #if defined(OS_MACOSX)
212 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
213 browser(), ui::VKEY_D, false, false, false, true));
215 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
216 browser(), ui::VKEY_D, true, false, false, false));
219 // The page should still be blue.
221 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
223 "setInterval(function() {"
224 " if (document.body.bgColor == 'blue') {"
225 " window.domAutomationController.send(true)}}, 100)",
229 // Activate the shortcut (Ctrl+F) to make the page red (should not work).
230 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
231 browser(), ui::VKEY_F, true, false, false, false));
233 // The page should still be blue.
235 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
237 "setInterval(function() {"
238 " if (document.body.bgColor == 'blue') {"
239 " window.domAutomationController.send(true)}}, 100)",
244 // This test validates that an extension can override the Chrome bookmark
245 // shortcut if it has requested to do so.
246 IN_PROC_BROWSER_TEST_F(CommandsApiTest, OverwriteBookmarkShortcut) {
247 ASSERT_TRUE(test_server()->Start());
249 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
251 // This functionality requires a feature flag.
252 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
253 "--enable-override-bookmarks-ui",
256 ASSERT_TRUE(RunExtensionTest("keybinding/overwrite_bookmark_shortcut"))
259 ui_test_utils::NavigateToURL(browser(),
260 test_server()->GetURL("files/extensions/test_file.txt"));
262 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
265 // Activate the shortcut (Ctrl+D) to make the page green.
267 ResultCatcher catcher;
268 #if defined(OS_MACOSX)
269 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
270 browser(), ui::VKEY_D, false, false, false, true));
272 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
273 browser(), ui::VKEY_D, true, false, false, false));
275 ASSERT_TRUE(catcher.GetNextResult());
279 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
281 "setInterval(function() {"
282 " if (document.body.bgColor == 'green') {"
283 " window.domAutomationController.send(true)}}, 100)",
289 // Currently this feature is implemented on Windows only.
290 #define MAYBE_AllowDuplicatedMediaKeys AllowDuplicatedMediaKeys
292 #define MAYBE_AllowDuplicatedMediaKeys DISABLED_AllowDuplicatedMediaKeys
295 // Test that media keys go to all extensions that register for them.
296 IN_PROC_BROWSER_TEST_F(CommandsApiTest, MAYBE_AllowDuplicatedMediaKeys) {
297 ResultCatcher catcher;
298 ASSERT_TRUE(RunExtensionTest("keybinding/non_global_media_keys_0"))
300 ASSERT_TRUE(catcher.GetNextResult());
301 ASSERT_TRUE(RunExtensionTest("keybinding/non_global_media_keys_1"))
303 ASSERT_TRUE(catcher.GetNextResult());
305 // Activate the Media Stop key.
306 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
307 browser(), ui::VKEY_MEDIA_STOP, false, false, false, false));
309 // We should get two success result.
310 ASSERT_TRUE(catcher.GetNextResult());
311 ASSERT_TRUE(catcher.GetNextResult());
314 } // namespace extensions