1 // Copyright (c) 2009 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.
5 #include <AppKit/NSEvent.h>
6 #include <Carbon/Carbon.h>
9 #include "chrome/browser/global_keyboard_shortcuts_mac.h"
11 #include "base/check_op.h"
12 #include "base/macros.h"
13 #include "base/stl_util.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/buildflags.h"
17 #include "ui/events/keycodes/keyboard_code_conversion_mac.h"
21 int CommandForKeys(bool command_key,
26 NSUInteger modifierFlags = 0;
28 modifierFlags |= NSCommandKeyMask;
30 modifierFlags |= NSShiftKeyMask;
32 modifierFlags |= NSControlKeyMask;
34 modifierFlags |= NSAlternateKeyMask;
41 // Docs say this is set whenever a key came from the numpad *or* the arrow
43 modifierFlags |= NSEventModifierFlagNumericPad;
49 unichar shifted_character;
51 int result = ui::MacKeyCodeForWindowsKeyCode(
52 ui::KeyboardCodeFromKeyCode(vkey_code), modifierFlags, &shifted_character,
54 DCHECK_NE(result, -1);
56 NSEvent* event = [NSEvent
57 keyEventWithType:NSKeyDown
59 modifierFlags:modifierFlags
63 characters:[NSString stringWithFormat:@"%C", character]
64 charactersIgnoringModifiers:[NSString
65 stringWithFormat:@"%C", shifted_character]
69 return CommandForKeyEvent(event).chrome_command;
74 TEST(GlobalKeyboardShortcuts, BasicFunctionality) {
75 // Test that an invalid shortcut translates into an invalid command id.
76 EXPECT_EQ(-1, CommandForKeys(false, false, false, false, 0));
78 // Check that all known keyboard shortcuts return valid results.
79 for (const auto& shortcut : GetShortcutsNotPresentInMainMenu()) {
80 int cmd_num = CommandForKeys(shortcut.command_key, shortcut.shift_key,
81 shortcut.cntrl_key, shortcut.opt_key,
83 EXPECT_EQ(cmd_num, shortcut.chrome_command);
85 // Test that switching tabs triggers off keycodes and not characters (visible
86 // with the Italian keyboard layout).
87 EXPECT_EQ(IDC_SELECT_TAB_0,
88 CommandForKeys(true, false, false, false, kVK_ANSI_1));
91 TEST(GlobalKeyboardShortcuts, KeypadNumberKeysMatch) {
92 // Test that the shortcuts that are generated by keypad number keys match the
98 {kVK_ANSI_0, kVK_ANSI_Keypad0},
99 {kVK_ANSI_1, kVK_ANSI_Keypad1},
100 {kVK_ANSI_2, kVK_ANSI_Keypad2},
101 {kVK_ANSI_3, kVK_ANSI_Keypad3},
102 {kVK_ANSI_4, kVK_ANSI_Keypad4},
103 {kVK_ANSI_5, kVK_ANSI_Keypad5},
104 {kVK_ANSI_6, kVK_ANSI_Keypad6},
105 {kVK_ANSI_7, kVK_ANSI_Keypad7},
106 {kVK_ANSI_8, kVK_ANSI_Keypad8},
107 {kVK_ANSI_9, kVK_ANSI_Keypad9},
110 // We only consider unshifted keys. A shifted numpad key gives a different
111 // keyEquivalent than a shifted number key.
113 for (unsigned int i = 0; i < base::size(equivalents); ++i) {
114 for (int command = 0; command <= 1; ++command) {
115 for (int control = 0; control <= 1; ++control) {
116 for (int option = 0; option <= 1; ++option) {
117 EXPECT_EQ(CommandForKeys(command, shift, control, option,
118 equivalents[i].keycode),
119 CommandForKeys(command, shift, control, option,
120 equivalents[i].keypad_keycode));
121 EXPECT_EQ(CommandForKeys(command, shift, control, option,
122 equivalents[i].keycode),
123 CommandForKeys(command, shift, control, option,
124 equivalents[i].keypad_keycode));