1 // Copyright 2015 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 "content/browser/renderer_host/input/web_input_event_builders_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/events/keycodes/keyboard_codes.h"
12 using blink::WebKeyboardEvent;
13 using blink::WebInputEvent;
14 using content::WebKeyboardEventBuilder;
18 struct KeyMappingEntry {
26 int left_or_right_mask;
27 int non_specific_mask;
30 // Modifier keys, grouped into left/right pairs.
31 const ModifierKey kModifierKeys[] = {
32 {56, 1 << 1, NSShiftKeyMask}, // Left Shift
33 {60, 1 << 2, NSShiftKeyMask}, // Right Shift
34 {55, 1 << 3, NSCommandKeyMask}, // Left Command
35 {54, 1 << 4, NSCommandKeyMask}, // Right Command
36 {58, 1 << 5, NSAlternateKeyMask}, // Left Alt
37 {61, 1 << 6, NSAlternateKeyMask}, // Right Alt
38 {59, 1 << 0, NSControlKeyMask}, // Left Control
39 {62, 1 << 13, NSControlKeyMask}, // Right Control
42 NSEvent* BuildFakeKeyEvent(NSUInteger key_code,
44 NSUInteger modifier_flags,
45 NSEventType event_type) {
46 NSString* string = [NSString stringWithCharacters:&character length:1];
47 return [NSEvent keyEventWithType:event_type
49 modifierFlags:modifier_flags
54 charactersIgnoringModifiers:string
61 // Test that arrow keys don't have numpad modifier set.
62 TEST(WebInputEventBuilderMacTest, ArrowKeyNumPad) {
64 NSEvent* mac_event = BuildFakeKeyEvent(0x7B, NSLeftArrowFunctionKey,
65 NSNumericPadKeyMask, NSKeyDown);
66 WebKeyboardEvent web_event = WebKeyboardEventBuilder::Build(mac_event);
67 EXPECT_EQ(0, web_event.modifiers);
70 mac_event = BuildFakeKeyEvent(0x7C, NSRightArrowFunctionKey,
71 NSNumericPadKeyMask, NSKeyDown);
72 web_event = WebKeyboardEventBuilder::Build(mac_event);
73 EXPECT_EQ(0, web_event.modifiers);
76 mac_event = BuildFakeKeyEvent(0x7D, NSDownArrowFunctionKey,
77 NSNumericPadKeyMask, NSKeyDown);
78 web_event = WebKeyboardEventBuilder::Build(mac_event);
79 EXPECT_EQ(0, web_event.modifiers);
82 mac_event = BuildFakeKeyEvent(0x7E, NSUpArrowFunctionKey, NSNumericPadKeyMask,
84 web_event = WebKeyboardEventBuilder::Build(mac_event);
85 EXPECT_EQ(0, web_event.modifiers);
88 // Test that numpad keys get mapped correctly.
89 TEST(WebInputEventBuilderMacTest, NumPadMapping) {
90 KeyMappingEntry table[] = {
91 {65, '.', ui::VKEY_DECIMAL},
92 {67, '*', ui::VKEY_MULTIPLY},
93 {69, '+', ui::VKEY_ADD},
94 {71, NSClearLineFunctionKey, ui::VKEY_CLEAR},
95 {75, '/', ui::VKEY_DIVIDE},
96 {76, 3, ui::VKEY_RETURN},
97 {78, '-', ui::VKEY_SUBTRACT},
98 {81, '=', ui::VKEY_OEM_PLUS},
99 {82, '0', ui::VKEY_0},
100 {83, '1', ui::VKEY_1},
101 {84, '2', ui::VKEY_2},
102 {85, '3', ui::VKEY_3},
103 {86, '4', ui::VKEY_4},
104 {87, '5', ui::VKEY_5},
105 {88, '6', ui::VKEY_6},
106 {89, '7', ui::VKEY_7},
107 {91, '8', ui::VKEY_8},
108 {92, '9', ui::VKEY_9},
111 for (size_t i = 0; i < arraysize(table); ++i) {
112 NSEvent* mac_event = BuildFakeKeyEvent(table[i].mac_key_code,
113 table[i].character, 0, NSKeyDown);
114 WebKeyboardEvent web_event = WebKeyboardEventBuilder::Build(mac_event);
115 EXPECT_EQ(table[i].windows_key_code, web_event.windowsKeyCode);
119 // Test that left- and right-hand modifier keys are interpreted correctly when
120 // pressed simultaneously.
121 TEST(WebInputEventFactoryTestMac, SimultaneousModifierKeys) {
122 for (size_t i = 0; i < arraysize(kModifierKeys) / 2; ++i) {
123 const ModifierKey& left = kModifierKeys[2 * i];
124 const ModifierKey& right = kModifierKeys[2 * i + 1];
125 // Press the left key.
126 NSEvent* mac_event = BuildFakeKeyEvent(
127 left.mac_key_code, 0, left.left_or_right_mask | left.non_specific_mask,
129 WebKeyboardEvent web_event = WebKeyboardEventBuilder::Build(mac_event);
130 EXPECT_EQ(WebInputEvent::RawKeyDown, web_event.type);
131 // Press the right key
133 BuildFakeKeyEvent(right.mac_key_code, 0,
134 left.left_or_right_mask | right.left_or_right_mask |
135 left.non_specific_mask,
137 web_event = WebKeyboardEventBuilder::Build(mac_event);
138 EXPECT_EQ(WebInputEvent::RawKeyDown, web_event.type);
139 // Release the right key
140 mac_event = BuildFakeKeyEvent(
141 right.mac_key_code, 0, left.left_or_right_mask | left.non_specific_mask,
143 // Release the left key
144 mac_event = BuildFakeKeyEvent(left.mac_key_code, 0, 0, NSFlagsChanged);
145 web_event = WebKeyboardEventBuilder::Build(mac_event);
146 EXPECT_EQ(WebInputEvent::KeyUp, web_event.type);
150 // Test that individual modifier keys are still reported correctly, even if the
151 // undocumented left- or right-hand flags are not set.
152 TEST(WebInputEventBuilderMacTest, MissingUndocumentedModifierFlags) {
153 for (size_t i = 0; i < arraysize(kModifierKeys); ++i) {
154 const ModifierKey& key = kModifierKeys[i];
155 NSEvent* mac_event = BuildFakeKeyEvent(
156 key.mac_key_code, 0, key.non_specific_mask, NSFlagsChanged);
157 WebKeyboardEvent web_event = WebKeyboardEventBuilder::Build(mac_event);
158 EXPECT_EQ(WebInputEvent::RawKeyDown, web_event.type);
159 mac_event = BuildFakeKeyEvent(key.mac_key_code, 0, 0, NSFlagsChanged);
160 web_event = WebKeyboardEventBuilder::Build(mac_event);
161 EXPECT_EQ(WebInputEvent::KeyUp, web_event.type);