| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/input/web_input_event_builders_mac.h" | 5 #include "content/browser/renderer_host/input/web_input_event_builders_mac.h" |
| 6 | 6 |
| 7 #include <Carbon/Carbon.h> | 7 #include <Carbon/Carbon.h> |
| 8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 {56, 1 << 1, NSShiftKeyMask}, // Left Shift | 39 {56, 1 << 1, NSShiftKeyMask}, // Left Shift |
| 40 {60, 1 << 2, NSShiftKeyMask}, // Right Shift | 40 {60, 1 << 2, NSShiftKeyMask}, // Right Shift |
| 41 {55, 1 << 3, NSCommandKeyMask}, // Left Command | 41 {55, 1 << 3, NSCommandKeyMask}, // Left Command |
| 42 {54, 1 << 4, NSCommandKeyMask}, // Right Command | 42 {54, 1 << 4, NSCommandKeyMask}, // Right Command |
| 43 {58, 1 << 5, NSAlternateKeyMask}, // Left Alt | 43 {58, 1 << 5, NSAlternateKeyMask}, // Left Alt |
| 44 {61, 1 << 6, NSAlternateKeyMask}, // Right Alt | 44 {61, 1 << 6, NSAlternateKeyMask}, // Right Alt |
| 45 {59, 1 << 0, NSControlKeyMask}, // Left Control | 45 {59, 1 << 0, NSControlKeyMask}, // Left Control |
| 46 {62, 1 << 13, NSControlKeyMask}, // Right Control | 46 {62, 1 << 13, NSControlKeyMask}, // Right Control |
| 47 }; | 47 }; |
| 48 | 48 |
| 49 NSEvent* BuildFakeKeyEvent(NSUInteger key_code, | 49 NSEvent* BuildFakeKeyEventWithModifiedCharacter( |
| 50 unichar character, | 50 NSUInteger key_code, |
| 51 NSUInteger modifier_flags, | 51 unichar character, |
| 52 NSEventType event_type) { | 52 unichar character_ignoring_modifiers, |
| 53 NSUInteger modifier_flags, |
| 54 NSEventType event_type) { |
| 53 NSString* string = [NSString stringWithCharacters:&character length:1]; | 55 NSString* string = [NSString stringWithCharacters:&character length:1]; |
| 56 NSString* string_ignoring_modifiers = |
| 57 [NSString stringWithCharacters:&character_ignoring_modifiers length:1]; |
| 54 return [NSEvent keyEventWithType:event_type | 58 return [NSEvent keyEventWithType:event_type |
| 55 location:NSZeroPoint | 59 location:NSZeroPoint |
| 56 modifierFlags:modifier_flags | 60 modifierFlags:modifier_flags |
| 57 timestamp:0.0 | 61 timestamp:0.0 |
| 58 windowNumber:0 | 62 windowNumber:0 |
| 59 context:nil | 63 context:nil |
| 60 characters:string | 64 characters:string |
| 61 charactersIgnoringModifiers:string | 65 charactersIgnoringModifiers:string_ignoring_modifiers |
| 62 isARepeat:NO | 66 isARepeat:NO |
| 63 keyCode:key_code]; | 67 keyCode:key_code]; |
| 64 } | 68 } |
| 65 | 69 |
| 70 NSEvent* BuildFakeKeyEvent(NSUInteger key_code, |
| 71 unichar character, |
| 72 NSUInteger modifier_flags, |
| 73 NSEventType event_type) { |
| 74 return BuildFakeKeyEventWithModifiedCharacter(key_code, character, character, |
| 75 modifier_flags, event_type); |
| 76 } |
| 77 |
| 66 } // namespace | 78 } // namespace |
| 67 | 79 |
| 68 // Test that arrow keys don't have numpad modifier set. | 80 // Test that arrow keys don't have numpad modifier set. |
| 69 TEST(WebInputEventBuilderMacTest, ArrowKeyNumPad) { | 81 TEST(WebInputEventBuilderMacTest, ArrowKeyNumPad) { |
| 70 // Left | 82 // Left |
| 71 NSEvent* mac_event = BuildFakeKeyEvent(0x7B, NSLeftArrowFunctionKey, | 83 NSEvent* mac_event = BuildFakeKeyEvent(0x7B, NSLeftArrowFunctionKey, |
| 72 NSNumericPadKeyMask, NSKeyDown); | 84 NSNumericPadKeyMask, NSKeyDown); |
| 73 WebKeyboardEvent web_event = WebKeyboardEventBuilder::Build(mac_event); | 85 WebKeyboardEvent web_event = WebKeyboardEventBuilder::Build(mac_event); |
| 74 EXPECT_EQ(0, web_event.modifiers); | 86 EXPECT_EQ(0, web_event.modifiers); |
| 75 EXPECT_EQ(ui::DomCode::ARROW_LEFT, | 87 EXPECT_EQ(ui::DomCode::ARROW_LEFT, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 99 NSKeyDown); | 111 NSKeyDown); |
| 100 web_event = WebKeyboardEventBuilder::Build(mac_event); | 112 web_event = WebKeyboardEventBuilder::Build(mac_event); |
| 101 EXPECT_EQ(0, web_event.modifiers); | 113 EXPECT_EQ(0, web_event.modifiers); |
| 102 EXPECT_EQ(ui::DomCode::ARROW_UP, static_cast<ui::DomCode>(web_event.domCode)); | 114 EXPECT_EQ(ui::DomCode::ARROW_UP, static_cast<ui::DomCode>(web_event.domCode)); |
| 103 EXPECT_EQ(ui::DomKey::ARROW_UP, web_event.domKey); | 115 EXPECT_EQ(ui::DomKey::ARROW_UP, web_event.domKey); |
| 104 } | 116 } |
| 105 | 117 |
| 106 // Test that control sequence generate the correct vkey code. | 118 // Test that control sequence generate the correct vkey code. |
| 107 TEST(WebInputEventBuilderMacTest, ControlSequence) { | 119 TEST(WebInputEventBuilderMacTest, ControlSequence) { |
| 108 // Ctrl-[ generates escape. | 120 // Ctrl-[ generates escape. |
| 109 NSEvent* mac_event = | 121 NSEvent* mac_event = BuildFakeKeyEventWithModifiedCharacter( |
| 110 BuildFakeKeyEvent(0x21, 0x1b, NSControlKeyMask, NSKeyDown); | 122 0x21, 0x1b, '[', NSControlKeyMask, NSKeyDown); |
| 111 WebKeyboardEvent web_event = WebKeyboardEventBuilder::Build(mac_event); | 123 WebKeyboardEvent web_event = WebKeyboardEventBuilder::Build(mac_event); |
| 112 EXPECT_EQ(ui::VKEY_OEM_4, web_event.windowsKeyCode); | 124 EXPECT_EQ(ui::VKEY_OEM_4, web_event.windowsKeyCode); |
| 113 EXPECT_EQ(ui::DomCode::BRACKET_LEFT, | 125 EXPECT_EQ(ui::DomCode::BRACKET_LEFT, |
| 114 static_cast<ui::DomCode>(web_event.domCode)); | 126 static_cast<ui::DomCode>(web_event.domCode)); |
| 115 EXPECT_EQ(ui::DomKey::FromCharacter(0x1b), web_event.domKey); | 127 EXPECT_EQ(ui::DomKey::FromCharacter('['), web_event.domKey); |
| 116 } | 128 } |
| 117 | 129 |
| 118 // Test that numpad keys get mapped correctly. | 130 // Test that numpad keys get mapped correctly. |
| 119 TEST(WebInputEventBuilderMacTest, NumPadMapping) { | 131 TEST(WebInputEventBuilderMacTest, NumPadMapping) { |
| 120 KeyMappingEntry table[] = { | 132 KeyMappingEntry table[] = { |
| 121 {65, '.', ui::VKEY_DECIMAL, ui::DomCode::NUMPAD_DECIMAL, | 133 {65, '.', ui::VKEY_DECIMAL, ui::DomCode::NUMPAD_DECIMAL, |
| 122 ui::DomKey::FromCharacter('.')}, | 134 ui::DomKey::FromCharacter('.')}, |
| 123 {67, '*', ui::VKEY_MULTIPLY, ui::DomCode::NUMPAD_MULTIPLY, | 135 {67, '*', ui::VKEY_MULTIPLY, ui::DomCode::NUMPAD_MULTIPLY, |
| 124 ui::DomKey::FromCharacter('*')}, | 136 ui::DomKey::FromCharacter('*')}, |
| 125 {69, '+', ui::VKEY_ADD, ui::DomCode::NUMPAD_ADD, | 137 {69, '+', ui::VKEY_ADD, ui::DomCode::NUMPAD_ADD, |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 webEvent = WebKeyboardEventBuilder::Build(macEvent); | 251 webEvent = WebKeyboardEventBuilder::Build(macEvent); |
| 240 EXPECT_TRUE(webEvent.isSystemKey); | 252 EXPECT_TRUE(webEvent.isSystemKey); |
| 241 | 253 |
| 242 // Cmd + <some other modifier> + <any letter other then B and I> should be | 254 // Cmd + <some other modifier> + <any letter other then B and I> should be |
| 243 // treated as system event. | 255 // treated as system event. |
| 244 macEvent = BuildFakeKeyEvent(kVK_ANSI_S, 'S', | 256 macEvent = BuildFakeKeyEvent(kVK_ANSI_S, 'S', |
| 245 NSCommandKeyMask | NSShiftKeyMask, NSKeyDown); | 257 NSCommandKeyMask | NSShiftKeyMask, NSKeyDown); |
| 246 webEvent = WebKeyboardEventBuilder::Build(macEvent); | 258 webEvent = WebKeyboardEventBuilder::Build(macEvent); |
| 247 EXPECT_TRUE(webEvent.isSystemKey); | 259 EXPECT_TRUE(webEvent.isSystemKey); |
| 248 } | 260 } |
| 261 |
| 262 // Test conversion from key combination with Control to DomKey. |
| 263 // TODO(chongz): Move DomKey tests for all platforms into one place. |
| 264 // http://crbug.com/587589 |
| 265 TEST(WebInputEventBuilderMacTest, DomKeyWithControlKey) { |
| 266 struct DomKeyTestCase { |
| 267 int mac_key_code; |
| 268 unichar character; |
| 269 unichar shift_character; |
| 270 ui::DomKey ctrl_dom_key; |
| 271 ui::DomKey ctrl_shift_dom_key; |
| 272 } table[] = {{kVK_ANSI_A, 'a', 'A', ui::DomKey::FromCharacter('a'), |
| 273 ui::DomKey::FromCharacter('A')}, |
| 274 {kVK_ANSI_B, 'b', 'B', ui::DomKey::FromCharacter('b'), |
| 275 ui::DomKey::FromCharacter('B')}, |
| 276 {kVK_ANSI_C, 'c', 'C', ui::DomKey::FromCharacter('c'), |
| 277 ui::DomKey::FromCharacter('C')}, |
| 278 {kVK_ANSI_D, 'd', 'D', ui::DomKey::FromCharacter('d'), |
| 279 ui::DomKey::FromCharacter('D')}, |
| 280 {kVK_ANSI_E, 'e', 'E', ui::DomKey::FromCharacter('e'), |
| 281 ui::DomKey::FromCharacter('E')}, |
| 282 {kVK_ANSI_F, 'f', 'F', ui::DomKey::FromCharacter('f'), |
| 283 ui::DomKey::FromCharacter('F')}, |
| 284 {kVK_ANSI_G, 'g', 'G', ui::DomKey::FromCharacter('g'), |
| 285 ui::DomKey::FromCharacter('G')}, |
| 286 {kVK_ANSI_H, 'h', 'H', ui::DomKey::FromCharacter('h'), |
| 287 ui::DomKey::FromCharacter('H')}, |
| 288 {kVK_ANSI_I, 'i', 'I', ui::DomKey::FromCharacter('i'), |
| 289 ui::DomKey::FromCharacter('I')}, |
| 290 {kVK_ANSI_J, 'j', 'J', ui::DomKey::FromCharacter('j'), |
| 291 ui::DomKey::FromCharacter('J')}, |
| 292 {kVK_ANSI_K, 'k', 'K', ui::DomKey::FromCharacter('k'), |
| 293 ui::DomKey::FromCharacter('K')}, |
| 294 {kVK_ANSI_L, 'l', 'L', ui::DomKey::FromCharacter('l'), |
| 295 ui::DomKey::FromCharacter('L')}, |
| 296 {kVK_ANSI_M, 'm', 'M', ui::DomKey::FromCharacter('m'), |
| 297 ui::DomKey::FromCharacter('M')}, |
| 298 {kVK_ANSI_N, 'n', 'N', ui::DomKey::FromCharacter('n'), |
| 299 ui::DomKey::FromCharacter('N')}, |
| 300 {kVK_ANSI_O, 'o', 'O', ui::DomKey::FromCharacter('o'), |
| 301 ui::DomKey::FromCharacter('O')}, |
| 302 {kVK_ANSI_P, 'p', 'P', ui::DomKey::FromCharacter('p'), |
| 303 ui::DomKey::FromCharacter('P')}, |
| 304 {kVK_ANSI_Q, 'q', 'Q', ui::DomKey::FromCharacter('q'), |
| 305 ui::DomKey::FromCharacter('Q')}, |
| 306 {kVK_ANSI_R, 'r', 'R', ui::DomKey::FromCharacter('r'), |
| 307 ui::DomKey::FromCharacter('R')}, |
| 308 {kVK_ANSI_S, 's', 'S', ui::DomKey::FromCharacter('s'), |
| 309 ui::DomKey::FromCharacter('S')}, |
| 310 {kVK_ANSI_T, 't', 'T', ui::DomKey::FromCharacter('t'), |
| 311 ui::DomKey::FromCharacter('T')}, |
| 312 {kVK_ANSI_U, 'u', 'U', ui::DomKey::FromCharacter('u'), |
| 313 ui::DomKey::FromCharacter('U')}, |
| 314 {kVK_ANSI_V, 'v', 'V', ui::DomKey::FromCharacter('v'), |
| 315 ui::DomKey::FromCharacter('V')}, |
| 316 {kVK_ANSI_W, 'w', 'W', ui::DomKey::FromCharacter('w'), |
| 317 ui::DomKey::FromCharacter('W')}, |
| 318 {kVK_ANSI_X, 'x', 'X', ui::DomKey::FromCharacter('x'), |
| 319 ui::DomKey::FromCharacter('X')}, |
| 320 {kVK_ANSI_Y, 'y', 'Y', ui::DomKey::FromCharacter('y'), |
| 321 ui::DomKey::FromCharacter('Y')}, |
| 322 {kVK_ANSI_Z, 'z', 'Z', ui::DomKey::FromCharacter('z'), |
| 323 ui::DomKey::FromCharacter('Z')}}; |
| 324 |
| 325 for (size_t i = 0; i < arraysize(table); ++i) { |
| 326 // Tests ctrl_dom_key |
| 327 NSEvent* mac_event = BuildFakeKeyEventWithModifiedCharacter( |
| 328 table[i].mac_key_code, table[i].character & 0x1F, table[i].character, |
| 329 NSControlKeyMask, NSKeyDown); |
| 330 WebKeyboardEvent web_event = WebKeyboardEventBuilder::Build(mac_event); |
| 331 EXPECT_EQ(table[i].ctrl_dom_key, web_event.domKey); |
| 332 // Tests ctrl_shift_dom_key |
| 333 mac_event = BuildFakeKeyEventWithModifiedCharacter( |
| 334 table[i].mac_key_code, table[i].character & 0x1F, |
| 335 table[i].shift_character, NSControlKeyMask | NSShiftKeyMask, NSKeyDown); |
| 336 web_event = WebKeyboardEventBuilder::Build(mac_event); |
| 337 EXPECT_EQ(table[i].ctrl_shift_dom_key, web_event.domKey); |
| 338 } |
| 339 } |
| OLD | NEW |