Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/extensions/command.h" | 5 #include "chrome/common/extensions/command.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 | 34 |
| 35 const struct { | 35 const struct { |
| 36 bool expected_result; | 36 bool expected_result; |
| 37 ui::Accelerator accelerator; | 37 ui::Accelerator accelerator; |
| 38 const char* command_name; | 38 const char* command_name; |
| 39 const char* key; | 39 const char* key; |
| 40 const char* description; | 40 const char* description; |
| 41 } kTests[] = { | 41 } kTests[] = { |
| 42 // Negative test (one or more missing required fields). We don't need to | 42 // Negative test (one or more missing required fields). We don't need to |
| 43 // test |command_name| being blank as it is used as a key in the manifest, | 43 // test |command_name| being blank as it is used as a key in the manifest, |
| 44 // so it can't be blank (and we CHECK() when it is). | 44 // so it can't be blank (and we CHECK() when it is). A blank shortcut is |
|
Yoyo Zhou
2013/03/27 00:44:46
You should mention this in the CL description too.
Finnur
2013/03/27 13:00:39
Done.
| |
| 45 // permitted. | |
| 45 { false, none, "command", "", "" }, | 46 { false, none, "command", "", "" }, |
| 46 { false, none, "command", "Ctrl+f", "" }, | 47 { false, none, "command", "Ctrl+f", "" }, |
| 47 { false, none, "command", "", "description" }, | |
| 48 // Ctrl+Alt is not permitted, see MSDN link in comments in Parse function. | 48 // Ctrl+Alt is not permitted, see MSDN link in comments in Parse function. |
| 49 { false, none, "command", "Ctrl+Alt+F", "description" }, | 49 { false, none, "command", "Ctrl+Alt+F", "description" }, |
| 50 // Unsupported shortcuts/too many, or missing modifier. | 50 // Unsupported shortcuts/too many, or missing modifier. |
| 51 { false, none, "command", "A", "description" }, | 51 { false, none, "command", "A", "description" }, |
| 52 { false, none, "command", "F10", "description" }, | 52 { false, none, "command", "F10", "description" }, |
| 53 { false, none, "command", "Ctrl+F+G", "description" }, | 53 { false, none, "command", "Ctrl+F+G", "description" }, |
| 54 { false, none, "command", "Ctrl+Alt+Shift+G", "description" }, | 54 { false, none, "command", "Ctrl+Alt+Shift+G", "description" }, |
| 55 // Shift on its own is not supported. | 55 // Shift on its own is not supported. |
| 56 { false, shift_f, "command", "Shift+F", "description" }, | 56 { false, shift_f, "command", "Shift+F", "description" }, |
| 57 { false, shift_f, "command", "F+Shift", "description" }, | 57 { false, shift_f, "command", "F+Shift", "description" }, |
| 58 // Basic tests. | 58 // Basic tests. |
| 59 { true, none, "command", "", "description" }, | |
| 59 { true, ctrl_f, "command", "Ctrl+F", "description" }, | 60 { true, ctrl_f, "command", "Ctrl+F", "description" }, |
| 60 { true, alt_f, "command", "Alt+F", "description" }, | 61 { true, alt_f, "command", "Alt+F", "description" }, |
| 61 { true, ctrl_shift_f, "command", "Ctrl+Shift+F", "description" }, | 62 { true, ctrl_shift_f, "command", "Ctrl+Shift+F", "description" }, |
| 62 { true, alt_shift_f, "command", "Alt+Shift+F", "description" }, | 63 { true, alt_shift_f, "command", "Alt+Shift+F", "description" }, |
| 63 { true, ctrl_1, "command", "Ctrl+1", "description" }, | 64 { true, ctrl_1, "command", "Ctrl+1", "description" }, |
| 64 // Shortcut token order tests. | 65 // Shortcut token order tests. |
| 65 { true, ctrl_f, "command", "F+Ctrl", "description" }, | 66 { true, ctrl_f, "command", "F+Ctrl", "description" }, |
| 66 { true, alt_f, "command", "F+Alt", "description" }, | 67 { true, alt_f, "command", "F+Alt", "description" }, |
| 67 { true, ctrl_shift_f, "command", "F+Ctrl+Shift", "description" }, | 68 { true, ctrl_shift_f, "command", "F+Ctrl+Shift", "description" }, |
| 68 { true, ctrl_shift_f, "command", "F+Shift+Ctrl", "description" }, | 69 { true, ctrl_shift_f, "command", "F+Shift+Ctrl", "description" }, |
| 69 { true, alt_shift_f, "command", "F+Alt+Shift", "description" }, | 70 { true, alt_shift_f, "command", "F+Alt+Shift", "description" }, |
| 70 { true, alt_shift_f, "command", "F+Shift+Alt", "description" }, | 71 { true, alt_shift_f, "command", "F+Shift+Alt", "description" }, |
| 71 // Case insensitivity is not OK. | 72 // Case insensitivity is not OK. |
| 72 { false, ctrl_f, "command", "Ctrl+f", "description" }, | 73 { false, ctrl_f, "command", "Ctrl+f", "description" }, |
| 73 { false, ctrl_f, "command", "cTrL+F", "description" }, | 74 { false, ctrl_f, "command", "cTrL+F", "description" }, |
| 74 // Skipping description is OK for browser- and pageActions. | 75 // Skipping description is OK for browser- and pageActions. |
| 75 { true, ctrl_f, "_execute_browser_action", "Ctrl+F", "" }, | 76 { true, ctrl_f, "_execute_browser_action", "Ctrl+F", "" }, |
| 76 { true, ctrl_f, "_execute_page_action", "Ctrl+F", "" }, | 77 { true, ctrl_f, "_execute_page_action", "Ctrl+ F", "" }, |
|
Yoyo Zhou
2013/03/27 00:44:46
Is this intentional?
Finnur
2013/03/27 13:00:39
Intentional? Nope. Removed.
| |
| 77 }; | 78 }; |
| 78 | 79 |
| 79 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { | 80 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { |
| 80 // First parse the command as a simple string. | 81 // First parse the command as a simple string. |
| 81 scoped_ptr<DictionaryValue> input(new DictionaryValue); | 82 scoped_ptr<DictionaryValue> input(new DictionaryValue); |
| 82 input->SetString("suggested_key", kTests[i].key); | 83 input->SetString("suggested_key", kTests[i].key); |
| 83 input->SetString("description", kTests[i].description); | 84 input->SetString("description", kTests[i].description); |
| 84 | 85 |
| 85 SCOPED_TRACE(std::string("Command name: |") + kTests[i].command_name + | 86 SCOPED_TRACE(std::string("Command name: |") + kTests[i].command_name + |
| 86 "| key: |" + kTests[i].key + | 87 "| key: |" + kTests[i].key + |
| 87 "| description: |" + kTests[i].description + | 88 "| description: |" + kTests[i].description + |
| 88 "| index: " + base::IntToString(i)); | 89 "| index: " + base::IntToString(i)); |
| 89 | 90 |
| 90 extensions::Command command; | 91 extensions::Command command; |
| 91 string16 error; | 92 string16 error; |
| 92 bool result = | 93 bool result = |
| 93 command.Parse(input.get(), kTests[i].command_name, i, &error); | 94 command.Parse(input.get(), kTests[i].command_name, i, &error); |
| 94 | 95 |
| 95 EXPECT_EQ(kTests[i].expected_result, result); | 96 EXPECT_EQ(kTests[i].expected_result, result); |
| 96 if (result) { | 97 if (result) { |
| 97 EXPECT_STREQ(kTests[i].description, | 98 EXPECT_STREQ(kTests[i].description, |
| 98 UTF16ToASCII(command.description()).c_str()); | 99 UTF16ToASCII(command.description()).c_str()); |
| 99 EXPECT_STREQ(kTests[i].command_name, command.command_name().c_str()); | 100 EXPECT_STREQ(kTests[i].command_name, command.command_name().c_str()); |
| 100 EXPECT_EQ(kTests[i].accelerator, command.accelerator()); | 101 EXPECT_EQ(kTests[i].accelerator, command.accelerator()); |
| 101 } | 102 } |
| 102 | 103 |
| 103 // Now parse the command as a dictionary of multiple values. | 104 // Now parse the command as a dictionary of multiple values. |
| 104 input.reset(new DictionaryValue); | 105 if (kTests[i].key[0] != 0) { |
|
Yoyo Zhou
2013/03/27 00:44:46
'\0' would make it a little clearer here that it's
| |
| 105 DictionaryValue* key_dict = new DictionaryValue(); | 106 input.reset(new DictionaryValue); |
| 106 key_dict->SetString("default", kTests[i].key); | 107 DictionaryValue* key_dict = new DictionaryValue(); |
| 107 key_dict->SetString("windows", kTests[i].key); | 108 key_dict->SetString("default", kTests[i].key); |
| 108 key_dict->SetString("mac", kTests[i].key); | 109 key_dict->SetString("windows", kTests[i].key); |
| 109 input->Set("suggested_key", key_dict); | 110 key_dict->SetString("mac", kTests[i].key); |
| 110 input->SetString("description", kTests[i].description); | 111 input->Set("suggested_key", key_dict); |
| 112 input->SetString("description", kTests[i].description); | |
| 111 | 113 |
| 112 result = command.Parse(input.get(), kTests[i].command_name, i, &error); | 114 result = command.Parse(input.get(), kTests[i].command_name, i, &error); |
| 113 | 115 |
| 114 EXPECT_EQ(kTests[i].expected_result, result); | 116 EXPECT_EQ(kTests[i].expected_result, result); |
| 115 if (result) { | 117 if (result) { |
| 116 EXPECT_STREQ(kTests[i].description, | 118 EXPECT_STREQ(kTests[i].description, |
| 117 UTF16ToASCII(command.description()).c_str()); | 119 UTF16ToASCII(command.description()).c_str()); |
| 118 EXPECT_STREQ(kTests[i].command_name, command.command_name().c_str()); | 120 EXPECT_STREQ(kTests[i].command_name, command.command_name().c_str()); |
| 119 EXPECT_EQ(kTests[i].accelerator, command.accelerator()); | 121 EXPECT_EQ(kTests[i].accelerator, command.accelerator()); |
| 122 } | |
| 120 } | 123 } |
| 121 } | 124 } |
| 122 } | 125 } |
| 123 | 126 |
| 124 TEST(CommandTest, ExtensionCommandParsingFallback) { | 127 TEST(CommandTest, ExtensionCommandParsingFallback) { |
| 125 std::string description = "desc"; | 128 std::string description = "desc"; |
| 126 std::string command_name = "foo"; | 129 std::string command_name = "foo"; |
| 127 | 130 |
| 128 // Test that platform specific keys are honored on each platform, despite | 131 // Test that platform specific keys are honored on each platform, despite |
| 129 // fallback being given. | 132 // fallback being given. |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 key_dict->SetString("windows", "Ctrl+Shift+W"); | 200 key_dict->SetString("windows", "Ctrl+Shift+W"); |
| 198 #endif | 201 #endif |
| 199 EXPECT_FALSE(command.Parse(input.get(), command_name, 0, &error)); | 202 EXPECT_FALSE(command.Parse(input.get(), command_name, 0, &error)); |
| 200 | 203 |
| 201 // Make sure Mac specific keys are not processed on other platforms. | 204 // Make sure Mac specific keys are not processed on other platforms. |
| 202 #if !defined(OS_MACOSX) | 205 #if !defined(OS_MACOSX) |
| 203 key_dict->SetString("windows", "Command+Shift+M"); | 206 key_dict->SetString("windows", "Command+Shift+M"); |
| 204 EXPECT_FALSE(command.Parse(input.get(), command_name, 0, &error)); | 207 EXPECT_FALSE(command.Parse(input.get(), command_name, 0, &error)); |
| 205 #endif | 208 #endif |
| 206 } | 209 } |
| OLD | NEW |