OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 | |
33 #import <Cocoa/Cocoa.h> | |
34 #include <gtest/gtest.h> | |
35 | |
36 #include "core/events/KeyboardEvent.h" | |
37 #include "platform/WindowsKeyboardCodes.h" | |
38 #include "public/web/WebInputEvent.h" | |
39 #include "public/web/mac/WebInputEventFactory.h" | |
40 | |
41 using blink::WebInputEventFactory; | |
42 using blink::WebKeyboardEvent; | |
43 using blink::WebInputEvent; | |
44 | |
45 namespace { | |
46 | |
47 struct KeyMappingEntry { | |
48 int macKeyCode; | |
49 unichar character; | |
50 int windowsKeyCode; | |
51 }; | |
52 | |
53 struct ModifierKey { | |
54 int macKeyCode; | |
55 int leftOrRightMask; | |
56 int nonSpecificMask; | |
57 }; | |
58 | |
59 // Modifier keys, grouped into left/right pairs. | |
60 ModifierKey modifierKeys[] = { | |
61 { 56, 1 << 1, NSShiftKeyMask }, // Left Shift | |
62 { 60, 1 << 2, NSShiftKeyMask }, // Right Shift | |
63 { 55, 1 << 3, NSCommandKeyMask }, // Left Command | |
64 { 54, 1 << 4, NSCommandKeyMask }, // Right Command | |
65 { 58, 1 << 5, NSAlternateKeyMask }, // Left Alt | |
66 { 61, 1 << 6, NSAlternateKeyMask }, // Right Alt | |
67 { 59, 1 << 0, NSControlKeyMask }, // Left Control | |
68 { 62, 1 << 13, NSControlKeyMask }, // Right Control | |
69 }; | |
70 | |
71 NSEvent* BuildFakeKeyEvent(NSUInteger keyCode, unichar character, NSUInteger mod
ifierFlags, NSEventType eventType) | |
72 { | |
73 NSString* string = [NSString stringWithCharacters:&character length:1]; | |
74 return [NSEvent keyEventWithType:eventType | |
75 location:NSZeroPoint | |
76 modifierFlags:modifierFlags | |
77 timestamp:0.0 | |
78 windowNumber:0 | |
79 context:nil | |
80 characters:string | |
81 charactersIgnoringModifiers:string | |
82 isARepeat:NO | |
83 keyCode:keyCode]; | |
84 } | |
85 | |
86 } // namespace | |
87 | |
88 // Test that arrow keys don't have numpad modifier set. | |
89 TEST(WebInputEventFactoryTestMac, ArrowKeyNumPad) | |
90 { | |
91 // Left | |
92 NSEvent* macEvent = BuildFakeKeyEvent(0x7B, NSLeftArrowFunctionKey, | |
93 NSNumericPadKeyMask, NSKeyDown); | |
94 WebKeyboardEvent webEvent = WebInputEventFactory::keyboardEvent(macEvent); | |
95 EXPECT_EQ(0, webEvent.modifiers); | |
96 | |
97 // Right | |
98 macEvent = BuildFakeKeyEvent(0x7C, NSRightArrowFunctionKey, | |
99 NSNumericPadKeyMask, NSKeyDown); | |
100 webEvent = WebInputEventFactory::keyboardEvent(macEvent); | |
101 EXPECT_EQ(0, webEvent.modifiers); | |
102 | |
103 // Down | |
104 macEvent = BuildFakeKeyEvent(0x7D, NSDownArrowFunctionKey, | |
105 NSNumericPadKeyMask, NSKeyDown); | |
106 webEvent = WebInputEventFactory::keyboardEvent(macEvent); | |
107 EXPECT_EQ(0, webEvent.modifiers); | |
108 | |
109 // Up | |
110 macEvent = BuildFakeKeyEvent(0x7E, NSUpArrowFunctionKey, | |
111 NSNumericPadKeyMask, NSKeyDown); | |
112 webEvent = WebInputEventFactory::keyboardEvent(macEvent); | |
113 EXPECT_EQ(0, webEvent.modifiers); | |
114 } | |
115 | |
116 // Test that numpad keys get mapped correctly. | |
117 TEST(WebInputEventFactoryTestMac, NumPadMapping) | |
118 { | |
119 KeyMappingEntry table[] = | |
120 { | |
121 {65, '.', VK_DECIMAL}, | |
122 {67, '*', VK_MULTIPLY}, | |
123 {69, '+', VK_ADD}, | |
124 {71, NSClearLineFunctionKey, VK_CLEAR}, | |
125 {75, '/', VK_DIVIDE}, | |
126 {76, 3, VK_RETURN}, | |
127 {78, '-', VK_SUBTRACT}, | |
128 {81, '=', VK_OEM_PLUS}, | |
129 {82, '0', VK_NUMPAD0}, | |
130 {83, '1', VK_NUMPAD1}, | |
131 {84, '2', VK_NUMPAD2}, | |
132 {85, '3', VK_NUMPAD3}, | |
133 {86, '4', VK_NUMPAD4}, | |
134 {87, '5', VK_NUMPAD5}, | |
135 {88, '6', VK_NUMPAD6}, | |
136 {89, '7', VK_NUMPAD7}, | |
137 {91, '8', VK_NUMPAD8}, | |
138 {92, '9', VK_NUMPAD9}, | |
139 }; | |
140 | |
141 for (size_t i = 0; i < arraysize(table); ++i) { | |
142 NSEvent* macEvent = BuildFakeKeyEvent(table[i].macKeyCode, | |
143 table[i].character, 0, NSKeyDown); | |
144 WebKeyboardEvent webEvent = WebInputEventFactory::keyboardEvent(macEvent
); | |
145 EXPECT_EQ(table[i].windowsKeyCode, webEvent.windowsKeyCode); | |
146 } | |
147 } | |
148 | |
149 // Test that left- and right-hand modifier keys are interpreted correctly when | |
150 // pressed simultaneously. | |
151 TEST(WebInputEventFactoryTestMac, SimultaneousModifierKeys) | |
152 { | |
153 for (size_t i = 0; i < arraysize(modifierKeys) / 2; ++i) { | |
154 const ModifierKey& left = modifierKeys[2 * i]; | |
155 const ModifierKey& right = modifierKeys[2 * i + 1]; | |
156 // Press the left key. | |
157 NSEvent* macEvent = BuildFakeKeyEvent( | |
158 left.macKeyCode, 0, left.leftOrRightMask | left.nonSpecificMask, | |
159 NSFlagsChanged); | |
160 WebKeyboardEvent webEvent = WebInputEventFactory::keyboardEvent(macEvent
); | |
161 EXPECT_EQ(WebInputEvent::RawKeyDown, webEvent.type); | |
162 // Press the right key | |
163 macEvent = BuildFakeKeyEvent( | |
164 right.macKeyCode, 0, | |
165 left.leftOrRightMask | right.leftOrRightMask | left.nonSpecificMask, | |
166 NSFlagsChanged); | |
167 webEvent = WebInputEventFactory::keyboardEvent(macEvent); | |
168 EXPECT_EQ(WebInputEvent::RawKeyDown, webEvent.type); | |
169 // Release the right key | |
170 macEvent = BuildFakeKeyEvent( | |
171 right.macKeyCode, 0, left.leftOrRightMask | left.nonSpecificMask, | |
172 NSFlagsChanged); | |
173 // Release the left key | |
174 macEvent = BuildFakeKeyEvent(left.macKeyCode, 0, 0,NSFlagsChanged); | |
175 webEvent = WebInputEventFactory::keyboardEvent(macEvent); | |
176 EXPECT_EQ(WebInputEvent::KeyUp, webEvent.type); | |
177 } | |
178 } | |
179 | |
180 // Test that individual modifier keys are still reported correctly, even if the | |
181 // undocumented left- or right-hand flags are not set. | |
182 TEST(WebInputEventFactoryTestMac, MissingUndocumentedModifierFlags) | |
183 { | |
184 for (size_t i = 0; i < arraysize(modifierKeys); ++i) { | |
185 const ModifierKey& key = modifierKeys[i]; | |
186 NSEvent* macEvent = BuildFakeKeyEvent( | |
187 key.macKeyCode, 0, key.nonSpecificMask, NSFlagsChanged); | |
188 WebKeyboardEvent webEvent = WebInputEventFactory::keyboardEvent(macEvent
); | |
189 EXPECT_EQ(WebInputEvent::RawKeyDown, webEvent.type); | |
190 macEvent = BuildFakeKeyEvent(key.macKeyCode, 0, 0, NSFlagsChanged); | |
191 webEvent = WebInputEventFactory::keyboardEvent(macEvent); | |
192 EXPECT_EQ(WebInputEvent::KeyUp, webEvent.type); | |
193 } | |
194 } | |
OLD | NEW |