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 "chrome/common/chrome_switches.h"
10 #include "chrome/common/extensions/api/commands/commands_handler.h"
11 #include "chrome/common/extensions/features/feature_channel.h"
12 #include "extensions/common/manifest_constants.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions
{
17 namespace errors
= manifest_errors
;
19 class CommandsManifestTest
: public ExtensionManifestTest
{
22 TEST_F(CommandsManifestTest
, CommandManifestSimple
) {
23 #if defined(OS_MACOSX)
24 int ctrl
= ui::EF_COMMAND_DOWN
;
26 int ctrl
= ui::EF_CONTROL_DOWN
;
29 const ui::Accelerator ctrl_f
= ui::Accelerator(ui::VKEY_F
, ctrl
);
30 const ui::Accelerator ctrl_shift_f
=
31 ui::Accelerator(ui::VKEY_F
, ctrl
| ui::EF_SHIFT_DOWN
);
32 const ui::Accelerator alt_shift_f
=
33 ui::Accelerator(ui::VKEY_F
, ui::EF_ALT_DOWN
| ui::EF_SHIFT_DOWN
);
35 scoped_refptr
<Extension
> extension
=
36 LoadAndExpectSuccess("command_simple.json");
37 ASSERT_TRUE(extension
.get());
39 const CommandMap
* commands
= CommandsInfo::GetNamedCommands(extension
.get());
40 ASSERT_TRUE(commands
);
41 ASSERT_EQ(1u, commands
->size());
42 CommandMap::const_iterator iter
= commands
->begin();
43 ASSERT_TRUE(commands
->end() != iter
);
44 const Command
* named_command
= &(*iter
).second
;
45 ASSERT_STREQ("feature1", named_command
->command_name().c_str());
46 ASSERT_STREQ("desc", UTF16ToASCII(named_command
->description()).c_str());
47 ASSERT_EQ(ctrl_shift_f
, named_command
->accelerator());
49 const Command
* browser_action
=
50 CommandsInfo::GetBrowserActionCommand(extension
.get());
51 ASSERT_TRUE(NULL
!= browser_action
);
52 ASSERT_STREQ("_execute_browser_action",
53 browser_action
->command_name().c_str());
54 ASSERT_STREQ("", UTF16ToASCII(browser_action
->description()).c_str());
55 ASSERT_EQ(alt_shift_f
, browser_action
->accelerator());
57 const Command
* page_action
=
58 CommandsInfo::GetPageActionCommand(extension
.get());
59 ASSERT_TRUE(NULL
!= page_action
);
60 ASSERT_STREQ("_execute_page_action",
61 page_action
->command_name().c_str());
62 ASSERT_STREQ("", UTF16ToASCII(page_action
->description()).c_str());
63 ASSERT_EQ(ctrl_f
, page_action
->accelerator());
66 TEST_F(CommandsManifestTest
, CommandManifestShortcutsTooMany
) {
67 LoadAndExpectError("command_too_many.json",
68 errors::kInvalidKeyBindingTooMany
);
71 TEST_F(CommandsManifestTest
, CommandManifestManyButWithinBounds
) {
72 scoped_refptr
<Extension
> extension
=
73 LoadAndExpectSuccess("command_many_but_shortcuts_under_limit.json");
76 TEST_F(CommandsManifestTest
, CommandManifestAllowNumbers
) {
77 scoped_refptr
<Extension
> extension
=
78 LoadAndExpectSuccess("command_allow_numbers.json");
81 TEST_F(CommandsManifestTest
, CommandManifestRejectJustShift
) {
82 LoadAndExpectError("command_reject_just_shift.json",
83 errors::kInvalidKeyBinding
);
86 TEST_F(CommandsManifestTest
, BrowserActionSynthesizesCommand
) {
87 scoped_refptr
<Extension
> extension
=
88 LoadAndExpectSuccess("browser_action_synthesizes_command.json");
89 // An extension with a browser action but no extension command specified
90 // should get a command assigned to it.
91 const extensions::Command
* command
=
92 CommandsInfo::GetBrowserActionCommand(extension
.get());
93 ASSERT_TRUE(command
!= NULL
);
94 ASSERT_EQ(ui::VKEY_UNKNOWN
, command
->accelerator().key_code());
97 // This test makes sure that the "commands" feature and the "commands.global"
98 // property behave as expected on dev and stable (enabled and working on dev,
99 // not working on stable).
100 TEST_F(CommandsManifestTest
, ChannelTests
) {
101 // This tests the following combinations.
102 // Ext+Command Stable : OK.
103 // Ext+Command+Global Stable : Property is silently ignored (expect success).
104 // App+Command Stable : NOT OK.
105 // App+Command+Global Stable : NOT OK.
107 std::string warning
= "'commands' requires Google Chrome dev channel or "
108 "newer, but this is the stable channel.";
109 ScopedCurrentChannel
channel(chrome::VersionInfo::CHANNEL_STABLE
);
110 scoped_refptr
<Extension
> extension1
=
111 LoadAndExpectSuccess("command_ext.json");
112 scoped_refptr
<Extension
> extension2
=
113 LoadAndExpectSuccess("command_ext_global.json");
114 LoadAndExpectWarning("command_app.json", warning
);
115 LoadAndExpectWarning("command_app_global.json", warning
);
118 // Ext+Command Dev : OK.
119 // App+Command Dev : OK.
120 // Ext+Command+Global Dev : OK.
121 // App+Command+Global Dev : OK.
123 ScopedCurrentChannel
channel(chrome::VersionInfo::CHANNEL_DEV
);
124 scoped_refptr
<Extension
> extension1
=
125 LoadAndExpectSuccess("command_ext.json");
126 scoped_refptr
<Extension
> extension2
=
127 LoadAndExpectSuccess("command_app.json");
128 scoped_refptr
<Extension
> extension3
=
129 LoadAndExpectSuccess("command_ext_global.json");
130 scoped_refptr
<Extension
> extension4
=
131 LoadAndExpectSuccess("command_app_global.json");
135 } // namespace extensions