зеркало из https://github.com/mozilla/gecko-dev.git
Bug 282097 - Part 7: Simulate native events for testing. r=masayuki
This commit is contained in:
Родитель
a03124214a
Коммит
7c416da7e2
|
@ -997,6 +997,7 @@ nsDOMWindowUtils::SendKeyEvent(const nsAString& aType,
|
|||
|
||||
event.refPoint.x = event.refPoint.y = 0;
|
||||
event.time = PR_IntervalNow();
|
||||
event.mFlags.mIsSynthesizedForTests = true;
|
||||
|
||||
if (aAdditionalFlags & KEY_FLAG_PREVENT_DEFAULT) {
|
||||
event.mFlags.mDefaultPrevented = true;
|
||||
|
|
|
@ -378,6 +378,15 @@ public:
|
|||
const nsAString& aCharacters,
|
||||
const nsAString& aUnmodifiedCharacters);
|
||||
|
||||
/**
|
||||
* Utility method intended for testing. Attempts to construct a native key
|
||||
* event that would have been generated during an actual key press. This
|
||||
* *does not dispatch* the native event. Instead, it is attached to the
|
||||
* |mNativeKeyEvent| field of the Gecko event that is passed in.
|
||||
* @param aKeyEvent Gecko key event to attach the native event to
|
||||
*/
|
||||
NS_IMETHOD AttachNativeKeyEvent(nsKeyEvent& aKeyEvent);
|
||||
|
||||
/**
|
||||
* GetWindowLevel() returns the window level of current focused (in Gecko)
|
||||
* window. E.g., if an <input> element in XUL panel has focus, this returns
|
||||
|
|
|
@ -4282,6 +4282,76 @@ TextInputHandlerBase::GetWindowLevel()
|
|||
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NSNormalWindowLevel);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
TextInputHandlerBase::AttachNativeKeyEvent(nsKeyEvent& aKeyEvent)
|
||||
{
|
||||
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
||||
|
||||
// Don't try to replace a native event if one already exists.
|
||||
// OS X doesn't have an OS modifier, can't make a native event.
|
||||
if (aKeyEvent.mNativeKeyEvent || aKeyEvent.modifiers & MODIFIER_OS) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
PR_LOG(gLog, PR_LOG_ALWAYS,
|
||||
("%p TextInputHandlerBase::AttachNativeKeyEvent, key=0x%X, char=0x%X, "
|
||||
"mod=0x%X", this, aKeyEvent.keyCode, aKeyEvent.charCode,
|
||||
aKeyEvent.modifiers));
|
||||
|
||||
NSEventType eventType;
|
||||
if (aKeyEvent.message == NS_KEY_UP) {
|
||||
eventType = NSKeyUp;
|
||||
} else {
|
||||
eventType = NSKeyDown;
|
||||
}
|
||||
|
||||
static const uint32_t sModifierFlagMap[][2] = {
|
||||
{ MODIFIER_SHIFT, NSShiftKeyMask },
|
||||
{ MODIFIER_CONTROL, NSControlKeyMask },
|
||||
{ MODIFIER_ALT, NSAlternateKeyMask },
|
||||
{ MODIFIER_ALTGRAPH, NSAlternateKeyMask },
|
||||
{ MODIFIER_META, NSCommandKeyMask },
|
||||
{ MODIFIER_CAPSLOCK, NSAlphaShiftKeyMask },
|
||||
{ MODIFIER_NUMLOCK, NSNumericPadKeyMask }
|
||||
};
|
||||
|
||||
NSUInteger modifierFlags = 0;
|
||||
for (uint32_t i = 0; i < ArrayLength(sModifierFlagMap); ++i) {
|
||||
if (aKeyEvent.modifiers & sModifierFlagMap[i][0]) {
|
||||
modifierFlags |= sModifierFlagMap[i][1];
|
||||
}
|
||||
}
|
||||
|
||||
NSInteger windowNumber = [[mView window] windowNumber];
|
||||
|
||||
NSString* characters;
|
||||
if (aKeyEvent.charCode) {
|
||||
characters = [NSString stringWithCharacters:
|
||||
reinterpret_cast<const unichar*>(&(aKeyEvent.charCode)) length:1];
|
||||
} else {
|
||||
uint32_t cocoaCharCode =
|
||||
nsCocoaUtils::ConvertGeckoKeyCodeToMacCharCode(aKeyEvent.keyCode);
|
||||
characters = [NSString stringWithCharacters:
|
||||
reinterpret_cast<const unichar*>(&cocoaCharCode) length:1];
|
||||
}
|
||||
|
||||
aKeyEvent.mNativeKeyEvent =
|
||||
[NSEvent keyEventWithType:eventType
|
||||
location:NSMakePoint(0,0)
|
||||
modifierFlags:modifierFlags
|
||||
timestamp:0
|
||||
windowNumber:windowNumber
|
||||
context:[NSGraphicsContext currentContext]
|
||||
characters:characters
|
||||
charactersIgnoringModifiers:characters
|
||||
isARepeat:NO
|
||||
keyCode:0]; // Native key code not currently needed
|
||||
|
||||
return NS_OK;
|
||||
|
||||
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
||||
}
|
||||
|
||||
bool
|
||||
TextInputHandlerBase::SetSelection(NSRange& aRange)
|
||||
{
|
||||
|
|
|
@ -1586,6 +1586,12 @@ NS_IMETHODIMP nsChildView::DispatchEvent(nsGUIEvent* event, nsEventStatus& aStat
|
|||
NS_IS_KEY_EVENT(event)),
|
||||
"Any key events should not be fired during IME composing");
|
||||
|
||||
if (event->mFlags.mIsSynthesizedForTests && NS_IS_KEY_EVENT(event)) {
|
||||
nsKeyEvent* keyEvent = reinterpret_cast<nsKeyEvent*>(event);
|
||||
nsresult rv = mTextInputHandler->AttachNativeKeyEvent(*keyEvent);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
|
||||
aStatus = nsEventStatus_eIgnore;
|
||||
|
||||
nsIWidgetListener* listener = mWidgetListener;
|
||||
|
|
Загрузка…
Ссылка в новой задаче