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"
15 const char kBasicBrowserActionKeybinding
[] = "Ctrl+Shift+F";
16 const char kBasicNamedKeybinding
[] = "Ctrl+Shift+Y";
17 const char kBasicAlternateKeybinding
[] = "Ctrl+Shift+G";
18 const char kBasicNamedCommand
[] = "toggle-feature";
20 // Get another command platform, whcih is used for simulating a command has been
21 // assigned with a shortcut on another platform.
22 std::string
GetAnotherCommandPlatform() {
24 return extensions::manifest_values::kKeybindingPlatformMac
;
25 #elif defined(OS_MACOSX)
26 return extensions::manifest_values::kKeybindingPlatformChromeOs
;
27 #elif defined(OS_CHROMEOS)
28 return extensions::manifest_values::kKeybindingPlatformLinux
;
29 #elif defined(OS_LINUX)
30 return extensions::manifest_values::kKeybindingPlatformWin
;
38 namespace extensions
{
40 typedef ExtensionApiTest CommandServiceTest
;
42 IN_PROC_BROWSER_TEST_F(CommandServiceTest
, RemoveShortcutSurvivesUpdate
) {
43 base::ScopedTempDir scoped_temp_dir
;
44 EXPECT_TRUE(scoped_temp_dir
.CreateUniqueTempDir());
45 base::FilePath pem_path
= test_data_dir_
.
46 AppendASCII("keybinding").AppendASCII("keybinding.pem");
47 base::FilePath path_v1
= PackExtensionWithOptions(
48 test_data_dir_
.AppendASCII("keybinding").AppendASCII("update")
50 scoped_temp_dir
.path().AppendASCII("v1.crx"),
53 base::FilePath path_v2
= PackExtensionWithOptions(
54 test_data_dir_
.AppendASCII("keybinding").AppendASCII("update")
56 scoped_temp_dir
.path().AppendASCII("v2.crx"),
60 ExtensionService
* service
= ExtensionSystem::Get(browser()->profile())->
62 CommandService
* command_service
= CommandService::Get(browser()->profile());
64 const char kId
[] = "pgoakhfeplldmjheffidklpoklkppipp";
66 // Install v1 of the extension.
67 ASSERT_TRUE(InstallExtension(path_v1
, 1));
68 EXPECT_TRUE(service
->GetExtensionById(kId
, false) != NULL
);
70 // Verify it has a command of Alt+Shift+F.
71 ui::Accelerator accelerator
= command_service
->FindCommandByName(
72 kId
, manifest_values::kBrowserActionCommandEvent
).accelerator();
73 EXPECT_EQ(ui::VKEY_F
, accelerator
.key_code());
74 EXPECT_FALSE(accelerator
.IsCtrlDown());
75 EXPECT_TRUE(accelerator
.IsShiftDown());
76 EXPECT_TRUE(accelerator
.IsAltDown());
78 // Remove the keybinding.
79 command_service
->RemoveKeybindingPrefs(
80 kId
, manifest_values::kBrowserActionCommandEvent
);
82 // Verify it got removed.
83 accelerator
= command_service
->FindCommandByName(
84 kId
, manifest_values::kBrowserActionCommandEvent
).accelerator();
85 EXPECT_EQ(ui::VKEY_UNKNOWN
, accelerator
.key_code());
87 // Update to version 2.
88 EXPECT_TRUE(UpdateExtension(kId
, path_v2
, 0));
89 EXPECT_TRUE(service
->GetExtensionById(kId
, false) != NULL
);
91 // Verify it is still set to nothing.
92 accelerator
= command_service
->FindCommandByName(
93 kId
, manifest_values::kBrowserActionCommandEvent
).accelerator();
94 EXPECT_EQ(ui::VKEY_UNKNOWN
, accelerator
.key_code());
97 IN_PROC_BROWSER_TEST_F(CommandServiceTest
,
98 RemoveKeybindingPrefsShouldBePlatformSpecific
) {
99 base::FilePath extension_dir
=
100 test_data_dir_
.AppendASCII("keybinding").AppendASCII("basics");
101 const Extension
* extension
= InstallExtension(extension_dir
, 1);
102 ASSERT_TRUE(extension
);
104 DictionaryPrefUpdate
updater(browser()->profile()->GetPrefs(),
105 prefs::kExtensionCommands
);
106 base::DictionaryValue
* bindings
= updater
.Get();
108 // Simulate command |toggle-feature| has been assigned with a shortcut on
110 std::string anotherPlatformKey
= GetAnotherCommandPlatform() + ":Alt+G";
111 const char kNamedCommandName
[] = "toggle-feature";
112 base::DictionaryValue
* keybinding
= new base::DictionaryValue();
113 keybinding
->SetString("extension", extension
->id());
114 keybinding
->SetString("command_name", kNamedCommandName
);
115 keybinding
->SetBoolean("global", false);
116 bindings
->Set(anotherPlatformKey
, keybinding
);
118 CommandService
* command_service
= CommandService::Get(browser()->profile());
119 command_service
->RemoveKeybindingPrefs(extension
->id(), kNamedCommandName
);
121 // Removal of keybinding preference should be platform-specific, so the key on
122 // another platform should always remained.
123 EXPECT_TRUE(bindings
->HasKey(anotherPlatformKey
));
126 IN_PROC_BROWSER_TEST_F(CommandServiceTest
,
127 GetExtensionActionCommandQueryAll
) {
128 base::FilePath extension_dir
=
129 test_data_dir_
.AppendASCII("keybinding").AppendASCII("basics");
130 const Extension
* extension
= InstallExtension(extension_dir
, 1);
131 ASSERT_TRUE(extension
);
133 CommandService
* command_service
= CommandService::Get(browser()->profile());
138 EXPECT_TRUE(command_service
->GetBrowserActionCommand(
139 extension
->id(), CommandService::ALL
, &command
, &active
));
141 EXPECT_EQ(kBasicBrowserActionKeybinding
,
142 Command::AcceleratorToString(command
.accelerator()));
146 command_service
->UpdateKeybindingPrefs(
147 extension
->id(), manifest_values::kBrowserActionCommandEvent
,
148 kBasicAlternateKeybinding
);
153 EXPECT_TRUE(command_service
->GetBrowserActionCommand(
154 extension
->id(), CommandService::ALL
, &command
, &active
));
156 EXPECT_EQ(kBasicAlternateKeybinding
,
157 Command::AcceleratorToString(command
.accelerator()));
161 command_service
->RemoveKeybindingPrefs(
162 extension
->id(), manifest_values::kBrowserActionCommandEvent
);
167 EXPECT_TRUE(command_service
->GetBrowserActionCommand(
168 extension
->id(), CommandService::ALL
, &command
, &active
));
170 EXPECT_EQ(kBasicBrowserActionKeybinding
,
171 Command::AcceleratorToString(command
.accelerator()));
172 EXPECT_FALSE(active
);
176 IN_PROC_BROWSER_TEST_F(CommandServiceTest
,
177 GetExtensionActionCommandQueryActive
) {
178 base::FilePath extension_dir
=
179 test_data_dir_
.AppendASCII("keybinding").AppendASCII("basics");
180 const Extension
* extension
= InstallExtension(extension_dir
, 1);
181 ASSERT_TRUE(extension
);
183 CommandService
* command_service
= CommandService::Get(browser()->profile());
188 EXPECT_TRUE(command_service
->GetBrowserActionCommand(
189 extension
->id(), CommandService::ACTIVE
, &command
, &active
));
191 EXPECT_EQ(kBasicBrowserActionKeybinding
,
192 Command::AcceleratorToString(command
.accelerator()));
196 command_service
->UpdateKeybindingPrefs(
197 extension
->id(), manifest_values::kBrowserActionCommandEvent
,
198 kBasicAlternateKeybinding
);
203 EXPECT_TRUE(command_service
->GetBrowserActionCommand(
204 extension
->id(), CommandService::ACTIVE
, &command
, &active
));
206 EXPECT_EQ(kBasicAlternateKeybinding
,
207 Command::AcceleratorToString(command
.accelerator()));
211 command_service
->RemoveKeybindingPrefs(
212 extension
->id(), manifest_values::kBrowserActionCommandEvent
);
217 EXPECT_FALSE(command_service
->GetBrowserActionCommand(
218 extension
->id(), CommandService::ACTIVE
, &command
, &active
));
222 IN_PROC_BROWSER_TEST_F(CommandServiceTest
,
223 GetExtensionActionCommandQuerySuggested
) {
224 base::FilePath extension_dir
=
225 test_data_dir_
.AppendASCII("keybinding").AppendASCII("basics");
226 const Extension
* extension
= InstallExtension(extension_dir
, 1);
227 ASSERT_TRUE(extension
);
229 CommandService
* command_service
= CommandService::Get(browser()->profile());
234 EXPECT_TRUE(command_service
->GetBrowserActionCommand(
235 extension
->id(), CommandService::SUGGESTED
, &command
, &active
));
237 EXPECT_EQ(kBasicBrowserActionKeybinding
,
238 Command::AcceleratorToString(command
.accelerator()));
242 command_service
->UpdateKeybindingPrefs(
243 extension
->id(), manifest_values::kBrowserActionCommandEvent
,
244 kBasicAlternateKeybinding
);
249 EXPECT_TRUE(command_service
->GetBrowserActionCommand(
250 extension
->id(), CommandService::SUGGESTED
, &command
, &active
));
252 EXPECT_EQ(kBasicBrowserActionKeybinding
,
253 Command::AcceleratorToString(command
.accelerator()));
254 EXPECT_FALSE(active
);
257 command_service
->RemoveKeybindingPrefs(
258 extension
->id(), manifest_values::kBrowserActionCommandEvent
);
263 EXPECT_TRUE(command_service
->GetBrowserActionCommand(
264 extension
->id(), CommandService::SUGGESTED
, &command
, &active
));
266 EXPECT_EQ(kBasicBrowserActionKeybinding
,
267 Command::AcceleratorToString(command
.accelerator()));
268 EXPECT_FALSE(active
);
272 IN_PROC_BROWSER_TEST_F(CommandServiceTest
,
273 GetNamedCommandsQueryAll
) {
274 base::FilePath extension_dir
=
275 test_data_dir_
.AppendASCII("keybinding").AppendASCII("basics");
276 const Extension
* extension
= InstallExtension(extension_dir
, 1);
277 ASSERT_TRUE(extension
);
279 CommandService
* command_service
= CommandService::Get(browser()->profile());
282 CommandMap command_map
;
283 EXPECT_TRUE(command_service
->GetNamedCommands(
284 extension
->id(), CommandService::ALL
, CommandService::ANY_SCOPE
,
287 ASSERT_EQ(1u, command_map
.count(kBasicNamedCommand
));
288 Command command
= command_map
[kBasicNamedCommand
];
289 EXPECT_EQ(kBasicNamedKeybinding
,
290 Command::AcceleratorToString(command
.accelerator()));
293 command_service
->UpdateKeybindingPrefs(
294 extension
->id(), kBasicNamedCommand
, kBasicAlternateKeybinding
);
297 CommandMap command_map
;
298 EXPECT_TRUE(command_service
->GetNamedCommands(
299 extension
->id(), CommandService::ALL
, CommandService::ANY_SCOPE
,
302 ASSERT_EQ(1u, command_map
.count(kBasicNamedCommand
));
303 Command command
= command_map
[kBasicNamedCommand
];
304 EXPECT_EQ(kBasicAlternateKeybinding
,
305 Command::AcceleratorToString(command
.accelerator()));
308 command_service
->RemoveKeybindingPrefs(extension
->id(), kBasicNamedCommand
);
311 CommandMap command_map
;
312 EXPECT_TRUE(command_service
->GetNamedCommands(
313 extension
->id(), CommandService::ALL
, CommandService::ANY_SCOPE
,
316 ASSERT_EQ(1u, command_map
.count(kBasicNamedCommand
));
317 Command command
= command_map
[kBasicNamedCommand
];
318 EXPECT_EQ(kBasicNamedKeybinding
,
319 Command::AcceleratorToString(command
.accelerator()));
323 IN_PROC_BROWSER_TEST_F(CommandServiceTest
, GetNamedCommandsQueryActive
) {
324 base::FilePath extension_dir
=
325 test_data_dir_
.AppendASCII("keybinding").AppendASCII("basics");
326 const Extension
* extension
= InstallExtension(extension_dir
, 1);
327 ASSERT_TRUE(extension
);
329 CommandService
* command_service
= CommandService::Get(browser()->profile());
332 CommandMap command_map
;
333 EXPECT_TRUE(command_service
->GetNamedCommands(
334 extension
->id(), CommandService::ACTIVE
, CommandService::ANY_SCOPE
,
337 ASSERT_EQ(1u, command_map
.count(kBasicNamedCommand
));
338 Command command
= command_map
[kBasicNamedCommand
];
339 EXPECT_EQ(kBasicNamedKeybinding
,
340 Command::AcceleratorToString(command
.accelerator()));
343 command_service
->UpdateKeybindingPrefs(
344 extension
->id(), kBasicNamedCommand
, kBasicAlternateKeybinding
);
347 CommandMap command_map
;
348 EXPECT_TRUE(command_service
->GetNamedCommands(
349 extension
->id(), CommandService::ACTIVE
, CommandService::ANY_SCOPE
,
352 ASSERT_EQ(1u, command_map
.count(kBasicNamedCommand
));
353 Command command
= command_map
[kBasicNamedCommand
];
354 EXPECT_EQ(kBasicAlternateKeybinding
,
355 Command::AcceleratorToString(command
.accelerator()));
358 command_service
->RemoveKeybindingPrefs(extension
->id(), kBasicNamedCommand
);
361 CommandMap command_map
;
362 command_service
->GetNamedCommands(
363 extension
->id(), CommandService::ACTIVE
, CommandService::ANY_SCOPE
,
365 EXPECT_EQ(0u, command_map
.count(kBasicNamedCommand
));
369 IN_PROC_BROWSER_TEST_F(CommandServiceTest
,
370 GetNamedCommandsQuerySuggested
) {
371 base::FilePath extension_dir
=
372 test_data_dir_
.AppendASCII("keybinding").AppendASCII("basics");
373 const Extension
* extension
= InstallExtension(extension_dir
, 1);
374 ASSERT_TRUE(extension
);
376 CommandService
* command_service
= CommandService::Get(browser()->profile());
379 CommandMap command_map
;
380 EXPECT_TRUE(command_service
->GetNamedCommands(
381 extension
->id(), CommandService::SUGGESTED
, CommandService::ANY_SCOPE
,
384 ASSERT_EQ(1u, command_map
.count(kBasicNamedCommand
));
385 Command command
= command_map
[kBasicNamedCommand
];
386 EXPECT_EQ(kBasicNamedKeybinding
,
387 Command::AcceleratorToString(command
.accelerator()));
390 command_service
->UpdateKeybindingPrefs(
391 extension
->id(), kBasicNamedCommand
, kBasicAlternateKeybinding
);
394 CommandMap command_map
;
395 EXPECT_TRUE(command_service
->GetNamedCommands(
396 extension
->id(), CommandService::SUGGESTED
, CommandService::ANY_SCOPE
,
399 ASSERT_EQ(1u, command_map
.count(kBasicNamedCommand
));
400 Command command
= command_map
[kBasicNamedCommand
];
401 EXPECT_EQ(kBasicNamedKeybinding
,
402 Command::AcceleratorToString(command
.accelerator()));
405 command_service
->RemoveKeybindingPrefs(extension
->id(), kBasicNamedCommand
);
408 CommandMap command_map
;
409 EXPECT_TRUE(command_service
->GetNamedCommands(
410 extension
->id(), CommandService::SUGGESTED
, CommandService::ANY_SCOPE
,
413 ASSERT_EQ(1u, command_map
.count(kBasicNamedCommand
));
414 Command command
= command_map
[kBasicNamedCommand
];
415 EXPECT_EQ(kBasicNamedKeybinding
,
416 Command::AcceleratorToString(command
.accelerator()));
420 } // namespace extensions