Bug 989198, Patch 5: Mochitest for BeforeAfterKeyboardEvent, r=smaug.

This commit is contained in:
Gina Yeh 2014-11-03 15:05:48 +08:00
parent a349e38686
commit 980a143e18
5 changed files with 518 additions and 0 deletions

View File

@ -0,0 +1,20 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Embedded iframe</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="getFocus();">
<p id="display"></p>
<div id="content" style="display: none"></div>
<input id="input" style="display: block;">
<pre id="test">
<script type="application/javascript">
function getFocus() {
input = document.getElementById("input");
input.focus();
}
</script>
</body>
</html>

View File

@ -0,0 +1,192 @@
/*
* Helper functions for testing BeforeAfterKeyboardEvent.
*/
const kUnknownEvent = 0x000;
const kKeyDownEvent = 0x001;
const kKeyUpEvent = 0x002;
const kBeforeEvent = 0x010;
const kAfterEvent = 0x020;
const kParent = 0x100;
const kChild = 0x200;
var gCurrentTest;
function frameScript()
{
function handler(e) {
var results = sendSyncMessage("forwardevent", { type: e.type });
if (results[0]) {
e.preventDefault();
}
}
addEventListener('keydown', handler);
addEventListener('keyup', handler);
addEventListener('mozbrowserbeforekeydown', handler);
addEventListener('mozbrowserbeforekeyup', handler);
addEventListener('mozbrowserafterkeydown', handler);
addEventListener('mozbrowserafterkeyup', handler);
}
function prepareTest(useRemote)
{
if (useRemote) {
setupHandlers(window, embedderHandler);
} else {
setupHandlers(window, embedderHandlerWithCheck);
}
var iframe = document.createElement("iframe");
iframe.id = "embedded";
iframe.src = "bug989198_embedded.html";
iframe.setAttribute("remote", useRemote ? "true" : "false");
SpecialPowers.wrap(iframe).mozbrowser = true;
iframe.addEventListener("mozbrowserloadend", function onloadend() {
iframe.removeEventListener("mozbrowserloadend", onloadend);
iframe.focus();
var mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
mm.addMessageListener("forwardevent", function(msg) {
return embeddedHandler(msg.json);
});
mm.loadFrameScript("data:,(" + frameScript.toString() + ")();", false);
runTests();
return;
});
document.body.appendChild(iframe);
}
function setupHandlers(element, handler)
{
element.addEventListener('keydown', handler);
element.addEventListener('keyup', handler);
element.addEventListener('mozbrowserbeforekeydown', handler);
element.addEventListener('mozbrowserbeforekeyup', handler);
element.addEventListener('mozbrowserafterkeydown', handler);
element.addEventListener('mozbrowserafterkeyup', handler);
}
function teardownHandlers(element, handler)
{
element.removeEventListener('keydown', handler);
element.removeEventListener('keyup', handler);
element.removeEventListener('mozbrowserbeforekeydown', handler);
element.removeEventListener('mozbrowserbeforekeyup', handler);
element.removeEventListener('mozbrowserafterkeydown', handler);
element.removeEventListener('mozbrowserafterkeyup', handler);
}
function convertNameToCode(name)
{
switch (name) {
case "mozbrowserbeforekeydown":
return kBeforeEvent | kKeyDownEvent;
case "mozbrowserafterkeydown":
return kAfterEvent | kKeyDownEvent;
case "mozbrowserbeforekeyup":
return kBeforeEvent | kKeyUpEvent;
case "mozbrowserafterkeyup":
return kAfterEvent | kKeyUpEvent;
case "keydown":
return kKeyDownEvent;
case "keyup":
return kKeyUpEvent;
default:
return kUnknownEvent;
}
}
function classifyEvents(test)
{
// Categorize resultEvents into KEYDOWN group and KEYUP group.
for (var i = 0; i < gCurrentTest.resultEvents.length ; i++) {
var code = test.resultEvents[i];
if ((code & 0xF) == 0x1) { // KEYDOWN
test.classifiedEvents[0].push(code);
} else if ((code & 0xF) == 0x2) { // KEYUP
test.classifiedEvents[1].push(code);
} else {
ok(false, "Invalid code for events");
}
}
}
function verifyResults(test)
{
for (var i = 0; i < gCurrentTest.expectedEvents.length; i++) {
is(test.classifiedEvents[i].length,
test.expectedEvents[i].length,
test.description + ": Wrong number of events");
for (var j = 0; j < gCurrentTest.classifiedEvents[i].length; j++) {
var item = test.classifiedEvents[i][j];
is(item, test.expectedEvents[i][j],
test.description + ": Wrong order of events");
}
}
}
function embeddedHandler(e)
{
return handler(e, kChild);
}
function embedderHandler(e, callback)
{
handler(e, kParent, callback);
}
function handler(e, highBit, callback)
{
var code = convertNameToCode(e.type);
var newCode = highBit | code;
gCurrentTest.resultEvents.push(newCode);
if (callback) {
callback(code);
}
if (highBit == kChild) {
// return and let frameScript to handle
return newCode == gCurrentTest.doPreventDefaultAt;
}
if (newCode == gCurrentTest.doPreventDefaultAt) {
e.preventDefault();
}
}
function embedderHandlerWithCheck(e)
{
// Verify value of attribute embeddedCancelled
embedderHandler(e, function checkEmbeddedCancelled(code){
switch (code) {
case kBeforeEvent | kKeyDownEvent:
case kBeforeEvent | kKeyUpEvent:
is(e.embeddedCancelled, null,
gCurrentTest.description + ": embeddedCancelled should be null");
break;
case kAfterEvent | kKeyDownEvent:
if ((gCurrentTest.doPreventDefaultAt & 0xFF) == kKeyDownEvent) {
is(e.embeddedCancelled, true,
gCurrentTest.description + ": embeddedCancelled should be true");
} else {
is(e.embeddedCancelled, false,
gCurrentTest.description + ": embeddedCancelled should be false");
}
break;
case kAfterEvent | kKeyUpEvent:
if ((gCurrentTest.doPreventDefaultAt & 0xFF) == kKeyUpEvent) {
is(e.embeddedCancelled, true,
gCurrentTest.description + ": embeddedCancelled should be true");
} else {
is(e.embeddedCancelled, false,
gCurrentTest.description + ": embeddedCancelled should be false");
}
break;
default:
break;
}
});
}

