blob: 99b6dbd7271a60843820bde06113b69690a3e306 [file] [log] [blame]
[email protected]3641da6c2009-07-08 14:59:061// 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.
4
[email protected]92f5adfa2010-01-05 09:49:125#include <AppKit/NSEvent.h>
[email protected]1d313b832009-10-09 01:26:206#include <Carbon/Carbon.h>
avi6846aef2015-12-26 01:09:387#include <stddef.h>
[email protected]1d313b832009-10-09 01:26:208
[email protected]3641da6c2009-07-08 14:59:069#include "chrome/browser/global_keyboard_shortcuts_mac.h"
10
Hans Wennborgf6ad69c2020-06-18 18:02:3211#include "base/check_op.h"
avi6846aef2015-12-26 01:09:3812#include "base/macros.h"
Erik Chen8f2f95682018-06-08 00:44:5413#include "base/stl_util.h"
[email protected]1a3aba82010-11-08 23:52:5414#include "chrome/app/chrome_command_ids.h"
[email protected]3641da6c2009-07-08 14:59:0615#include "testing/gtest/include/gtest/gtest.h"
Scott Violetb72577d2019-01-09 22:18:1816#include "ui/base/buildflags.h"
erikchen008ab23a2018-08-09 02:06:3917#include "ui/events/keycodes/keyboard_code_conversion_mac.h"
[email protected]3641da6c2009-07-08 14:59:0618
Erik Chen8f2f95682018-06-08 00:44:5419namespace {
20
21int CommandForKeys(bool command_key,
22 bool shift_key,
23 bool cntrl_key,
24 bool opt_key,
Erik Chene4ab96f2018-06-09 04:21:1125 int vkey_code) {
Erik Chen8f2f95682018-06-08 00:44:5426 NSUInteger modifierFlags = 0;
27 if (command_key)
28 modifierFlags |= NSCommandKeyMask;
29 if (shift_key)
30 modifierFlags |= NSShiftKeyMask;
31 if (cntrl_key)
32 modifierFlags |= NSControlKeyMask;
33 if (opt_key)
34 modifierFlags |= NSAlternateKeyMask;
35
Elly Fong-Jones03312b62019-10-11 21:35:4936 switch (vkey_code) {
37 case kVK_UpArrow:
38 case kVK_DownArrow:
39 case kVK_LeftArrow:
40 case kVK_RightArrow:
41 // Docs say this is set whenever a key came from the numpad *or* the arrow
42 // keys.
43 modifierFlags |= NSEventModifierFlagNumericPad;
44 break;
45 default:
46 break;
47 }
48
erikchen008ab23a2018-08-09 02:06:3949 unichar shifted_character;
50 unichar character;
51 int result = ui::MacKeyCodeForWindowsKeyCode(
52 ui::KeyboardCodeFromKeyCode(vkey_code), modifierFlags, &shifted_character,
53 &character);
54 DCHECK_NE(result, -1);
Erik Chen8f2f95682018-06-08 00:44:5455
erikchen008ab23a2018-08-09 02:06:3956 NSEvent* event = [NSEvent
57 keyEventWithType:NSKeyDown
58 location:NSZeroPoint
59 modifierFlags:modifierFlags
60 timestamp:0.0
61 windowNumber:0
62 context:nil
63 characters:[NSString stringWithFormat:@"%C", character]
64 charactersIgnoringModifiers:[NSString
65 stringWithFormat:@"%C", shifted_character]
66 isARepeat:NO
67 keyCode:vkey_code];
Erik Chen8f2f95682018-06-08 00:44:5468
erikchen41281cd2018-06-20 18:15:0569 return CommandForKeyEvent(event).chrome_command;
Erik Chen8f2f95682018-06-08 00:44:5470}
71
72} // namespace
73
74TEST(GlobalKeyboardShortcuts, BasicFunctionality) {
[email protected]3641da6c2009-07-08 14:59:0675 // Test that an invalid shortcut translates into an invalid command id.
Erik Chene4ab96f2018-06-09 04:21:1176 EXPECT_EQ(-1, CommandForKeys(false, false, false, false, 0));
[email protected]70be00a2009-07-08 23:40:0877
[email protected]3641da6c2009-07-08 14:59:0678 // Check that all known keyboard shortcuts return valid results.
Erik Chen8f2f95682018-06-08 00:44:5479 for (const auto& shortcut : GetShortcutsNotPresentInMainMenu()) {
80 int cmd_num = CommandForKeys(shortcut.command_key, shortcut.shift_key,
81 shortcut.cntrl_key, shortcut.opt_key,
Erik Chene4ab96f2018-06-09 04:21:1182 shortcut.vkey_code);
mblshacb9c6b9d2016-11-21 17:11:1883 EXPECT_EQ(cmd_num, shortcut.chrome_command);
[email protected]1d313b832009-10-09 01:26:2084 }
[email protected]b7b0bcb2010-11-17 17:12:2485 // Test that switching tabs triggers off keycodes and not characters (visible
86 // with the Italian keyboard layout).
Erik Chen8f2f95682018-06-08 00:44:5487 EXPECT_EQ(IDC_SELECT_TAB_0,
Erik Chene4ab96f2018-06-09 04:21:1188 CommandForKeys(true, false, false, false, kVK_ANSI_1));
[email protected]b7b0bcb2010-11-17 17:12:2489}
90
91TEST(GlobalKeyboardShortcuts, KeypadNumberKeysMatch) {
92 // Test that the shortcuts that are generated by keypad number keys match the
93 // equivalent keys.
94 static const struct {
95 int keycode;
96 int keypad_keycode;
97 } equivalents[] = {
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},
108 };
109
erikchen008ab23a2018-08-09 02:06:39110 // We only consider unshifted keys. A shifted numpad key gives a different
111 // keyEquivalent than a shifted number key.
112 int shift = 0;
Erik Chen8f2f95682018-06-08 00:44:54113 for (unsigned int i = 0; i < base::size(equivalents); ++i) {
[email protected]b7b0bcb2010-11-17 17:12:24114 for (int command = 0; command <= 1; ++command) {
erikchen008ab23a2018-08-09 02:06:39115 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));
[email protected]b7b0bcb2010-11-17 17:12:24125 }
126 }
127 }
128 }
[email protected]1d313b832009-10-09 01:26:20129}