"use strict";

const {assert} = require("chai");

const {setupTestDOMWindow} = require("./setup");

describe("browser-polyfill", () => {
  describe("proxies non-wrapped functions", () => {
    it("should proxy getters and setters", () => {
      const fakeChrome = {
        runtime: {myprop: "previous-value"},
        nowrapns: {nowrapkey: "previous-value"},
      };
      return setupTestDOMWindow(fakeChrome).then(window => {
        const setResult = window.browser.runtime.myprop = "new-value";
        const setResult2 = window.browser.nowrapns.nowrapkey = "new-value";

        assert.equal(setResult, "new-value",
                     "Got the expected result from setting a wrapped property name");
        assert.equal(setResult2, "new-value",
                     "Got the expected result from setting a wrapped property name");
      });
    });

    it("deletes proxy getter/setter that are not wrapped", () => {
      const fakeChrome = {};
      return setupTestDOMWindow(fakeChrome).then(window => {
        window.browser.newns = {newkey: "test-value"};
        assert.equal(window.browser.newns.newkey, "test-value",
                     "Got the expected result from setting a wrapped property name");

        const setRes = window.browser.newns = {newkey2: "new-value"};
        assert.equal(window.browser.newns.newkey2, "new-value",
                     "The new non-wrapped getter is cached");
        assert.deepEqual(setRes, {newkey2: "new-value"},
                     "Got the expected result from setting a new wrapped property name");
        assert.deepEqual(window.browser.newns, window.chrome.newns,
                         "chrome.newns and browser.newns are the same");


        delete window.browser.newns.newkey2;
        assert.equal(window.browser.newns.newkey2, undefined,
                     "Got the expected result from setting a wrapped property name");
        assert.ok(!("newkey2" in window.browser.newns),
                  "The deleted property is not listed anymore");
      });
    });
  });
});

@@ -324,16 +324,27 @@
 rapns: %7B
+%0A          
 nowrapke
@@ -354,32 +354,82 @@
 %22previous-value%22
+,%0A          nowrapkey2: %22previous-value%22,%0A        
 %7D,%0A      %7D;%0A    
@@ -477,32 +477,476 @@
 hen(window =%3E %7B%0A
+        // Check that the property values on the generated wrapper.%0A        assert.equal(window.browser.runtime.myprop, %22previous-value%22,%0A                     %22Got the expected result from setting a wrapped property name%22);%0A        assert.equal(window.browser.nowrapns.nowrapkey, %22previous-value%22,%0A                     %22Got the expected result from setting a wrapped property name%22);%0A%0A        // Update the properties on the generated wrapper.%0A
         const se
@@ -1077,16 +1077,97 @@
 alue%22;%0A%0A
+        // Check the results of setting the new value of the wrapped properties.%0A
         
@@ -1416,24 +1416,1239 @@
 erty name%22);
+%0A%0A        // Verify that the wrapped properties has been updated.%0A        assert.equal(window.browser.runtime.myprop, %22new-value%22,%0A                     %22Got the expected updated value from the browser property%22);%0A        assert.equal(window.browser.nowrapns.nowrapkey, %22new-value%22,%0A                     %22Got the expected updated value from the browser property%22);%0A%0A        // Verify that the target properties has been updated.%0A        assert.equal(window.chrome.runtime.myprop, %22new-value%22,%0A                     %22Got the expected updated value on the related chrome property%22);%0A        assert.equal(window.chrome.nowrapns.nowrapkey, %22new-value%22,%0A                     %22Got the expected updated value on the related chrome property%22);%0A%0A        // Set a property multiple times before read.%0A        window.browser.nowrapns.nowrapkey2 = %22new-value2%22;%0A        window.browser.nowrapns.nowrapkey2 = %22new-value3%22;%0A%0A        assert.equal(window.chrome.nowrapns.nowrapkey2, %22new-value3%22,%0A                     %22Got the expected updated value on the related chrome property%22);%0A        assert.equal(window.browser.nowrapns.nowrapkey2, %22new-value3%22,%0A                     %22Got the expected updated value on the wrapped property%22);
 %0A      %7D);%0A 

tweak the proxied properties tests.