test_bug517851.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=517851
  5. -->
  6. <head>
  7. <title>Test for Bug 517851</title>
  8. <script type="application/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=517851">Mozilla Bug 517851</a>
  13. <p id="display"></p>
  14. <div id="content" style="display: none">
  15. <iframe id="subframe"></iframe>
  16. </div>
  17. <pre id="test">
  18. <script type="application/javascript">
  19. /** Test for Bug 517851 **/
  20. SimpleTest.waitForExplicitFinish();
  21. addLoadEvent(function() {
  22. window.handledCount = 0;
  23. window.testReturnValue = false;
  24. var target = document.createElement("div");
  25. var target2 = $("subframe").contentDocument.body;
  26. target.setAttribute("onerror", "++window.handledCount; return window.testReturnValue;");
  27. target.setAttribute("onmouseover", "++window.handledCount; return window.testReturnValue;");
  28. target.setAttribute("onbeforeunload", "++window.handledCount; return window.testReturnValue;");
  29. target2.setAttribute("onbeforeunload", "++window.parent.handledCount; return window.parent.testReturnValue;");
  30. target.setAttribute("onmousemove", "++window.handledCount; return window.testReturnValue;");
  31. var e = document.createEvent("Event");
  32. e.initEvent("error", true, true);
  33. window.testReturnValue = false;
  34. is(target.dispatchEvent(e), !window.testReturnValue,
  35. "error event should have reverse return value handling!");
  36. is(handledCount, 1, "Wrong event count!");
  37. window.testReturnValue = true;
  38. is(target.dispatchEvent(e), !window.testReturnValue,
  39. "error event should have reverse return value handling (2)!");
  40. is(handledCount, 2, "Wrong event count!");
  41. e = document.createEvent("MouseEvent");
  42. e.initEvent("mouseover", true, true);
  43. window.testReturnValue = false;
  44. is(target.dispatchEvent(e), !window.testReturnValue,
  45. "mouseover event should have reverse return value handling!");
  46. is(handledCount, 3, "Wrong event count!");
  47. window.testReturnValue = true;
  48. is(target.dispatchEvent(e), !window.testReturnValue,
  49. "mouseover event should have reverse return value handling (2)!");
  50. is(handledCount, 4, "Wrong event count!");
  51. e = document.createEvent("BeforeUnloadEvent");
  52. e.initEvent("beforeunload", true, true);
  53. window.testReturnValue = true;
  54. is(target.dispatchEvent(e), true,
  55. "beforeunload event on random element should not be prevented!");
  56. is(handledCount, 4, "Wrong event count; handler should not have run!");
  57. is(target2.dispatchEvent(e), false,
  58. "beforeunload event should be prevented!");
  59. is(handledCount, 5, "Wrong event count!");
  60. window.testReturnValue = false;
  61. is(target.dispatchEvent(e), false,
  62. "beforeunload event on random element should be prevented because the event was already cancelled!");
  63. is(handledCount, 5, "Wrong event count; handler should not have run! (2)");
  64. e = document.createEvent("BeforeUnloadEvent");
  65. e.initEvent("beforeunload", true, true);
  66. window.testReturnValue = false;
  67. is(target.dispatchEvent(e), true,
  68. "beforeunload event on random element should not be prevented (2)!");
  69. is(handledCount, 5, "Wrong event count; handler should not have run! (2)");
  70. is(target2.dispatchEvent(e), false,
  71. "beforeunload event should be prevented (2)!");
  72. is(handledCount, 6, "Wrong event count!");
  73. // Create normal event for beforeunload.
  74. e = document.createEvent("Event");
  75. e.initEvent("beforeunload", true, true);
  76. window.testReturnValue = true;
  77. is(target.dispatchEvent(e), true,
  78. "beforeunload event shouldn't be prevented (3)!");
  79. is(handledCount, 6, "Wrong event count: handler should not have run(3)!");
  80. is(target2.dispatchEvent(e), true,
  81. "beforeunload event shouldn't be prevented (3)!");
  82. is(handledCount, 7, "Wrong event count!");
  83. e = document.createEvent("MouseEvent");
  84. e.initEvent("mousemove", true, true);
  85. window.testReturnValue = true;
  86. is(target.dispatchEvent(e), window.testReturnValue,
  87. "mousemove event shouldn't have reverse return value handling!");
  88. is(handledCount, 8, "Wrong event count!");
  89. window.testReturnValue = false;
  90. is(target.dispatchEvent(e), window.testReturnValue,
  91. "mousemove event shouldn't have reverse return value handling (2)!");
  92. is(handledCount, 9, "Wrong event count!");
  93. // Now unhook the beforeunload handler in the subframe, so we don't prompt to
  94. // unload.
  95. target2.onbeforeunload = null;
  96. SimpleTest.finish();
  97. });
  98. </script>
  99. </pre>
  100. </body>
  101. </html>