Bug 1586189 - Re-implement waitForMessage based on waitForContentEvent; r=mccr8,janv

Differential Revision: https://phabricator.services.mozilla.com/D49752

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Simon Giesecke 2019-10-30 15:26:25 +00:00
Родитель d9b564795e
Коммит 1a9ec13557
2 изменённых файлов: 42 добавлений и 62 удалений

Просмотреть файл

@ -79,40 +79,29 @@ function dismissNotification(popup) {
}
function waitForMessage(aMessage, browser) {
return new Promise((resolve, reject) => {
/* eslint-disable no-undef */
// When contentScript runs, "this" is a ContentFrameMessageManager (so that's where
// addEventListener will add the listener), but the non-bubbling "message" event is
// sent to the Window involved, so we need a capturing listener.
function contentScript() {
addEventListener(
"message",
function(event) {
sendAsyncMessage("testLocal:message", { message: event.data });
},
{ once: true, capture: true },
true
);
}
/* eslint-enable no-undef */
let script = "data:,(" + contentScript.toString() + ")();";
let mm = browser.selectedBrowser.messageManager;
mm.addMessageListener("testLocal:message", function listener(msg) {
mm.removeMessageListener("testLocal:message", listener);
mm.removeDelayedFrameScript(script);
is(msg.data.message, aMessage, "received " + aMessage);
if (msg.data.message == aMessage) {
resolve();
} else {
reject();
// We cannot capture aMessage inside the checkFn, so we override the
// checkFn.toSource to tunnel aMessage instead.
let checkFn = function() {};
checkFn.toSource = function() {
return `function checkFn(event) {
let message = ${aMessage.toSource()};
is(event.data, message, "Received: " + message);
if (event.data == message) {
return true;
}
});
throw new Error(
\`Unexpected result: \$\{event.data\}, expected \$\{message\}\`
);
}`;
};
mm.loadFrameScript(script, true);
});
return BrowserTestUtils.waitForContentEvent(
browser.selectedBrowser,
"message",
/* capture */ true,
checkFn,
/* wantsUntrusted */ true
);
}
function dispatchEvent(eventName) {

Просмотреть файл

@ -101,38 +101,29 @@ function dismissNotification(popup, win) {
}
function waitForMessage(aMessage, browser) {
return new Promise((resolve, reject) => {
// When contentScript runs, "this" is a ContentFrameMessageManager (so that's where
// addEventListener will add the listener), but the non-bubbling "message" event is
// sent to the Window involved, so we need a capturing listener.
function contentScript() {
addEventListener(
"message",
function(event) {
sendAsyncMessage("testLocal:persisted", { persisted: event.data });
},
{ once: true, capture: true },
true
);
}
let script = "data:,(" + contentScript.toString() + ")();";
let mm = browser.selectedBrowser.messageManager;
mm.addMessageListener("testLocal:persisted", function listener(msg) {
mm.removeMessageListener("testLocal:persisted", listener);
mm.removeDelayedFrameScript(script);
is(msg.data.persisted, aMessage, "received " + aMessage);
if (msg.data.persisted == aMessage) {
resolve();
} else {
reject();
// We cannot capture aMessage inside the checkFn, so we override the
// checkFn.toSource to tunnel aMessage instead.
let checkFn = function() {};
checkFn.toSource = function() {
return `function checkFn(event) {
let message = ${aMessage.toSource()};
is(event.data, message, "Received: " + message);
if (event.data == message) {
return true;
}
});
throw new Error(
\`Unexpected result: \$\{event.data\}, expected \$\{message\}\`
);
}`;
};
mm.loadFrameScript(script, true);
});
return BrowserTestUtils.waitForContentEvent(
browser.selectedBrowser,
"message",
/* capture */ true,
checkFn,
/* wantsUntrusted */ true
);
}
function removePermission(url, permission) {