Upstream version 9.38.198.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/api/commands/command_service.h"
8 #include "chrome/browser/extensions/browser_action_test_util.h"
9 #include "chrome/browser/extensions/extension_action.h"
10 #include "chrome/browser/extensions/extension_action_manager.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "chrome/browser/extensions/tab_helper.h"
13 #include "chrome/browser/sessions/session_tab_helper.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/test/base/interactive_test_utils.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/browser_test_utils.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/common/extension.h"
23 #include "extensions/common/feature_switch.h"
24 #include "extensions/common/manifest_constants.h"
25 #include "extensions/common/permissions/permissions_data.h"
26
27 using content::WebContents;
28
29 namespace extensions {
30
31 namespace {
32 // This extension ID is used for tests require a stable ID over multiple
33 // extension installs.
34 const char kId[] = "pgoakhfeplldmjheffidklpoklkppipp";
35
36 // Default keybinding to use for emulating user-defined shortcut overrides. The
37 // test extensions use Alt+Shift+F and Alt+Shift+H.
38 const char kAltShiftG[] = "Alt+Shift+G";
39 }
40
41 class CommandsApiTest : public ExtensionApiTest {
42  public:
43   CommandsApiTest() {}
44   virtual ~CommandsApiTest() {}
45
46  protected:
47   BrowserActionTestUtil GetBrowserActionsBar() {
48     return BrowserActionTestUtil(browser());
49   }
50
51   bool IsGrantedForTab(const Extension* extension,
52                        const content::WebContents* web_contents) {
53     return extension->permissions_data()->HasAPIPermissionForTab(
54         SessionID::IdForTab(web_contents), APIPermission::kTab);
55   }
56 };
57
58 // Test the basic functionality of the Keybinding API:
59 // - That pressing the shortcut keys should perform actions (activate the
60 //   browser action or send an event).
61 // - Note: Page action keybindings are tested in PageAction test below.
62 // - The shortcut keys taken by one extension are not overwritten by the last
63 //   installed extension.
64 IN_PROC_BROWSER_TEST_F(CommandsApiTest, Basic) {
65   ASSERT_TRUE(test_server()->Start());
66   ASSERT_TRUE(RunExtensionTest("keybinding/basics")) << message_;
67   const Extension* extension = GetSingleLoadedExtension();
68   ASSERT_TRUE(extension) << message_;
69
70   // Load this extension, which uses the same keybindings but sets the page
71   // to different colors. This is so we can see that it doesn't interfere. We
72   // don't test this extension in any other way (it should otherwise be
73   // immaterial to this test).
74   ASSERT_TRUE(RunExtensionTest("keybinding/conflicting")) << message_;
75
76   // Test that there are two browser actions in the toolbar.
77   ASSERT_EQ(2, GetBrowserActionsBar().NumberOfBrowserActions());
78
79   ui_test_utils::NavigateToURL(browser(),
80       test_server()->GetURL("files/extensions/test_file.txt"));
81
82   // activeTab shouldn't have been granted yet.
83   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
84   ASSERT_TRUE(tab);
85
86   EXPECT_FALSE(IsGrantedForTab(extension, tab));
87
88   // Activate the shortcut (Ctrl+Shift+F).
89   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
90       browser(), ui::VKEY_F, true, true, false, false));
91
92   // activeTab should now be granted.
93   EXPECT_TRUE(IsGrantedForTab(extension, tab));
94
95   // Verify the command worked.
96   bool result = false;
97   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
98       tab,
99       "setInterval(function(){"
100       "  if(document.body.bgColor == 'red'){"
101       "    window.domAutomationController.send(true)}}, 100)",
102       &result));
103   ASSERT_TRUE(result);
104
105   // Activate the shortcut (Ctrl+Shift+Y).
106   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
107       browser(), ui::VKEY_Y, true, true, false, false));
108
109   result = false;
110   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
111       tab,
112       "setInterval(function(){"
113       "  if(document.body.bgColor == 'blue'){"
114       "    window.domAutomationController.send(true)}}, 100)",
115       &result));
116   ASSERT_TRUE(result);
117 }
118
119 // Flaky on linux and chromeos, http://crbug.com/165825
120 #if defined(OS_MACOSX) || defined(OS_WIN)
121 #define MAYBE_PageAction PageAction
122 #else
123 #define MAYBE_PageAction DISABLED_PageAction
124 #endif
125 IN_PROC_BROWSER_TEST_F(CommandsApiTest, MAYBE_PageAction) {
126   ASSERT_TRUE(test_server()->Start());
127   ASSERT_TRUE(RunExtensionTest("keybinding/page_action")) << message_;
128   const Extension* extension = GetSingleLoadedExtension();
129   ASSERT_TRUE(extension) << message_;
130
131   {
132     // Load a page, the extension will detect the navigation and request to show
133     // the page action icon.
134     ResultCatcher catcher;
135     ui_test_utils::NavigateToURL(browser(),
136         test_server()->GetURL("files/extensions/test_file.txt"));
137     ASSERT_TRUE(catcher.GetNextResult());
138   }
139
140   // Make sure it appears and is the right one.
141   ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
142   int tab_id = SessionTabHelper::FromWebContents(
143       browser()->tab_strip_model()->GetActiveWebContents())->session_id().id();
144   ExtensionAction* action =
145       ExtensionActionManager::Get(browser()->profile())->
146       GetPageAction(*extension);
147   ASSERT_TRUE(action);
148   EXPECT_EQ("Make this page red", action->GetTitle(tab_id));
149
150   // Activate the shortcut (Alt+Shift+F).
151   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
152       browser(), ui::VKEY_F, false, true, true, false));
153
154   // Verify the command worked (the page action turns the page red).
155   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
156   bool result = false;
157   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
158       tab,
159       "setInterval(function(){"
160       "  if(document.body.bgColor == 'red'){"
161       "    window.domAutomationController.send(true)}}, 100)",
162       &result));
163   ASSERT_TRUE(result);
164 }
165
166 // This test validates that the getAll query API function returns registered
167 // commands as well as synthesized ones and that inactive commands (like the
168 // synthesized ones are in nature) have no shortcuts.
169 IN_PROC_BROWSER_TEST_F(CommandsApiTest, SynthesizedCommand) {
170   ASSERT_TRUE(test_server()->Start());
171   ASSERT_TRUE(RunExtensionTest("keybinding/synthesized")) << message_;
172 }
173
174 // This test validates that an extension cannot request a shortcut that is
175 // already in use by Chrome.
176 IN_PROC_BROWSER_TEST_F(CommandsApiTest, DontOverwriteSystemShortcuts) {
177   ASSERT_TRUE(test_server()->Start());
178
179   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
180
181   ASSERT_TRUE(RunExtensionTest("keybinding/dont_overwrite_system")) << message_;
182
183   ui_test_utils::NavigateToURL(browser(),
184       test_server()->GetURL("files/extensions/test_file.txt"));
185
186   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
187   ASSERT_TRUE(tab);
188
189   // Activate the shortcut (Alt+Shift+F) to make the page blue.
190   {
191     ResultCatcher catcher;
192     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
193         browser(), ui::VKEY_F, false, true, true, false));
194     ASSERT_TRUE(catcher.GetNextResult());
195   }
196
197   bool result = false;
198   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
199       tab,
200       "setInterval(function() {"
201       "  if (document.body.bgColor == 'blue') {"
202       "    window.domAutomationController.send(true)}}, 100)",
203       &result));
204   ASSERT_TRUE(result);
205
206   // Activate the bookmark shortcut (Ctrl+D) to make the page green (should not
207   // work without requesting via chrome_settings_overrides).
208 #if defined(OS_MACOSX)
209     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
210         browser(), ui::VKEY_D, false, false, false, true));
211 #else
212     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
213         browser(), ui::VKEY_D, true, false, false, false));
214 #endif
215
216   // The page should still be blue.
217   result = false;
218   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
219       tab,
220       "setInterval(function() {"
221       "  if (document.body.bgColor == 'blue') {"
222       "    window.domAutomationController.send(true)}}, 100)",
223       &result));
224   ASSERT_TRUE(result);
225
226   // Activate the shortcut (Ctrl+F) to make the page red (should not work).
227   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
228       browser(), ui::VKEY_F, true, false, false, false));
229
230   // The page should still be blue.
231   result = false;
232   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
233       tab,
234       "setInterval(function() {"
235       "  if (document.body.bgColor == 'blue') {"
236       "    window.domAutomationController.send(true)}}, 100)",
237       &result));
238   ASSERT_TRUE(result);
239 }
240
241 // This test validates that an extension can override the Chrome bookmark
242 // shortcut if it has requested to do so.
243 IN_PROC_BROWSER_TEST_F(CommandsApiTest, OverwriteBookmarkShortcut) {
244   ASSERT_TRUE(test_server()->Start());
245
246   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
247
248   // This functionality requires a feature flag.
249   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
250       "--enable-override-bookmarks-ui",
251       "1");
252
253   ASSERT_TRUE(RunExtensionTest("keybinding/overwrite_bookmark_shortcut"))
254       << message_;
255
256   ui_test_utils::NavigateToURL(browser(),
257       test_server()->GetURL("files/extensions/test_file.txt"));
258
259   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
260   ASSERT_TRUE(tab);
261
262   // Activate the shortcut (Ctrl+D) to make the page green.
263   {
264     ResultCatcher catcher;
265 #if defined(OS_MACOSX)
266     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
267         browser(), ui::VKEY_D, false, false, false, true));
268 #else
269     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
270         browser(), ui::VKEY_D, true, false, false, false));
271 #endif
272     ASSERT_TRUE(catcher.GetNextResult());
273   }
274
275   bool result = false;
276   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
277       tab,
278       "setInterval(function() {"
279       "  if (document.body.bgColor == 'green') {"
280       "    window.domAutomationController.send(true)}}, 100)",
281       &result));
282   ASSERT_TRUE(result);
283 }
284
285 // This test validates that an extension override of the Chrome bookmark
286 // shortcut does not supersede the same keybinding by web pages.
287 IN_PROC_BROWSER_TEST_F(CommandsApiTest,
288                        OverwriteBookmarkShortcutDoesNotOverrideWebKeybinding) {
289   ASSERT_TRUE(test_server()->Start());
290
291   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
292
293   // This functionality requires a feature flag.
294   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
295       "--enable-override-bookmarks-ui",
296       "1");
297
298   ASSERT_TRUE(RunExtensionTest("keybinding/overwrite_bookmark_shortcut"))
299       << message_;
300
301   ui_test_utils::NavigateToURL(browser(),
302       test_server()->GetURL(
303           "files/extensions/test_file_with_ctrl-d_keybinding.html"));
304
305   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
306   ASSERT_TRUE(tab);
307
308   // Activate the shortcut (Ctrl+D) which should be handled by the page and make
309   // the background color magenta.
310 #if defined(OS_MACOSX)
311   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
312       browser(), ui::VKEY_D, false, false, false, true));
313 #else
314   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
315       browser(), ui::VKEY_D, true, false, false, false));
316 #endif
317
318   bool result = false;
319   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
320       tab,
321       "setInterval(function() {"
322       "  if (document.body.bgColor == 'magenta') {"
323       "    window.domAutomationController.send(true)}}, 100)",
324       &result));
325   ASSERT_TRUE(result);
326 }
327
328 // This test validates that user-set override of the Chrome bookmark shortcut in
329 // an extension that does not request it does supersede the same keybinding by
330 // web pages.
331 IN_PROC_BROWSER_TEST_F(CommandsApiTest,
332                        OverwriteBookmarkShortcutByUserOverridesWebKeybinding) {
333   ASSERT_TRUE(test_server()->Start());
334
335   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
336
337   // This functionality requires a feature flag.
338   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
339       "--enable-override-bookmarks-ui",
340       "1");
341
342   ASSERT_TRUE(RunExtensionTest("keybinding/basics"))
343       << message_;
344
345   CommandService* command_service = CommandService::Get(browser()->profile());
346
347   const Extension* extension = GetSingleLoadedExtension();
348   // Simulate the user setting the keybinding to Ctrl+D.
349 #if defined(OS_MACOSX)
350   const char* hotkey = "Command+D";
351 #else
352   const char* hotkey = "Ctrl+D";
353 #endif  // defined(OS_MACOSX)
354   command_service->UpdateKeybindingPrefs(
355       extension->id(), manifest_values::kBrowserActionCommandEvent, hotkey);
356
357   ui_test_utils::NavigateToURL(browser(),
358       test_server()->GetURL(
359           "files/extensions/test_file_with_ctrl-d_keybinding.html"));
360
361   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
362   ASSERT_TRUE(tab);
363
364   // Activate the shortcut (Ctrl+D) which should be handled by the extension and
365   // make the background color red.
366 #if defined(OS_MACOSX)
367   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
368       browser(), ui::VKEY_D, false, false, false, true));
369 #else
370   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
371       browser(), ui::VKEY_D, true, false, false, false));
372 #endif
373
374   bool result = false;
375   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
376       tab,
377       "setInterval(function() {"
378       "  if (document.body.bgColor == 'red') {"
379       "    window.domAutomationController.send(true)}}, 100)",
380       &result));
381   ASSERT_TRUE(result);
382 }
383
384 #if defined(OS_WIN)
385 // Currently this feature is implemented on Windows only.
386 #define MAYBE_AllowDuplicatedMediaKeys AllowDuplicatedMediaKeys
387 #else
388 #define MAYBE_AllowDuplicatedMediaKeys DISABLED_AllowDuplicatedMediaKeys
389 #endif
390
391 // Test that media keys go to all extensions that register for them.
392 IN_PROC_BROWSER_TEST_F(CommandsApiTest, MAYBE_AllowDuplicatedMediaKeys) {
393   ResultCatcher catcher;
394   ASSERT_TRUE(RunExtensionTest("keybinding/non_global_media_keys_0"))
395       << message_;
396   ASSERT_TRUE(catcher.GetNextResult());
397   ASSERT_TRUE(RunExtensionTest("keybinding/non_global_media_keys_1"))
398       << message_;
399   ASSERT_TRUE(catcher.GetNextResult());
400
401   // Activate the Media Stop key.
402   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
403       browser(), ui::VKEY_MEDIA_STOP, false, false, false, false));
404
405   // We should get two success result.
406   ASSERT_TRUE(catcher.GetNextResult());
407   ASSERT_TRUE(catcher.GetNextResult());
408 }
409
410 IN_PROC_BROWSER_TEST_F(CommandsApiTest, ShortcutAddedOnUpdate) {
411   base::ScopedTempDir scoped_temp_dir;
412   EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
413   base::FilePath pem_path = test_data_dir_.
414       AppendASCII("keybinding").AppendASCII("keybinding.pem");
415   base::FilePath path_v1_unassigned = PackExtensionWithOptions(
416       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
417                     .AppendASCII("v1_unassigned"),
418       scoped_temp_dir.path().AppendASCII("v1_unassigned.crx"),
419       pem_path,
420       base::FilePath());
421   base::FilePath path_v2 = PackExtensionWithOptions(
422       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
423                     .AppendASCII("v2"),
424       scoped_temp_dir.path().AppendASCII("v2.crx"),
425       pem_path,
426       base::FilePath());
427
428   ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
429   CommandService* command_service = CommandService::Get(browser()->profile());
430
431   // Install v1 of the extension without keybinding assigned.
432   ASSERT_TRUE(InstallExtension(path_v1_unassigned, 1));
433   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
434               NULL);
435
436   // Verify it is set to nothing.
437   ui::Accelerator accelerator = command_service->FindCommandByName(
438       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
439   EXPECT_EQ(ui::VKEY_UNKNOWN, accelerator.key_code());
440
441   // Update to version 2 with keybinding.
442   EXPECT_TRUE(UpdateExtension(kId, path_v2, 0));
443   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
444               NULL);
445
446   // Verify it has a command of Alt+Shift+F.
447   accelerator = command_service->FindCommandByName(
448       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
449   EXPECT_EQ(ui::VKEY_F, accelerator.key_code());
450   EXPECT_FALSE(accelerator.IsCtrlDown());
451   EXPECT_TRUE(accelerator.IsShiftDown());
452   EXPECT_TRUE(accelerator.IsAltDown());
453 }
454
455 IN_PROC_BROWSER_TEST_F(CommandsApiTest, ShortcutChangedOnUpdate) {
456   base::ScopedTempDir scoped_temp_dir;
457   EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
458   base::FilePath pem_path = test_data_dir_.
459       AppendASCII("keybinding").AppendASCII("keybinding.pem");
460   base::FilePath path_v1 = PackExtensionWithOptions(
461       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
462                     .AppendASCII("v1"),
463       scoped_temp_dir.path().AppendASCII("v1.crx"),
464       pem_path,
465       base::FilePath());
466   base::FilePath path_v2_reassigned = PackExtensionWithOptions(
467       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
468                     .AppendASCII("v2_reassigned"),
469       scoped_temp_dir.path().AppendASCII("v2_reassigned.crx"),
470       pem_path,
471       base::FilePath());
472
473   ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
474   CommandService* command_service = CommandService::Get(browser()->profile());
475
476   // Install v1 of the extension.
477   ASSERT_TRUE(InstallExtension(path_v1, 1));
478   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
479               NULL);
480
481   // Verify it has a command of Alt+Shift+F.
482   ui::Accelerator accelerator = command_service->FindCommandByName(
483       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
484   EXPECT_EQ(ui::VKEY_F, accelerator.key_code());
485   EXPECT_FALSE(accelerator.IsCtrlDown());
486   EXPECT_TRUE(accelerator.IsShiftDown());
487   EXPECT_TRUE(accelerator.IsAltDown());
488
489   // Update to version 2 with different keybinding assigned.
490   EXPECT_TRUE(UpdateExtension(kId, path_v2_reassigned, 0));
491   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
492               NULL);
493
494   // Verify it has a command of Alt+Shift+H.
495   accelerator = command_service->FindCommandByName(
496       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
497   EXPECT_EQ(ui::VKEY_H, accelerator.key_code());
498   EXPECT_FALSE(accelerator.IsCtrlDown());
499   EXPECT_TRUE(accelerator.IsShiftDown());
500   EXPECT_TRUE(accelerator.IsAltDown());
501 }
502
503 IN_PROC_BROWSER_TEST_F(CommandsApiTest, ShortcutRemovedOnUpdate) {
504   base::ScopedTempDir scoped_temp_dir;
505   EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
506   base::FilePath pem_path = test_data_dir_.
507       AppendASCII("keybinding").AppendASCII("keybinding.pem");
508   base::FilePath path_v1 = PackExtensionWithOptions(
509       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
510                     .AppendASCII("v1"),
511       scoped_temp_dir.path().AppendASCII("v1.crx"),
512       pem_path,
513       base::FilePath());
514   base::FilePath path_v2_unassigned = PackExtensionWithOptions(
515       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
516                     .AppendASCII("v2_unassigned"),
517       scoped_temp_dir.path().AppendASCII("v2_unassigned.crx"),
518       pem_path,
519       base::FilePath());
520
521   ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
522   CommandService* command_service = CommandService::Get(browser()->profile());
523
524   // Install v1 of the extension.
525   ASSERT_TRUE(InstallExtension(path_v1, 1));
526   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
527               NULL);
528
529   // Verify it has a command of Alt+Shift+F.
530   ui::Accelerator accelerator = command_service->FindCommandByName(
531       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
532   EXPECT_EQ(ui::VKEY_F, accelerator.key_code());
533   EXPECT_FALSE(accelerator.IsCtrlDown());
534   EXPECT_TRUE(accelerator.IsShiftDown());
535   EXPECT_TRUE(accelerator.IsAltDown());
536
537   // Update to version 2 without keybinding assigned.
538   EXPECT_TRUE(UpdateExtension(kId, path_v2_unassigned, 0));
539   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
540               NULL);
541
542   // Verify the keybinding gets set to nothing.
543   accelerator = command_service->FindCommandByName(
544       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
545   EXPECT_EQ(ui::VKEY_UNKNOWN, accelerator.key_code());
546 }
547
548 IN_PROC_BROWSER_TEST_F(CommandsApiTest,
549                        ShortcutAddedOnUpdateAfterBeingAssignedByUser) {
550   base::ScopedTempDir scoped_temp_dir;
551   EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
552   base::FilePath pem_path = test_data_dir_.
553       AppendASCII("keybinding").AppendASCII("keybinding.pem");
554   base::FilePath path_v1_unassigned = PackExtensionWithOptions(
555       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
556                     .AppendASCII("v1_unassigned"),
557       scoped_temp_dir.path().AppendASCII("v1_unassigned.crx"),
558       pem_path,
559       base::FilePath());
560   base::FilePath path_v2 = PackExtensionWithOptions(
561       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
562                     .AppendASCII("v2"),
563       scoped_temp_dir.path().AppendASCII("v2.crx"),
564       pem_path,
565       base::FilePath());
566
567   ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
568   CommandService* command_service = CommandService::Get(browser()->profile());
569
570   // Install v1 of the extension without keybinding assigned.
571   ASSERT_TRUE(InstallExtension(path_v1_unassigned, 1));
572   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
573               NULL);
574
575   // Verify it is set to nothing.
576   ui::Accelerator accelerator = command_service->FindCommandByName(
577       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
578   EXPECT_EQ(ui::VKEY_UNKNOWN, accelerator.key_code());
579
580   // Simulate the user setting the keybinding to Alt+Shift+G.
581   command_service->UpdateKeybindingPrefs(
582       kId, manifest_values::kBrowserActionCommandEvent, kAltShiftG);
583
584   // Update to version 2 with keybinding.
585   EXPECT_TRUE(UpdateExtension(kId, path_v2, 0));
586   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
587               NULL);
588
589   // Verify the previously-set keybinding is still set.
590   accelerator = command_service->FindCommandByName(
591       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
592   EXPECT_EQ(ui::VKEY_G, accelerator.key_code());
593   EXPECT_FALSE(accelerator.IsCtrlDown());
594   EXPECT_TRUE(accelerator.IsShiftDown());
595   EXPECT_TRUE(accelerator.IsAltDown());
596 }
597
598 IN_PROC_BROWSER_TEST_F(CommandsApiTest,
599                        ShortcutChangedOnUpdateAfterBeingReassignedByUser) {
600   base::ScopedTempDir scoped_temp_dir;
601   EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
602   base::FilePath pem_path = test_data_dir_.
603       AppendASCII("keybinding").AppendASCII("keybinding.pem");
604   base::FilePath path_v1 = PackExtensionWithOptions(
605       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
606                     .AppendASCII("v1"),
607       scoped_temp_dir.path().AppendASCII("v1.crx"),
608       pem_path,
609       base::FilePath());
610   base::FilePath path_v2_reassigned = PackExtensionWithOptions(
611       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
612                     .AppendASCII("v2_reassigned"),
613       scoped_temp_dir.path().AppendASCII("v2_reassigned.crx"),
614       pem_path,
615       base::FilePath());
616
617   ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
618   CommandService* command_service = CommandService::Get(browser()->profile());
619
620   // Install v1 of the extension.
621   ASSERT_TRUE(InstallExtension(path_v1, 1));
622   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
623               NULL);
624
625   // Verify it has a command of Alt+Shift+F.
626   ui::Accelerator accelerator = command_service->FindCommandByName(
627       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
628   EXPECT_EQ(ui::VKEY_F, accelerator.key_code());
629   EXPECT_FALSE(accelerator.IsCtrlDown());
630   EXPECT_TRUE(accelerator.IsShiftDown());
631   EXPECT_TRUE(accelerator.IsAltDown());
632
633   // Simulate the user setting the keybinding to Alt+Shift+G.
634   command_service->UpdateKeybindingPrefs(
635       kId, manifest_values::kBrowserActionCommandEvent, kAltShiftG);
636
637   // Update to version 2 with different keybinding assigned.
638   EXPECT_TRUE(UpdateExtension(kId, path_v2_reassigned, 0));
639   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
640               NULL);
641
642   // Verify it has a command of Alt+Shift+G.
643   accelerator = command_service->FindCommandByName(
644       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
645   EXPECT_EQ(ui::VKEY_G, accelerator.key_code());
646   EXPECT_FALSE(accelerator.IsCtrlDown());
647   EXPECT_TRUE(accelerator.IsShiftDown());
648   EXPECT_TRUE(accelerator.IsAltDown());
649 }
650
651 IN_PROC_BROWSER_TEST_F(CommandsApiTest,
652                        ShortcutRemovedOnUpdateAfterBeingReassignedByUser) {
653   base::ScopedTempDir scoped_temp_dir;
654   EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
655   base::FilePath pem_path = test_data_dir_.
656       AppendASCII("keybinding").AppendASCII("keybinding.pem");
657   base::FilePath path_v1 = PackExtensionWithOptions(
658       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
659                     .AppendASCII("v1"),
660       scoped_temp_dir.path().AppendASCII("v1.crx"),
661       pem_path,
662       base::FilePath());
663   base::FilePath path_v2_unassigned = PackExtensionWithOptions(
664       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
665                     .AppendASCII("v2_unassigned"),
666       scoped_temp_dir.path().AppendASCII("v2_unassigned.crx"),
667       pem_path,
668       base::FilePath());
669
670   ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
671   CommandService* command_service = CommandService::Get(browser()->profile());
672
673   // Install v1 of the extension.
674   ASSERT_TRUE(InstallExtension(path_v1, 1));
675   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
676               NULL);
677
678   // Verify it has a command of Alt+Shift+F.
679   ui::Accelerator accelerator = command_service->FindCommandByName(
680       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
681   EXPECT_EQ(ui::VKEY_F, accelerator.key_code());
682   EXPECT_FALSE(accelerator.IsCtrlDown());
683   EXPECT_TRUE(accelerator.IsShiftDown());
684   EXPECT_TRUE(accelerator.IsAltDown());
685
686   // Simulate the user reassigning the keybinding to Alt+Shift+G.
687   command_service->UpdateKeybindingPrefs(
688       kId, manifest_values::kBrowserActionCommandEvent, kAltShiftG);
689
690   // Update to version 2 without keybinding assigned.
691   EXPECT_TRUE(UpdateExtension(kId, path_v2_unassigned, 0));
692   EXPECT_TRUE(registry->GetExtensionById(kId, ExtensionRegistry::ENABLED) !=
693               NULL);
694
695   // Verify the keybinding is still set.
696   accelerator = command_service->FindCommandByName(
697       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
698   EXPECT_EQ(ui::VKEY_G, accelerator.key_code());
699   EXPECT_FALSE(accelerator.IsCtrlDown());
700   EXPECT_TRUE(accelerator.IsShiftDown());
701   EXPECT_TRUE(accelerator.IsAltDown());
702 }
703
704 }  // namespace extensions