Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: chrome/browser/ui/views/accelerator_table_unittest_mac.mm

Issue 2074643003: MacViews: Views accelerators table should match the Cocoa one. (Closed) Base URL: ssh://bitbucket.browser.yandex-team.ru/chromium/src.git@master
Patch Set: #ifdef out the Alt accelerators, sort the #if !Mac accelerators, fix includes. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // Verifies that we're not processing any duplicate accelerators in
108 // global_keyboard_shortcuts_mac.mm functions.
109 TEST(AcceleratorTableTest, CheckNoDuplicatesGlobalKeyboardShortcutsMac) {
110 VerifyTableDoesntHaveDuplicates(GetWindowKeyboardShortcutTable(),
111 "WindowKeyboardShortcutTable");
112 VerifyTableDoesntHaveDuplicates(GetDelayedWindowKeyboardShortcutTable(),
113 "DelayedWindowKeyboardShortcutTable");
114 VerifyTableDoesntHaveDuplicates(GetBrowserKeyboardShortcutTable(),
115 "BrowserKeyboardShortcutTable");
116 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698