content/base/test/chrome/file_bug549682.xul

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 <?xml version="1.0"?>
michael@0 2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
michael@0 3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
michael@0 4 type="text/css"?>
michael@0 5 <!--
michael@0 6 https://bugzilla.mozilla.org/show_bug.cgi?id=549682
michael@0 7 -->
michael@0 8 <window title="Mozilla Bug 549682"
michael@0 9 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
michael@0 10 onload="run()">
michael@0 11 <label value="Mozilla Bug 549682"/>
michael@0 12 <!-- test code goes here -->
michael@0 13 <script type="application/javascript"><![CDATA[
michael@0 14 var Cc = Components.classes;
michael@0 15 var Ci = Components.interfaces;
michael@0 16 var Cr = Components.results;
michael@0 17 var Cu = Components.utils;
michael@0 18
michael@0 19 var didRunAsync = false;
michael@0 20 var didRunLocal = false;
michael@0 21
michael@0 22 var global = Cc["@mozilla.org/globalmessagemanager;1"]
michael@0 23 .getService(Components.interfaces.nsIMessageBroadcaster);
michael@0 24 var ppm = Cc["@mozilla.org/parentprocessmessagemanager;1"]
michael@0 25 .getService(Components.interfaces.nsIMessageBroadcaster);
michael@0 26 var cpm = Cc["@mozilla.org/childprocessmessagemanager;1"]
michael@0 27 .getService(Components.interfaces.nsISyncMessageSender);
michael@0 28
michael@0 29 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 30
michael@0 31 function ok(cond, msg) {
michael@0 32 opener.wrappedJSObject.ok(cond, msg);
michael@0 33 }
michael@0 34
michael@0 35 function is(actual, expected, msg) {
michael@0 36 opener.wrappedJSObject.is(actual, expected, msg);
michael@0 37 }
michael@0 38
michael@0 39 var asyncPPML = false;
michael@0 40 function ppmASL(m) {
michael@0 41 asyncPPML = true;
michael@0 42 }
michael@0 43 var syncPPML = false;
michael@0 44 function ppmSL(m) {
michael@0 45 syncPPML = true;
michael@0 46 }
michael@0 47 ppm.addMessageListener("processmessageAsync", ppmASL);
michael@0 48 ppm.addMessageListener("processmessageSync", ppmSL);
michael@0 49
michael@0 50 cpm.sendAsyncMessage("processmessageAsync", "");
michael@0 51 cpm.sendSyncMessage("processmessageSync", "");
michael@0 52
michael@0 53 var asyncCPML = false;
michael@0 54 function cpmASL(m) {
michael@0 55 asyncCPML = true;
michael@0 56 }
michael@0 57 cpm.addMessageListener("childprocessmessage", cpmASL);
michael@0 58 ppm.broadcastAsyncMessage("childprocessmessage", "");
michael@0 59
michael@0 60 function checkPMMMessages() {
michael@0 61 ok(asyncPPML, "should have handled async message");
michael@0 62 ok(syncPPML, "should have handled sync message");
michael@0 63 ok(asyncCPML, "should have handled async message");
michael@0 64 ppm.removeMessageListener("processmessageAsync", ppmASL);
michael@0 65 ppm.removeMessageListener("processmessageSync", ppmSL);
michael@0 66 cpm.removeMessageListener("childprocessmessage", cpmASL);
michael@0 67 }
michael@0 68
michael@0 69 var globalListenerCallCount = 0;
michael@0 70 function globalListener(m) {
michael@0 71 ++globalListenerCallCount;
michael@0 72 if (m.name == "sync") {
michael@0 73 global.removeMessageListener("async", globalListener);
michael@0 74 global.removeMessageListener("sync", globalListener);
michael@0 75 global.removeMessageListener("global-sync", globalListener);
michael@0 76 // Note, the result depends on what other windows are open.
michael@0 77 ok(globalListenerCallCount >= 4,
michael@0 78 "Global listener should have been called at least 4 times!");
michael@0 79 ok(didRunLocal, "Local message received.");
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83 function asyncL(m) {
michael@0 84 didRunAsync = true;
michael@0 85 is(m.name, "async", "Wrong message!");
michael@0 86 is(m.json.data, 1234, "Wrong data!");
michael@0 87 }
michael@0 88
michael@0 89 function syncL(m) {
michael@0 90 is(m.name, "sync", "Wrong message!");
michael@0 91 is(m.json.data, 1234, "Wrong data!");
michael@0 92 ok(didRunAsync, "Should have run async!");
michael@0 93 }
michael@0 94
michael@0 95 function localL(m) {
michael@0 96 is(m.name, "lasync", "Wrong message!");
michael@0 97 is(m.json.data, 2345, "Wrong data!");
michael@0 98 didRunLocal = true;
michael@0 99 }
michael@0 100
michael@0 101 var weakMessageReceived = false;
michael@0 102 var weakListener = {
michael@0 103 QueryInterface: XPCOMUtils.generateQI([Ci.nsIMessageListener,
michael@0 104 Ci.nsISupportsWeakReference]),
michael@0 105
michael@0 106 receiveMessage: function(msg) {
michael@0 107 if (weakMessageReceived) {
michael@0 108 ok(false, 'Weak listener fired twice.');
michael@0 109 return;
michael@0 110 }
michael@0 111
michael@0 112 ok(true, 'Weak listener fired once.');
michael@0 113 weakMessageReceived = true;
michael@0 114 document.getElementById('ifr').messageManager
michael@0 115 .removeWeakMessageListener('weak', weakListener);
michael@0 116 }
michael@0 117 };
michael@0 118
michael@0 119 var weakListener2 = {
michael@0 120 QueryInterface: XPCOMUtils.generateQI([Ci.nsIMessageListener,
michael@0 121 Ci.nsISupportsWeakReference]),
michael@0 122
michael@0 123 receiveMessage: function(msg) {
michael@0 124 ok(false, 'Should not have received a message.');
michael@0 125 }
michael@0 126 };
michael@0 127
michael@0 128 function weakDoneListener() {
michael@0 129 ok(weakMessageReceived, 'Got "weak" message.');
michael@0 130 finish();
michael@0 131 }
michael@0 132
michael@0 133 function finish() {
michael@0 134 opener.setTimeout("done()", 0);
michael@0 135 var i = document.getElementById("ifr");
michael@0 136 i.parentNode.removeChild(i); // This is a crash test!
michael@0 137 window.close();
michael@0 138 }
michael@0 139
michael@0 140 function loadScript() {
michael@0 141 // Async should be processed first!
michael@0 142 messageManager.loadFrameScript("data:,\
michael@0 143 sendAsyncMessage('async', { data: 1234 });\
michael@0 144 sendSyncMessage('sync', { data: 1234 });\
michael@0 145 sendAsyncMessage('weak', {});\
michael@0 146 sendAsyncMessage('weak', {});\
michael@0 147 sendAsyncMessage('weakdone', {});", false);
michael@0 148 }
michael@0 149
michael@0 150 function run() {
michael@0 151 var localmm = document.getElementById('ifr').messageManager;
michael@0 152
michael@0 153 var wn = document.getElementById('ifr').contentWindow
michael@0 154 .getInterface(Components.interfaces.nsIWebNavigation);
michael@0 155 ok(wn, "Should have webnavigation");
michael@0 156 var cfmm = wn.getInterface(Components.interfaces.nsIContentFrameMessageManager);
michael@0 157 ok(cfmm, "Should have content messageManager");
michael@0 158
michael@0 159 var didGetSyncMessage = false;
michael@0 160 function syncContinueTestFn() {
michael@0 161 didGetSyncMessage = true;
michael@0 162 }
michael@0 163 localmm.addMessageListener("syncContinueTest", syncContinueTestFn);
michael@0 164 cfmm.sendSyncMessage("syncContinueTest", {});
michael@0 165 localmm.removeMessageListener("syncContinueTest", syncContinueTestFn);
michael@0 166 ok(didGetSyncMessage, "Should have got sync message!");
michael@0 167
michael@0 168 localmm.addMessageListener("lasync", localL);
michael@0 169 localmm.loadFrameScript("data:,sendAsyncMessage('lasync', { data: 2345 })", false);
michael@0 170
michael@0 171 messageManager.addMessageListener("async", asyncL);
michael@0 172 messageManager.addMessageListener("sync", syncL);
michael@0 173 global.addMessageListener("async", globalListener);
michael@0 174 global.addMessageListener("sync", globalListener);
michael@0 175 global.addMessageListener("global-sync", globalListener);
michael@0 176 global.loadFrameScript("data:,sendSyncMessage('global-sync', { data: 1234 });", true);
michael@0 177 var toBeRemovedScript = "data:,sendAsyncMessage('toberemoved', { data: 2345 })";
michael@0 178 var c = 0;
michael@0 179 messageManager.addMessageListener("toberemoved", function() {
michael@0 180 ++c;
michael@0 181 is(c, 1, "Should be called only once!");
michael@0 182 });
michael@0 183 // This loads the script in the existing <browser>
michael@0 184 messageManager.loadFrameScript(toBeRemovedScript, true);
michael@0 185 // But it won't be loaded in the dynamically created <browser>
michael@0 186 messageManager.removeDelayedFrameScript(toBeRemovedScript);
michael@0 187
michael@0 188 var oldValue = globalListenerCallCount;
michael@0 189 var b = document.createElement("browser");
michael@0 190 b.setAttribute("type", "content");
michael@0 191 document.documentElement.appendChild(b);
michael@0 192 is(globalListenerCallCount, oldValue + 1,
michael@0 193 "Wrong message count");
michael@0 194
michael@0 195 localmm.addWeakMessageListener('weak', weakListener);
michael@0 196 localmm.addMessageListener('weakdone', weakDoneListener);
michael@0 197
michael@0 198 // Add weakListener2 as a weak message listener, then force weakListener2
michael@0 199 // to be gc'ed. weakListener2 shouldn't be run.
michael@0 200 var weakRef = Cu.getWeakReference(weakListener2);
michael@0 201 localmm.addWeakMessageListener('weak', weakListener2);
michael@0 202 weakListener2 = null;
michael@0 203
michael@0 204 // Force a gc/cc in a loop until weakRef's referent has gone away.
michael@0 205 function waitForWeakRefToDie() {
michael@0 206 if (weakRef.get()) {
michael@0 207 var mgr = Cc["@mozilla.org/memory-reporter-manager;1"]
michael@0 208 .getService(Ci.nsIMemoryReporterManager);
michael@0 209 mgr.minimizeMemoryUsage(waitForWeakRefToDie);
michael@0 210
michael@0 211 // Print a message so that if the test hangs in a minimizeMemoryUsage
michael@0 212 // loop, we'll be able to see it in the log.
michael@0 213 ok(true, "waitForWeakRefToDie spinning...");
michael@0 214 return;
michael@0 215 }
michael@0 216
michael@0 217 setTimeout(checkPMMMessages, 0);
michael@0 218 setTimeout(loadScript, 0);
michael@0 219 }
michael@0 220
michael@0 221 waitForWeakRefToDie();
michael@0 222 }
michael@0 223
michael@0 224 ]]></script>
michael@0 225 <browser type="content" src="about:blank" id="ifr"/>
michael@0 226 </window>

mercurial