summaryrefslogtreecommitdiffstats
path: root/dom/events/test/bug426082.html
blob: 8eaaa7391a447cf65f7dca3740b1188b9b59898a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=426082
-->
<head>
  <title>Test for Bug 426082</title>
  <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>
<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>
<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>
<pre id="test">
<script type="application/javascript;version=1.8">

/** Test for Bug 426082 **/

var normalButtonCanvas, pressedButtonCanvas, normalFocusedButtonCanvas,
    pressedFocusedButtonCanvas, currentSnapshot, button, label, outside;

function runTests() {
  button = $("button");
  label = $("label");
  outside = $("outside");
  SimpleTest.executeSoon(executeTests);
}

SimpleTest.waitForFocus(runTests);

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(), SpecialPowers.wrap(e).clientRects)) {
    gNeedsPaint = false;
    currentSnapshot = takeSnapshot();
  }
}

var gNeedsPaint = false;
function executeTests() {
  var testYielder = tests();
  function execNext() {
    try {
      if (!gNeedsPaint) {
        testYielder.next();
        button.getBoundingClientRect(); // Flush.
        gNeedsPaint = true;
      }
      SimpleTest.executeSoon(execNext);
    } catch (e) {}
  }
  execNext();
}

function tests() {
  window.addEventListener("MozAfterPaint", paintListener, false);
  normalButtonCanvas = takeSnapshot();
  // Press the button.
  sendMouseEvent("mousemove", button);
  sendMouseEvent("mousedown", button);
  yield undefined;
  pressedFocusedButtonCanvas = takeSnapshot();
  compareSnapshots_(normalButtonCanvas, pressedFocusedButtonCanvas, false, "Pressed focused buttons should look different from normal buttons.");
  // Release.
  sendMouseEvent("mouseup", button);
  yield undefined;
  // make sure the button is focused as this doesn't happen on click on Mac
  button.focus();
  normalFocusedButtonCanvas = takeSnapshot();
  compareSnapshots_(normalFocusedButtonCanvas, pressedFocusedButtonCanvas, false, "Pressed focused buttons should look different from normal focused buttons.");
  // Unfocus the button.
  sendMouseEvent("mousedown", outside);
  sendMouseEvent("mouseup", outside);
  yield undefined;

  // Press the label.
  sendMouseEvent("mousemove", label);
  sendMouseEvent("mousedown", label);
  yield undefined;
  compareSnapshots_(normalButtonCanvas, currentSnapshot, false, "Pressing the label should have pressed the button.");
  pressedButtonCanvas = takeSnapshot();
  // Move the mouse down from the label.
  sendMouseEvent("mousemove", outside);
  yield undefined;
  compareSnapshots_(normalButtonCanvas, currentSnapshot, true, "Moving the mouse down from the label should have unpressed the button.");
  // ... and up again.
  sendMouseEvent("mousemove", label);
  yield undefined;
  compareSnapshots_(pressedButtonCanvas, currentSnapshot, true, "Moving the mouse back on top of the label should have pressed the button.");
  // Release.
  sendMouseEvent("mouseup", label);
  yield undefined;
  var focusOnMouse = (navigator.platform.indexOf("Mac") != 0);
  compareSnapshots_(focusOnMouse ? normalFocusedButtonCanvas : normalButtonCanvas,
                    currentSnapshot, true, "Releasing the mouse over the label should have unpressed" +
                                            (focusOnMouse ? " (and focused)" : "") + " the button.");
  // Press the label and remove it.
  sendMouseEvent("mousemove", label);
  sendMouseEvent("mousedown", label);
  yield undefined;
  label.parentNode.removeChild(label);
  yield undefined;
  compareSnapshots_(normalButtonCanvas, currentSnapshot, true, "Removing the label should have unpressed the button.");
  sendMouseEvent("mouseup", label);
  window.removeEventListener("MozAfterPaint", paintListener, false);
  window.opener.finishTests();
 }

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) {
    if (shouldBeIdentical) {
      window.opener.ok(true, msg + " - expected " + c1url);
    } else {
      window.opener.ok(true, msg + " - got " + c1url + " and " + c2url);
    }
  } else {
    if (shouldBeIdentical) {
      window.opener.ok(false, msg + " - expected " + c1url + " but got " + c2url);
    } else {
      window.opener.ok(false, msg + " - expected something other than " + c1url);
    }
  }
}

function takeSnapshot() {
  var r = buttonRect();
  adjustedRect = { left: r.left - 2, top: r.top - 2,
                   width: r.width + 4, height: r.height + 4 };
  return SpecialPowers.snapshotRect(window, adjustedRect);
}

function buttonRect() {
  return button.getBoundingClientRect();
}
</script>
</pre>
</body>
</html>