Bug 930374 part.2 Fix test_assign_event_data.html for new defaultPrevented behavior and make it a chrome test r=smaug

This commit is contained in:
Masayuki Nakano 2013-12-09 00:51:16 +09:00
parent 91b716ae1a
commit c77bb1ddd8
4 changed files with 158 additions and 46 deletions

View File

@ -9,6 +9,7 @@
* synthesizeMouseAtCenter
* synthesizeWheel
* synthesizeKey
* synthesizeNativeKey
* synthesizeMouseExpectEvent
* synthesizeKeyExpectEvent
*
@ -568,6 +569,114 @@ function synthesizeKey(aKey, aEvent, aWindow)
}
}
function _parseNativeModifiers(aModifiers)
{
var modifiers;
if (aModifiers.capsLockKey) {
modifiers |= 0x00000001;
}
if (aModifiers.numLockKey) {
modifiers |= 0x00000002;
}
if (aModifiers.shiftKey) {
modifiers |= 0x00000100;
}
if (aModifiers.shiftRightKey) {
modifiers |= 0x00000200;
}
if (aModifiers.ctrlKey) {
modifiers |= 0x00000400;
}
if (aModifiers.ctrlRightKey) {
modifiers |= 0x00000800;
}
if (aModifiers.altKey) {
modifiers |= 0x00001000;
}
if (aModifiers.altRightKey) {
modifiers |= 0x00002000;
}
if (aModifiers.metaKey) {
modifiers |= 0x00004000;
}
if (aModifiers.metaRightKey) {
modifiers |= 0x00008000;
}
if (aModifiers.helpKey) {
modifiers |= 0x00010000;
}
if (aModifiers.fnKey) {
modifiers |= 0x00100000;
}
if (aModifiers.numericKeyPadKey) {
modifiers |= 0x01000000;
}
if (aModifiers.accelKey) {
modifiers |=
(navigator.platform.indexOf("Mac") == 0) ? 0x00004000 : 0x00000400;
}
if (aModifiers.accelRightKey) {
modifiers |=
(navigator.platform.indexOf("Mac") == 0) ? 0x00008000 : 0x00000800;
}
return modifiers;
}
const KEYBOARD_LAYOUT_EN_US = 0;
/**
* synthesizeNativeKey() dispatches native key event on active window.
* This is implemented only on Windows and Mac.
*
* @param aKeyboardLayout One of KEYBOARD_LAYOUT_* defined above.
* @param aNativeKeyCode A native keycode value defined in
* NativeKeyCodes.js.
* @param aModifiers Modifier keys. If no modifire key is pressed,
* this must be {}. Otherwise, one or more items
* referred in _parseNativeModifiers() must be
* true.
* @param aChars Specify characters which should be generated
* by the key event.
* @param aUnmodifiedChars Specify characters of unmodified (except Shift)
* aChar value.
* @return True if this function succeed dispatching
* native key event. Otherwise, false.
*/
function synthesizeNativeKey(aKeyboardLayout, aNativeKeyCode, aModifiers,
aChars, aUnmodifiedChars)
{
var utils = _getDOMWindowUtils(window);
if (!utils) {
return false;
}
var nativeKeyboardLayout;
if (navigator.platform.indexOf("Mac") == 0) {
switch (aKeyboardLayout) {
case KEYBOARD_LAYOUT_EN_US:
nativeKeyboardLayout = 0;
break;
default:
return false;
}
} else if (navigator.platform.indexOf("Win") == 0) {
switch (aKeyboardLayout) {
case KEYBOARD_LAYOUT_EN_US:
nativeKeyboardLayout = 0x409;
break;
default:
return false;
}
} else {
return false;
}
utils.sendNativeKeyEvent(nativeKeyboardLayout, aNativeKeyCode,
_parseNativeModifiers(aModifiers),
aChars, aUnmodifiedChars);
return true;
}
var _gSeenEvent = false;
/**

View File

@ -26,7 +26,6 @@ support-files = window_composition_text_querycontent.xul
[test_position_on_resize.xul]
[test_sizemode_events.xul]
[test_bug760802.xul]
[test_assign_event_data.html]
# Cocoa
[test_native_menus.xul]

View File

@ -1,3 +1,5 @@
[test_assign_event_data.html]
skip-if = toolkit == "cocoa" # Bug 933303
[test_bug565392.html]
skip-if = toolkit != "windows"
[test_picker_no_crash.html]

View File

@ -2,11 +2,10 @@
<html>
<head>
<title>Testing ns*Event::Assign*EventData()</title>
<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"></script>
<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/NativeKeyCodes.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/NativeKeyCodes.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
<style>
#a {
background-color: transparent;
@ -50,16 +49,19 @@
SimpleTest.waitForExplicitFinish();
var gUtils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindowUtils);
const kIsMac = (navigator.platform.indexOf("Mac") == 0);
const kIsWin = (navigator.platform.indexOf("Win") == 0);
var gEvent = null;
var gCopiedEvent = [];
var gCallback = null;
var gCallPreventDefault = false;
function onEvent(aEvent)
{
if (gCallPreventDefault) {
aEvent.preventDefault();
}
gEvent = aEvent;
for (var attr in aEvent) {
if (!attr.match(/^[A-Z0-9_]+$/) && // ignore const attributes
@ -71,13 +73,6 @@ function onEvent(aEvent)
setTimeout(gCallback, 0);
}
const WIN_KL_US = 0x409;
const MAC_KL_US = 0;
const NATIVE_SHIFT_LEFT = 0x0100;
const NATIVE_CONTROL_RIGHT = 0x0800;
const NATIVE_META_RIGHT = 0x8000;
const kTests = [
{ description: "InternalScriptErrorEvent",
targetID: "input-text", eventType: "error",
@ -137,11 +132,8 @@ const kTests = [
dispatchEvent: function () {
document.getElementById(this.targetID).value = "";
document.getElementById(this.targetID).focus();
if (kIsWin) {
gUtils.sendNativeKeyEvent(WIN_KL_US, WIN_VK_A, 0, "a", "a");
} else if (kIsMac) {
gUtils.sendNativeKeyEvent(MAC_KL_US, MAC_VK_ANSI_A, 0, "a", "a");
}
synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, kIsWin ? WIN_VK_A : MAC_VK_ANSI_A,
{}, "a", "a");
},
canRun: function () {
return (kIsMac || kIsWin);
@ -153,11 +145,8 @@ const kTests = [
dispatchEvent: function () {
document.getElementById(this.targetID).value = "";
document.getElementById(this.targetID).focus();
if (kIsWin) {
gUtils.sendNativeKeyEvent(WIN_KL_US, WIN_VK_A, 0, "a", "a");
} else if (kIsMac) {
gUtils.sendNativeKeyEvent(MAC_KL_US, MAC_VK_ANSI_A, 0, "a", "a");
}
synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, kIsWin ? WIN_VK_A : MAC_VK_ANSI_A,
{}, "a", "a");
},
canRun: function () {
return (kIsMac || kIsWin);
@ -169,34 +158,26 @@ const kTests = [
dispatchEvent: function () {
document.getElementById(this.targetID).value = "";
document.getElementById(this.targetID).focus();
if (kIsWin) {
gUtils.sendNativeKeyEvent(WIN_KL_US, WIN_VK_B, NATIVE_SHIFT_LEFT, "B", "B");
} else if (kIsMac) {
gUtils.sendNativeKeyEvent(MAC_KL_US, MAC_VK_ANSI_B, NATIVE_SHIFT_LEFT, "B", "B");
}
synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, kIsWin ? WIN_VK_B : MAC_VK_ANSI_B,
{ shiftKey: true }, "B", "B");
},
canRun: function () {
return (kIsMac || kIsWin);
},
// "defaultPrevented" becomes true because the editor consumes the keypress event.
todoMismatch: [ "defaultPrevented" ],
todoMismatch: [],
},
{ description: "WidgetKeyboardEvent (keypress of 'c' key with Accel)",
targetID: "input-text", eventType: "keypress",
dispatchEvent: function () {
document.getElementById(this.targetID).value = "";
document.getElementById(this.targetID).focus();
if (kIsWin) {
gUtils.sendNativeKeyEvent(WIN_KL_US, WIN_VK_C, NATIVE_CONTROL_RIGHT, "\u0003", "c");
} else if (kIsMac) {
gUtils.sendNativeKeyEvent(MAC_KL_US, MAC_VK_ANSI_C, NATIVE_META_RIGHT, "c", "c");
}
synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, kIsWin ? WIN_VK_C : MAC_VK_ANSI_C,
{ accelKey: true }, kIsWin ? "\u0003" : "c", "c");
},
canRun: function () {
return (kIsMac || kIsWin);
},
// "defaultPrevented" becomes true because the editor consumes the keypress event.
todoMismatch: [ "defaultPrevented" ],
todoMismatch: [],
},
{ description: "WidgetMouseEvent (mousedown of left button without modifier)",
targetID: "button", eventType: "mousedown",
@ -539,21 +520,22 @@ function doTest(aTest)
var target = aTest.target ? aTest.target() : document.getElementById(aTest.targetID);
target.addEventListener(aTest.eventType, onEvent, true);
gCallback = function () {
var description = aTest.description + " (gCallPreventDefault=" + gCallPreventDefault + ")";
target.removeEventListener(aTest.eventType, onEvent, true);
ok(gEvent !== null, aTest.description + ": failed to get duplicated event");
ok(gCopiedEvent.length > 0, aTest.description + ": count of attribute of the event must be larger than 0");
ok(gEvent !== null, description + ": failed to get duplicated event");
ok(gCopiedEvent.length > 0, description + ": count of attribute of the event must be larger than 0");
for (var i = 0; i < gCopiedEvent.length; ++i) {
var name = gCopiedEvent[i].name;
if (name == "rangeOffset") {
todo(false, aTest.description + ": " + name + " attribute value is never reset (" + gEvent[name] + ")");
todo(false, description + ": " + name + " attribute value is never reset (" + gEvent[name] + ")");
} else if (name == "eventPhase") {
is(gEvent[name], 0, aTest.description + ": mismatch with fixed value (" + name + ")");
is(gEvent[name], 0, description + ": mismatch with fixed value (" + name + ")");
} else if (name == "rangeParent" || name == "currentTarget") {
is(gEvent[name], null, aTest.description + ": mismatch with fixed value (" + name + ")");
is(gEvent[name], null, description + ": mismatch with fixed value (" + name + ")");
} else if (aTest.todoMismatch.indexOf(name) >= 0) {
todo_is(gEvent[name], gCopiedEvent[i].value, aTest.description + ": mismatch (" + name + ")");
todo_is(gEvent[name], gCopiedEvent[i].value, description + ": mismatch (" + name + ")");
} else {
is(gEvent[name], gCopiedEvent[i].value, aTest.description + ": mismatch (" + name + ")");
is(gEvent[name], gCopiedEvent[i].value, description + ": mismatch (" + name + ")");
}
}
runNextTest();
@ -565,7 +547,27 @@ var gIndex = -1;
function runNextTest()
{
if (++gIndex == kTests.length) {
finish();
if (gCallPreventDefault) {
finish();
return;
}
// Test with a call of preventDefault() of the events.
gCallPreventDefault = true;
gIndex = -1;
// Restoring the initial state of all elements.
document.getElementById("scrollable-div").style.height = "30px";
document.getElementById("scrollable-div").style.width = "30px";
document.getElementById("scrolled-div").style.height = "10px";
document.getElementById("scrolled-div").style.width = "10px";
document.getElementById("input-text").value = "";
document.getElementById("animated-div").className = "";
document.getElementById("animated-div").removeAttribute("x-data");
if (document.activeElement) {
document.activeElement.blur();
}
window.requestAnimationFrame(function () {
setTimeout(runNextTest, 0);
});
return;
}
doTest(kTests[gIndex]);