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