dom/events/test/bug426082.html

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:4e655af9ec40
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=426082
5 -->
6 <head>
7 <title>Test for Bug 426082</title>
8 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
9 <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
10 <script type="application/javascript" src="/tests/SimpleTest/WindowSnapshot.js"></script>
11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
12 <style>
13 canvas {
14 display: none;
15 }
16 </style>
17 </head>
18 <body onload="runTests()">
19 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=426082">Mozilla Bug 426082</a>
20 <p id="display"></p>
21 <div id="content" style="display: none">
22
23 </div>
24 <p><input type="button" value="Button" id="button"></p>
25 <p><label for="button" id="label">Label</label></p>
26 <p id="outside">Something under the label</p>
27 <pre id="test">
28 <script type="application/javascript;version=1.8">
29
30 /** Test for Bug 426082 **/
31
32 var normalButtonCanvas, pressedButtonCanvas, normalFocusedButtonCanvas,
33 pressedFocusedButtonCanvas, currentSnapshot, button, label, outside;
34
35 function runTests() {
36 button = $("button");
37 label = $("label");
38 outside = $("outside");
39 SimpleTest.executeSoon(executeTests);
40 }
41
42 function isRectContainedInRectFromRegion(rect, region) {
43 return Array.some(region, function (r) {
44 return rect.left >= r.left &&
45 rect.top >= r.top &&
46 rect.right <= r.right &&
47 rect.bottom <= r.bottom;
48 });
49 }
50
51 function paintListener(e) {
52 if (isRectContainedInRectFromRegion(buttonRect(), SpecialPowers.wrap(e).clientRects)) {
53 gNeedsPaint = false;
54 currentSnapshot = takeSnapshot();
55 }
56 }
57
58 var gNeedsPaint = false;
59 function executeTests() {
60 var testYielder = tests();
61 function execNext() {
62 try {
63 if (!gNeedsPaint) {
64 testYielder.next();
65 button.getBoundingClientRect(); // Flush.
66 gNeedsPaint = true;
67 }
68 SimpleTest.executeSoon(execNext);
69 } catch (e) {}
70 }
71 execNext();
72 }
73
74 function tests() {
75 window.addEventListener("MozAfterPaint", paintListener, false);
76 normalButtonCanvas = takeSnapshot();
77 // Press the button.
78 sendMouseEvent("mousemove", button);
79 sendMouseEvent("mousedown", button);
80 yield undefined;
81 pressedFocusedButtonCanvas = takeSnapshot();
82 compareSnapshots_(normalButtonCanvas, pressedFocusedButtonCanvas, false, "Pressed focused buttons should look different from normal buttons.");
83 // Release.
84 sendMouseEvent("mouseup", button);
85 yield undefined;
86 // make sure the button is focused as this doesn't happen on click on Mac
87 button.focus();
88 normalFocusedButtonCanvas = takeSnapshot();
89 compareSnapshots_(normalFocusedButtonCanvas, pressedFocusedButtonCanvas, false, "Pressed focused buttons should look different from normal focused buttons.");
90 // Unfocus the button.
91 sendMouseEvent("mousedown", outside);
92 sendMouseEvent("mouseup", outside);
93 yield undefined;
94
95 // Press the label.
96 sendMouseEvent("mousemove", label);
97 sendMouseEvent("mousedown", label);
98 yield undefined;
99 compareSnapshots_(normalButtonCanvas, currentSnapshot, false, "Pressing the label should have pressed the button.");
100 pressedButtonCanvas = takeSnapshot();
101 // Move the mouse down from the label.
102 sendMouseEvent("mousemove", outside);
103 yield undefined;
104 compareSnapshots_(normalButtonCanvas, currentSnapshot, true, "Moving the mouse down from the label should have unpressed the button.");
105 // ... and up again.
106 sendMouseEvent("mousemove", label);
107 yield undefined;
108 compareSnapshots_(pressedButtonCanvas, currentSnapshot, true, "Moving the mouse back on top of the label should have pressed the button.");
109 // Release.
110 sendMouseEvent("mouseup", label);
111 yield undefined;
112 compareSnapshots_(normalFocusedButtonCanvas, currentSnapshot, true, "Releasing the mouse over the label should have unpressed (and focused) the button.");
113 // Press the label and remove it.
114 sendMouseEvent("mousemove", label);
115 sendMouseEvent("mousedown", label);
116 yield undefined;
117 label.parentNode.removeChild(label);
118 yield undefined;
119 compareSnapshots_(normalButtonCanvas, currentSnapshot, true, "Removing the label should have unpressed the button.");
120 sendMouseEvent("mouseup", label);
121 window.removeEventListener("MozAfterPaint", paintListener, false);
122 window.opener.finishTests();
123 }
124
125 function sendMouseEvent(t, elem) {
126 var r = elem.getBoundingClientRect();
127 synthesizeMouse(elem, r.width / 2, r.height / 2, {type: t});
128 }
129
130 function compareSnapshots_(c1, c2, shouldBeIdentical, msg) {
131 var [correct, c1url, c2url] = compareSnapshots(c1, c2, shouldBeIdentical);
132 if (correct) {
133 if (shouldBeIdentical) {
134 window.opener.ok(true, msg + " - expected " + c1url);
135 } else {
136 window.opener.ok(true, msg + " - got " + c1url + " and " + c2url);
137 }
138 } else {
139 if (shouldBeIdentical) {
140 window.opener.ok(false, msg + " - expected " + c1url + " but got " + c2url);
141 } else {
142 window.opener.ok(false, msg + " - expected something other than " + c1url);
143 }
144 }
145 }
146
147 function takeSnapshot() {
148 var r = buttonRect();
149 adjustedRect = { left: r.left - 2, top: r.top - 2,
150 width: r.width + 4, height: r.height + 4 };
151 return SpecialPowers.snapshotRect(window, adjustedRect);
152 }
153
154 function buttonRect() {
155 return button.getBoundingClientRect();
156 }
157 </script>
158 </pre>
159 </body>
160 </html>

mercurial