2016-10-10 00:48:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
2017-04-12 15:30:33 +00:00
|
|
|
const {deepEqual, equal, ok} = require("chai").assert;
|
2016-10-10 00:48:35 +00:00
|
|
|
|
|
|
|
const {setupTestDOMWindow} = require("./setup");
|
|
|
|
|
|
|
|
describe("browser-polyfill", () => {
|
2019-07-18 13:21:49 +00:00
|
|
|
it("throws an error in a non-extension environment", async () => {
|
|
|
|
try {
|
|
|
|
await setupTestDOMWindow(null);
|
|
|
|
ok(false, "The polyfill script should have failed to load.");
|
|
|
|
} catch (e) {
|
|
|
|
equal(e.message,
|
|
|
|
"This script should only be loaded in a browser extension.",
|
|
|
|
"Expected script to not load in a non-extension environment");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-11 12:27:26 +00:00
|
|
|
it("wraps the global chrome namespace with a global browser namespace", () => {
|
2016-10-10 00:48:35 +00:00
|
|
|
const fakeChrome = {};
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(typeof window.browser, "object", "Got the window.browser object");
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|
2016-10-11 14:39:13 +00:00
|
|
|
|
2016-11-02 20:31:56 +00:00
|
|
|
it("does not override the global browser namespace if it already exists", () => {
|
2016-10-11 14:39:13 +00:00
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {lastError: null},
|
|
|
|
};
|
|
|
|
const fakeBrowser = {
|
2017-04-12 15:30:33 +00:00
|
|
|
mycustomns: {mybrowserkey: true},
|
2016-10-11 14:39:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome, fakeBrowser).then(window => {
|
2016-10-12 00:08:52 +00:00
|
|
|
deepEqual(window.browser, fakeBrowser,
|
2016-11-02 20:31:56 +00:00
|
|
|
"The existing browser has not been wrapped");
|
2016-10-11 14:39:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("browser wrapper", () => {
|
|
|
|
it("supports custom properties defined using Object.defineProperty", () => {
|
|
|
|
const fakeChrome = {};
|
2017-04-12 15:30:33 +00:00
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
Object.defineProperty(window.browser, "myns", {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
value: {mykey: true},
|
|
|
|
});
|
|
|
|
|
|
|
|
ok("myns" in window.browser, "The custom property exists");
|
|
|
|
ok("mykey" in window.browser.myns,
|
|
|
|
"The content of the custom property exists");
|
|
|
|
|
|
|
|
deepEqual(window.browser.myns, {mykey: true},
|
|
|
|
"The custom property has the expected content");
|
|
|
|
|
|
|
|
delete window.browser.myns;
|
|
|
|
|
|
|
|
ok(!("myns" in window.browser),
|
|
|
|
"The deleted custom defined property has been removed");
|
|
|
|
});
|
2016-10-11 14:39:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns undefined for property undefined in the target", () => {
|
2017-04-12 15:30:33 +00:00
|
|
|
const fakeChrome = {myns: {mychromekey: true}};
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
equal(window.browser.myns.mychromekey, true,
|
|
|
|
"Got the expected result from a wrapped property");
|
|
|
|
equal(window.browser.myns.nonexistent, undefined,
|
|
|
|
"Got undefined for non existent property");
|
|
|
|
equal(window.browser.nonexistent, undefined,
|
|
|
|
"Got undefined for non existent namespaces");
|
|
|
|
});
|
2016-10-11 14:39:13 +00:00
|
|
|
});
|
|
|
|
});
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|