View File

@ -167,3 +167,12 @@ skip-if = buildapp == 'mulet'
[test_onerror_handler_args.html]
[test_wheel_default_action.html]
skip-if = buildapp == 'mulet' || buildapp == 'b2g' || e10s
[test_dom_before_after_keyboard_event.html]
support-files =
bug989198_embedded.html
bug989198_helper.js
[test_dom_before_after_keyboard_event_remote.html]
support-files =
bug989198_embedded.html
bug989198_helper.js
skip-if = buildapp == 'b2g' || e10s

View File

@ -0,0 +1,135 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for Bug 989198</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/NativeKeyCodes.js"></script>
<script type="text/javascript" src="bug989198_helper.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="runTests();">
<a target="_blank"
href="https://bugzilla.mozilla.org/show_bug.cgi?id=989198">Mozilla Bug 989198</a>
<p id="display"></p>
<pre id="test">
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
function cleanupTest()
{
teardownHandlers(window, embedderHandler);
runTests();
}
function testEventOrderAndAttr()
{
const kTests = [
{
description: "Testing the order of the events",
expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
kChild | kKeyDownEvent,
kParent | kAfterEvent | kKeyDownEvent ],
[ kParent | kBeforeEvent | kKeyUpEvent,
kChild | kKeyUpEvent,
kParent | kAfterEvent | kKeyUpEvent ] ],
resultEvents: [],
classifiedEvents: [ [], [] ],
doPreventDefaultAt: kUnknownEvent
},
{
description: "Testing the order of the events, calling preventDefault() at \"mozbrowserbeforekeydown\" event",
expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
kParent | kAfterEvent | kKeyDownEvent ],
[ kParent | kBeforeEvent | kKeyUpEvent,
kChild | kKeyUpEvent,
kParent | kAfterEvent | kKeyUpEvent ] ],
resultEvents: [],
classifiedEvents: [ [], [] ],
doPreventDefaultAt: kParent | kBeforeEvent | kKeyDownEvent
},
{
description: "Testing the order of the events, calling preventDefault() at \"mozbrowserbeforekeyup\" event",
expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
kChild | kKeyDownEvent,
kParent | kAfterEvent | kKeyDownEvent ],
[ kParent | kBeforeEvent | kKeyUpEvent,
kParent | kAfterEvent | kKeyUpEvent ] ],
resultEvents: [],
classifiedEvents: [ [], [] ],
doPreventDefaultAt: kParent | kBeforeEvent | kKeyUpEvent
},
{
description: "Testing the order of the events, calling preventDefault() at \"keydown\" event",
expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
kChild | kKeyDownEvent,
kParent | kAfterEvent | kKeyDownEvent ],
[ kParent | kBeforeEvent | kKeyUpEvent,
kChild | kKeyUpEvent,
kParent | kAfterEvent | kKeyUpEvent ] ],
resultEvents: [],
classifiedEvents: [ [], [] ],
doPreventDefaultAt: kChild | kKeyDownEvent
},
{
description: "Testing the order of the events, calling preventDefault() at \"keyup\" event",
expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
kChild | kKeyDownEvent,
kParent | kAfterEvent | kKeyDownEvent ],
[ kParent | kBeforeEvent | kKeyUpEvent,
kChild | kKeyUpEvent,
kParent | kAfterEvent | kKeyUpEvent ] ],
resultEvents: [],
classifiedEvents: [ [], [] ],
doPreventDefaultAt: kChild | kKeyUpEvent
}
];
for (var k = 0; k < kTests.length; k++ ) {
gCurrentTest = kTests[k];
synthesizeKey('a', {}, document.getElementById("embedded").contentWindow);
classifyEvents(kTests[k]);
verifyResults(kTests[k]);
}
runTests();
}
var tests = [
function addPermissions() {
SpecialPowers.pushPermissions(
[{ type: "before-after-keyboard-event", allow: true, context: document },
{ type: "browser", allow: true, context: document }],
runTests);
},
function addPreferences() {
SpecialPowers.pushPrefEnv(
{ "set": [["dom.beforeAfterKeyboardEvent.enabled", true],
["dom.mozBrowserFramesEnabled", true],
["dom.ipc.tabs.disabled", false]] },
runTests);
},
// Tests for in-process iframe, i.e. <iframe mozbrowser>.
function () {
prepareTest(false);
},
testEventOrderAndAttr,
cleanupTest,
];
function runTests()
{
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
}
</script>
</body>
</html>

