diff options
author | tapted <tapted@chromium.org> | 2015-01-27 20:20:26 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-28 04:22:18 +0000 |
commit | c3217c83c5638ee9f4669628003b68df6694a02f (patch) | |
tree | 65e48cc5f3cbe03578b527a4caac5ace55544dc6 | |
parent | 189f9ba326f89d6baa2fef0f565a98f6f75ab42e (diff) | |
download | chromium_src-c3217c83c5638ee9f4669628003b68df6694a02f.zip chromium_src-c3217c83c5638ee9f4669628003b68df6694a02f.tar.gz chromium_src-c3217c83c5638ee9f4669628003b68df6694a02f.tar.bz2 |
Mac: Refine ui_controls_mac's SynthesizeKeyEvent()
In preparation for moving SynthesizeKeyEvent to cocoa_test_event_utils,
this refines the behavior of SynthesizeKeyEvent to more closely match
actual events. There were two bugs:
The unichar character being set on a simulated "Delete" key was actually
the unichar that Mac uses for "Backspace" (0x7f). Cocoa actually uses
0xf728 for the delete key (NSDeleteFunctionKey). Note "Backspace" stays
sending 0x8 (Ctrl+H) rather than being updated to 0x7f (ASCII DEL).
Either seems to work for Backspace (which meant simulated Deletes were
becoming deleteBackward action messages rather than deleteForward).
Also, SynthesizeKeyEvent is updated to closely follow the behavior on
Mac wrt Command/Shift modifiers. The bug here was that when *just*
Shift+letter is pressed, characters and charactersIgnoringModifiers
should *both* be uppercase. (r311815 swapped them for Ctrl, but didn't
try to fix Shift).
These changes are needed for comprehensive testing of views::TextField
on Mac. Specifically, to get TextfieldTest.ControlAndSelectTest and
TextfieldTest.ControlAndSelectTest passing with simulated NSEvents.
BUG=378134
Review URL: https://codereview.chromium.org/856313003
Cr-Commit-Position: refs/heads/master@{#313441}
-rw-r--r-- | ui/base/test/ui_controls_mac.mm | 45 | ||||
-rw-r--r-- | ui/events/keycodes/keyboard_code_conversion_mac.mm | 2 |
2 files changed, 31 insertions, 16 deletions
diff --git a/ui/base/test/ui_controls_mac.mm b/ui/base/test/ui_controls_mac.mm index 1308de0..5689518 100644 --- a/ui/base/test/ui_controls_mac.mm +++ b/ui/base/test/ui_controls_mac.mm @@ -96,13 +96,29 @@ NSEvent* SynthesizeKeyEvent(NSWindow* window, // Note that, in line with AppKit's documentation (and tracing "real" events), // -[NSEvent charactersIngoringModifiers]" are "the characters generated by // the receiving key event as if no modifier key (except for Shift)". - // So |charactersIgnoringModifiers| uses |shifted_characters|, while - // |characters| uses keyboard characters. + // So |charactersIgnoringModifiers| uses |shifted_character|. NSString* charactersIgnoringModifiers = [[[NSString alloc] initWithCharacters:&shifted_character length:1] autorelease]; - NSString* characters = - [[[NSString alloc] initWithCharacters:&character length:1] autorelease]; + NSString* characters; + // The following were determined empirically on OSX 10.9. + if (flags & NSControlKeyMask) { + // If Ctrl is pressed, Cocoa always puts an empty string into |characters|. + characters = [NSString string]; + } else if (flags & NSCommandKeyMask) { + // If Cmd is pressed, Cocoa puts a lowercase character into |characters|, + // regardless of Shift. If, however, Alt is also pressed then shift *is* + // preserved, but re-mappings for Alt are not implemented. Although we still + // need to support Alt for things like Alt+Left/Right which don't care. + characters = + [[[NSString alloc] initWithCharacters:&character length:1] autorelease]; + } else { + // If just Shift or nothing is pressed, |characters| will match + // |charactersIgnoringModifiers|. Alt puts a special character into + // |characters| (not |charactersIgnoringModifiers|), but they're not mapped + // here. + characters = charactersIgnoringModifiers; + } NSEventType type = (keyDown ? NSKeyDown : NSKeyUp); @@ -115,17 +131,16 @@ NSEvent* SynthesizeKeyEvent(NSWindow* window, // For events other than mouse moved, [event locationInWindow] is // UNDEFINED if the event is not NSMouseMoved. Thus, the (0,0) // location should be fine. - NSEvent* event = - [NSEvent keyEventWithType:type - location:NSZeroPoint - modifierFlags:flags - timestamp:TimeIntervalSinceSystemStartup() - windowNumber:[window windowNumber] - context:nil - characters:characters - charactersIgnoringModifiers:charactersIgnoringModifiers - isARepeat:NO - keyCode:(unsigned short)macKeycode]; + NSEvent* event = [NSEvent keyEventWithType:type + location:NSZeroPoint + modifierFlags:flags + timestamp:TimeIntervalSinceSystemStartup() + windowNumber:[window windowNumber] + context:nil + characters:characters + charactersIgnoringModifiers:charactersIgnoringModifiers + isARepeat:NO + keyCode:(unsigned short)macKeycode]; return event; } diff --git a/ui/events/keycodes/keyboard_code_conversion_mac.mm b/ui/events/keycodes/keyboard_code_conversion_mac.mm index 4caa7a5..4d5cb1f 100644 --- a/ui/events/keycodes/keyboard_code_conversion_mac.mm +++ b/ui/events/keycodes/keyboard_code_conversion_mac.mm @@ -66,7 +66,7 @@ const KeyCodeMap kKeyCodesMap[] = { { VKEY_EXECUTE /* 0x2B */, -1, NSExecuteFunctionKey }, { VKEY_SNAPSHOT /* 0x2C */, -1, NSPrintScreenFunctionKey }, { VKEY_INSERT /* 0x2D */, -1, NSInsertFunctionKey }, - { VKEY_DELETE /* 0x2E */, kVK_ForwardDelete, kDeleteCharCode }, + { VKEY_DELETE /* 0x2E */, kVK_ForwardDelete, NSDeleteFunctionKey }, { VKEY_HELP /* 0x2F */, kVK_Help, kHelpCharCode }, { VKEY_0 /* 0x30 */, kVK_ANSI_0, '0' }, { VKEY_1 /* 0x31 */, kVK_ANSI_1, '1' }, |