1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
// Copyright 2009 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/global_keyboard_shortcuts_mac.h"
#include <AppKit/NSEvent.h>
#include <Carbon/Carbon.h>
#include <stddef.h>
#include <initializer_list>
#include "base/check_op.h"
#include "chrome/app/chrome_command_ids.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/buildflags.h"
#include "ui/events/keycodes/keyboard_code_conversion_mac.h"
namespace {
enum class CommandKeyState : bool {
kUp,
kDown,
};
enum class ShiftKeyState : bool {
kUp,
kDown,
};
enum class OptionKeyState : bool {
kUp,
kDown,
};
enum class ControlKeyState : bool {
kUp,
kDown,
};
int CommandForKeys(int vkey_code,
CommandKeyState command,
ShiftKeyState shift = ShiftKeyState::kUp,
OptionKeyState option = OptionKeyState::kUp,
ControlKeyState control = ControlKeyState::kUp) {
NSUInteger modifier_flags = 0;
if (command == CommandKeyState::kDown)
modifier_flags |= NSEventModifierFlagCommand;
if (shift == ShiftKeyState::kDown)
modifier_flags |= NSEventModifierFlagShift;
if (option == OptionKeyState::kDown)
modifier_flags |= NSEventModifierFlagOption;
if (control == ControlKeyState::kDown)
modifier_flags |= NSEventModifierFlagControl;
switch (vkey_code) {
case kVK_UpArrow:
case kVK_DownArrow:
case kVK_LeftArrow:
case kVK_RightArrow:
// Docs say that this is set for numpad *and* arrow keys.
modifier_flags |= NSEventModifierFlagNumericPad;
[[fallthrough]];
case kVK_Help:
case kVK_ForwardDelete:
case kVK_Home:
case kVK_End:
case kVK_PageUp:
case kVK_PageDown:
// Docs say that this is set for function keys *and* the cluster of six
// navigation keys in the center of the keyboard *and* arrow keys.
modifier_flags |= NSEventModifierFlagFunction;
break;
default:
break;
}
unichar shifted_character;
unichar character;
int result = ui::MacKeyCodeForWindowsKeyCode(
ui::KeyboardCodeFromKeyCode(vkey_code), modifier_flags,
&shifted_character, &character);
DCHECK_NE(result, -1);
NSEvent* event = [NSEvent
keyEventWithType:NSEventTypeKeyDown
location:NSZeroPoint
modifierFlags:modifier_flags
timestamp:0.0
windowNumber:0
context:nil
characters:[NSString stringWithFormat:@"%C", character]
charactersIgnoringModifiers:[NSString
stringWithFormat:@"%C", shifted_character]
isARepeat:NO
keyCode:vkey_code];
return CommandForKeyEvent(event).chrome_command;
}
} // namespace
TEST(GlobalKeyboardShortcuts, BasicFunctionality) {
// Test that an invalid shortcut translates into an invalid command id.
const int kInvalidCommandId = -1;
const int no_key_code = 0;
EXPECT_EQ(
kInvalidCommandId,
CommandForKeys(no_key_code, CommandKeyState::kUp, ShiftKeyState::kUp,
OptionKeyState::kUp, ControlKeyState::kUp));
// Check that all known keyboard shortcuts return valid results.
for (const auto& shortcut : GetShortcutsNotPresentInMainMenu()) {
CommandKeyState command =
shortcut.command_key ? CommandKeyState::kDown : CommandKeyState::kUp;
ShiftKeyState shift =
shortcut.shift_key ? ShiftKeyState::kDown : ShiftKeyState::kUp;
OptionKeyState option =
shortcut.opt_key ? OptionKeyState::kDown : OptionKeyState::kUp;
ControlKeyState control =
shortcut.cntrl_key ? ControlKeyState::kDown : ControlKeyState::kUp;
int cmd_num =
CommandForKeys(shortcut.vkey_code, command, shift, option, control);
EXPECT_EQ(cmd_num, shortcut.chrome_command);
}
// Test that switching tabs triggers off keycodes and not characters (visible
// with the Italian keyboard layout).
EXPECT_EQ(
IDC_SELECT_TAB_0,
CommandForKeys(kVK_ANSI_1, CommandKeyState::kDown, ShiftKeyState::kUp,
OptionKeyState::kUp, ControlKeyState::kUp));
}
TEST(GlobalKeyboardShortcuts, KeypadNumberKeysMatch) {
// Test that the shortcuts that are generated by keypad number keys match the
// equivalent keys.
static const struct {
int keycode;
int keypad_keycode;
} equivalents[] = {
{kVK_ANSI_0, kVK_ANSI_Keypad0},
{kVK_ANSI_1, kVK_ANSI_Keypad1},
{kVK_ANSI_2, kVK_ANSI_Keypad2},
{kVK_ANSI_3, kVK_ANSI_Keypad3},
{kVK_ANSI_4, kVK_ANSI_Keypad4},
{kVK_ANSI_5, kVK_ANSI_Keypad5},
{kVK_ANSI_6, kVK_ANSI_Keypad6},
{kVK_ANSI_7, kVK_ANSI_Keypad7},
{kVK_ANSI_8, kVK_ANSI_Keypad8},
{kVK_ANSI_9, kVK_ANSI_Keypad9},
};
// We only consider unshifted keys. A shifted numpad key gives a different
// keyEquivalent than a shifted number key.
const ShiftKeyState shift = ShiftKeyState::kUp;
for (auto equivalent : equivalents) {
for (CommandKeyState command :
{CommandKeyState::kUp, CommandKeyState::kDown}) {
for (OptionKeyState option :
{OptionKeyState::kUp, OptionKeyState::kDown}) {
for (ControlKeyState control :
{ControlKeyState::kUp, ControlKeyState::kDown}) {
EXPECT_EQ(CommandForKeys(equivalent.keycode, command, shift, option,
control),
CommandForKeys(equivalent.keypad_keycode, command, shift,
option, control));
}
}
}
}
}
|