bug656379-1.html 6.0 KB

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