test_bug372964.html (4043B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=372964 5 --> 6 <head> 7 <title>Test for Bug 372964</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=372964">Mozilla Bug 372964</a> 13 <p id="display"></p> 14 <div id="content" style="display: none"> 15 16 </div> 17 <pre id="test"> 18 <script class="testbody" type="text/javascript"> 19 20 /** Test for Bug 372964 */ 21 22 var expectedEventType = ""; 23 var shouldBeTrusted = false; 24 var eventHandlerCallCount = 0; 25 26 function eventHandler(evt) { 27 ++eventHandlerCallCount; 28 is(evt.type, expectedEventType, "Wrong event type"); 29 is(evt.isTrusted, shouldBeTrusted, "Wrong .isTrusted"); 30 } 31 32 function test(trusted, type, removeAddedListener, removeSetListener, allowUntrusted) { 33 if (trusted) { 34 var x1 = SpecialPowers.wrap(new XMLHttpRequest()); 35 } else { 36 x1 = new XMLHttpRequest(); 37 } 38 39 var handlerCount = 0; 40 if (trusted || allowUntrusted || allowUntrusted == undefined) { 41 ++handlerCount; 42 } 43 44 if (allowUntrusted == undefined) { 45 // Test .addEventListener with 3 parameters. 46 x1.addEventListener(type, eventHandler); 47 } else { 48 // Test .addEventListener with 4 parameters. 49 x1.addEventListener(type, eventHandler, false, allowUntrusted); 50 } 51 52 if (("on" + type) in x1) { 53 ++handlerCount; 54 x1["on" + type] = eventHandler; 55 } 56 57 if (removeAddedListener) { 58 x1.removeEventListener(type, eventHandler); 59 if (trusted || allowUntrusted || allowUntrusted == undefined) { 60 --handlerCount; 61 } 62 } 63 64 if (removeSetListener) { 65 if (("on" + type) in x1) { 66 --handlerCount; 67 x1["on" + type] = null; 68 } 69 } 70 71 var e1 = document.createEvent("Events"); 72 e1.initEvent(type, true, true); 73 expectedEventType = type; 74 shouldBeTrusted = trusted; 75 var ecc = eventHandlerCallCount; 76 x1.dispatchEvent(e1); 77 is(eventHandlerCallCount, ecc + handlerCount, 78 "Wrong number event handler calls. (1)"); 79 80 e1 = document.createEvent("Events"); 81 e1.initEvent(type, true, true); 82 expectedEventType = type; 83 // Set trusted since open() may cause events to be sent. 84 shouldBeTrusted = true; 85 x1.open("GET", window.location); 86 x1.abort(); // This should not remove event listeners. 87 ecc = eventHandlerCallCount; 88 shouldBeTrusted = trusted; 89 x1.dispatchEvent(e1); 90 is(eventHandlerCallCount, ecc + handlerCount, 91 "Wrong number event handler calls. (2)"); 92 93 e1 = document.createEvent("Events"); 94 e1.initEvent(type, true, true); 95 expectedEventType = type; 96 // Set trusted since open()/send() may cause events to be sent. 97 shouldBeTrusted = true; 98 x1.open("GET", window.location); 99 x1.send(""); 100 x1.abort(); // This should not remove event listeners! 101 ecc = eventHandlerCallCount; 102 shouldBeTrusted = trusted; 103 x1.dispatchEvent(e1); 104 is(eventHandlerCallCount, ecc + handlerCount, 105 "Wrong number event handler calls. (3)"); 106 } 107 108 var events = 109 ["load", "error", "progress", "readystatechange", "foo"]; 110 111 do { 112 var e = events.shift(); 113 test(false, e, false, false); 114 test(false, e, false, true); 115 test(false, e, true, false); 116 test(false, e, true, true); 117 test(true, e, false, false); 118 test(true, e, false, true); 119 test(true, e, true, false); 120 test(true, e, true, true); 121 122 test(false, e, false, false, false); 123 test(false, e, false, false, true); 124 test(false, e, false, true, false); 125 test(false, e, false, true, true); 126 test(false, e, true, false, false); 127 test(false, e, true, false, true); 128 test(false, e, true, true, false); 129 test(false, e, true, true, true); 130 test(true, e, false, false, false); 131 test(true, e, false, false, true); 132 test(true, e, false, true, false); 133 test(true, e, false, true, true); 134 test(true, e, true, false, false); 135 test(true, e, true, false, true); 136 test(true, e, true, true, false); 137 test(true, e, true, true, true); 138 } while(events.length); 139 140 </script> 141 </pre> 142 </body> 143 </html>