Bug 1153128 - Add sendSyncMessage to SpecialPowers.loadChromeScript. r=ted
MozReview-Commit-ID: AsbBiOkhhLo
--- a/testing/mochitest/tests/Harness_sanity/SpecialPowersLoadChromeScript.js
+++ b/testing/mochitest/tests/Harness_sanity/SpecialPowersLoadChromeScript.js
@@ -4,8 +4,12 @@ addMessageListener("foo", function (mess
sendAsyncMessage("bar", message);
});
addMessageListener("valid-assert", function (message) {
assert.ok(true, "valid assertion");
assert.equal(1, 1, "another valid assertion");
sendAsyncMessage("valid-assert-done");
});
+
+addMessageListener("sync-message", () => {
+ return "Received a synchronous message.";
+});
--- a/testing/mochitest/tests/Harness_sanity/test_SpecialPowersLoadChromeScript.html
+++ b/testing/mochitest/tests/Harness_sanity/test_SpecialPowersLoadChromeScript.html
@@ -28,12 +28,22 @@ function checkAssert() {
}
function endOfTest() {
script.destroy();
SimpleTest.finish();
}
script.sendAsyncMessage("foo", MESSAGE);
+
+/*
+ * [0][0] is because we're using one real message listener in SpecialPowersObserverAPI.js
+ * and dispatching that to multiple _chromeScriptListeners. The outer array comes
+ * from the message manager since there can be multiple real listeners. The inner
+ * array is for the return values of _chromeScriptListeners.
+ */
+is(script.sendSyncMessage("sync-message")[0][0], "Received a synchronous message.",
+ "Check sync return value");
+
</script>
</pre>
</body>
</html>
--- a/testing/specialpowers/content/SpecialPowersObserverAPI.js
+++ b/testing/specialpowers/content/SpecialPowersObserverAPI.js
@@ -488,20 +488,19 @@ SpecialPowersObserverAPI.prototype = {
}
return undefined; // See comment at the beginning of this function.
}
case "SPChromeScriptMessage": {
let id = aMessage.json.id;
let name = aMessage.json.name;
let message = aMessage.json.message;
- this._chromeScriptListeners
- .filter(o => (o.name == name && o.id == id))
- .forEach(o => o.listener(message));
- return undefined; // See comment at the beginning of this function.
+ return this._chromeScriptListeners
+ .filter(o => (o.name == name && o.id == id))
+ .map(o => o.listener(message));
}
case "SPImportInMainProcess": {
var message = { hadError: false, errorMessage: null };
try {
Components.utils.import(aMessage.data);
} catch (e) {
message.hadError = true;
--- a/testing/specialpowers/content/specialpowersAPI.js
+++ b/testing/specialpowers/content/specialpowersAPI.js
@@ -486,16 +486,21 @@ SpecialPowersAPI.prototype = {
);
},
sendAsyncMessage: (name, message) => {
this._sendSyncMessage("SPChromeScriptMessage",
{ id: id, name: name, message: message });
},
+ sendSyncMessage: (name, message) => {
+ return this._sendSyncMessage("SPChromeScriptMessage",
+ { id, name, message });
+ },
+
destroy: () => {
listeners = [];
this._removeMessageListener("SPChromeScriptMessage", chromeScript);
this._removeMessageListener("SPChromeScriptAssert", chromeScript);
},
receiveMessage: (aMessage) => {
let messageId = aMessage.json.id;