2016-10-10 00:48:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
const {deepEqual, equal, ok} = require("chai").assert;
|
2016-10-10 00:48:35 +00:00
|
|
|
const sinon = require("sinon");
|
|
|
|
|
|
|
|
const {setupTestDOMWindow} = require("./setup");
|
|
|
|
|
|
|
|
describe("browser-polyfill", () => {
|
|
|
|
describe("wrapped runtime.onMessage listener", () => {
|
2016-11-02 20:31:56 +00:00
|
|
|
it("does not wrap the listener if it is not a function", () => {
|
2016-10-11 14:39:13 +00:00
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
|
|
|
lastError: null,
|
|
|
|
onMessage: {
|
|
|
|
addListener: sinon.spy(),
|
|
|
|
hasListener: sinon.stub(),
|
|
|
|
removeListener: sinon.spy(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
const fakeNonFunctionListener = {fake: "non function listener"};
|
|
|
|
|
|
|
|
window.browser.runtime.onMessage.addListener(fakeNonFunctionListener);
|
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
deepEqual(fakeChrome.runtime.onMessage.addListener.firstCall.args[0],
|
2016-11-02 19:39:07 +00:00
|
|
|
fakeNonFunctionListener,
|
|
|
|
"The non-function listener has not been wrapped");
|
2016-10-11 14:39:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-11 12:27:26 +00:00
|
|
|
it("keeps track of the listeners added", () => {
|
2016-10-10 00:48:35 +00:00
|
|
|
const messageListener = sinon.spy();
|
2016-10-12 00:17:55 +00:00
|
|
|
const fakeChromeListeners = new Set();
|
2016-10-10 00:48:35 +00:00
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
2016-10-11 11:56:00 +00:00
|
|
|
lastError: null,
|
2016-10-10 00:48:35 +00:00
|
|
|
onMessage: {
|
2016-10-12 00:17:55 +00:00
|
|
|
addListener: sinon.spy((listener, ...args) => {
|
|
|
|
fakeChromeListeners.add(listener);
|
|
|
|
}),
|
|
|
|
hasListener: sinon.spy(listener => fakeChromeListeners.has(listener)),
|
|
|
|
removeListener: sinon.spy(listener => {
|
|
|
|
fakeChromeListeners.delete(listener);
|
|
|
|
}),
|
2016-10-10 00:48:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.browser.runtime.onMessage.hasListener(messageListener),
|
2016-11-02 19:39:07 +00:00
|
|
|
false, "Got hasListener==false before the listener has been added");
|
2016-10-11 12:34:39 +00:00
|
|
|
|
2016-10-10 00:48:35 +00:00
|
|
|
window.browser.runtime.onMessage.addListener(messageListener);
|
2016-10-11 12:34:39 +00:00
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.browser.runtime.onMessage.hasListener(messageListener),
|
2016-11-02 19:39:07 +00:00
|
|
|
true, "Got hasListener==true once the listener has been added");
|
2016-10-11 12:34:39 +00:00
|
|
|
|
|
|
|
// Add the same listener again to test that it will be called with the
|
|
|
|
// same wrapped listener.
|
2016-10-10 00:48:35 +00:00
|
|
|
window.browser.runtime.onMessage.addListener(messageListener);
|
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
ok(fakeChrome.runtime.onMessage.addListener.calledTwice,
|
2016-11-02 19:39:07 +00:00
|
|
|
"addListener has been called twice");
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(fakeChrome.runtime.onMessage.addListener.secondCall.args[0],
|
2016-11-02 19:39:07 +00:00
|
|
|
fakeChrome.runtime.onMessage.addListener.firstCall.args[0],
|
|
|
|
"both the addListener calls received the same wrapped listener");
|
2016-10-10 00:48:35 +00:00
|
|
|
|
2016-10-11 12:34:39 +00:00
|
|
|
// Retrieve the wrapped listener and execute it to fake a received message.
|
2016-10-10 00:48:35 +00:00
|
|
|
const wrappedListener = fakeChrome.runtime.onMessage.addListener.firstCall.args[0];
|
|
|
|
wrappedListener("msg", {name: "sender"}, function sendResponse() {});
|
2016-10-12 00:08:52 +00:00
|
|
|
ok(messageListener.calledOnce, "The listener has been called once");
|
2016-10-10 00:48:35 +00:00
|
|
|
|
2016-10-11 12:34:39 +00:00
|
|
|
// Remove the listener.
|
2016-10-10 00:48:35 +00:00
|
|
|
window.browser.runtime.onMessage.removeListener(messageListener);
|
2016-10-12 00:08:52 +00:00
|
|
|
ok(fakeChrome.runtime.onMessage.removeListener.calledOnce,
|
2016-11-02 19:39:07 +00:00
|
|
|
"removeListener has been called once");
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(fakeChrome.runtime.onMessage.addListener.secondCall.args[0],
|
2016-11-02 19:39:07 +00:00
|
|
|
fakeChrome.runtime.onMessage.removeListener.firstCall.args[0],
|
|
|
|
"both the addListener and removeListenercalls received the same wrapped listener");
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(fakeChrome.runtime.onMessage.hasListener(messageListener), false,
|
2016-11-02 19:39:07 +00:00
|
|
|
"Got hasListener==false once the listener has been removed");
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-02 20:32:32 +00:00
|
|
|
it("generates different wrappers for different listeners", () => {
|
|
|
|
const fakeChromeListeners = [];
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
|
|
|
lastError: null,
|
|
|
|
onMessage: {
|
|
|
|
addListener: sinon.spy((listener, ...args) => {
|
|
|
|
fakeChromeListeners.push(listener);
|
|
|
|
}),
|
|
|
|
hasListener: sinon.spy(),
|
|
|
|
removeListener: sinon.spy(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
const firstMessageListener = sinon.spy();
|
|
|
|
const secondMessageListener = sinon.spy();
|
|
|
|
|
|
|
|
window.browser.runtime.onMessage.addListener(firstMessageListener);
|
|
|
|
window.browser.runtime.onMessage.addListener(secondMessageListener);
|
|
|
|
|
|
|
|
equal(fakeChromeListeners.length, 2, "Got two wrapped listeners");
|
|
|
|
|
|
|
|
fakeChromeListeners[0]("call first wrapper");
|
|
|
|
ok(firstMessageListener.calledOnce);
|
|
|
|
equal(firstMessageListener.firstCall.args[0], "call first wrapper");
|
|
|
|
|
|
|
|
fakeChromeListeners[1]("call second wrapper");
|
|
|
|
ok(secondMessageListener.calledOnce);
|
|
|
|
equal(secondMessageListener.firstCall.args[0], "call second wrapper");
|
|
|
|
});
|
|
|
|
});
|
2018-05-14 18:38:21 +00:00
|
|
|
});
|
2016-11-02 20:32:32 +00:00
|
|
|
|
2018-05-14 18:38:21 +00:00
|
|
|
describe("sendResponse callback", () => {
|
|
|
|
it("ignores the sendResponse calls when the listener returns a promise", () => {
|
2016-10-10 00:48:35 +00:00
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
2016-10-11 11:56:00 +00:00
|
|
|
lastError: null,
|
2016-10-10 00:48:35 +00:00
|
|
|
onMessage: {
|
|
|
|
addListener: sinon.spy(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-05-14 18:38:21 +00:00
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
const listener = sinon.spy((msg, sender, sendResponse) => {
|
|
|
|
sendResponse("Ignored sendReponse callback on returned Promise");
|
|
|
|
|
|
|
|
return Promise.resolve("listener resolved value");
|
|
|
|
});
|
|
|
|
|
|
|
|
const sendResponseSpy = sinon.spy();
|
|
|
|
|
|
|
|
window.browser.runtime.onMessage.addListener(listener);
|
|
|
|
|
|
|
|
ok(fakeChrome.runtime.onMessage.addListener.calledOnce,
|
|
|
|
"runtime.onMessage.addListener should have been called once");
|
|
|
|
|
|
|
|
let wrappedListener = fakeChrome.runtime.onMessage.addListener.firstCall.args[0];
|
|
|
|
|
|
|
|
let returnedValue = wrappedListener("test message", {name: "fake sender"}, sendResponseSpy);
|
|
|
|
equal(returnedValue, true, "the wrapped listener should have returned true");
|
|
|
|
|
|
|
|
ok(listener.calledOnce, "listener has been called once");
|
2016-10-11 14:39:13 +00:00
|
|
|
|
2018-05-14 18:38:21 +00:00
|
|
|
// Wait a promise to be resolved and then check the wrapped listener behaviors.
|
|
|
|
return Promise.resolve().then(() => {
|
|
|
|
ok(sendResponseSpy.calledOnce, "sendResponse callback called once");
|
2016-10-10 00:48:35 +00:00
|
|
|
|
2018-05-14 18:38:21 +00:00
|
|
|
equal(sendResponseSpy.firstCall.args[0], "listener resolved value",
|
|
|
|
"sendResponse has been called with the expected value");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("ignores asynchronous sendResponse calls if the listener does not return true", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
|
|
|
lastError: null,
|
|
|
|
onMessage: {
|
|
|
|
addListener: sinon.spy(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2016-10-11 14:39:13 +00:00
|
|
|
|
2018-05-14 18:38:21 +00:00
|
|
|
const waitPromises = [];
|
2016-10-10 00:48:35 +00:00
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
2018-05-14 18:38:21 +00:00
|
|
|
const listenerReturnsFalse = sinon.spy((msg, sender, sendResponse) => {
|
|
|
|
waitPromises.push(Promise.resolve().then(() => {
|
|
|
|
sendResponse("Ignored sendReponse callback on returned false");
|
|
|
|
}));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
const listenerReturnsValue = sinon.spy((msg, sender, sendResponse) => {
|
|
|
|
waitPromises.push(Promise.resolve().then(() => {
|
|
|
|
sendResponse("Ignored sendReponse callback on non boolean/thenable return values");
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Any return value that is not a promise should not be sent as a response,
|
|
|
|
// and any return value that is not true should make the sendResponse
|
|
|
|
// calls to be ignored.
|
|
|
|
return "Ignored return value";
|
|
|
|
});
|
|
|
|
|
|
|
|
const listenerReturnsTrue = sinon.spy((msg, sender, sendResponse) => {
|
|
|
|
waitPromises.push(Promise.resolve().then(() => {
|
|
|
|
sendResponse("expected sendResponse value");
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Expect the asynchronous sendResponse call to be used to send a response
|
|
|
|
// when the listener returns true.
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const sendResponseSpy = sinon.spy();
|
|
|
|
|
|
|
|
window.browser.runtime.onMessage.addListener(listenerReturnsFalse);
|
|
|
|
window.browser.runtime.onMessage.addListener(listenerReturnsValue);
|
|
|
|
window.browser.runtime.onMessage.addListener(listenerReturnsTrue);
|
|
|
|
|
|
|
|
equal(fakeChrome.runtime.onMessage.addListener.callCount, 3,
|
|
|
|
"runtime.onMessage.addListener should have been called 3 times");
|
|
|
|
|
|
|
|
let wrappedListenerReturnsFalse = fakeChrome.runtime.onMessage.addListener.firstCall.args[0];
|
|
|
|
let wrappedListenerReturnsValue = fakeChrome.runtime.onMessage.addListener.secondCall.args[0];
|
|
|
|
let wrappedListenerReturnsTrue = fakeChrome.runtime.onMessage.addListener.thirdCall.args[0];
|
|
|
|
|
|
|
|
let returnedValue = wrappedListenerReturnsFalse("test message", {name: "fake sender"}, sendResponseSpy);
|
|
|
|
equal(returnedValue, false, "the first wrapped listener should return false");
|
|
|
|
|
|
|
|
returnedValue = wrappedListenerReturnsValue("test message2", {name: "fake sender"}, sendResponseSpy);
|
|
|
|
equal(returnedValue, false, "the second wrapped listener should return false");
|
|
|
|
|
|
|
|
returnedValue = wrappedListenerReturnsTrue("test message3", {name: "fake sender"}, sendResponseSpy);
|
|
|
|
equal(returnedValue, true, "the third wrapped listener should return true");
|
|
|
|
|
|
|
|
ok(listenerReturnsFalse.calledOnce, "first listener has been called once");
|
|
|
|
ok(listenerReturnsValue.calledOnce, "second listener has been called once");
|
|
|
|
ok(listenerReturnsTrue.calledOnce, "third listener has been called once");
|
|
|
|
|
|
|
|
// Wait all the collected promises to be resolved and then check the wrapped listener behaviors.
|
|
|
|
return Promise.all(waitPromises).then(() => {
|
|
|
|
ok(sendResponseSpy.calledOnce, "sendResponse callback should have been called once");
|
2016-10-10 00:48:35 +00:00
|
|
|
|
2018-05-14 18:38:21 +00:00
|
|
|
equal(sendResponseSpy.firstCall.args[0], "expected sendResponse value",
|
|
|
|
"sendResponse has been called with the expected value");
|
2016-10-11 14:39:13 +00:00
|
|
|
});
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|
2018-07-02 10:38:05 +00:00
|
|
|
|
|
|
|
it("resolves to undefined when no listeners reply", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
|
|
|
// This error message is defined as CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE
|
|
|
|
// in the polyfill sources and it is used to recognize when Chrome has detected that
|
|
|
|
// none of the listeners replied.
|
|
|
|
lastError: {
|
|
|
|
message: "The message port closed before a response was received.",
|
|
|
|
},
|
|
|
|
sendMessage: sinon.stub(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
fakeChrome.runtime.sendMessage.onFirstCall().callsArgWith(1, [undefined]);
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
const promise = window.browser.runtime.sendMessage("some_message");
|
|
|
|
ok(fakeChrome.runtime.sendMessage.calledOnce, "sendMessage has been called once");
|
|
|
|
|
|
|
|
return promise.then(reply => {
|
|
|
|
deepEqual(reply, undefined, "sendMessage promise should be resolved to undefined");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|