test_bug238987.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=238987
  5. -->
  6. <head>
  7. <title>Test for Bug 238987</title>
  8. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  10. </head>
  11. <body>
  12. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=238987">Mozilla Bug 238987</a>
  13. <p id="display"></p>
  14. <div id="content" style="display: none">
  15. </div>
  16. <pre id="test">
  17. <script class="testbody" type="text/javascript">
  18. /** Test for Bug 238987 **/
  19. var shouldStop = false;
  20. var modifier = 0;
  21. var expectedResult = "i1,i2,i3,i4,i5,i6,i7,i8,number,i9,i10,i11,i12";
  22. var forwardFocusArray = expectedResult.split(",");
  23. var backwardFocusArray = expectedResult.split(",");
  24. var forwardBlurArray = expectedResult.split(",");
  25. var backwardBlurArray = expectedResult.split(",");
  26. // Adding 3 for "begin", "end", "begin" and one for the <a> in the Mochitest template,
  27. var expectedWindowFocusCount = forwardFocusArray.length + backwardFocusArray.length + 4;
  28. // but the last blur event goes to i1, not "begin".
  29. var expectedWindowBlurCount = forwardFocusArray.length + backwardFocusArray.length + 3;
  30. function handleFocus(e) {
  31. if (e.target.id == "begin") {
  32. // if the modifier is set, the test is coming back from the end.
  33. if (modifier) {
  34. shouldStop = true;
  35. }
  36. } else if (e.target.id == "end") {
  37. modifier = Components.interfaces.nsIDOMEvent.SHIFT_MASK;
  38. } else if (modifier) {
  39. var expected = backwardFocusArray.pop();
  40. ok(expected == e.target.id,
  41. "(focus) Backward tabbing, expected [" +
  42. expected + "], got [" + e.target.id + "]");
  43. } else {
  44. var expected = forwardFocusArray.shift();
  45. is(e.target, document.activeElement, "Wrong activeElement!");
  46. ok(expected == e.target.id,
  47. "(focus) Forward tabbing, expected [" +
  48. expected + "], got [" + e.target.id + "]");
  49. }
  50. }
  51. function handleWindowFocus(e) {
  52. --expectedWindowFocusCount;
  53. var s = "target " + e.target;
  54. if ("id" in e.target) {
  55. s = s + ", id=\"" + e.target.id + "\"";
  56. }
  57. ok(e.eventPhase == Components.interfaces.nsIDOMEvent.CAPTURING_PHASE,
  58. "|window| should not have got a focus event, " + s);
  59. }
  60. function handleBlur(e) {
  61. if (e.target.id == "begin" || e.target.id == "end") {
  62. return;
  63. } else if (modifier) {
  64. var expected = backwardBlurArray.pop();
  65. ok(expected == e.target.id,
  66. "(blur) backward tabbing, expected [" +
  67. expected + "], got [" + e.target.id + "]");
  68. } else {
  69. var expected = forwardBlurArray.shift();
  70. ok(expected == e.target.id,
  71. "(blur) forward tabbing, expected [" +
  72. expected + "], got [" + e.target.id + "]");
  73. }
  74. }
  75. function handleWindowBlur(e) {
  76. --expectedWindowBlurCount;
  77. var s = "target " + e.target;
  78. if ("id" in e.target) {
  79. s = s + ", id=\"" + e.target.id + "\"";
  80. }
  81. ok(e.eventPhase == Components.interfaces.nsIDOMEvent.CAPTURING_PHASE,
  82. "|window| should not have got a blur event, " + s);
  83. }
  84. function tab() {
  85. var utils = SpecialPowers.DOMWindowUtils;
  86. // Send tab key events.
  87. var key = Components.interfaces.nsIDOMKeyEvent.DOM_VK_TAB;
  88. utils.sendKeyEvent("keydown", key, 0, modifier);
  89. utils.sendKeyEvent("keypress", key, 0, modifier);
  90. utils.sendKeyEvent("keyup", key, 0, modifier);
  91. if (shouldStop) {
  92. // Did focus handling succeed
  93. is(forwardFocusArray.length, 0,
  94. "Not all forward tabbing focus tests were run, " +
  95. forwardFocusArray.toString());
  96. is(backwardFocusArray.length, 0,
  97. "Not all backward tabbing focus tests were run, " +
  98. backwardFocusArray.toString());
  99. is(expectedWindowFocusCount, 0,
  100. "|window| didn't get the right amount of focus events");
  101. // and blur.
  102. is(forwardBlurArray.length, 0,
  103. "Not all forward tabbing blur tests were run, " +
  104. forwardBlurArray.toString());
  105. is(backwardBlurArray.length, 0,
  106. "Not all backward tabbing blur tests were run, " +
  107. backwardBlurArray.toString());
  108. is(expectedWindowBlurCount, 0,
  109. "|window| didn't get the right amount of blur events");
  110. // Cleanup
  111. window.removeEventListener("focus", handleWindowFocus, true);
  112. window.removeEventListener("focus", handleWindowFocus, false);
  113. window.removeEventListener("blur", handleWindowBlur, true);
  114. window.removeEventListener("blur", handleWindowBlur, false);
  115. var elements = document.getElementsByTagName("*");
  116. for (var i = 0; i < elements.length; ++i) {
  117. if (elements[i].hasAttribute("id")) {
  118. elements[i].removeEventListener("focus", handleFocus, false);
  119. elements[i].removeEventListener("blur", handleBlur, false);
  120. }
  121. }
  122. SimpleTest.finish();
  123. } else {
  124. setTimeout(tab, 0);
  125. }
  126. }
  127. function start() {
  128. window.focus();
  129. window.addEventListener("focus", handleWindowFocus, true);
  130. window.addEventListener("focus", handleWindowFocus, false);
  131. window.addEventListener("blur", handleWindowBlur, true);
  132. window.addEventListener("blur", handleWindowBlur, false);
  133. var elements = document.getElementsByTagName("*");
  134. for (var i = 0; i < elements.length; ++i) {
  135. if (elements[i].hasAttribute("id")) {
  136. elements[i].addEventListener("focus", handleFocus, false);
  137. elements[i].addEventListener("blur", handleBlur, false);
  138. }
  139. if (elements[i].getAttribute("tabindex") == "1") {
  140. elements[i].setAttribute("tabindex", "-1");
  141. }
  142. }
  143. tab();
  144. }
  145. // accessibility.tabfocus must be set to value 7 before running test also
  146. // on a mac.
  147. function doTest() {
  148. SpecialPowers.pushPrefEnv({"set": [["accessibility.tabfocus", 7]]}, start);
  149. }
  150. SimpleTest.waitForExplicitFinish();
  151. addLoadEvent(doTest);
  152. </script>
  153. </pre>
  154. <h4 tabindex="0" id="begin">Test:</h4>
  155. <table>
  156. <tbody>
  157. <tr>
  158. <td>type="text"</td><td><input type="text" id="i1" value=""></td>
  159. </tr>
  160. <tr>
  161. <td>type="button"</td><td><input type="button" id="i2" value="type='button'"></td>
  162. </tr>
  163. <tr>
  164. <td>type="checkbox"</td><td><input type="checkbox" id="i3" ></td>
  165. </tr>
  166. <tr>
  167. <td>type="radio" checked</td><td><input type="radio" id="i4" name="radio" checked>
  168. <input type="radio" id="i4b" name="radio"></td>
  169. </tr>
  170. <tr>
  171. <td>type="radio"</td><td><input type="radio" id="i5" name="radio2">
  172. <input type="radio" id="i6" name="radio2"></td>
  173. </tr>
  174. <tr>
  175. <td>type="password"</td><td><input type="password" id="i7"></td>
  176. </tr>
  177. <tr>
  178. <td>type="file"</td><td><input type="file" id="i8"></td>
  179. </tr>
  180. <tr>
  181. <td>type="number"</td><td><input type="number" id="number"></td>
  182. </tr>
  183. <tr>
  184. <td>button</td><td><button id="i9">button</button></td>
  185. </tr>
  186. <tr>
  187. <td>select</td><td><select id="i10"><option>select</option></select></td>
  188. </tr>
  189. <tr>
  190. <td>a</td><td><a href="#radio" id="i11">a link</a></td>
  191. </tr>
  192. <tr>
  193. <td>tabindex="0"</td><td><span tabindex="0" id="i12">span</span></td>
  194. </tr>
  195. <tr>
  196. <td><h3>Form elements with tabindex="-1"</h3></td>
  197. </tr>
  198. <tr>
  199. <td>type="text"</td><td><input type="text" tabindex="-1" value=""></td>
  200. </tr>
  201. <tr>
  202. <td>type="button"</td><td><input type="button" tabindex="-1" value="type='button'"></td>
  203. </tr>
  204. <tr>
  205. <td>type="checkbox"</td><td><input type="checkbox" tabindex="-1"></td>
  206. </tr>
  207. <tr>
  208. <td>type="radio" checked</td><td><input type="radio" tabindex="-1" name="radio3" checked>
  209. <input type="radio" tabindex="-1" name="radio3"></td>
  210. </tr>
  211. <tr>
  212. <td>type="radio"</td><td><input type="radio" tabindex="-1" name="radio4">
  213. <input type="radio" tabindex="-1" name="radio4"></td>
  214. </tr>
  215. <tr>
  216. <td>type="password"</td><td><input type="password" tabindex="-1"></td>
  217. </tr>
  218. <tr>
  219. <td>type="file"</td><td><input type="file" tabindex="-1"></td>
  220. </tr>
  221. <tr>
  222. <td>button</td><td><button tabindex="-1">button</button></td>
  223. </tr>
  224. <tr>
  225. <td>select</td><td><select tabindex="-1"><option>select</option></select></td>
  226. </tr>
  227. <tr>
  228. <td><h3>Form elements with .setAttribute("tabindex", "-1")</h3></td>
  229. </tr>
  230. <tr>
  231. <td>type="text"</td><td><input type="text" tabindex="1" value=""></td>
  232. </tr>
  233. <tr>
  234. <td>type="button"</td><td><input type="button" tabindex="1" value="type='button'"></td>
  235. </tr>
  236. <tr>
  237. <td>type="checkbox"</td><td><input type="checkbox" tabindex="1"></td>
  238. </tr>
  239. <tr>
  240. <td>type="radio" checked</td><td><input type="radio" tabindex="1" name="radio5" checked>
  241. <input type="radio" tabindex="1" name="radio5"></td>
  242. </tr>
  243. <tr>
  244. <td>type="radio"</td><td><input type="radio" tabindex="1" name="radio6">
  245. <input type="radio" tabindex="1" name="radio6"></td>
  246. </tr>
  247. <tr>
  248. <td>type="password"</td><td><input type="password" tabindex="1"></td>
  249. </tr>
  250. <tr>
  251. <td>type="file"</td><td><input type="file" tabindex="1"></td>
  252. </tr>
  253. <tr>
  254. <td>button</td><td><button tabindex="1">button</button></td>
  255. </tr>
  256. <tr>
  257. <td>select</td><td><select tabindex="1"><option>select</option></select></td>
  258. </tr>
  259. </tbody>
  260. </table>
  261. <h4 tabindex="0" id="end">done.</h4>
  262. </body>
  263. </html>