- add sources.
[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 "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"
22
23 using content::WebContents;
24
25 namespace extensions {
26
27 class CommandsApiTest : public ExtensionApiTest {
28  public:
29   CommandsApiTest() {}
30   virtual ~CommandsApiTest() {}
31
32  protected:
33   BrowserActionTestUtil GetBrowserActionsBar() {
34     return BrowserActionTestUtil(browser());
35   }
36
37   bool IsGrantedForTab(const Extension* extension,
38                        const content::WebContents* web_contents) {
39     return PermissionsData::HasAPIPermissionForTab(
40         extension,
41         SessionID::IdForTab(web_contents),
42         APIPermission::kTab);
43   }
44 };
45
46 class ScriptBadgesCommandsApiTest : public ExtensionApiTest {
47  public:
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");
53   }
54   virtual ~ScriptBadgesCommandsApiTest() {}
55 };
56
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_;
68
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_;
74
75   // Test that there are two browser actions in the toolbar.
76   ASSERT_EQ(2, GetBrowserActionsBar().NumberOfBrowserActions());
77
78   ui_test_utils::NavigateToURL(browser(),
79       test_server()->GetURL("files/extensions/test_file.txt"));
80
81   // activeTab shouldn't have been granted yet.
82   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
83   ASSERT_TRUE(tab);
84
85   EXPECT_FALSE(IsGrantedForTab(extension, tab));
86
87   // Activate the shortcut (Ctrl+Shift+F).
88   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
89       browser(), ui::VKEY_F, true, true, false, false));
90
91   // activeTab should now be granted.
92   EXPECT_TRUE(IsGrantedForTab(extension, tab));
93
94   // Verify the command worked.
95   bool result = false;
96   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
97       tab,
98       "setInterval(function(){"
99       "  if(document.body.bgColor == 'red'){"
100       "    window.domAutomationController.send(true)}}, 100)",
101       &result));
102   ASSERT_TRUE(result);
103
104   // Activate the shortcut (Ctrl+Shift+Y).
105   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
106       browser(), ui::VKEY_Y, true, true, false, false));
107
108   result = false;
109   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
110       tab,
111       "setInterval(function(){"
112       "  if(document.body.bgColor == 'blue'){"
113       "    window.domAutomationController.send(true)}}, 100)",
114       &result));
115   ASSERT_TRUE(result);
116 }
117
118 // Flaky on linux and chromeos, http://crbug.com/165825
119 #if defined(OS_MACOSX) || defined(OS_WIN)
120 #define MAYBE_PageAction PageAction
121 #else
122 #define MAYBE_PageAction DISABLED_PageAction
123 #endif
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_;
129
130   {
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());
137   }
138
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);
146   ASSERT_TRUE(action);
147   EXPECT_EQ("Make this page red", action->GetTitle(tab_id));
148
149   // Activate the shortcut (Alt+Shift+F).
150   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
151       browser(), ui::VKEY_F, false, true, true, false));
152
153   // Verify the command worked (the page action turns the page red).
154   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
155   bool result = false;
156   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
157       tab,
158       "setInterval(function(){"
159       "  if(document.body.bgColor == 'red'){"
160       "    window.domAutomationController.send(true)}}, 100)",
161       &result));
162   ASSERT_TRUE(result);
163 }
164
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_;
177
178   {
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());
184   }
185
186   {
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());
192   }
193 }
194
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
198 #else
199 #define MAYBE_SynthesizedCommand SynthesizedCommand
200 #endif
201
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_;
208 }
209
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
213 #else
214 #define MAYBE_DontOverwriteSystemShortcuts DontOverwriteSystemShortcuts
215 #endif
216
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());
221
222   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
223
224   ASSERT_TRUE(RunExtensionTest("keybinding/dont_overwrite_system")) << message_;
225
226   ui_test_utils::NavigateToURL(browser(),
227       test_server()->GetURL("files/extensions/test_file.txt"));
228
229   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
230   ASSERT_TRUE(tab);
231
232   // Activate the shortcut (Alt+Shift+F) to make page blue.
233   {
234     ResultCatcher catcher;
235     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
236         browser(), ui::VKEY_F, false, true, true, false));
237     ASSERT_TRUE(catcher.GetNextResult());
238   }
239
240   bool result = false;
241   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
242       tab,
243       "setInterval(function() {"
244       "  if (document.body.bgColor == 'blue') {"
245       "    window.domAutomationController.send(true)}}, 100)",
246       &result));
247   ASSERT_TRUE(result);
248
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));
252
253   // The page should still be blue.
254   result = false;
255   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
256       tab,
257       "setInterval(function() {"
258       "  if (document.body.bgColor == 'blue') {"
259       "    window.domAutomationController.send(true)}}, 100)",
260       &result));
261   ASSERT_TRUE(result);
262 }
263
264 }  // namespace extensions