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/extension.h" | 5 #include "chrome/common/extensions/extension.h" |
6 | 6 |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
(...skipping 952 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
963 { true, CtrlF, "command", "F+Ctrl", "description" }, | 963 { true, CtrlF, "command", "F+Ctrl", "description" }, |
964 { true, ShiftF, "command", "F+Shift", "description" }, | 964 { true, ShiftF, "command", "F+Shift", "description" }, |
965 { true, AltF, "command", "F+Alt", "description" }, | 965 { true, AltF, "command", "F+Alt", "description" }, |
966 { true, CtrlShiftF, "command", "F+Ctrl+Shift", "description" }, | 966 { true, CtrlShiftF, "command", "F+Ctrl+Shift", "description" }, |
967 { true, CtrlShiftF, "command", "F+Shift+Ctrl", "description" }, | 967 { true, CtrlShiftF, "command", "F+Shift+Ctrl", "description" }, |
968 { true, AltShiftF, "command", "F+Alt+Shift", "description" }, | 968 { true, AltShiftF, "command", "F+Alt+Shift", "description" }, |
969 { true, AltShiftF, "command", "F+Shift+Alt", "description" }, | 969 { true, AltShiftF, "command", "F+Shift+Alt", "description" }, |
970 // Case insensitivity is OK. | 970 // Case insensitivity is OK. |
971 { true, CtrlF, "command", "Ctrl+F", "description" }, | 971 { true, CtrlF, "command", "Ctrl+F", "description" }, |
972 { true, CtrlF, "command", "cTrL+f", "description" }, | 972 { true, CtrlF, "command", "cTrL+f", "description" }, |
973 // Extra spaces are fine. | |
974 { true, CtrlShiftF, "command", " Ctrl + Shift +F", "description" }, | |
975 // Minus is equivalent to plus. | |
976 { true, CtrlShiftF, "command", "Ctrl+Shift-F", "description" }, | |
977 // Skipping description is OK for browser- and pageActions. | 973 // Skipping description is OK for browser- and pageActions. |
978 { true, CtrlF, "browserAction", "Ctrl+F", "" }, | 974 { true, CtrlF, "_execute_browser_action", "Ctrl+F", "" }, |
979 { true, CtrlF, "pageAction", "Ctrl+F", "" }, | 975 { true, CtrlF, "_execute_page_action", "Ctrl+F", "" }, |
980 }; | 976 }; |
981 | 977 |
982 // TODO(finnur): test Command/Options on Mac when implemented. | 978 // TODO(finnur): test Command/Options on Mac when implemented. |
983 | 979 |
984 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { | 980 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { |
| 981 // First parse the command as a simple string. |
985 scoped_ptr<DictionaryValue> command(new DictionaryValue); | 982 scoped_ptr<DictionaryValue> command(new DictionaryValue); |
986 command->SetString("key", kTests[i].key); | 983 command->SetString("suggested_key", kTests[i].key); |
987 command->SetString("description", kTests[i].description); | 984 command->SetString("description", kTests[i].description); |
988 | 985 |
| 986 SCOPED_TRACE(std::string("Command name: |") + kTests[i].command_name + |
| 987 "| key: |" + kTests[i].key + |
| 988 "| description: |" + kTests[i].description + |
| 989 "| index: " + base::IntToString(i)); |
| 990 |
989 Extension::ExtensionKeybinding keybinding; | 991 Extension::ExtensionKeybinding keybinding; |
990 string16 error; | 992 string16 error; |
991 bool result = | 993 bool result = |
992 keybinding.Parse(command.get(), kTests[i].command_name, i, &error); | 994 keybinding.Parse(command.get(), kTests[i].command_name, i, &error); |
993 | 995 |
994 SCOPED_TRACE(std::string("Command name: |") + kTests[i].command_name + | 996 EXPECT_EQ(kTests[i].expected_result, result); |
995 "| key: |" + kTests[i].key + | 997 if (result) { |
996 "| description: |" + kTests[i].description + | 998 EXPECT_STREQ(kTests[i].description, keybinding.description().c_str()); |
997 "| index: " + base::IntToString(i)); | 999 EXPECT_STREQ(kTests[i].command_name, keybinding.command_name().c_str()); |
| 1000 EXPECT_EQ(kTests[i].accelerator, keybinding.accelerator()); |
| 1001 } |
| 1002 |
| 1003 // Now parse the command as a dictionary of multiple values. |
| 1004 command.reset(new DictionaryValue); |
| 1005 DictionaryValue* key_dict = new DictionaryValue(); |
| 1006 key_dict->SetString("default", kTests[i].key); |
| 1007 key_dict->SetString("windows", kTests[i].key); |
| 1008 key_dict->SetString("mac", kTests[i].key); |
| 1009 command->Set("suggested_key", key_dict); |
| 1010 command->SetString("description", kTests[i].description); |
| 1011 |
| 1012 result = keybinding.Parse(command.get(), kTests[i].command_name, i, &error); |
998 | 1013 |
999 EXPECT_EQ(kTests[i].expected_result, result); | 1014 EXPECT_EQ(kTests[i].expected_result, result); |
1000 if (result) { | 1015 if (result) { |
1001 EXPECT_STREQ(kTests[i].description, keybinding.description().c_str()); | 1016 EXPECT_STREQ(kTests[i].description, keybinding.description().c_str()); |
1002 EXPECT_STREQ(kTests[i].command_name, keybinding.command_name().c_str()); | 1017 EXPECT_STREQ(kTests[i].command_name, keybinding.command_name().c_str()); |
1003 EXPECT_EQ(kTests[i].accelerator, keybinding.accelerator()); | 1018 EXPECT_EQ(kTests[i].accelerator, keybinding.accelerator()); |
1004 } | 1019 } |
1005 } | 1020 } |
1006 } | 1021 } |
1007 | 1022 |
| 1023 TEST(ExtensionTest, ExtensionKeybindingParsingFallback) { |
| 1024 std::string description = "desc"; |
| 1025 std::string command_name = "foo"; |
| 1026 |
| 1027 // Test that platform specific keys are honored on each platform, despite |
| 1028 // fallback being given. |
| 1029 scoped_ptr<DictionaryValue> command(new DictionaryValue); |
| 1030 DictionaryValue* key_dict = new DictionaryValue(); |
| 1031 key_dict->SetString("default", "Ctrl+Shift+D"); |
| 1032 key_dict->SetString("windows", "Ctrl+Shift+W"); |
| 1033 key_dict->SetString("mac", "Ctrl+Shift+M"); |
| 1034 key_dict->SetString("linux", "Ctrl+Shift+L"); |
| 1035 key_dict->SetString("chromeos", "Ctrl+Shift+C"); |
| 1036 command->Set("suggested_key", key_dict); |
| 1037 command->SetString("description", description); |
| 1038 |
| 1039 Extension::ExtensionKeybinding keybinding; |
| 1040 string16 error; |
| 1041 EXPECT_TRUE(keybinding.Parse(command.get(), command_name, 0, &error)); |
| 1042 EXPECT_STREQ(description.c_str(), keybinding.description().c_str()); |
| 1043 EXPECT_STREQ(command_name.c_str(), keybinding.command_name().c_str()); |
| 1044 |
| 1045 #if defined(OS_WIN) |
| 1046 ui::Accelerator accelerator(ui::VKEY_W, true, true, false); |
| 1047 #elif defined(OS_MACOSX) |
| 1048 ui::Accelerator accelerator(ui::VKEY_M, true, true, false); |
| 1049 #elif defined(OS_CHROMEOS) |
| 1050 ui::Accelerator accelerator(ui::VKEY_C, true, true, false); |
| 1051 #elif defined(OS_LINUX) |
| 1052 ui::Accelerator accelerator(ui::VKEY_L, true, true, false); |
| 1053 #else |
| 1054 ui::Accelerator accelerator(ui::VKEY_D, true, true, false); |
| 1055 #endif |
| 1056 EXPECT_EQ(accelerator, keybinding.accelerator()); |
| 1057 |
| 1058 // Now remove platform specific keys (leaving just "default") and make sure |
| 1059 // every platform falls back to the default. |
| 1060 EXPECT_TRUE(key_dict->Remove("windows", NULL)); |
| 1061 EXPECT_TRUE(key_dict->Remove("mac", NULL)); |
| 1062 EXPECT_TRUE(key_dict->Remove("linux", NULL)); |
| 1063 EXPECT_TRUE(key_dict->Remove("chromeos", NULL)); |
| 1064 EXPECT_TRUE(keybinding.Parse(command.get(), command_name, 0, &error)); |
| 1065 EXPECT_EQ(ui::VKEY_D, keybinding.accelerator().key_code()); |
| 1066 |
| 1067 // Now remove "default", leaving no option but failure. Or, in the words of |
| 1068 // the immortal Adam Savage: "Failure is always an option". |
| 1069 EXPECT_TRUE(key_dict->Remove("default", NULL)); |
| 1070 EXPECT_FALSE(keybinding.Parse(command.get(), command_name, 0, &error)); |
| 1071 |
| 1072 // Now add only a valid platform that we are not running on to make sure devs |
| 1073 // are notified of errors on other platforms. |
| 1074 #if defined(OS_WIN) |
| 1075 key_dict->SetString("mac", "Ctrl+Shift+M"); |
| 1076 #else |
| 1077 key_dict->SetString("windows", "Ctrl+Shift+W"); |
| 1078 #endif |
| 1079 EXPECT_FALSE(keybinding.Parse(command.get(), command_name, 0, &error)); |
| 1080 |
| 1081 // Make sure Mac specific keys are not processed on other platforms. |
| 1082 #if !defined(OS_MACOSX) |
| 1083 key_dict->SetString("windows", "Command+Shift+M"); |
| 1084 EXPECT_FALSE(keybinding.Parse(command.get(), command_name, 0, &error)); |
| 1085 key_dict->SetString("windows", "Options+Shift+M"); |
| 1086 EXPECT_FALSE(keybinding.Parse(command.get(), command_name, 0, &error)); |
| 1087 #endif |
| 1088 } |
| 1089 |
1008 // These last 2 tests don't make sense on Chrome OS, where extension plugins | 1090 // These last 2 tests don't make sense on Chrome OS, where extension plugins |
1009 // are not allowed. | 1091 // are not allowed. |
1010 #if !defined(OS_CHROMEOS) | 1092 #if !defined(OS_CHROMEOS) |
1011 TEST(ExtensionTest, GetSyncTypeExtensionWithPlugin) { | 1093 TEST(ExtensionTest, GetSyncTypeExtensionWithPlugin) { |
1012 scoped_refptr<Extension> extension( | 1094 scoped_refptr<Extension> extension( |
1013 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), | 1095 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), |
1014 Extension::INTERNAL, 1, FilePath())); | 1096 Extension::INTERNAL, 1, FilePath())); |
1015 if (extension) | 1097 if (extension) |
1016 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); | 1098 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); |
1017 } | 1099 } |
1018 | 1100 |
1019 TEST(ExtensionTest, GetSyncTypeExtensionWithTwoPlugins) { | 1101 TEST(ExtensionTest, GetSyncTypeExtensionWithTwoPlugins) { |
1020 scoped_refptr<Extension> extension( | 1102 scoped_refptr<Extension> extension( |
1021 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), | 1103 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), |
1022 Extension::INTERNAL, 2, FilePath())); | 1104 Extension::INTERNAL, 2, FilePath())); |
1023 if (extension) | 1105 if (extension) |
1024 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); | 1106 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); |
1025 } | 1107 } |
1026 #endif // !defined(OS_CHROMEOS) | 1108 #endif // !defined(OS_CHROMEOS) |
OLD | NEW |