dom/events/test/test_bug448602.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=448602
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Bug 448602</title>
michael@0 8 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
michael@0 10 </head>
michael@0 11 <body>
michael@0 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=448602">Mozilla Bug 448602</a>
michael@0 13 <p id="display"></p>
michael@0 14 <div id="content" style="display: none">
michael@0 15
michael@0 16 </div>
michael@0 17 <pre id="test">
michael@0 18 <script type="application/javascript">
michael@0 19
michael@0 20 /** Test for Bug 448602 **/
michael@0 21
michael@0 22 var els, root, l2, l3;
michael@0 23
michael@0 24 function runTests() {
michael@0 25 /*
michael@0 26 Disabled due to lack of present support for JSD in JM
michael@0 27 var jsdIDebuggerService = SpecialPowers.Ci.jsdIDebuggerService;
michael@0 28 var jsd = SpecialPowers.Components.classes['@mozilla.org/js/jsd/debugger-service;1']
michael@0 29 .getService(jsdIDebuggerService);
michael@0 30 */
michael@0 31 els = SpecialPowers.Cc["@mozilla.org/eventlistenerservice;1"]
michael@0 32 .getService(SpecialPowers.Ci.nsIEventListenerService);
michael@0 33
michael@0 34 // Event listener info tests
michael@0 35 root = document.getElementById("testroot");
michael@0 36 var infos = els.getListenerInfoFor(root, {});
michael@0 37 is(infos.length, 0, "Element shouldn't have listeners (1)");
michael@0 38
michael@0 39 var listenerSource = 'alert(event);';
michael@0 40 root.setAttribute("onclick", listenerSource);
michael@0 41 infos = els.getListenerInfoFor(root, {});
michael@0 42 is(infos.length, 1, "Element should have listeners (1)");
michael@0 43 is(infos[0].toSource(), 'function onclick(event) {\n' + listenerSource + '\n}',
michael@0 44 "Unexpected serialization (1)");
michael@0 45 is(infos[0].type, "click", "Wrong type (1)");
michael@0 46 is(infos[0].capturing, false, "Wrong phase (1)");
michael@0 47 is(infos[0].allowsUntrusted, true, "Should allow untrusted events (1)");
michael@0 48 is(SpecialPowers.unwrap(infos[0].listenerObject), root.onclick,
michael@0 49 "Should have the right listener object (1)");
michael@0 50
michael@0 51 /*
michael@0 52 var jsdOn = jsd.isOn;
michael@0 53 if (!jsdOn) {
michael@0 54 is(infos[0].getDebugObject(), null,
michael@0 55 "If JSD isn't running, getDebugObject() should return null.")
michael@0 56 jsd.on();
michael@0 57 ok(jsd.isOn, "JSD should be running.");
michael@0 58 }
michael@0 59 var jsdvalue = infos[0].getDebugObject().QueryInterface(SpecialPowers.Ci.jsdIValue);
michael@0 60 is(jsdvalue.jsType, 3, "Event listener should be a function! (1)");
michael@0 61 */
michael@0 62
michael@0 63 root.removeAttribute("onclick");
michael@0 64 root.setAttribute("onclick", "...invalid script...");
michael@0 65 SimpleTest.expectUncaughtException(true);
michael@0 66 infos = els.getListenerInfoFor(root, {});
michael@0 67 SimpleTest.expectUncaughtException(false);
michael@0 68 is(infos.length, 1);
michael@0 69 is(infos[0].listenerObject, null);
michael@0 70
michael@0 71 root.removeAttribute("onclick");
michael@0 72 infos = els.getListenerInfoFor(root, {});
michael@0 73 is(infos.length, 0, "Element shouldn't have listeners (2)");
michael@0 74
michael@0 75 var l = function (e) { alert(e); };
michael@0 76 root.addEventListener("foo", l, true, true);
michael@0 77 root.addEventListener("foo", l, false, false);
michael@0 78 infos = els.getListenerInfoFor(root, {});
michael@0 79 is(infos.length, 2, "Element should have listeners (2)");
michael@0 80 is(infos[0].toSource(), "(function (e) { alert(e); })",
michael@0 81 "Unexpected serialization (2)");
michael@0 82 is(infos[0].type, "foo", "Wrong type (2)");
michael@0 83 is(infos[0].capturing, true, "Wrong phase (2)");
michael@0 84 is(infos[0].allowsUntrusted, true, "Should allow untrusted events (2)");
michael@0 85 is(SpecialPowers.unwrap(infos[0].listenerObject), l,
michael@0 86 "Should have the right listener object (2)");
michael@0 87 /*
michael@0 88 jsdvalue = infos[0].getDebugObject().QueryInterface(SpecialPowers.Ci.jsdIValue);
michael@0 89 is(jsdvalue.jsType, 3, "Event listener should be a function!(2)");
michael@0 90 is(jsdvalue.getWrappedValue(), l, "Wrong JS value! (1)");
michael@0 91 */
michael@0 92
michael@0 93 is(infos[1].toSource(), "(function (e) { alert(e); })",
michael@0 94 "Unexpected serialization (3)");
michael@0 95 is(infos[1].type, "foo", "Wrong type (3)");
michael@0 96 is(infos[1].capturing, false, "Wrong phase (3)");
michael@0 97 is(infos[1].allowsUntrusted, false, "Shouldn't allow untrusted events (1)");
michael@0 98 is(SpecialPowers.unwrap(infos[1].listenerObject), l,
michael@0 99 "Should have the right listener object (3)");
michael@0 100
michael@0 101 /*
michael@0 102 jsdvalue2 = infos[1].getDebugObject().QueryInterface(SpecialPowers.Ci.jsdIValue);
michael@0 103 is(jsdvalue2.jsType, 3, "Event listener should be a function! (3)");
michael@0 104 is(jsdvalue2.getWrappedValue(), l, "Wrong JS value! (2)");
michael@0 105 */
michael@0 106 root.removeEventListener("foo", l, true);
michael@0 107 root.removeEventListener("foo", l, false);
michael@0 108 infos = els.getListenerInfoFor(root, {});
michael@0 109 is(infos.length, 0, "Element shouldn't have listeners (3)");
michael@0 110
michael@0 111 root.onclick = l;
michael@0 112 infos = els.getListenerInfoFor(root, {});
michael@0 113 is(infos.length, 1, "Element should have listeners (3)");
michael@0 114 is(infos[0].toSource(), '(function (e) { alert(e); })',
michael@0 115 "Unexpected serialization (4)");
michael@0 116 is(infos[0].type, "click", "Wrong type (4)");
michael@0 117 is(infos[0].capturing, false, "Wrong phase (4)");
michael@0 118 is(infos[0].allowsUntrusted, true, "Should allow untrusted events (3)");
michael@0 119 is(SpecialPowers.unwrap(infos[0].listenerObject), l,
michael@0 120 "Should have the right listener object (4)");
michael@0 121
michael@0 122 // Event target chain tests
michael@0 123 l2 = document.getElementById("testlevel2");
michael@0 124 l3 = document.getElementById("testlevel3");
michael@0 125 var textnode = l3.firstChild;
michael@0 126 var chain = els.getEventTargetChainFor(textnode, {});
michael@0 127 ok(chain.length > 3, "Too short event target chain.");
michael@0 128 ok(SpecialPowers.compare(chain[0], textnode), "Wrong chain item (1)");
michael@0 129 ok(SpecialPowers.compare(chain[1], l3), "Wrong chain item (2)");
michael@0 130 ok(SpecialPowers.compare(chain[2], l2), "Wrong chain item (3)");
michael@0 131 ok(SpecialPowers.compare(chain[3], root), "Wrong chain item (4)");
michael@0 132
michael@0 133 var hasDocumentInChain = false;
michael@0 134 var hasWindowInChain = false;
michael@0 135 for (var i = 0; i < chain.length; ++i) {
michael@0 136 if (SpecialPowers.compare(chain[i], document)) {
michael@0 137 hasDocumentInChain = true;
michael@0 138 } else if (SpecialPowers.compare(chain[i], window)) {
michael@0 139 hasWindowInChain = true;
michael@0 140 }
michael@0 141 }
michael@0 142
michael@0 143 ok(hasDocumentInChain, "Should have document in event target chain!");
michael@0 144 ok(hasWindowInChain, "Should have window in event target chain!");
michael@0 145 /*
michael@0 146 if (!jsdOn) {
michael@0 147 jsd.off();
michael@0 148 ok(!jsd.isOn, "JSD shouldn't be running anymore.");
michael@0 149 }
michael@0 150 */
michael@0 151
michael@0 152 try {
michael@0 153 els.getListenerInfoFor(null, {});
michael@0 154 ok(false, "Should have thrown an exception.");
michael@0 155 } catch (ex) {
michael@0 156 ok(true, "We should be still running.");
michael@0 157 }
michael@0 158 setTimeout(testAllListener, 0);
michael@0 159 }
michael@0 160
michael@0 161 function dispatchTrusted(t, o) {
michael@0 162 SpecialPowers.dispatchEvent(window, t, new Event("testevent", o));
michael@0 163 }
michael@0 164
michael@0 165 function testAllListener() {
michael@0 166 els = SpecialPowers.wrap(els);
michael@0 167 var results = [];
michael@0 168 var expectedResults =
michael@0 169 [ { target: "testlevel3", phase: 3, trusted: false },
michael@0 170 { target: "testlevel3", phase: 3, trusted: false },
michael@0 171 { target: "testlevel3", phase: 3, trusted: true },
michael@0 172 { target: "testlevel3", phase: 3, trusted: true },
michael@0 173 { target: "testlevel3", phase: 3, trusted: true }
michael@0 174 ];
michael@0 175
michael@0 176 function allListener(e) {
michael@0 177 results.push({
michael@0 178 target: e.target.id,
michael@0 179 phase: e.eventPhase,
michael@0 180 trusted: e.isTrusted
michael@0 181 });
michael@0 182 e.stopPropagation();
michael@0 183 }
michael@0 184 function allListenerTrustedOnly(e) {
michael@0 185 results.push({
michael@0 186 target: e.target.id,
michael@0 187 phase: e.eventPhase,
michael@0 188 trusted: e.isTrusted
michael@0 189 });
michael@0 190 e.stopPropagation();
michael@0 191 }
michael@0 192
michael@0 193 els.addListenerForAllEvents(root, allListener, false, true);
michael@0 194 var infos = els.getListenerInfoFor(root);
michael@0 195 var nullTypes = 0;
michael@0 196 for (var i = 0; i < infos.length; ++i) {
michael@0 197 if (infos[i].type == null) {
michael@0 198 ++nullTypes;
michael@0 199 }
michael@0 200 }
michael@0 201 is(nullTypes, 1, "Should have one all-event-listener!");
michael@0 202
michael@0 203 els.addListenerForAllEvents(root, allListener, false, true, true);
michael@0 204 els.addListenerForAllEvents(root, allListenerTrustedOnly, false, false, true);
michael@0 205 l3.dispatchEvent(new Event("testevent", { bubbles: true }));
michael@0 206 dispatchTrusted(l3, { bubbles: true });
michael@0 207 els.removeListenerForAllEvents(root, allListener, false);
michael@0 208 els.removeListenerForAllEvents(root, allListener, false, true);
michael@0 209 els.removeListenerForAllEvents(root, allListenerTrustedOnly, false, true);
michael@0 210 // make sure removeListenerForAllEvents works.
michael@0 211 l3.dispatchEvent(new Event("testevent", { bubbles: true }));
michael@0 212 dispatchTrusted(l3, { bubbles: true });
michael@0 213
michael@0 214 // Test the order of event listeners.
michael@0 215 var clickListenerCalled = false;
michael@0 216 var allListenerCalled = false;
michael@0 217 function clickListener() {
michael@0 218 clickListenerCalled = true;
michael@0 219 ok(allListenerCalled, "Should have called '*' listener before normal listener!");
michael@0 220 }
michael@0 221 function allListener2() {
michael@0 222 allListenerCalled = true;
michael@0 223 notok(clickListenerCalled, "Shouldn't have called click listener before '*' listener!");
michael@0 224 }
michael@0 225 root.onclick = null; // Remove the listener added in earlier tests.
michael@0 226 root.addEventListener("click", clickListener);
michael@0 227 els.addListenerForAllEvents(root, allListener2, false, true);
michael@0 228 l3.dispatchEvent(new MouseEvent("click", { bubbles: true }));
michael@0 229 root.removeEventListener("click", clickListener);
michael@0 230 els.removeListenerForAllEvents(root, allListener2, false);
michael@0 231 ok(allListenerCalled, "Should have called '*' listener");
michael@0 232 ok(clickListenerCalled, "Should have called click listener");
michael@0 233
michael@0 234 is(results.length, expectedResults.length, "count");
michael@0 235 for (var i = 0; i < expectedResults.length; ++i) {
michael@0 236 for (var p in expectedResults[i]) {
michael@0 237 is(results[i][p], expectedResults[i][p], p);
michael@0 238 }
michael@0 239 }
michael@0 240 SimpleTest.finish();
michael@0 241 }
michael@0 242
michael@0 243 SimpleTest.waitForExplicitFinish();
michael@0 244 addLoadEvent(runTests);
michael@0 245 </script>
michael@0 246 </pre>
michael@0 247 <div id="testroot">
michael@0 248 <div id="testlevel2">
michael@0 249 <div id="testlevel3">
michael@0 250 Test
michael@0 251 </div>
michael@0 252 </div>
michael@0 253 </div>
michael@0 254 </body>
michael@0 255 </html>

mercurial