dom/events/test/bug656379-1.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial