gecko-dev/content/events/test/test_bug508906.html

212 lines
6.3 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=508906
-->
<head>
<title>Test for Bug 508906</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=508906">Mozilla Bug 508906</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.8">
/** Test for Bug 508906 - MozTouch* Events **/
let tests = [], testTarget, parent;
function nextTest() {
if (tests.length)
SimpleTest.executeSoon(tests.shift());
}
function random() {
return Math.floor(Math.random() * 100);
}
function createTestEventValue(name) {
let detail = random();
let screenX = random();
let screenY = random();
let clientX = random();
let clientY = random();
let ctrlKey = random() % 2 ? true : false;
let altKey = random() % 2 ? true : false;
let shiftKey = random() % 2 ? true : false;
let metaKey = random() % 2 ? true : false;
let button = random();
let streamId = random();
return function() {
let event = document.createEvent("MozTouchEvent");
event.initMozTouchEvent(name, true, true, window,
detail, screenX, screenY, clientX, clientY,
ctrlKey, altKey, shiftKey, metaKey, button,
null, streamId);
function check(ev) {
is(ev.detail, detail, "Correct detail");
is(ev.screenX, screenX, "Correct screenX");
is(ev.screenY, screenY, "Correct screenY");
is(ev.clientX, clientX, "Correct clientX");
is(ev.clientY, clientY, "Correct clientY");
is(ev.ctrlKey, ctrlKey, "Correct ctrlKey");
is(ev.altKey, altKey, "Correct altKey");
is(ev.shiftKey, shiftKey, "Correct shiftKey");
is(ev.metaKey, metaKey, "Correct metaKey");
is(ev.button, button, "Correct buttonArg");
is(ev.streamId, streamId, "Correct streamId");
}
for each (let target in [document, window, testTarget, parent])
target.addEventListener(name, check, false);
testTarget.dispatchEvent(event);
for each (let target in [document, window, testTarget, parent])
target.removeEventListener(name, check, false);
nextTest();
}
}
function testDefaultArg() {
let event = document.createEvent("MozTouchEvent");
event.initMouseEvent("MozTouchDown", true, true, window, 0, 0, 0, 0, 0,
false, false, false, false, 0, null);
testTarget.addEventListener("MozTouchDown", function(ev) {
testTarget.removeEventListener("MozTouchDown", arguments.callee, false);
is(ev.streamId, 0, "Correct default streamId");
}, false);
testTarget.dispatchEvent(event);
nextTest();
}
function testStopPropagation() {
let event = document.createEvent("MozTouchEvent");
event.initMozTouchEvent("MozTouchDown", true, true, window, 0, 0, 0, 0, 0,
false, false, false, false, 0, null, 0);
let unreachableListener = function () {
ok(false, "Event should have been stopped");
}
// Capturing phase
let captured = false;
parent.addEventListener("MozTouchDown", function() {
parent.removeEventListener("MozTouchDown", arguments.callee, true);
captured = true;
}, true); // Capturing phase
// Bubbling phase
parent.addEventListener("MozTouchDown", unreachableListener, false);
testTarget.addEventListener("MozTouchDown", function(ev) {
testTarget.removeEventListener("MozTouchDown", arguments.callee, false);
is(captured, true, "Event should have been captured");
ev.stopPropagation();
}, false);
testTarget.dispatchEvent(event);
parent.removeEventListener("MozTouchDown", unreachableListener, false);
nextTest();
}
function testPreventDefault() {
let event = document.createEvent("MozTouchEvent");
event.initMozTouchEvent("MozTouchDown", true, true, window, 0, 0, 0, 0, 0,
false, false, false, false, 0, null, 0);
parent.addEventListener("MozTouchDown", function(ev) {
parent.removeEventListener("MozTouchDown", arguments.callee, false);
is(ev.defaultPrevented, true, "preventDefault can be called");
nextTest();
}, false);
testTarget.addEventListener("MozTouchDown", function(ev) {
testTarget.removeEventListener("MozTouchDown", arguments.callee, false);
ev.preventDefault();
}, false);
testTarget.dispatchEvent(event);
}
function testBlockPreventDefault() {
let event = document.createEvent("MozTouchEvent");
event.initMozTouchEvent("MozTouchDown", true, false, window, 0, 0, 0, 0, 0,
false, false, false, false, 0, null, 0);
parent.addEventListener("MozTouchDown", function(ev) {
parent.removeEventListener("MozTouchDown", arguments.callee, false);
is(ev.defaultPrevented, false, "aCancelableArg works");
nextTest();
}, false);
testTarget.addEventListener("MozTouchDown", function(ev) {
testTarget.removeEventListener("MozTouchDown", arguments.callee, false);
ev.preventDefault();
}, false);
testTarget.dispatchEvent(event);
}
function testBlockBubbling() {
let unreachableListener = function () {
ok(false, "aCanBubble doesn't work");
}
let event = document.createEvent("MozTouchEvent");
event.initMozTouchEvent("MozTouchDown", false, true, window, 0, 0, 0, 0, 0,
false, false, false, false, 0, null, 0);
parent.addEventListener("MozTouchDown", unreachableListener, false);
testTarget.dispatchEvent(event);
parent.removeEventListener("MozTouchDown", unreachableListener, false);
nextTest();
}
function doTest() {
testTarget = document.getElementById("testTarget");
parent = testTarget.parentNode;
tests.push(createTestEventValue("MozTouchDown"));
tests.push(createTestEventValue("MozTouchMove"));
tests.push(createTestEventValue("MozTouchUp"));
tests.push(testDefaultArg);
tests.push(testStopPropagation);
tests.push(testPreventDefault);
tests.push(testBlockPreventDefault);
tests.push(testBlockBubbling);
tests.push(function() {
SimpleTest.finish();
});
nextTest();
}
SimpleTest.waitForExplicitFinish();
addLoadEvent(doTest);
</script>
</pre>
<div id="parent">
<span id="testTarget" style="border: 1px solid black;">testTarget</span>
</div>
</body>
</html>