test_bug517851.html (4992B)
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 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 20 /** Test for Bug 517851 */ 21 SimpleTest.waitForExplicitFinish(); 22 23 addLoadEvent(function() { 24 window.handledCount = 0; 25 window.testReturnValue = false; 26 var target = document.createElement("div"); 27 var target2 = $("subframe").contentDocument.body; 28 target.setAttribute("onerror", "++window.handledCount; return window.testReturnValue;"); 29 target2.setAttribute("onerror", "++window.parent.handledCount; return window.parent.testReturnValue;"); 30 target.setAttribute("onmouseover", "++window.handledCount; return window.testReturnValue;"); 31 target.setAttribute("onbeforeunload", "++window.handledCount; return window.testReturnValue;"); 32 target2.setAttribute("onbeforeunload", "++window.parent.handledCount; return window.parent.testReturnValue;"); 33 target.setAttribute("onmousemove", "++window.handledCount; return window.testReturnValue;"); 34 35 var e = new ErrorEvent("error", {bubbles: true, cancelable: true}); 36 window.testReturnValue = true; 37 is(target.dispatchEvent(e), window.testReturnValue, 38 "error event should not have reverse return value handling on div!"); 39 is(handledCount, 1, "Wrong event count!"); 40 window.testReturnValue = false; 41 is(target.dispatchEvent(e), window.testReturnValue, 42 "error event should not have reverse return value handling on div (2)!"); 43 is(handledCount, 2, "Wrong event count!"); 44 45 var e = new ErrorEvent("error", {bubbles: true, cancelable: true}); 46 window.testReturnValue = false; 47 is(target2.dispatchEvent(e), !window.testReturnValue, 48 "error event should have reverse return value handling!"); 49 is(handledCount, 3, "Wrong event count!"); 50 window.testReturnValue = true; 51 is(target2.dispatchEvent(e), !window.testReturnValue, 52 "error event should have reverse return value handling (2)!"); 53 is(handledCount, 4, "Wrong event count!"); 54 55 e = document.createEvent("MouseEvent"); 56 e.initEvent("mouseover", true, true); 57 window.testReturnValue = true; 58 is(target.dispatchEvent(e), window.testReturnValue, 59 "mouseover event should not have reverse return value handling!"); 60 is(handledCount, 5, "Wrong event count!"); 61 window.testReturnValue = false; 62 is(target.dispatchEvent(e), window.testReturnValue, 63 "mouseover event should not have reverse return value handling (2)!"); 64 is(handledCount, 6, "Wrong event count!"); 65 66 e = document.createEvent("BeforeUnloadEvent"); 67 e.initEvent("beforeunload", true, true); 68 window.testReturnValue = true; 69 is(target.dispatchEvent(e), true, 70 "beforeunload event on random element should not be prevented!"); 71 is(handledCount, 6, "Wrong event count; handler should not have run!"); 72 is(target2.dispatchEvent(e), false, 73 "beforeunload event should be prevented!"); 74 is(handledCount, 7, "Wrong event count!"); 75 window.testReturnValue = false; 76 is(target.dispatchEvent(e), false, 77 "beforeunload event on random element should be prevented because the event was already cancelled!"); 78 is(handledCount, 7, "Wrong event count; handler should not have run! (2)"); 79 80 e = document.createEvent("BeforeUnloadEvent"); 81 e.initEvent("beforeunload", true, true); 82 window.testReturnValue = false; 83 is(target.dispatchEvent(e), true, 84 "beforeunload event on random element should not be prevented (2)!"); 85 is(handledCount, 7, "Wrong event count; handler should not have run! (2)"); 86 87 is(target2.dispatchEvent(e), false, 88 "beforeunload event should be prevented (2)!"); 89 is(handledCount, 8, "Wrong event count!"); 90 91 // Create normal event for beforeunload. 92 e = document.createEvent("Event"); 93 e.initEvent("beforeunload", true, true); 94 window.testReturnValue = true; 95 is(target.dispatchEvent(e), true, 96 "beforeunload event shouldn't be prevented (3)!"); 97 is(handledCount, 8, "Wrong event count: handler should not have run(3)!"); 98 is(target2.dispatchEvent(e), true, 99 "beforeunload event shouldn't be prevented (3)!"); 100 is(handledCount, 9, "Wrong event count!"); 101 102 e = document.createEvent("MouseEvent"); 103 e.initEvent("mousemove", true, true); 104 window.testReturnValue = true; 105 is(target.dispatchEvent(e), window.testReturnValue, 106 "mousemove event shouldn't have reverse return value handling!"); 107 is(handledCount, 10, "Wrong event count!"); 108 window.testReturnValue = false; 109 is(target.dispatchEvent(e), window.testReturnValue, 110 "mousemove event shouldn't have reverse return value handling (2)!"); 111 is(handledCount, 11, "Wrong event count!"); 112 113 // Now unhook the beforeunload handler in the subframe, so we don't prompt to 114 // unload. 115 target2.onbeforeunload = null; 116 117 SimpleTest.finish(); 118 }); 119 </script> 120 </pre> 121 </body> 122 </html>