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 "build/build_config.h" |
| 10 #include "chrome/app/chrome_command_ids.h" |
| 11 #include "chrome/browser/global_keyboard_shortcuts_mac.h" |
| 12 #include "chrome/browser/ui/views/accelerator_table.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/events/event_constants.h" |
| 15 #include "ui/events/keycodes/keyboard_code_conversion_mac.h" |
| 16 |
| 17 namespace chrome { |
| 18 |
| 19 // Vefifies that only the whitelisted accelerators could have Control key |
| 20 // modifier, while running on macOS. |
| 21 TEST(AcceleratorTableTest, CheckMacOSControlAccelerators) { |
| 22 // Only the accelerators that also work in Cocoa browser are allowed to appear |
| 23 // on this whitelist. |
| 24 const std::set<int> whitelisted_control_shortcuts = { |
| 25 IDC_SELECT_NEXT_TAB, |
| 26 IDC_SELECT_PREVIOUS_TAB, |
| 27 IDC_FULLSCREEN, |
| 28 }; |
| 29 |
| 30 const std::vector<AcceleratorMapping> accelerator_list(GetAcceleratorList()); |
| 31 |
| 32 // Control modifier is rarely used on Mac, and all valid uses must be |
| 33 // whitelisted. |
| 34 for (const auto& entry : accelerator_list) { |
| 35 if (whitelisted_control_shortcuts.find(entry.command_id) != |
| 36 whitelisted_control_shortcuts.end()) |
| 37 continue; |
| 38 EXPECT_FALSE(entry.modifiers & ui::EF_CONTROL_DOWN) |
| 39 << "Found non-whitelisted accelerator that contains Control " |
| 40 "modifier: " << entry.command_id; |
| 41 } |
| 42 |
| 43 // Test that whitelist is not outdated. |
| 44 for (const auto& whitelist_entry : whitelisted_control_shortcuts) { |
| 45 const auto entry = |
| 46 std::find_if(accelerator_list.begin(), accelerator_list.end(), |
| 47 [whitelist_entry](const AcceleratorMapping& a) { |
| 48 return a.command_id == whitelist_entry && |
| 49 a.modifiers & ui::EF_CONTROL_DOWN; |
| 50 }); |
| 51 EXPECT_TRUE(entry != accelerator_list.end()) |
| 52 << "Whitelisted accelerator not found in the actual list: " |
| 53 << whitelist_entry; |
| 54 } |
| 55 } |
| 56 |
| 57 // Verifies that Alt-only (or with just Shift) accelerators are not present in |
| 58 // the list. |
| 59 TEST(AcceleratorTableTest, CheckMacOSAltAccelerators) { |
| 60 const int kNonShiftMask = |
| 61 ui::EF_COMMAND_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN; |
| 62 for (const auto& entry : GetAcceleratorList()) { |
| 63 EXPECT_FALSE((entry.modifiers & kNonShiftMask) == ui::EF_ALT_DOWN) |
| 64 << "Found accelerator that uses solely Alt modifier: " |
| 65 << entry.command_id; |
| 66 } |
| 67 } |
| 68 |
| 69 // Vefifies that accelerator filtering works correctly and we have an |
| 70 // alternative shortcut for each filtered-out accelerator. |
| 71 TEST(AcceleratorTableTest, CheckMacOSFilteredOutAcceleratorsHaveAlternatives) { |
| 72 // It's okay not to have an alternative accelerator for these commands as |
| 73 // there's no alternative for them in the Cocoa browser. |
| 74 const std::set<int> whitelisted_unique_shortcuts = { |
| 75 IDC_FOCUS_TOOLBAR, |
| 76 IDC_FOCUS_BOOKMARKS, |
| 77 IDC_FOCUS_INFOBARS, |
| 78 IDC_SHOW_APP_MENU, |
| 79 }; |
| 80 |
| 81 const std::vector<AcceleratorMapping> accelerator_list(GetAcceleratorList()); |
| 82 const std::vector<AcceleratorMapping> full_accelerator_list( |
| 83 GetUnfilteredAcceleratorListForTesting()); |
| 84 |
| 85 // Ensure that we still have alternative mappings for all filtered-out |
| 86 // accelerators. |
| 87 std::vector<AcceleratorMapping> filtered_list(full_accelerator_list); |
| 88 auto remove_start = |
| 89 std::remove_if(filtered_list.begin(), filtered_list.end(), |
| 90 [&accelerator_list](const AcceleratorMapping& m1) { |
| 91 // Remove all accelerators that are present in |accelerator_list| |
| 92 return std::find_if(accelerator_list.begin(), accelerator_list.end(), |
| 93 [&m1](const AcceleratorMapping& m2) { |
| 94 return m1.command_id == m2.command_id && |
| 95 m1.keycode == m2.keycode && |
| 96 m1.modifiers == m2.modifiers; |
| 97 }) != accelerator_list.end(); |
| 98 }); |
| 99 filtered_list.erase(remove_start, filtered_list.end()); |
| 100 EXPECT_GT(filtered_list.size(), 1u); |
| 101 |
| 102 for (const auto& removed_entry : filtered_list) { |
| 103 if (whitelisted_unique_shortcuts.find(removed_entry.command_id) != |
| 104 whitelisted_unique_shortcuts.end()) |
| 105 continue; |
| 106 |
| 107 const auto entry = |
| 108 std::find_if(accelerator_list.begin(), accelerator_list.end(), |
| 109 [&removed_entry](const AcceleratorMapping& m) { |
| 110 return removed_entry.command_id == m.command_id; |
| 111 }); |
| 112 EXPECT_TRUE(entry != accelerator_list.end()) |
| 113 << "Filtered command doesn't have an alternative mapping: " |
| 114 << removed_entry.command_id; |
| 115 } |
| 116 |
| 117 for (const auto& whitelist_entry : whitelisted_unique_shortcuts) { |
| 118 const auto entry = |
| 119 std::find_if(filtered_list.begin(), filtered_list.end(), |
| 120 [&whitelist_entry](const AcceleratorMapping& m) { |
| 121 return whitelist_entry == m.command_id; |
| 122 }); |
| 123 EXPECT_TRUE(entry != filtered_list.end()) |
| 124 << "Whitelisted accelerator not found in the filtered-out list: " |
| 125 << whitelist_entry; |
| 126 } |
| 127 } |
| 128 |
| 129 namespace { |
| 130 |
| 131 static void VerifyTable( |
| 132 const KeyboardShortcutData* (*get_keyboard_shortcut_table)(size_t*), |
| 133 const std::string& table_name) { |
| 134 const std::vector<AcceleratorMapping> accelerator_list(GetAcceleratorList()); |
| 135 |
| 136 size_t num_shortcuts = 0; |
| 137 const KeyboardShortcutData *it = get_keyboard_shortcut_table(&num_shortcuts); |
| 138 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { |
| 139 int modifiers = 0; |
| 140 if (it->command_key) |
| 141 modifiers |= ui::EF_COMMAND_DOWN; |
| 142 if (it->shift_key) |
| 143 modifiers |= ui::EF_SHIFT_DOWN; |
| 144 if (it->cntrl_key) |
| 145 modifiers |= ui::EF_CONTROL_DOWN; |
| 146 if (it->opt_key) |
| 147 modifiers |= ui::EF_ALT_DOWN; |
| 148 |
| 149 for (const auto& entry : accelerator_list) { |
| 150 unichar character; |
| 151 unichar shifted_character; |
| 152 const int vkey_code = ui::MacKeyCodeForWindowsKeyCode( |
| 153 entry.keycode, entry.modifiers, &shifted_character, &character); |
| 154 |
| 155 if (it->vkey_code) { |
| 156 if (modifiers == entry.modifiers && it->vkey_code == vkey_code && |
| 157 it->chrome_command == entry.command_id) { |
| 158 EXPECT_TRUE(false) << "Duplicate command: " << entry.command_id |
| 159 << " in table " << table_name; |
| 160 } |
| 161 } else { |
| 162 if (modifiers == entry.modifiers && |
| 163 (it->key_char == character || it->key_char == shifted_character) && |
| 164 it->chrome_command == entry.command_id) { |
| 165 EXPECT_TRUE(false) << "Duplicate command: " << entry.command_id |
| 166 << " in table " << table_name; |
| 167 } |
| 168 } |
| 169 } |
| 170 } |
| 171 } |
| 172 |
| 173 } // namespace |
| 174 |
| 175 // Verifies that we're not processing any duplicate accelerators in |
| 176 // global_keyboard_shortcuts_mac.mm functions. |
| 177 TEST(AcceleratorTableTest, CheckNoDuplicatesGlobalKeyboardShortcutsMac) { |
| 178 VerifyTable(GetWindowKeyboardShortcutTable, "WindowKeyboardShortcutTable"); |
| 179 VerifyTable(GetDelayedWindowKeyboardShortcutTable, |
| 180 "DelayedWindowKeyboardShortcutTable"); |
| 181 VerifyTable(GetBrowserKeyboardShortcutTable, "BrowserKeyboardShortcutTable"); |
| 182 |
| 183 // https://cs.chromium.org/chromium/src/ui/content_accelerators/accelerator_ut
il.cc?sq=package:chromium&dr=CSs&rcl=1476256712&l=38 |
| 184 // ui::Accelerator GetAcceleratorFromNativeWebKeyboardEvent( |
| 185 // const content::NativeWebKeyboardEvent& event) { |
| 186 // ui::Accelerator accelerator( |
| 187 // static_cast<ui::KeyboardCode>(event.windowsKeyCode), |
| 188 // GetModifiersFromNativeWebKeyboardEvent(event)); |
| 189 // if (event.type == blink::WebInputEvent::KeyUp) |
| 190 // accelerator.set_type(ui::ET_KEY_RELEASED); |
| 191 // return accelerator; |
| 192 // } |
| 193 // https://cs.chromium.org/chromium/src/content/browser/renderer_host/input/we
b_input_event_builders_mac_unittest.mm?sq=package:chromium&dr=CSs |
| 194 } |
| 195 |
| 196 } // namespace chrome |
OLD | NEW |