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