2016-10-10 00:48:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
2017-09-22 21:38:44 +00:00
|
|
|
const {deepEqual, equal, fail, ok, throws} = require("chai").assert;
|
2016-10-10 00:48:35 +00:00
|
|
|
const sinon = require("sinon");
|
|
|
|
|
|
|
|
const {setupTestDOMWindow} = require("./setup");
|
|
|
|
|
|
|
|
describe("browser-polyfill", () => {
|
|
|
|
describe("wrapped async functions", () => {
|
|
|
|
it("returns a promise which resolves to the callback parameters", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
alarms: {clear: sinon.stub()},
|
2016-10-11 12:27:26 +00:00
|
|
|
runtime: {
|
|
|
|
lastError: null,
|
2018-06-19 20:35:41 +00:00
|
|
|
openOptionsPage: sinon.stub(),
|
2016-10-11 12:27:26 +00:00
|
|
|
requestUpdateCheck: sinon.stub(),
|
|
|
|
},
|
|
|
|
tabs: {
|
|
|
|
query: sinon.stub(),
|
|
|
|
},
|
2016-10-10 00:48:35 +00:00
|
|
|
};
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
2016-10-11 12:27:26 +00:00
|
|
|
// Test for single callback argument.
|
2016-10-10 00:48:35 +00:00
|
|
|
fakeChrome.alarms.clear
|
2016-10-11 12:27:26 +00:00
|
|
|
.onFirstCall().callsArgWith(1, "res1");
|
|
|
|
|
|
|
|
// Test for single array callback argument.
|
|
|
|
fakeChrome.tabs.query
|
|
|
|
.onFirstCall().callsArgWith(1, ["res1", "res2"]);
|
|
|
|
|
|
|
|
// Test for multiple callback arguments.
|
|
|
|
fakeChrome.runtime.requestUpdateCheck
|
|
|
|
.onFirstCall().callsArgWith(0, "res1", "res2");
|
2016-10-10 00:48:35 +00:00
|
|
|
|
2018-06-19 20:35:41 +00:00
|
|
|
// Test for no callback arguments.
|
|
|
|
fakeChrome.runtime.openOptionsPage
|
|
|
|
.onFirstCall().callsArg(0);
|
|
|
|
|
2016-10-10 00:48:35 +00:00
|
|
|
return Promise.all([
|
|
|
|
window.browser.alarms.clear("test1"),
|
2016-10-11 12:27:26 +00:00
|
|
|
window.browser.tabs.query({active: true}),
|
|
|
|
window.browser.runtime.requestUpdateCheck(),
|
2018-06-19 20:35:41 +00:00
|
|
|
window.browser.runtime.openOptionsPage(),
|
2016-10-10 00:48:35 +00:00
|
|
|
]);
|
|
|
|
}).then(results => {
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(results[0], "res1", "Fake alarms.clear call resolved to a single value");
|
|
|
|
deepEqual(results[1], ["res1", "res2"],
|
2016-11-02 19:39:07 +00:00
|
|
|
"Fake tabs.query resolved to an array of values");
|
2016-10-12 00:08:52 +00:00
|
|
|
deepEqual(results[2], ["res1", "res2"],
|
2016-11-02 19:39:07 +00:00
|
|
|
"Fake runtime.requestUpdateCheck resolved to an array of values");
|
2018-06-19 20:35:41 +00:00
|
|
|
|
|
|
|
equal(results[3], undefined, "Fake runtime.openOptionsPage resolved to a void value.");
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|
2016-10-11 13:08:24 +00:00
|
|
|
|
|
|
|
it("rejects the returned promise if chrome.runtime.lastError is not null", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
|
|
|
lastError: new Error("fake lastError"),
|
|
|
|
},
|
|
|
|
tabs: {
|
|
|
|
query: sinon.stub(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
// Test for single array callback argument.
|
|
|
|
fakeChrome.tabs.query
|
|
|
|
.onFirstCall().callsArgWith(1, ["res1", "res2"]);
|
|
|
|
|
|
|
|
return window.browser.tabs.query({active: true}).then(
|
2016-10-12 00:08:52 +00:00
|
|
|
() => fail("Expected a rejected promise"),
|
|
|
|
(err) => equal(err, fakeChrome.runtime.lastError,
|
2016-11-02 19:39:07 +00:00
|
|
|
"Got the expected error in the rejected promise")
|
2016-10-11 13:08:24 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("throws if the number of arguments are not in the range defined in the metadata", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
|
|
|
lastError: null,
|
|
|
|
sendMessage: sinon.spy(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
2016-10-12 00:08:52 +00:00
|
|
|
throws(() => {
|
2016-10-11 13:08:24 +00:00
|
|
|
window.browser.runtime.sendMessage();
|
2016-11-02 20:45:02 +00:00
|
|
|
}, "Expected at least 1 argument for sendMessage(), got 0");
|
2016-10-11 13:08:24 +00:00
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
throws(() => {
|
2016-10-11 13:08:24 +00:00
|
|
|
window.browser.runtime.sendMessage("0", "1", "2", "3");
|
|
|
|
}, "Expected at most 3 arguments for sendMessage(), got 4");
|
|
|
|
});
|
|
|
|
});
|
2017-09-22 21:38:44 +00:00
|
|
|
|
|
|
|
it("resolves to a single parameter on singleCallbackArg metadata property", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {lastError: null},
|
|
|
|
devtools: {
|
|
|
|
panels: {
|
|
|
|
create: sinon.spy((title, iconPath, panelURL, cb) => {
|
|
|
|
// when the callback of a API method which specifies the "singleCallbackArg" metadata
|
|
|
|
// is called with two parameters only the first one is resolved by the returned promise.
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
cb({isFakePanel: true}, "unwanted parameter");
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(({browser}) => {
|
|
|
|
return browser.devtools.panels.create("title", "icon.png", "panel.html").then(panel => {
|
|
|
|
ok(fakeChrome.devtools.panels.create.calledOnce,
|
|
|
|
"devtools.panels.create has been called once");
|
|
|
|
|
|
|
|
ok("isFakePanel" in panel && panel.isFakePanel,
|
|
|
|
"Got the expected result from devtools.panels.create");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("resolves to a single undefined parameter on singleCallbackArg metadata and no params", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {lastError: null},
|
|
|
|
devtools: {
|
|
|
|
panels: {
|
|
|
|
create: sinon.spy((title, iconPath, panelURL, cb) => {
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
// when the callback of a API method which specifies the "singleCallbackArg" metadata
|
|
|
|
// is called without any parameter, the returned promise resolves to undefined
|
|
|
|
// instead of an empty array.
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(({browser}) => {
|
|
|
|
return browser.devtools.panels.create("title", "icon.png", "panel.html").then(panel => {
|
|
|
|
ok(fakeChrome.devtools.panels.create.calledOnce,
|
|
|
|
"devtools.panels.create has been called once");
|
|
|
|
|
|
|
|
ok(panel === undefined,
|
|
|
|
"Got the expected undefined result from devtools.panels.create");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-09-26 12:32:06 +00:00
|
|
|
|
|
|
|
it("returns a Promise for wrapped API methods without a callback on Chrome", () => {
|
|
|
|
const FAKE_ERROR_MSG = "API Schema validation error";
|
|
|
|
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {lastError: null},
|
|
|
|
pageAction: {
|
|
|
|
show: sinon.spy((tabId, cb) => {
|
|
|
|
if (cb) {
|
|
|
|
throw new Error("Chrome do not expect a callback");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tabId == null) {
|
|
|
|
throw new Error(FAKE_ERROR_MSG);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
hide: sinon.spy((tabId, cb) => {
|
|
|
|
if (cb) {
|
|
|
|
throw new Error("Chrome do not expect a callback");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tabId == null) {
|
|
|
|
throw new Error(FAKE_ERROR_MSG);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
const {browser, Promise} = window;
|
|
|
|
|
|
|
|
const pageActionShowPromise = browser.pageAction.show(1).catch(err => err);
|
|
|
|
const pageActionHidePromise = browser.pageAction.hide(undefined).catch(err => err);
|
|
|
|
|
|
|
|
ok(pageActionShowPromise instanceof Promise,
|
|
|
|
"browser.pageAction.show returned a promise instance");
|
|
|
|
ok(pageActionHidePromise instanceof Promise,
|
|
|
|
"browser.pageAction.hide returned a promise instance");
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
pageActionShowPromise, pageActionHidePromise,
|
|
|
|
]).then(([pageActionShowResolved, pageActionHideRejected]) => {
|
|
|
|
ok(fakeChrome.pageAction.show.calledTwice, "chrome.pageAction.show has been called twice");
|
|
|
|
equal(fakeChrome.pageAction.show.firstCall.args.length, 2,
|
|
|
|
"chrome.pageAction.show first call has received a callback parameter");
|
|
|
|
equal(fakeChrome.pageAction.show.secondCall.args.length, 1,
|
|
|
|
"chrome.pageAction.show second call has received a single parameter");
|
|
|
|
equal(pageActionShowResolved, undefined, "pageAction.show resolved successfully");
|
|
|
|
|
|
|
|
ok(fakeChrome.pageAction.hide.calledTwice, "chrome.pageAction.hide has been called twice");
|
|
|
|
equal(fakeChrome.pageAction.hide.firstCall.args.length, 2,
|
|
|
|
"chrome.pageAction.hide first call has received a callback parameter");
|
|
|
|
equal(fakeChrome.pageAction.hide.secondCall.args.length, 1,
|
|
|
|
"chrome.pageAction.hide second call has received a single parameter");
|
|
|
|
|
|
|
|
ok(pageActionHideRejected instanceof Error,
|
|
|
|
"browser.pageAction.hide rejected value is an Error instance");
|
|
|
|
equal(pageActionHideRejected.message, FAKE_ERROR_MSG,
|
|
|
|
"browser.pageAction.hide rejected error has the expected message");
|
|
|
|
}).then(() => {
|
|
|
|
// Call pageAction.show and hide again to ensure that only after a successfull
|
|
|
|
// API call the wrapper will always call the API method without the callback parameter.
|
|
|
|
|
|
|
|
fakeChrome.pageAction.show.reset();
|
|
|
|
fakeChrome.pageAction.hide.reset();
|
|
|
|
|
|
|
|
const secondPageActionShowPromise = browser.pageAction.show(1).catch(err => err);
|
|
|
|
const secondPageActionHidePromise = browser.pageAction.hide(undefined).catch(err => err);
|
|
|
|
|
|
|
|
return Promise.all([secondPageActionShowPromise, secondPageActionHidePromise]);
|
|
|
|
}).then(([pageActionShowResolved, pageActionHideRejected]) => {
|
|
|
|
ok(fakeChrome.pageAction.show.calledOnce, "chrome.pageAction.show has been called once");
|
|
|
|
equal(fakeChrome.pageAction.show.firstCall.args.length, 1,
|
|
|
|
"chrome.pageAction.show call has not received a callback parameter");
|
|
|
|
|
|
|
|
ok(fakeChrome.pageAction.hide.calledTwice, "chrome.pageAction.hide has been called twice");
|
|
|
|
equal(fakeChrome.pageAction.hide.firstCall.args.length, 2,
|
|
|
|
"chrome.pageAction.hide first call has received a callback parameter");
|
|
|
|
equal(fakeChrome.pageAction.hide.secondCall.args.length, 1,
|
|
|
|
"chrome.pageAction.hide second call has received a single parameter");
|
|
|
|
|
|
|
|
ok(pageActionHideRejected instanceof Error,
|
|
|
|
"browser.pageAction.hide rejected value is an Error instance");
|
|
|
|
equal(pageActionHideRejected.message, FAKE_ERROR_MSG,
|
|
|
|
"browser.pageAction.hide rejected error has the expected message");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|