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-11 14:39:13 +00:00
|
|
|
const sinon = require("sinon");
|
2016-10-10 00:48:35 +00:00
|
|
|
|
|
|
|
const {setupTestDOMWindow} = require("./setup");
|
|
|
|
|
|
|
|
describe("browser-polyfill", () => {
|
|
|
|
describe("proxies non-wrapped functions", () => {
|
2016-10-11 14:39:13 +00:00
|
|
|
it("should proxy non-wrapped methods", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
|
|
|
nonwrappedmethod: sinon.spy(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
2016-10-12 00:08:52 +00:00
|
|
|
ok(window.browser.runtime.nonwrappedmethod);
|
2016-10-11 14:39:13 +00:00
|
|
|
|
|
|
|
const fakeCallback = () => {};
|
|
|
|
window.browser.runtime.nonwrappedmethod(fakeCallback);
|
|
|
|
|
|
|
|
const receivedCallback = fakeChrome.runtime.nonwrappedmethod.firstCall.args[0];
|
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(fakeCallback, receivedCallback,
|
2016-10-11 14:39:13 +00:00
|
|
|
"The callback has not been wrapped for the nonwrappedmethod");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-10 00:48:35 +00:00
|
|
|
it("should proxy getters and setters", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {myprop: "previous-value"},
|
2016-10-11 12:47:38 +00:00
|
|
|
nowrapns: {
|
|
|
|
nowrapkey: "previous-value",
|
|
|
|
nowrapkey2: "previous-value",
|
|
|
|
},
|
2016-10-10 00:48:35 +00:00
|
|
|
};
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
2016-10-11 12:47:38 +00:00
|
|
|
// Check that the property values on the generated wrapper.
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.browser.runtime.myprop, "previous-value",
|
2016-10-11 12:47:38 +00:00
|
|
|
"Got the expected result from setting a wrapped property name");
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.browser.nowrapns.nowrapkey, "previous-value",
|
2016-10-11 12:47:38 +00:00
|
|
|
"Got the expected result from setting a wrapped property name");
|
|
|
|
|
|
|
|
// Update the properties on the generated wrapper.
|
2016-10-10 00:48:35 +00:00
|
|
|
const setResult = window.browser.runtime.myprop = "new-value";
|
|
|
|
const setResult2 = window.browser.nowrapns.nowrapkey = "new-value";
|
|
|
|
|
2016-10-11 12:47:38 +00:00
|
|
|
// Check the results of setting the new value of the wrapped properties.
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(setResult, "new-value",
|
2016-10-10 00:48:35 +00:00
|
|
|
"Got the expected result from setting a wrapped property name");
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(setResult2, "new-value",
|
2016-10-10 00:48:35 +00:00
|
|
|
"Got the expected result from setting a wrapped property name");
|
2016-10-11 12:47:38 +00:00
|
|
|
|
|
|
|
// Verify that the wrapped properties has been updated.
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.browser.runtime.myprop, "new-value",
|
2016-10-11 12:47:38 +00:00
|
|
|
"Got the expected updated value from the browser property");
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.browser.nowrapns.nowrapkey, "new-value",
|
2016-10-11 12:47:38 +00:00
|
|
|
"Got the expected updated value from the browser property");
|
|
|
|
|
|
|
|
// Verify that the target properties has been updated.
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.chrome.runtime.myprop, "new-value",
|
2016-10-11 12:47:38 +00:00
|
|
|
"Got the expected updated value on the related chrome property");
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.chrome.nowrapns.nowrapkey, "new-value",
|
2016-10-11 12:47:38 +00:00
|
|
|
"Got the expected updated value on the related chrome property");
|
|
|
|
|
|
|
|
// Set a property multiple times before read.
|
|
|
|
window.browser.nowrapns.nowrapkey2 = "new-value2";
|
|
|
|
window.browser.nowrapns.nowrapkey2 = "new-value3";
|
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.chrome.nowrapns.nowrapkey2, "new-value3",
|
2016-10-11 12:47:38 +00:00
|
|
|
"Got the expected updated value on the related chrome property");
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.browser.nowrapns.nowrapkey2, "new-value3",
|
2016-10-11 12:47:38 +00:00
|
|
|
"Got the expected updated value on the wrapped property");
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-11 12:27:26 +00:00
|
|
|
it("deletes proxy getter/setter that are not wrapped", () => {
|
2016-10-10 00:48:35 +00:00
|
|
|
const fakeChrome = {};
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
window.browser.newns = {newkey: "test-value"};
|
2016-10-11 14:39:13 +00:00
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
ok("newns" in window.browser, "The custom namespace is in the wrapper");
|
|
|
|
ok("newns" in window.chrome, "The custom namespace is in the target");
|
2016-10-11 14:39:13 +00:00
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.browser.newns.newkey, "test-value",
|
2016-10-10 00:48:35 +00:00
|
|
|
"Got the expected result from setting a wrapped property name");
|
|
|
|
|
|
|
|
const setRes = window.browser.newns = {newkey2: "new-value"};
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.browser.newns.newkey2, "new-value",
|
2016-10-10 00:48:35 +00:00
|
|
|
"The new non-wrapped getter is cached");
|
2016-10-12 00:08:52 +00:00
|
|
|
deepEqual(setRes, {newkey2: "new-value"},
|
2016-10-10 00:48:35 +00:00
|
|
|
"Got the expected result from setting a new wrapped property name");
|
2016-10-12 00:08:52 +00:00
|
|
|
deepEqual(window.browser.newns, window.chrome.newns,
|
2016-10-10 00:48:35 +00:00
|
|
|
"chrome.newns and browser.newns are the same");
|
|
|
|
|
|
|
|
delete window.browser.newns.newkey2;
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(window.browser.newns.newkey2, undefined,
|
2016-10-10 00:48:35 +00:00
|
|
|
"Got the expected result from setting a wrapped property name");
|
2016-10-12 00:08:52 +00:00
|
|
|
ok(!("newkey2" in window.browser.newns),
|
2016-10-11 12:27:26 +00:00
|
|
|
"The deleted property is not listed anymore");
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|