1 // Copyright (c) 2013 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/prefs/scoped_user_pref_update.h"
6 #include "chrome/browser/extensions/api/commands/command_service.h"
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/common/pref_names.h"
10 #include "content/public/test/browser_test.h"
11 #include "content/public/test/test_utils.h"
12 #include "extensions/common/manifest_constants.h"
16 // Get another command platform, whcih is used for simulating a command has been
17 // assigned with a shortcut on another platform.
18 std::string
GetAnotherCommandPlatform() {
20 return extensions::manifest_values::kKeybindingPlatformMac
;
21 #elif defined(OS_MACOSX)
22 return extensions::manifest_values::kKeybindingPlatformChromeOs
;
23 #elif defined(OS_CHROMEOS)
24 return extensions::manifest_values::kKeybindingPlatformLinux
;
25 #elif defined(OS_LINUX)
26 return extensions::manifest_values::kKeybindingPlatformWin
;
34 namespace extensions
{
36 typedef ExtensionApiTest CommandServiceTest
;
38 IN_PROC_BROWSER_TEST_F(CommandServiceTest
, RemoveShortcutSurvivesUpdate
) {
39 base::ScopedTempDir scoped_temp_dir
;
40 EXPECT_TRUE(scoped_temp_dir
.CreateUniqueTempDir());
41 base::FilePath pem_path
= test_data_dir_
.
42 AppendASCII("keybinding").AppendASCII("keybinding.pem");
43 base::FilePath path_v1
= PackExtensionWithOptions(
44 test_data_dir_
.AppendASCII("keybinding").AppendASCII("update")
46 scoped_temp_dir
.path().AppendASCII("v1.crx"),
49 base::FilePath path_v2
= PackExtensionWithOptions(
50 test_data_dir_
.AppendASCII("keybinding").AppendASCII("update")
52 scoped_temp_dir
.path().AppendASCII("v2.crx"),
56 ExtensionService
* service
= ExtensionSystem::Get(browser()->profile())->
58 CommandService
* command_service
= CommandService::Get(browser()->profile());
60 const char kId
[] = "pgoakhfeplldmjheffidklpoklkppipp";
62 // Install v1 of the extension.
63 ASSERT_TRUE(InstallExtension(path_v1
, 1));
64 EXPECT_TRUE(service
->GetExtensionById(kId
, false) != NULL
);
66 // Verify it has a command of Alt+Shift+F.
67 ui::Accelerator accelerator
= command_service
->FindCommandByName(
68 kId
, manifest_values::kBrowserActionCommandEvent
).accelerator();
69 EXPECT_EQ(ui::VKEY_F
, accelerator
.key_code());
70 EXPECT_FALSE(accelerator
.IsCtrlDown());
71 EXPECT_TRUE(accelerator
.IsShiftDown());
72 EXPECT_TRUE(accelerator
.IsAltDown());
74 // Remove the keybinding.
75 command_service
->RemoveKeybindingPrefs(
76 kId
, manifest_values::kBrowserActionCommandEvent
);
78 // Verify it got removed.
79 accelerator
= command_service
->FindCommandByName(
80 kId
, manifest_values::kBrowserActionCommandEvent
).accelerator();
81 EXPECT_EQ(ui::VKEY_UNKNOWN
, accelerator
.key_code());
83 // Update to version 2.
84 EXPECT_TRUE(UpdateExtension(kId
, path_v2
, 0));
85 EXPECT_TRUE(service
->GetExtensionById(kId
, false) != NULL
);
87 // Verify it is still set to nothing.
88 accelerator
= command_service
->FindCommandByName(
89 kId
, manifest_values::kBrowserActionCommandEvent
).accelerator();
90 EXPECT_EQ(ui::VKEY_UNKNOWN
, accelerator
.key_code());
93 IN_PROC_BROWSER_TEST_F(CommandServiceTest
,
94 RemoveKeybindingPrefsShouldBePlatformSpecific
) {
95 base::FilePath extension_dir
=
96 test_data_dir_
.AppendASCII("keybinding").AppendASCII("basics");
97 const Extension
* extension
= InstallExtension(extension_dir
, 1);
98 ASSERT_TRUE(extension
);
100 DictionaryPrefUpdate
updater(browser()->profile()->GetPrefs(),
101 prefs::kExtensionCommands
);
102 base::DictionaryValue
* bindings
= updater
.Get();
104 // Simulate command |toggle-feature| has been assigned with a shortcut on
106 std::string anotherPlatformKey
= GetAnotherCommandPlatform() + ":Alt+G";
107 const char kNamedCommandName
[] = "toggle-feature";
108 base::DictionaryValue
* keybinding
= new base::DictionaryValue();
109 keybinding
->SetString("extension", extension
->id());
110 keybinding
->SetString("command_name", kNamedCommandName
);
111 keybinding
->SetBoolean("global", false);
112 bindings
->Set(anotherPlatformKey
, keybinding
);
114 CommandService
* command_service
= CommandService::Get(browser()->profile());
115 command_service
->RemoveKeybindingPrefs(extension
->id(), kNamedCommandName
);
117 // Removal of keybinding preference should be platform-specific, so the key on
118 // another platform should always remained.
119 EXPECT_TRUE(bindings
->HasKey(anotherPlatformKey
));
122 } // namespace extensions