test_bug1096146.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=1096146
  5. -->
  6. <head>
  7. <meta charset="utf-8">
  8. <title>Test for Bug 1096146</title>
  9. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  10. <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  11. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  12. </head>
  13. <body onload="runTests();">
  14. <a target="_blank"
  15. href="https://bugzilla.mozilla.org/show_bug.cgi?id=1096146">Mozilla Bug 1096146</a>
  16. <div id="content" style="display: none;">
  17. </div>
  18. <pre id="test">
  19. <script type="application/javascript">
  20. const kKeydownEvent = 0x1;
  21. const kScrollEvent = 0x2;
  22. var gCurrentTest = 0;
  23. var gNumEvents = 0;
  24. var kTests = [
  25. {
  26. description: "no preventDefault at 'mozbrowserbeforekeydown'",
  27. expectedEvents: kKeydownEvent | kScrollEvent,
  28. resultEvents: 0x0,
  29. doPreventDefault: false
  30. },
  31. {
  32. description: "do preventDefault at 'mozbrowserbeforekeydown'",
  33. expectedEvents: 0x0,
  34. resultEvents: 0x0,
  35. doPreventDefault: true
  36. }
  37. ]
  38. function frameScript()
  39. {
  40. function handler(e) {
  41. sendSyncMessage("forwardevent", { type: e.type });
  42. }
  43. addEventListener('keydown', handler);
  44. addEventListener('scroll', handler);
  45. }
  46. function waitAndVerifyResult(count) {
  47. if (gNumEvents >= 3 || count > 10) {
  48. is(kTests[gCurrentTest].resultEvents,
  49. kTests[gCurrentTest].expectedEvents,
  50. "verify result");
  51. runTests();
  52. } else {
  53. SimpleTest.requestFlakyTimeout("We must delay to wait for scroll/keydown events.");
  54. setTimeout(() => waitAndVerifyResult(count + 1), 100);
  55. }
  56. }
  57. function testDefaultAction()
  58. {
  59. synthesizeKey('VK_END', {}, document.getElementById("embedded").contentWindow);
  60. waitAndVerifyResult(0);
  61. }
  62. function prepareTest()
  63. {
  64. gNumEvents = 0;
  65. var handler;
  66. if (kTests[gCurrentTest].doPreventDefault) {
  67. handler = preventDefaultHandler;
  68. } else {
  69. handler = noPreventDefaultHandler;
  70. }
  71. window.addEventListener("mozbrowserbeforekeydown", handler);
  72. var iframe = document.createElement("iframe");
  73. iframe.id = "embedded";
  74. iframe.src = "bug1096146_embedded.html";
  75. iframe.setAttribute("remote", "false");
  76. SpecialPowers.wrap(iframe).mozbrowser = true;
  77. iframe.addEventListener("mozbrowserloadend", function onloadend() {
  78. iframe.removeEventListener("mozbrowserloadend", onloadend);
  79. iframe.focus();
  80. var mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
  81. mm.addMessageListener("forwardevent", function(msg) {
  82. var value = 0;
  83. switch(msg.json.type) {
  84. case "scroll":
  85. ++gNumEvents;
  86. value = kScrollEvent;
  87. break;
  88. case "keydown":
  89. ++gNumEvents;
  90. value = kKeydownEvent;
  91. break;
  92. default:
  93. ok(false, "unexpected event");
  94. }
  95. kTests[gCurrentTest].resultEvents =
  96. kTests[gCurrentTest].resultEvents | value;
  97. });
  98. mm.loadFrameScript("data:,(" + frameScript.toString() + ")();", false);
  99. runTests();
  100. return;
  101. });
  102. document.body.appendChild(iframe);
  103. }
  104. function preventDefaultHandler(evt)
  105. {
  106. ok(true, "receive " + evt.type + " and do preventDefault.");
  107. ++gNumEvents;
  108. evt.preventDefault();
  109. }
  110. function noPreventDefaultHandler(evt)
  111. {
  112. ok(true, "receive " + evt.type + ".");
  113. ++gNumEvents;
  114. }
  115. function teardownHandler()
  116. {
  117. var handler;
  118. if (kTests[gCurrentTest].doPreventDefault) {
  119. handler = preventDefaultHandler;
  120. } else {
  121. handler = noPreventDefaultHandler;
  122. }
  123. window.removeEventListener("mozbrowserbeforekeydown", handler);
  124. document.body.removeChild(document.getElementById("embedded"));
  125. runTests();
  126. }
  127. var tests = [
  128. function addPermissions() {
  129. SpecialPowers.pushPermissions(
  130. [{ type: "before-after-keyboard-event", allow: true, context: document },
  131. { type: "browser", allow: true, context: document }],
  132. runTests);
  133. },
  134. function addPreferences() {
  135. SpecialPowers.pushPrefEnv(
  136. { "set": [["dom.beforeAfterKeyboardEvent.enabled", true],
  137. ["dom.mozBrowserFramesEnabled", true],
  138. ["dom.ipc.tabs.disabled", false],
  139. ["network.disable.ipc.security", true]] },
  140. runTests);
  141. },
  142. prepareTest,
  143. testDefaultAction,
  144. teardownHandler,
  145. function() {
  146. gCurrentTest++;
  147. runTests();
  148. },
  149. prepareTest,
  150. testDefaultAction,
  151. teardownHandler
  152. ];
  153. function runTests()
  154. {
  155. if (!tests.length) {
  156. SimpleTest.finish();
  157. return;
  158. }
  159. var test = tests.shift();
  160. test();
  161. }
  162. </script>
  163. </pre>
  164. </body>
  165. </html>