OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 <stddef.h> | |
6 | |
7 #include <set> | |
8 | |
9 #include "base/stl_util.h" | |
10 #include "build/build_config.h" | |
11 #include "chrome/app/chrome_command_ids.h" | |
12 #import "chrome/browser/global_keyboard_shortcuts_mac.h" | |
13 #include "chrome/browser/ui/views/accelerator_table.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "ui/events/event_constants.h" | |
16 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | |
17 | |
18 using namespace chrome; | |
19 | |
20 namespace { | |
21 | |
22 void VerifyTableDoesntHaveDuplicates( | |
23 const std::vector<KeyboardShortcutData>& table, | |
24 const std::string& table_name) { | |
25 const std::vector<AcceleratorMapping> accelerators(GetAcceleratorList()); | |
26 | |
27 for (const auto& e : table) { | |
28 int modifiers = 0; | |
29 if (e.command_key) | |
30 modifiers |= ui::EF_COMMAND_DOWN; | |
31 if (e.shift_key) | |
32 modifiers |= ui::EF_SHIFT_DOWN; | |
33 if (e.cntrl_key) | |
34 modifiers |= ui::EF_CONTROL_DOWN; | |
35 if (e.opt_key) | |
36 modifiers |= ui::EF_ALT_DOWN; | |
37 | |
38 for (const auto& accelerator_entry : accelerators) { | |
39 unichar character; | |
40 unichar shifted_character; | |
41 const int vkey_code = ui::MacKeyCodeForWindowsKeyCode( | |
42 accelerator_entry.keycode, accelerator_entry.modifiers, | |
43 &shifted_character, &character); | |
44 | |
45 EXPECT_FALSE(modifiers == accelerator_entry.modifiers && | |
46 e.chrome_command == accelerator_entry.command_id && | |
47 (e.vkey_code ? (e.vkey_code == vkey_code) | |
48 : (e.key_char == character || | |
49 e.key_char == shifted_character))) | |
50 << "Duplicate command: " << accelerator_entry.command_id | |
51 << " in table " << table_name; | |
52 } | |
53 } | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 // Vefifies that only the whitelisted accelerators could have Control key | |
59 // modifier, while running on macOS. | |
60 TEST(AcceleratorTableTest, CheckMacOSControlAccelerators) { | |
61 // Only the accelerators that also work in Cocoa browser are allowed to appear | |
62 // on this whitelist. | |
63 const std::set<int> whitelisted_control_shortcuts = { | |
64 IDC_SELECT_NEXT_TAB, | |
65 IDC_SELECT_PREVIOUS_TAB, | |
66 IDC_FULLSCREEN, | |
67 }; | |
68 | |
69 const std::vector<AcceleratorMapping> accelerators(GetAcceleratorList()); | |
70 | |
71 // Control modifier is rarely used on Mac, and all valid uses must be | |
72 // whitelisted. | |
73 for (const auto& entry : accelerators) { | |
74 if (base::ContainsKey(whitelisted_control_shortcuts, entry.command_id)) | |
75 continue; | |
76 EXPECT_FALSE(entry.modifiers & ui::EF_CONTROL_DOWN) | |
77 << "Found non-whitelisted accelerator that contains Control " | |
78 "modifier: " << entry.command_id; | |
79 } | |
80 | |
81 // Test that whitelist is not outdated. | |
82 for (const auto& whitelist_entry : whitelisted_control_shortcuts) { | |
83 const auto entry = | |
84 std::find_if(accelerators.begin(), accelerators.end(), | |
85 [whitelist_entry](const AcceleratorMapping& a) { | |
86 return a.command_id == whitelist_entry && | |
87 (a.modifiers & ui::EF_CONTROL_DOWN) != 0; | |
88 }); | |
89 EXPECT_NE(entry, accelerators.end()) | |
90 << "Whitelisted accelerator not found in the actual list: " | |
91 << whitelist_entry; | |
92 } | |
93 } | |
94 | |
95 // Verifies that Alt-only (or with just Shift) accelerators are not present in | |
96 // the list. | |
97 TEST(AcceleratorTableTest, CheckMacOSAltAccelerators) { | |
98 const int kNonShiftMask = | |
99 ui::EF_COMMAND_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN; | |
100 for (const auto& entry : GetAcceleratorList()) { | |
101 EXPECT_FALSE((entry.modifiers & kNonShiftMask) == ui::EF_ALT_DOWN) | |
102 << "Found accelerator that uses solely Alt modifier: " | |
103 << entry.command_id; | |
104 } | |
105 } | |
106 | |
107 // Vefifies that accelerator filtering works correctly and we have an | |
108 // alternative shortcut for each filtered-out accelerator. | |
109 TEST(AcceleratorTableTest, CheckMacOSFilteredOutAcceleratorsHaveAlternatives) { | |
110 // It's okay not to have an alternative accelerator for these commands as | |
111 // there's no alternative for them in the Cocoa browser. | |
112 const std::set<int> whitelisted_unique_shortcuts = { | |
113 IDC_FOCUS_TOOLBAR, | |
114 IDC_FOCUS_BOOKMARKS, | |
115 IDC_FOCUS_INFOBARS, | |
116 IDC_SHOW_APP_MENU, | |
117 }; | |
118 | |
119 // Convert the accelerator lists to sets for easy searching/filtering. | |
120 auto ToSet = [](const std::vector<AcceleratorMapping>& v) { | |
121 return std::set<AcceleratorMapping, bool (*)(const AcceleratorMapping&, | |
122 const AcceleratorMapping&)>( | |
123 v.begin(), v.end(), | |
124 [](const AcceleratorMapping& lhs, const AcceleratorMapping& rhs) { | |
125 return std::tie(lhs.command_id, lhs.keycode, lhs.modifiers) < | |
126 std::tie(rhs.command_id, rhs.keycode, rhs.modifiers); | |
127 }); | |
128 }; | |
129 | |
130 const auto accelerators = ToSet(GetAcceleratorList()); | |
131 const auto full_accelerators = | |
132 ToSet(GetUnfilteredAcceleratorListForTesting()); | |
133 | |
134 // Ensure that we still have alternative mappings for all filtered-out | |
135 // accelerators. | |
136 auto filtered_accelerators(full_accelerators); | |
137 // Remove all accelerators that are present in |accelerators|. | |
138 for (const auto& m : accelerators) { | |
Peter Kasting
2016/11/01 18:20:59
Nit: {} not necessary
themblsha
2016/11/02 17:31:37
Done.
| |
139 filtered_accelerators.erase(m); | |
140 } | |
141 EXPECT_GT(filtered_accelerators.size(), 1u); | |
142 | |
143 for (const auto& removed_entry : filtered_accelerators) { | |
144 if (base::ContainsKey(whitelisted_unique_shortcuts, | |
145 removed_entry.command_id)) | |
146 continue; | |
147 | |
148 const auto entry = | |
149 std::find_if(accelerators.begin(), accelerators.end(), | |
150 [&removed_entry](const AcceleratorMapping& m) { | |
151 return removed_entry.command_id == m.command_id; | |
152 }); | |
153 EXPECT_NE(entry, accelerators.end()) | |
154 << "Filtered command doesn't have an alternative mapping: " | |
155 << removed_entry.command_id; | |
156 } | |
157 | |
158 for (const auto& whitelist_entry : whitelisted_unique_shortcuts) { | |
159 const auto entry = | |
160 std::find_if(filtered_accelerators.begin(), filtered_accelerators.end(), | |
161 [&whitelist_entry](const AcceleratorMapping& m) { | |
162 return whitelist_entry == m.command_id; | |
163 }); | |
164 EXPECT_NE(entry, filtered_accelerators.end()) | |
165 << "Whitelisted accelerator not found in the filtered-out list: " | |
166 << whitelist_entry; | |
167 } | |
168 } | |
169 | |
170 // Verifies that we're not processing any duplicate accelerators in | |
171 // global_keyboard_shortcuts_mac.mm functions. | |
172 TEST(AcceleratorTableTest, CheckNoDuplicatesGlobalKeyboardShortcutsMac) { | |
173 VerifyTableDoesntHaveDuplicates(GetWindowKeyboardShortcutTable(), | |
174 "WindowKeyboardShortcutTable"); | |
175 VerifyTableDoesntHaveDuplicates(GetDelayedWindowKeyboardShortcutTable(), | |
176 "DelayedWindowKeyboardShortcutTable"); | |
177 VerifyTableDoesntHaveDuplicates(GetBrowserKeyboardShortcutTable(), | |
178 "BrowserKeyboardShortcutTable"); | |
179 } | |
OLD | NEW |