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 namespace { |
| 19 |
| 20 void VerifyTableDoesntHaveDuplicates( |
| 21 const std::vector<KeyboardShortcutData>& table, |
| 22 const std::string& table_name) { |
| 23 const std::vector<AcceleratorMapping> accelerators(GetAcceleratorList()); |
| 24 |
| 25 for (const auto& e : table) { |
| 26 int modifiers = 0; |
| 27 if (e.command_key) |
| 28 modifiers |= ui::EF_COMMAND_DOWN; |
| 29 if (e.shift_key) |
| 30 modifiers |= ui::EF_SHIFT_DOWN; |
| 31 if (e.cntrl_key) |
| 32 modifiers |= ui::EF_CONTROL_DOWN; |
| 33 if (e.opt_key) |
| 34 modifiers |= ui::EF_ALT_DOWN; |
| 35 |
| 36 for (const auto& accelerator_entry : accelerators) { |
| 37 unichar character; |
| 38 unichar shifted_character; |
| 39 const int vkey_code = ui::MacKeyCodeForWindowsKeyCode( |
| 40 accelerator_entry.keycode, accelerator_entry.modifiers, |
| 41 &shifted_character, &character); |
| 42 |
| 43 EXPECT_FALSE(modifiers == accelerator_entry.modifiers && |
| 44 e.chrome_command == accelerator_entry.command_id && |
| 45 (e.vkey_code ? (e.vkey_code == vkey_code) |
| 46 : (e.key_char == character || |
| 47 e.key_char == shifted_character))) |
| 48 << "Duplicate command: " << accelerator_entry.command_id |
| 49 << " in table " << table_name; |
| 50 } |
| 51 } |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
| 56 // Vefifies that only the whitelisted accelerators could have Control key |
| 57 // modifier, while running on macOS. |
| 58 TEST(AcceleratorTableTest, CheckMacOSControlAccelerators) { |
| 59 // Only the accelerators that also work in Cocoa browser are allowed to appear |
| 60 // on this whitelist. |
| 61 const std::set<int> whitelisted_control_shortcuts = { |
| 62 IDC_SELECT_NEXT_TAB, |
| 63 IDC_SELECT_PREVIOUS_TAB, |
| 64 IDC_FULLSCREEN, |
| 65 }; |
| 66 |
| 67 const std::vector<AcceleratorMapping> accelerators(GetAcceleratorList()); |
| 68 |
| 69 // Control modifier is rarely used on Mac, and all valid uses must be |
| 70 // whitelisted. |
| 71 for (const auto& entry : accelerators) { |
| 72 if (base::ContainsKey(whitelisted_control_shortcuts, entry.command_id)) |
| 73 continue; |
| 74 EXPECT_FALSE(entry.modifiers & ui::EF_CONTROL_DOWN) |
| 75 << "Found non-whitelisted accelerator that contains Control " |
| 76 "modifier: " << entry.command_id; |
| 77 } |
| 78 |
| 79 // Test that whitelist is not outdated. |
| 80 for (const auto& whitelist_entry : whitelisted_control_shortcuts) { |
| 81 const auto entry = |
| 82 std::find_if(accelerators.begin(), accelerators.end(), |
| 83 [whitelist_entry](const AcceleratorMapping& a) { |
| 84 return a.command_id == whitelist_entry && |
| 85 (a.modifiers & ui::EF_CONTROL_DOWN) != 0; |
| 86 }); |
| 87 EXPECT_NE(entry, accelerators.end()) |
| 88 << "Whitelisted accelerator not found in the actual list: " |
| 89 << whitelist_entry; |
| 90 } |
| 91 } |
| 92 |
| 93 // Verifies that Alt-only (or with just Shift) accelerators are not present in |
| 94 // the list. |
| 95 TEST(AcceleratorTableTest, CheckMacOSAltAccelerators) { |
| 96 const int kNonShiftMask = |
| 97 ui::EF_COMMAND_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN; |
| 98 for (const auto& entry : GetAcceleratorList()) { |
| 99 EXPECT_FALSE((entry.modifiers & kNonShiftMask) == ui::EF_ALT_DOWN) |
| 100 << "Found accelerator that uses solely Alt modifier: " |
| 101 << entry.command_id; |
| 102 } |
| 103 } |
| 104 |
| 105 // Verifies that we're not processing any duplicate accelerators in |
| 106 // global_keyboard_shortcuts_mac.mm functions. |
| 107 TEST(AcceleratorTableTest, CheckNoDuplicatesGlobalKeyboardShortcutsMac) { |
| 108 VerifyTableDoesntHaveDuplicates(GetWindowKeyboardShortcutTable(), |
| 109 "WindowKeyboardShortcutTable"); |
| 110 VerifyTableDoesntHaveDuplicates(GetDelayedWindowKeyboardShortcutTable(), |
| 111 "DelayedWindowKeyboardShortcutTable"); |
| 112 VerifyTableDoesntHaveDuplicates(GetBrowserKeyboardShortcutTable(), |
| 113 "BrowserKeyboardShortcutTable"); |
| 114 } |
OLD | NEW |