gecko/dom/events/test/test_bug1096146.html
2014-11-21 17:48:04 +08:00

171 lines
4.0 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1096146
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1096146</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.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=1096146">Mozilla Bug 1096146</a>
<div id="content" style="display: none;">
</div>
<pre id="test">
<script type="application/javascript">
const kKeydownEvent = 0x1;
const kScrollEvent = 0x2;
var gCurrentTest = 0;
var kTests = [
{
description: "no preventDefault at 'mozbrowserbeforekeydown'",
expectedEvents: kKeydownEvent | kScrollEvent,
resultEvents: 0x0,
doPreventDefault: false
},
{
description: "do preventDefault at 'mozbrowserbeforekeydown'",
expectedEvents: 0x0,
resultEvents: 0x0,
doPreventDefault: true
}
]
function frameScript()
{
function handler(e) {
sendSyncMessage("forwardevent", { type: e.type });
}
addEventListener('keydown', handler);
addEventListener('scroll', handler);
}
function testDefaultAction()
{
synthesizeKey('VK_END', {}, document.getElementById("embedded").contentWindow);
setTimeout(function () {
is(kTests[gCurrentTest].expectedEvents,
kTests[gCurrentTest].resultEvents,
"verify result");
runTests();
}, 500);
}
function prepareTest()
{
var handler;
if (kTests[gCurrentTest].doPreventDefault) {
handler = preventDefaultHandler;
} else {
handler = noPreventDefaultHandler;
}
window.addEventListener("mozbrowserbeforekeydown", handler);
var iframe = document.createElement("iframe");
iframe.id = "embedded";
iframe.src = "bug1096146_embedded.html";
iframe.setAttribute("remote", "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) {
var value = 0;
switch(msg.json.type) {
case "scroll":
value = kScrollEvent;
break;
case "keydown":
value = kKeydownEvent;
break;
default:
ok(false, "unexpected event");
}
kTests[gCurrentTest].resultEvents =
kTests[gCurrentTest].resultEvents | value;
});
mm.loadFrameScript("data:,(" + frameScript.toString() + ")();", false);
runTests();
return;
});
document.body.appendChild(iframe);
}
function preventDefaultHandler(evt)
{
ok(true, "receive " + evt.type + " and do preventDefault.");
evt.preventDefault();
}
function noPreventDefaultHandler(evt)
{
ok(true, "receive " + evt.type + ".");
}
function teardownHandler()
{
var handler;
if (kTests[gCurrentTest].doPreventDefault) {
handler = preventDefaultHandler;
} else {
handler = noPreventDefaultHandler;
}
window.removeEventListener("mozbrowserbeforekeydown", handler);
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);
},
prepareTest,
testDefaultAction,
teardownHandler,
function() {
gCurrentTest++;
runTests();
},
prepareTest,
testDefaultAction,
teardownHandler
];
function runTests()
{
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
}
</script>
</pre>
</body>
</html>