View File

@ -0,0 +1,162 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for Bug 989198</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/NativeKeyCodes.js"></script>
<script type="text/javascript" src="bug989198_helper.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="runTests();">
<a target="_blank"
href="https://bugzilla.mozilla.org/show_bug.cgi?id=989198">Mozilla Bug 989198</a>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
var testsForEventOrder = [
{
description: "Testing the order of the events (OOP)",
expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
kParent | kKeyDownEvent,
kChild | kKeyDownEvent,
kParent | kAfterEvent | kKeyDownEvent ],
[ kParent | kBeforeEvent | kKeyUpEvent,
kParent | kKeyUpEvent,
kChild | kKeyUpEvent,
kParent | kAfterEvent | kKeyUpEvent ] ],
resultEvents: [],
classifiedEvents: [ [], [] ],
doPreventDefaultAt: kUnknownEvent
},
{
description: "Testing the order of the events (OOP), calling preventDefault() at \"mozbrowserbeforekeydown\" event",
expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
kParent | kAfterEvent | kKeyDownEvent ],
[ kParent | kBeforeEvent | kKeyUpEvent,
kParent | kKeyUpEvent,
kChild | kKeyUpEvent,
kParent | kAfterEvent | kKeyUpEvent ] ],
resultEvents: [],
classifiedEvents: [ [], [] ],
doPreventDefaultAt: kParent | kBeforeEvent | kKeyDownEvent
},
{
description: "Testing the order of the events (OOP), calling preventDefault() at \"mozbrowserbeforekeyup\" event",
expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
kParent | kKeyDownEvent,
kChild | kKeyDownEvent,
kParent | kAfterEvent | kKeyDownEvent ],
[ kParent | kBeforeEvent | kKeyUpEvent,
kParent | kAfterEvent | kKeyUpEvent ] ],
resultEvents: [],
classifiedEvents: [ [], [] ],
doPreventDefaultAt: kParent | kBeforeEvent | kKeyUpEvent
},
{
description: "Testing the order of the events (OOP), calling preventDefault() at \"keydown\" event",
expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
kParent | kKeyDownEvent,
kChild | kKeyDownEvent,
kParent | kAfterEvent | kKeyDownEvent ],
[ kParent | kBeforeEvent | kKeyUpEvent,
kParent | kKeyUpEvent,
kChild | kKeyUpEvent,
kParent | kAfterEvent | kKeyUpEvent ] ],
resultEvents: [],
classifiedEvents: [ [], [] ],
doPreventDefaultAt: kChild | kKeyDownEvent
},
{
description: "Testing the order of the events (OOP), calling preventDefault() at \"keyup\" event",
expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
kParent | kKeyDownEvent,
kChild | kKeyDownEvent,
kParent | kAfterEvent | kKeyDownEvent ],
[ kParent | kBeforeEvent | kKeyUpEvent,
kParent | kKeyUpEvent,
kChild | kKeyUpEvent,
kParent | kAfterEvent | kKeyUpEvent ] ],
resultEvents: [],
classifiedEvents: [ [], [] ],
doPreventDefaultAt: kChild | kKeyUpEvent
}
];
function cleanupTest()
{
teardownHandlers(window, embedderHandler);
runTests();
}
function testEventOrder()
{
if (!testsForEventOrder.length) {
runTests();
return;
}
gCurrentTest = testsForEventOrder.shift();
synthesizeKey('a', {}, document.getElementById("embedded").contentWindow);
// It take some time to propagate events to a remote iframe.
waitAndVerifyResult(0);
}
function waitAndVerifyResult(aCount) {
expectedEventLength = gCurrentTest.expectedEvents[0].length +
gCurrentTest.expectedEvents[1].length;
if (gCurrentTest.resultEvents.length >= expectedEventLength || aCount > 10) {
classifyEvents(gCurrentTest);
verifyResults(gCurrentTest);
testEventOrder();
}
else {
setTimeout(function () waitAndVerifyResult(aCount + 1),
100);
}
}
var tests = [
function addPermissions() {
SpecialPowers.pushPermissions(
[{ type: "before-after-keyboard-event", allow: true, context: document },
{ type: "browser", allow: true, context: document }],
runTests);
},
function addPreferences() {
SpecialPowers.pushPrefEnv(
{ "set": [["dom.beforeAfterKeyboardEvent.enabled", true],
["dom.mozBrowserFramesEnabled", true],
["dom.ipc.tabs.disabled", false]] },
runTests);
},
// Tests for out-of-process iframe, i.el. <iframe mozbrowser remote>
function () {
prepareTest(true);
},
testEventOrder,
cleanupTest
];
function runTests()
{
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
}
</script>
</body>
</html>