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 "chrome/common/extensions/manifest_tests/chrome_manifest_test.h"
6
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"
15
16namespace extensions {
17
18namespace errors = manifest_errors;
19
20class CommandsManifestTest : public ChromeManifestTest {
21};
22
23TEST_F(CommandsManifestTest, CommandManifestSimple) {
24#if defined(OS_MACOSX)
25  int ctrl = ui::EF_COMMAND_DOWN;
26#else
27  int ctrl = ui::EF_CONTROL_DOWN;
28#endif
29
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);
35
36  scoped_refptr<Extension> extension =
37      LoadAndExpectSuccess("command_simple.json");
38  ASSERT_TRUE(extension.get());
39
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());
47  ASSERT_STREQ("desc",
48               base::UTF16ToASCII(named_command->description()).c_str());
49  ASSERT_EQ(ctrl_shift_f, named_command->accelerator());
50
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());
58
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());
66}
67
68TEST_F(CommandsManifestTest, CommandManifestShortcutsTooMany) {
69  LoadAndExpectError("command_too_many.json",
70                     errors::kInvalidKeyBindingTooMany);
71}
72
73TEST_F(CommandsManifestTest, CommandManifestManyButWithinBounds) {
74  scoped_refptr<Extension> extension =
75      LoadAndExpectSuccess("command_many_but_shortcuts_under_limit.json");
76}
77
78TEST_F(CommandsManifestTest, CommandManifestAllowNumbers) {
79  scoped_refptr<Extension> extension =
80      LoadAndExpectSuccess("command_allow_numbers.json");
81}
82
83TEST_F(CommandsManifestTest, CommandManifestRejectJustShift) {
84  LoadAndExpectError("command_reject_just_shift.json",
85      errors::kInvalidKeyBinding);
86}
87
88TEST_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());
97}
98
99// This test makes sure that the "commands" feature and the "commands.global"
100// property load properly.
101TEST_F(CommandsManifestTest, LoadsOnStable) {
102  scoped_refptr<Extension> extension1 =
103      LoadAndExpectSuccess("command_ext.json");
104  scoped_refptr<Extension> extension2 =
105      LoadAndExpectSuccess("command_app.json");
106  scoped_refptr<Extension> extension3 =
107      LoadAndExpectSuccess("command_ext_global.json");
108  scoped_refptr<Extension> extension4 =
109      LoadAndExpectSuccess("command_app_global.json");
110}
111
112TEST_F(CommandsManifestTest, CommandManifestShouldNotCountMediaKeys) {
113  scoped_refptr<Extension> extension =
114      LoadAndExpectSuccess("command_should_not_count_media_keys.json");
115}
116
117}  // namespace extensions
118