1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/events/test/test_bug448602.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,255 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=448602 1.8 +--> 1.9 +<head> 1.10 + <title>Test for Bug 448602</title> 1.11 + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 1.13 +</head> 1.14 +<body> 1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=448602">Mozilla Bug 448602</a> 1.16 +<p id="display"></p> 1.17 +<div id="content" style="display: none"> 1.18 + 1.19 +</div> 1.20 +<pre id="test"> 1.21 +<script type="application/javascript"> 1.22 + 1.23 +/** Test for Bug 448602 **/ 1.24 + 1.25 +var els, root, l2, l3; 1.26 + 1.27 +function runTests() { 1.28 +/* 1.29 + Disabled due to lack of present support for JSD in JM 1.30 + var jsdIDebuggerService = SpecialPowers.Ci.jsdIDebuggerService; 1.31 + var jsd = SpecialPowers.Components.classes['@mozilla.org/js/jsd/debugger-service;1'] 1.32 + .getService(jsdIDebuggerService); 1.33 +*/ 1.34 + els = SpecialPowers.Cc["@mozilla.org/eventlistenerservice;1"] 1.35 + .getService(SpecialPowers.Ci.nsIEventListenerService); 1.36 + 1.37 + // Event listener info tests 1.38 + root = document.getElementById("testroot"); 1.39 + var infos = els.getListenerInfoFor(root, {}); 1.40 + is(infos.length, 0, "Element shouldn't have listeners (1)"); 1.41 + 1.42 + var listenerSource = 'alert(event);'; 1.43 + root.setAttribute("onclick", listenerSource); 1.44 + infos = els.getListenerInfoFor(root, {}); 1.45 + is(infos.length, 1, "Element should have listeners (1)"); 1.46 + is(infos[0].toSource(), 'function onclick(event) {\n' + listenerSource + '\n}', 1.47 + "Unexpected serialization (1)"); 1.48 + is(infos[0].type, "click", "Wrong type (1)"); 1.49 + is(infos[0].capturing, false, "Wrong phase (1)"); 1.50 + is(infos[0].allowsUntrusted, true, "Should allow untrusted events (1)"); 1.51 + is(SpecialPowers.unwrap(infos[0].listenerObject), root.onclick, 1.52 + "Should have the right listener object (1)"); 1.53 + 1.54 +/* 1.55 + var jsdOn = jsd.isOn; 1.56 + if (!jsdOn) { 1.57 + is(infos[0].getDebugObject(), null, 1.58 + "If JSD isn't running, getDebugObject() should return null.") 1.59 + jsd.on(); 1.60 + ok(jsd.isOn, "JSD should be running."); 1.61 + } 1.62 + var jsdvalue = infos[0].getDebugObject().QueryInterface(SpecialPowers.Ci.jsdIValue); 1.63 + is(jsdvalue.jsType, 3, "Event listener should be a function! (1)"); 1.64 +*/ 1.65 + 1.66 + root.removeAttribute("onclick"); 1.67 + root.setAttribute("onclick", "...invalid script..."); 1.68 + SimpleTest.expectUncaughtException(true); 1.69 + infos = els.getListenerInfoFor(root, {}); 1.70 + SimpleTest.expectUncaughtException(false); 1.71 + is(infos.length, 1); 1.72 + is(infos[0].listenerObject, null); 1.73 + 1.74 + root.removeAttribute("onclick"); 1.75 + infos = els.getListenerInfoFor(root, {}); 1.76 + is(infos.length, 0, "Element shouldn't have listeners (2)"); 1.77 + 1.78 + var l = function (e) { alert(e); }; 1.79 + root.addEventListener("foo", l, true, true); 1.80 + root.addEventListener("foo", l, false, false); 1.81 + infos = els.getListenerInfoFor(root, {}); 1.82 + is(infos.length, 2, "Element should have listeners (2)"); 1.83 + is(infos[0].toSource(), "(function (e) { alert(e); })", 1.84 + "Unexpected serialization (2)"); 1.85 + is(infos[0].type, "foo", "Wrong type (2)"); 1.86 + is(infos[0].capturing, true, "Wrong phase (2)"); 1.87 + is(infos[0].allowsUntrusted, true, "Should allow untrusted events (2)"); 1.88 + is(SpecialPowers.unwrap(infos[0].listenerObject), l, 1.89 + "Should have the right listener object (2)"); 1.90 +/* 1.91 + jsdvalue = infos[0].getDebugObject().QueryInterface(SpecialPowers.Ci.jsdIValue); 1.92 + is(jsdvalue.jsType, 3, "Event listener should be a function!(2)"); 1.93 + is(jsdvalue.getWrappedValue(), l, "Wrong JS value! (1)"); 1.94 +*/ 1.95 + 1.96 + is(infos[1].toSource(), "(function (e) { alert(e); })", 1.97 + "Unexpected serialization (3)"); 1.98 + is(infos[1].type, "foo", "Wrong type (3)"); 1.99 + is(infos[1].capturing, false, "Wrong phase (3)"); 1.100 + is(infos[1].allowsUntrusted, false, "Shouldn't allow untrusted events (1)"); 1.101 + is(SpecialPowers.unwrap(infos[1].listenerObject), l, 1.102 + "Should have the right listener object (3)"); 1.103 + 1.104 +/* 1.105 + jsdvalue2 = infos[1].getDebugObject().QueryInterface(SpecialPowers.Ci.jsdIValue); 1.106 + is(jsdvalue2.jsType, 3, "Event listener should be a function! (3)"); 1.107 + is(jsdvalue2.getWrappedValue(), l, "Wrong JS value! (2)"); 1.108 +*/ 1.109 + root.removeEventListener("foo", l, true); 1.110 + root.removeEventListener("foo", l, false); 1.111 + infos = els.getListenerInfoFor(root, {}); 1.112 + is(infos.length, 0, "Element shouldn't have listeners (3)"); 1.113 + 1.114 + root.onclick = l; 1.115 + infos = els.getListenerInfoFor(root, {}); 1.116 + is(infos.length, 1, "Element should have listeners (3)"); 1.117 + is(infos[0].toSource(), '(function (e) { alert(e); })', 1.118 + "Unexpected serialization (4)"); 1.119 + is(infos[0].type, "click", "Wrong type (4)"); 1.120 + is(infos[0].capturing, false, "Wrong phase (4)"); 1.121 + is(infos[0].allowsUntrusted, true, "Should allow untrusted events (3)"); 1.122 + is(SpecialPowers.unwrap(infos[0].listenerObject), l, 1.123 + "Should have the right listener object (4)"); 1.124 + 1.125 + // Event target chain tests 1.126 + l2 = document.getElementById("testlevel2"); 1.127 + l3 = document.getElementById("testlevel3"); 1.128 + var textnode = l3.firstChild; 1.129 + var chain = els.getEventTargetChainFor(textnode, {}); 1.130 + ok(chain.length > 3, "Too short event target chain."); 1.131 + ok(SpecialPowers.compare(chain[0], textnode), "Wrong chain item (1)"); 1.132 + ok(SpecialPowers.compare(chain[1], l3), "Wrong chain item (2)"); 1.133 + ok(SpecialPowers.compare(chain[2], l2), "Wrong chain item (3)"); 1.134 + ok(SpecialPowers.compare(chain[3], root), "Wrong chain item (4)"); 1.135 + 1.136 + var hasDocumentInChain = false; 1.137 + var hasWindowInChain = false; 1.138 + for (var i = 0; i < chain.length; ++i) { 1.139 + if (SpecialPowers.compare(chain[i], document)) { 1.140 + hasDocumentInChain = true; 1.141 + } else if (SpecialPowers.compare(chain[i], window)) { 1.142 + hasWindowInChain = true; 1.143 + } 1.144 + } 1.145 + 1.146 + ok(hasDocumentInChain, "Should have document in event target chain!"); 1.147 + ok(hasWindowInChain, "Should have window in event target chain!"); 1.148 +/* 1.149 + if (!jsdOn) { 1.150 + jsd.off(); 1.151 + ok(!jsd.isOn, "JSD shouldn't be running anymore."); 1.152 + } 1.153 +*/ 1.154 + 1.155 + try { 1.156 + els.getListenerInfoFor(null, {}); 1.157 + ok(false, "Should have thrown an exception."); 1.158 + } catch (ex) { 1.159 + ok(true, "We should be still running."); 1.160 + } 1.161 + setTimeout(testAllListener, 0); 1.162 +} 1.163 + 1.164 +function dispatchTrusted(t, o) { 1.165 + SpecialPowers.dispatchEvent(window, t, new Event("testevent", o)); 1.166 +} 1.167 + 1.168 +function testAllListener() { 1.169 + els = SpecialPowers.wrap(els); 1.170 + var results = []; 1.171 + var expectedResults = 1.172 + [ { target: "testlevel3", phase: 3, trusted: false }, 1.173 + { target: "testlevel3", phase: 3, trusted: false }, 1.174 + { target: "testlevel3", phase: 3, trusted: true }, 1.175 + { target: "testlevel3", phase: 3, trusted: true }, 1.176 + { target: "testlevel3", phase: 3, trusted: true } 1.177 + ]; 1.178 + 1.179 + function allListener(e) { 1.180 + results.push({ 1.181 + target: e.target.id, 1.182 + phase: e.eventPhase, 1.183 + trusted: e.isTrusted 1.184 + }); 1.185 + e.stopPropagation(); 1.186 + } 1.187 + function allListenerTrustedOnly(e) { 1.188 + results.push({ 1.189 + target: e.target.id, 1.190 + phase: e.eventPhase, 1.191 + trusted: e.isTrusted 1.192 + }); 1.193 + e.stopPropagation(); 1.194 + } 1.195 + 1.196 + els.addListenerForAllEvents(root, allListener, false, true); 1.197 + var infos = els.getListenerInfoFor(root); 1.198 + var nullTypes = 0; 1.199 + for (var i = 0; i < infos.length; ++i) { 1.200 + if (infos[i].type == null) { 1.201 + ++nullTypes; 1.202 + } 1.203 + } 1.204 + is(nullTypes, 1, "Should have one all-event-listener!"); 1.205 + 1.206 + els.addListenerForAllEvents(root, allListener, false, true, true); 1.207 + els.addListenerForAllEvents(root, allListenerTrustedOnly, false, false, true); 1.208 + l3.dispatchEvent(new Event("testevent", { bubbles: true })); 1.209 + dispatchTrusted(l3, { bubbles: true }); 1.210 + els.removeListenerForAllEvents(root, allListener, false); 1.211 + els.removeListenerForAllEvents(root, allListener, false, true); 1.212 + els.removeListenerForAllEvents(root, allListenerTrustedOnly, false, true); 1.213 + // make sure removeListenerForAllEvents works. 1.214 + l3.dispatchEvent(new Event("testevent", { bubbles: true })); 1.215 + dispatchTrusted(l3, { bubbles: true }); 1.216 + 1.217 + // Test the order of event listeners. 1.218 + var clickListenerCalled = false; 1.219 + var allListenerCalled = false; 1.220 + function clickListener() { 1.221 + clickListenerCalled = true; 1.222 + ok(allListenerCalled, "Should have called '*' listener before normal listener!"); 1.223 + } 1.224 + function allListener2() { 1.225 + allListenerCalled = true; 1.226 + notok(clickListenerCalled, "Shouldn't have called click listener before '*' listener!"); 1.227 + } 1.228 + root.onclick = null; // Remove the listener added in earlier tests. 1.229 + root.addEventListener("click", clickListener); 1.230 + els.addListenerForAllEvents(root, allListener2, false, true); 1.231 + l3.dispatchEvent(new MouseEvent("click", { bubbles: true })); 1.232 + root.removeEventListener("click", clickListener); 1.233 + els.removeListenerForAllEvents(root, allListener2, false); 1.234 + ok(allListenerCalled, "Should have called '*' listener"); 1.235 + ok(clickListenerCalled, "Should have called click listener"); 1.236 + 1.237 + is(results.length, expectedResults.length, "count"); 1.238 + for (var i = 0; i < expectedResults.length; ++i) { 1.239 + for (var p in expectedResults[i]) { 1.240 + is(results[i][p], expectedResults[i][p], p); 1.241 + } 1.242 + } 1.243 + SimpleTest.finish(); 1.244 +} 1.245 + 1.246 +SimpleTest.waitForExplicitFinish(); 1.247 +addLoadEvent(runTests); 1.248 +</script> 1.249 +</pre> 1.250 +<div id="testroot"> 1.251 + <div id="testlevel2"> 1.252 + <div id="testlevel3"> 1.253 + Test 1.254 + </div> 1.255 + </div> 1.256 +</div> 1.257 +</body> 1.258 +</html>