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.
5 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h"
7 #include "base/command_line.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/extensions/api/commands/commands_handler.h"
12 #include "chrome/common/extensions/features/feature_channel.h"
13 #include "extensions/common/manifest_constants.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace extensions {
18 namespace errors = manifest_errors;
20 class CommandsManifestTest : public ExtensionManifestTest {
23 TEST_F(CommandsManifestTest, CommandManifestSimple) {
24 #if defined(OS_MACOSX)
25 int ctrl = ui::EF_COMMAND_DOWN;
27 int ctrl = ui::EF_CONTROL_DOWN;
30 const ui::Accelerator ctrl_f = ui::Accelerator(ui::VKEY_F, ctrl);
31 const ui::Accelerator ctrl_shift_f =
32 ui::Accelerator(ui::VKEY_F, ctrl | ui::EF_SHIFT_DOWN);
33 const ui::Accelerator alt_shift_f =
34 ui::Accelerator(ui::VKEY_F, ui::EF_ALT_DOWN | ui::EF_SHIFT_DOWN);
36 scoped_refptr<Extension> extension =
37 LoadAndExpectSuccess("command_simple.json");
38 ASSERT_TRUE(extension.get());
40 const CommandMap* commands = CommandsInfo::GetNamedCommands(extension.get());
41 ASSERT_TRUE(commands);
42 ASSERT_EQ(1u, commands->size());
43 CommandMap::const_iterator iter = commands->begin();
44 ASSERT_TRUE(commands->end() != iter);
45 const Command* named_command = &(*iter).second;
46 ASSERT_STREQ("feature1", named_command->command_name().c_str());
48 base::UTF16ToASCII(named_command->description()).c_str());
49 ASSERT_EQ(ctrl_shift_f, named_command->accelerator());
51 const Command* browser_action =
52 CommandsInfo::GetBrowserActionCommand(extension.get());
53 ASSERT_TRUE(NULL != browser_action);
54 ASSERT_STREQ("_execute_browser_action",
55 browser_action->command_name().c_str());
56 ASSERT_STREQ("", base::UTF16ToASCII(browser_action->description()).c_str());
57 ASSERT_EQ(alt_shift_f, browser_action->accelerator());
59 const Command* page_action =
60 CommandsInfo::GetPageActionCommand(extension.get());
61 ASSERT_TRUE(NULL != page_action);
62 ASSERT_STREQ("_execute_page_action",
63 page_action->command_name().c_str());
64 ASSERT_STREQ("", base::UTF16ToASCII(page_action->description()).c_str());
65 ASSERT_EQ(ctrl_f, page_action->accelerator());
68 TEST_F(CommandsManifestTest, CommandManifestShortcutsTooMany) {
69 LoadAndExpectError("command_too_many.json",
70 errors::kInvalidKeyBindingTooMany);
73 TEST_F(CommandsManifestTest, CommandManifestManyButWithinBounds) {
74 scoped_refptr<Extension> extension =
75 LoadAndExpectSuccess("command_many_but_shortcuts_under_limit.json");
78 TEST_F(CommandsManifestTest, CommandManifestAllowNumbers) {
79 scoped_refptr<Extension> extension =
80 LoadAndExpectSuccess("command_allow_numbers.json");
83 TEST_F(CommandsManifestTest, CommandManifestRejectJustShift) {
84 LoadAndExpectError("command_reject_just_shift.json",
85 errors::kInvalidKeyBinding);
88 TEST_F(CommandsManifestTest, BrowserActionSynthesizesCommand) {
89 scoped_refptr<Extension> extension =
90 LoadAndExpectSuccess("browser_action_synthesizes_command.json");
91 // An extension with a browser action but no extension command specified
92 // should get a command assigned to it.
93 const extensions::Command* command =
94 CommandsInfo::GetBrowserActionCommand(extension.get());
95 ASSERT_TRUE(command != NULL);
96 ASSERT_EQ(ui::VKEY_UNKNOWN, command->accelerator().key_code());
99 // This test makes sure that the "commands" feature and the "commands.global"
100 // property behave as expected on dev and stable (enabled and working on dev,
101 // not working on stable).
102 TEST_F(CommandsManifestTest, ChannelTests) {
103 // This tests the following combinations.
104 // Ext+Command Stable : OK.
105 // Ext+Command+Global Stable : Property is silently ignored (expect success).
106 // App+Command Stable : NOT OK.
107 // App+Command+Global Stable : NOT OK.
109 std::string warning = "'commands' requires Google Chrome dev channel or "
110 "newer, but this is the stable channel.";
111 ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_STABLE);
112 scoped_refptr<Extension> extension1 =
113 LoadAndExpectSuccess("command_ext.json");
114 scoped_refptr<Extension> extension2 =
115 LoadAndExpectSuccess("command_ext_global.json");
116 LoadAndExpectWarning("command_app.json", warning);
117 LoadAndExpectWarning("command_app_global.json", warning);
120 // Ext+Command Dev : OK.
121 // App+Command Dev : OK.
122 // Ext+Command+Global Dev : OK.
123 // App+Command+Global Dev : OK.
125 ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV);
126 scoped_refptr<Extension> extension1 =
127 LoadAndExpectSuccess("command_ext.json");
128 scoped_refptr<Extension> extension2 =
129 LoadAndExpectSuccess("command_app.json");
130 scoped_refptr<Extension> extension3 =
131 LoadAndExpectSuccess("command_ext_global.json");
132 scoped_refptr<Extension> extension4 =
133 LoadAndExpectSuccess("command_app_global.json");
137 TEST_F(CommandsManifestTest, CommandManifestShouldNotCountMediaKeys) {
138 scoped_refptr<Extension> extension =
139 LoadAndExpectSuccess("command_should_not_count_media_keys.json");
142 } // namespace extensions