Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_keybinding_apitest.cc
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 "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"
23
24 using content::WebContents;
25
26 namespace extensions {
27
28 class CommandsApiTest : public ExtensionApiTest {
29  public:
30   CommandsApiTest() {}
31   virtual ~CommandsApiTest() {}
32
33  protected:
34   BrowserActionTestUtil GetBrowserActionsBar() {
35     return BrowserActionTestUtil(browser());
36   }
37
38   bool IsGrantedForTab(const Extension* extension,
39                        const content::WebContents* web_contents) {
40     return PermissionsData::HasAPIPermissionForTab(
41         extension,
42         SessionID::IdForTab(web_contents),
43         APIPermission::kTab);
44   }
45 };
46
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_;
58
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_;
64
65   // Test that there are two browser actions in the toolbar.
66   ASSERT_EQ(2, GetBrowserActionsBar().NumberOfBrowserActions());
67
68   ui_test_utils::NavigateToURL(browser(),
69       test_server()->GetURL("files/extensions/test_file.txt"));
70
71   // activeTab shouldn't have been granted yet.
72   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
73   ASSERT_TRUE(tab);
74
75   EXPECT_FALSE(IsGrantedForTab(extension, tab));
76
77   // Activate the shortcut (Ctrl+Shift+F).
78   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
79       browser(), ui::VKEY_F, true, true, false, false));
80
81   // activeTab should now be granted.
82   EXPECT_TRUE(IsGrantedForTab(extension, tab));
83
84   // Verify the command worked.
85   bool result = false;
86   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
87       tab,
88       "setInterval(function(){"
89       "  if(document.body.bgColor == 'red'){"
90       "    window.domAutomationController.send(true)}}, 100)",
91       &result));
92   ASSERT_TRUE(result);
93
94   // Activate the shortcut (Ctrl+Shift+Y).
95   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
96       browser(), ui::VKEY_Y, true, true, false, false));
97
98   result = false;
99   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
100       tab,
101       "setInterval(function(){"
102       "  if(document.body.bgColor == 'blue'){"
103       "    window.domAutomationController.send(true)}}, 100)",
104       &result));
105   ASSERT_TRUE(result);
106 }
107
108 // Flaky on linux and chromeos, http://crbug.com/165825
109 #if defined(OS_MACOSX) || defined(OS_WIN)
110 #define MAYBE_PageAction PageAction
111 #else
112 #define MAYBE_PageAction DISABLED_PageAction
113 #endif
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_;
119
120   {
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());
127   }
128
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);
136   ASSERT_TRUE(action);
137   EXPECT_EQ("Make this page red", action->GetTitle(tab_id));
138
139   // Activate the shortcut (Alt+Shift+F).
140   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
141       browser(), ui::VKEY_F, false, true, true, false));
142
143   // Verify the command worked (the page action turns the page red).
144   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
145   bool result = false;
146   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
147       tab,
148       "setInterval(function(){"
149       "  if(document.body.bgColor == 'red'){"
150       "    window.domAutomationController.send(true)}}, 100)",
151       &result));
152   ASSERT_TRUE(result);
153 }
154
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
158 #else
159 #define MAYBE_SynthesizedCommand SynthesizedCommand
160 #endif
161
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_;
168 }
169
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
173 #else
174 #define MAYBE_DontOverwriteSystemShortcuts DontOverwriteSystemShortcuts
175 #endif
176
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());
181
182   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
183
184   ASSERT_TRUE(RunExtensionTest("keybinding/dont_overwrite_system")) << message_;
185
186   ui_test_utils::NavigateToURL(browser(),
187       test_server()->GetURL("files/extensions/test_file.txt"));
188
189   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
190   ASSERT_TRUE(tab);
191
192   // Activate the shortcut (Alt+Shift+F) to make the page blue.
193   {
194     ResultCatcher catcher;
195     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
196         browser(), ui::VKEY_F, false, true, true, false));
197     ASSERT_TRUE(catcher.GetNextResult());
198   }
199
200   bool result = false;
201   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
202       tab,
203       "setInterval(function() {"
204       "  if (document.body.bgColor == 'blue') {"
205       "    window.domAutomationController.send(true)}}, 100)",
206       &result));
207   ASSERT_TRUE(result);
208
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));
214 #else
215     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
216         browser(), ui::VKEY_D, true, false, false, false));
217 #endif
218
219   // The page should still be blue.
220   result = false;
221   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
222       tab,
223       "setInterval(function() {"
224       "  if (document.body.bgColor == 'blue') {"
225       "    window.domAutomationController.send(true)}}, 100)",
226       &result));
227   ASSERT_TRUE(result);
228
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));
232
233   // The page should still be blue.
234   result = false;
235   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
236       tab,
237       "setInterval(function() {"
238       "  if (document.body.bgColor == 'blue') {"
239       "    window.domAutomationController.send(true)}}, 100)",
240       &result));
241   ASSERT_TRUE(result);
242 }
243
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());
248
249   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
250
251   // This functionality requires a feature flag.
252   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
253       "--enable-override-bookmarks-ui",
254       "1");
255
256   ASSERT_TRUE(RunExtensionTest("keybinding/overwrite_bookmark_shortcut"))
257       << message_;
258
259   ui_test_utils::NavigateToURL(browser(),
260       test_server()->GetURL("files/extensions/test_file.txt"));
261
262   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
263   ASSERT_TRUE(tab);
264
265   // Activate the shortcut (Ctrl+D) to make the page green.
266   {
267     ResultCatcher catcher;
268 #if defined(OS_MACOSX)
269     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
270         browser(), ui::VKEY_D, false, false, false, true));
271 #else
272     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
273         browser(), ui::VKEY_D, true, false, false, false));
274 #endif
275     ASSERT_TRUE(catcher.GetNextResult());
276   }
277
278   bool result = false;
279   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
280       tab,
281       "setInterval(function() {"
282       "  if (document.body.bgColor == 'green') {"
283       "    window.domAutomationController.send(true)}}, 100)",
284       &result));
285   ASSERT_TRUE(result);
286 }
287
288 #if defined(OS_WIN)
289 // Currently this feature is implemented on Windows only.
290 #define MAYBE_AllowDuplicatedMediaKeys AllowDuplicatedMediaKeys
291 #else
292 #define MAYBE_AllowDuplicatedMediaKeys DISABLED_AllowDuplicatedMediaKeys
293 #endif
294
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"))
299       << message_;
300   ASSERT_TRUE(catcher.GetNextResult());
301   ASSERT_TRUE(RunExtensionTest("keybinding/non_global_media_keys_1"))
302       << message_;
303   ASSERT_TRUE(catcher.GetNextResult());
304
305   // Activate the Media Stop key.
306   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
307       browser(), ui::VKEY_MEDIA_STOP, false, false, false, false));
308
309   // We should get two success result.
310   ASSERT_TRUE(catcher.GetNextResult());
311   ASSERT_TRUE(catcher.GetNextResult());
312 }
313
314 }  // namespace extensions