Commit c3217c83 authored by tapted's avatar tapted Committed by Commit bot

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}
parent 189f9ba3
......@@ -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;
}
......
......@@ -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' },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment