Bug 426082, part 4: Test. r=smaug

This commit is contained in:
Markus Stange 2010-05-31 19:50:55 +02:00
parent 23751c9400
commit 6439c85c56
2 changed files with 148 additions and 0 deletions

View File

@ -67,6 +67,7 @@ _TEST_FILES = \
test_bug405632.html \
test_bug409604.html \
test_bug412567.html \
test_bug426082.html \
test_bug443985.html \
test_bug447736.html \
test_bug448602.html \

View File

@ -0,0 +1,147 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=426082
-->
<head>
<title>Test for Bug 426082</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/WindowSnapshot.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<style>
canvas {
display: none;
}
</style>
</head>
<body onload="runTests()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=426082">Mozilla Bug 426082</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript;version=1.8">
/** Test for Bug 426082 **/
SimpleTest.waitForExplicitFinish();
var normalButtonCanvas, pressedButtonCanvas, currentSnapshot, button, label, outside;
function runTests() {
normalButtonCanvas = $("normalButtonCanvas");
pressedButtonCanvas = $("pressedButtonCanvas");
currentSnapshot = $("currentSnapshot");
button = $("button");
label = $("label");
outside = $("outside");
SimpleTest.executeSoon(start);
}
function start() {
takeSnapshot(normalButtonCanvas);
executeTests();
}
function isRectContainedInRectFromRegion(rect, region) {
return Array.some(region, function (r) {
return rect.left >= r.left &&
rect.top >= r.top &&
rect.right <= r.right &&
rect.bottom <= r.bottom;
});
}
function paintListener(e) {
if (isRectContainedInRectFromRegion(buttonRect(), e.clientRects))
takeSnapshot(currentSnapshot);
}
function executeTests() {
var testYielder = tests();
function execNext() {
try {
testYielder.next();
SimpleTest.executeSoon(execNext);
} catch (e) {}
}
execNext();
}
function tests() {
window.addEventListener("MozAfterPaint", paintListener, false);
// Press the label.
sendMouseEvent("mousemove", label);
sendMouseEvent("mousedown", label);
yield;
compareSnapshots_(normalButtonCanvas, currentSnapshot, false, "Pressing the label should have pressed the button.");
takeSnapshot(pressedButtonCanvas);
// Move the mouse down from the label.
sendMouseEvent("mousemove", outside);
yield;
compareSnapshots_(normalButtonCanvas, currentSnapshot, true, "Moving the mouse down from the label should have unpressed the button.");
// ... and up again.
sendMouseEvent("mousemove", label);
yield;
compareSnapshots_(pressedButtonCanvas, currentSnapshot, true, "Moving the mouse back on top of the label should have pressed the button.");
// Release.
sendMouseEvent("mouseup", label);
yield;
compareSnapshots_(normalButtonCanvas, currentSnapshot, true, "Releasing the mouse over the label should have unpressed the button.");
// Press the label and remove it.
sendMouseEvent("mousemove", label);
sendMouseEvent("mousedown", label);
yield;
label.parentNode.removeChild(label);
yield;
compareSnapshots_(normalButtonCanvas, currentSnapshot, true, "Removing the label should have unpressed the button.");
sendMouseEvent("mouseup", label);
window.removeEventListener("MozAfterPaint", paintListener, false);
SimpleTest.finish();
}
function sendMouseEvent(t, elem) {
var r = elem.getBoundingClientRect();
synthesizeMouse(elem, r.width / 2, r.height / 2, {type: t});
}
function compareSnapshots_(c1, c2, shouldBeIdentical, msg) {
var [correct, c1url, c2url] = compareSnapshots(c1, c2, shouldBeIdentical);
if (correct) {
ok(true, msg);
} else {
if (shouldBeIdentical) {
ok(false, msg + " - expected " + c1url + " but got " + c2url);
} else {
ok(false, msg + " - expected something other than " + c1url);
}
}
}
function takeSnapshot(canvas) {
var r = buttonRect();
var ctx = canvas.getContext("2d");
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawWindow(window, r.left, r.top, r.width, r.height, "#FFF");
}
function buttonRect() {
return button.getBoundingClientRect();
}
</script>
</pre>
<p><input type="button" value="Button" id="button"></p>
<p><label for="button" id="label">Label</label></p>
<p id="outside">Something under the label</p>
<canvas id="normalButtonCanvas" width="200" height="100"></canvas>
<canvas id="pressedButtonCanvas" width="200" height="100"></canvas>
<canvas id="currentSnapshot" width="200" height="100"></canvas>
</body>
</html>