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-10-11 14:39:13 +00:00
|
|
|
it("do not wrap the listener if it is not a function", () => {
|
|
|
|
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-10-11 14:39:13 +00:00
|
|
|
fakeNonFunctionListener,
|
|
|
|
"The non-function listener has not been wrapped");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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-10-10 00:48:35 +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-10-11 12:27:26 +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-10-10 00:48:35 +00:00
|
|
|
"addListener has been called twice");
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(fakeChrome.runtime.onMessage.addListener.secondCall.args[0],
|
2016-10-10 00:48:35 +00:00
|
|
|
fakeChrome.runtime.onMessage.addListener.firstCall.args[0],
|
|
|
|
"both the addListener calls received the same wrapped listener");
|
|
|
|
|
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-10-10 00:48:35 +00:00
|
|
|
"removeListener has been called once");
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(fakeChrome.runtime.onMessage.addListener.secondCall.args[0],
|
2016-10-10 00:48:35 +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-10-11 12:27:26 +00:00
|
|
|
"Got hasListener==false once the listener has been removed");
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sends the returned value as a message response", () => {
|
|
|
|
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(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-10-11 14:39:13 +00:00
|
|
|
// Plain value returned.
|
2016-10-10 00:48:35 +00:00
|
|
|
const messageListener = sinon.stub();
|
|
|
|
const firstResponse = "fake reply";
|
2016-10-11 14:39:13 +00:00
|
|
|
// Resolved Promise returned.
|
|
|
|
const secondResponse = Promise.resolve("fake reply 2");
|
|
|
|
// Rejected Promise returned.
|
|
|
|
const thirdResponse = Promise.reject("fake error 3");
|
|
|
|
|
2016-10-10 00:48:35 +00:00
|
|
|
const sendResponseSpy = sinon.spy();
|
|
|
|
|
2016-10-11 14:39:13 +00:00
|
|
|
messageListener
|
|
|
|
.onFirstCall().returns(firstResponse)
|
|
|
|
.onSecondCall().returns(secondResponse)
|
|
|
|
.onThirdCall().returns(thirdResponse);
|
|
|
|
|
|
|
|
let wrappedListener;
|
2016-10-10 00:48:35 +00:00
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
window.browser.runtime.onMessage.addListener(messageListener);
|
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
ok(fakeChrome.runtime.onMessage.addListener.calledOnce);
|
2016-10-10 00:48:35 +00:00
|
|
|
|
2016-10-11 14:39:13 +00:00
|
|
|
wrappedListener = fakeChrome.runtime.onMessage.addListener.firstCall.args[0];
|
2016-10-10 00:48:35 +00:00
|
|
|
|
|
|
|
wrappedListener("fake message", {name: "fake sender"}, sendResponseSpy);
|
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
ok(messageListener.calledOnce, "The unwrapped message listener has been called");
|
|
|
|
deepEqual(messageListener.firstCall.args,
|
2016-10-10 00:48:35 +00:00
|
|
|
["fake message", {name: "fake sender"}],
|
|
|
|
"The unwrapped message listener has received the expected parameters");
|
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
ok(sendResponseSpy.calledOnce, "The sendResponse function has been called");
|
|
|
|
equal(sendResponseSpy.firstCall.args[0], "fake reply",
|
2016-10-10 00:48:35 +00:00
|
|
|
"sendResponse callback has been called with the expected parameters");
|
|
|
|
|
|
|
|
wrappedListener("fake message2", {name: "fake sender2"}, sendResponseSpy);
|
|
|
|
|
|
|
|
// Wait the second response promise to be resolved.
|
|
|
|
return secondResponse;
|
|
|
|
}).then(() => {
|
2016-10-12 00:08:52 +00:00
|
|
|
ok(messageListener.calledTwice,
|
2016-10-10 00:48:35 +00:00
|
|
|
"The unwrapped message listener has been called");
|
2016-10-12 00:08:52 +00:00
|
|
|
deepEqual(messageListener.secondCall.args,
|
2016-10-10 00:48:35 +00:00
|
|
|
["fake message2", {name: "fake sender2"}],
|
2016-10-11 14:39:13 +00:00
|
|
|
"The unwrapped listener has received the expected parameters");
|
2016-10-10 00:48:35 +00:00
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
ok(sendResponseSpy.calledTwice, "The sendResponse function has been called");
|
|
|
|
equal(sendResponseSpy.secondCall.args[0], "fake reply 2",
|
2016-10-10 00:48:35 +00:00
|
|
|
"sendResponse callback has been called with the expected parameters");
|
2016-10-11 14:39:13 +00:00
|
|
|
}).then(() => {
|
|
|
|
wrappedListener("fake message3", {name: "fake sender3"}, sendResponseSpy);
|
|
|
|
|
|
|
|
// Wait the third response promise to be rejected.
|
|
|
|
return thirdResponse.catch(err => {
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(messageListener.callCount, 3,
|
2016-10-11 14:39:13 +00:00
|
|
|
"The unwrapped message listener has been called");
|
2016-10-12 00:08:52 +00:00
|
|
|
deepEqual(messageListener.thirdCall.args,
|
2016-10-11 14:39:13 +00:00
|
|
|
["fake message3", {name: "fake sender3"}],
|
|
|
|
"The unwrapped listener has received the expected parameters");
|
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(sendResponseSpy.callCount, 3,
|
2016-10-11 14:39:13 +00:00
|
|
|
"The sendResponse function has been called");
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(sendResponseSpy.thirdCall.args[0], err,
|
2016-10-11 14:39:13 +00:00
|
|
|
"sendResponse callback has been called with the expected parameters");
|
|
|
|
});
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|