blob: ee40eea98c679a3be16a109130705c3b2ba290ba [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
avi6846aef2015-12-26 01:09:3811#include "base/macros.h"
Erik Chen8f2f95682018-06-08 00:44:5412#include "base/stl_util.h"
[email protected]1a3aba82010-11-08 23:52:5413#include "chrome/app/chrome_command_ids.h"
[email protected]3641da6c2009-07-08 14:59:0614#include "testing/gtest/include/gtest/gtest.h"
Scott Violetb72577d2019-01-09 22:18:1815#include "ui/base/buildflags.h"
erikchen008ab23a2018-08-09 02:06:3916#include "ui/events/keycodes/keyboard_code_conversion_mac.h"
[email protected]3641da6c2009-07-08 14:59:0617
Erik Chen8f2f95682018-06-08 00:44:5418namespace {
19
20int CommandForKeys(bool command_key,
21 bool shift_key,
22 bool cntrl_key,
23 bool opt_key,
Erik Chene4ab96f2018-06-09 04:21:1124 int vkey_code) {
Erik Chen8f2f95682018-06-08 00:44:5425 NSUInteger modifierFlags = 0;
26 if (command_key)
27 modifierFlags |= NSCommandKeyMask;
28 if (shift_key)
29 modifierFlags |= NSShiftKeyMask;
30 if (cntrl_key)
31 modifierFlags |= NSControlKeyMask;
32 if (opt_key)
33 modifierFlags |= NSAlternateKeyMask;
34
Elly Fong-Jones03312b62019-10-11 21:35:4935 switch (vkey_code) {
36 case kVK_UpArrow:
37 case kVK_DownArrow:
38 case kVK_LeftArrow:
39 case kVK_RightArrow:
40 // Docs say this is set whenever a key came from the numpad *or* the arrow
41 // keys.
42 modifierFlags |= NSEventModifierFlagNumericPad;
43 break;
44 default:
45 break;
46 }
47
erikchen008ab23a2018-08-09 02:06:3948 unichar shifted_character;
49 unichar character;
50 int result = ui::MacKeyCodeForWindowsKeyCode(
51 ui::KeyboardCodeFromKeyCode(vkey_code), modifierFlags, &shifted_character,
52 &character);
53 DCHECK_NE(result, -1);
Erik Chen8f2f95682018-06-08 00:44:5454
erikchen008ab23a2018-08-09 02:06:3955 NSEvent* event = [NSEvent
56 keyEventWithType:NSKeyDown
57 location:NSZeroPoint
58 modifierFlags:modifierFlags
59 timestamp:0.0
60 windowNumber:0
61 context:nil
62 characters:[NSString stringWithFormat:@"%C", character]
63 charactersIgnoringModifiers:[NSString
64 stringWithFormat:@"%C", shifted_character]
65 isARepeat:NO
66 keyCode:vkey_code];
Erik Chen8f2f95682018-06-08 00:44:5467
erikchen41281cd2018-06-20 18:15:0568 return CommandForKeyEvent(event).chrome_command;
Erik Chen8f2f95682018-06-08 00:44:5469}
70
71} // namespace
72
73TEST(GlobalKeyboardShortcuts, BasicFunctionality) {
[email protected]3641da6c2009-07-08 14:59:0674 // Test that an invalid shortcut translates into an invalid command id.
Erik Chene4ab96f2018-06-09 04:21:1175 EXPECT_EQ(-1, CommandForKeys(false, false, false, false, 0));
[email protected]70be00a2009-07-08 23:40:0876
[email protected]3641da6c2009-07-08 14:59:0677 // Check that all known keyboard shortcuts return valid results.
Erik Chen8f2f95682018-06-08 00:44:5478 for (const auto& shortcut : GetShortcutsNotPresentInMainMenu()) {
79 int cmd_num = CommandForKeys(shortcut.command_key, shortcut.shift_key,
80 shortcut.cntrl_key, shortcut.opt_key,
Erik Chene4ab96f2018-06-09 04:21:1181 shortcut.vkey_code);
mblshacb9c6b9d2016-11-21 17:11:1882 EXPECT_EQ(cmd_num, shortcut.chrome_command);
[email protected]1d313b832009-10-09 01:26:2083 }
[email protected]b7b0bcb2010-11-17 17:12:2484 // Test that switching tabs triggers off keycodes and not characters (visible
85 // with the Italian keyboard layout).
Erik Chen8f2f95682018-06-08 00:44:5486 EXPECT_EQ(IDC_SELECT_TAB_0,
Erik Chene4ab96f2018-06-09 04:21:1187 CommandForKeys(true, false, false, false, kVK_ANSI_1));
[email protected]b7b0bcb2010-11-17 17:12:2488}
89
90TEST(GlobalKeyboardShortcuts, KeypadNumberKeysMatch) {
91 // Test that the shortcuts that are generated by keypad number keys match the
92 // equivalent keys.
93 static const struct {
94 int keycode;
95 int keypad_keycode;
96 } equivalents[] = {
97 {kVK_ANSI_0, kVK_ANSI_Keypad0},
98 {kVK_ANSI_1, kVK_ANSI_Keypad1},
99 {kVK_ANSI_2, kVK_ANSI_Keypad2},
100 {kVK_ANSI_3, kVK_ANSI_Keypad3},
101 {kVK_ANSI_4, kVK_ANSI_Keypad4},
102 {kVK_ANSI_5, kVK_ANSI_Keypad5},
103 {kVK_ANSI_6, kVK_ANSI_Keypad6},
104 {kVK_ANSI_7, kVK_ANSI_Keypad7},
105 {kVK_ANSI_8, kVK_ANSI_Keypad8},
106 {kVK_ANSI_9, kVK_ANSI_Keypad9},
107 };
108
erikchen008ab23a2018-08-09 02:06:39109 // We only consider unshifted keys. A shifted numpad key gives a different
110 // keyEquivalent than a shifted number key.
111 int shift = 0;
Erik Chen8f2f95682018-06-08 00:44:54112 for (unsigned int i = 0; i < base::size(equivalents); ++i) {
[email protected]b7b0bcb2010-11-17 17:12:24113 for (int command = 0; command <= 1; ++command) {
erikchen008ab23a2018-08-09 02:06:39114 for (int control = 0; control <= 1; ++control) {
115 for (int option = 0; option <= 1; ++option) {
116 EXPECT_EQ(CommandForKeys(command, shift, control, option,
117 equivalents[i].keycode),
118 CommandForKeys(command, shift, control, option,
119 equivalents[i].keypad_keycode));
120 EXPECT_EQ(CommandForKeys(command, shift, control, option,
121 equivalents[i].keycode),
122 CommandForKeys(command, shift, control, option,
123 equivalents[i].keypad_keycode));
[email protected]b7b0bcb2010-11-17 17:12:24124 }
125 }
126 }
127 }
[email protected]1d313b832009-10-09 01:26:20128}