addon-sdk/source/test/addons/private-browsing-supported/test-page-mod.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/addons/private-browsing-supported/test-page-mod.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +const { getMostRecentBrowserWindow } = require('sdk/window/utils');
     1.5 +const { PageMod } = require("sdk/page-mod");
     1.6 +const { getActiveTab, setTabURL, openTab, closeTab } = require('sdk/tabs/utils');
     1.7 +const xulApp = require('sdk/system/xul-app');
     1.8 +const windowHelpers = require('sdk/window/helpers');
     1.9 +const { defer } = require("sdk/core/promise");
    1.10 +const { isPrivate } = require('sdk/private-browsing');
    1.11 +const { isTabPBSupported, isWindowPBSupported } = require('sdk/private-browsing/utils');
    1.12 +
    1.13 +function openWebpage(url, enablePrivate) {
    1.14 +  let { promise, resolve, reject } = defer();
    1.15 +
    1.16 +  if (xulApp.is("Fennec")) {
    1.17 +    let chromeWindow = getMostRecentBrowserWindow();
    1.18 +    let rawTab = openTab(chromeWindow, url, {
    1.19 +      isPrivate: enablePrivate
    1.20 +    });
    1.21 +
    1.22 +    resolve(function() {
    1.23 +      closeTab(rawTab)
    1.24 +    });
    1.25 +
    1.26 +    return promise;
    1.27 +  }
    1.28 +  else {
    1.29 +    windowHelpers.open("", {
    1.30 +      features: {
    1.31 +        toolbar: true,
    1.32 +        private: enablePrivate
    1.33 +      }
    1.34 +    }).then(function(chromeWindow) {
    1.35 +      if (isPrivate(chromeWindow) !== !!enablePrivate)
    1.36 +        reject(new Error("Window should have Private set to " + !!enablePrivate));
    1.37 +
    1.38 +      let tab = getActiveTab(chromeWindow);
    1.39 +      setTabURL(tab, url);
    1.40 +
    1.41 +      resolve(function(){
    1.42 +        windowHelpers.close(chromeWindow);
    1.43 +      });
    1.44 +    });
    1.45 +
    1.46 +    return promise;
    1.47 +  }
    1.48 +}
    1.49 +
    1.50 +exports["test page-mod on private tab"] = function (assert, done) {
    1.51 +  // Only set private mode when explicitely supported.
    1.52 +  // (fennec 19 has some intermediate PB support where isTabPBSupported
    1.53 +  // will be false, but isPrivate(worker.tab) will be true if we open a private
    1.54 +  // tab)
    1.55 +  let setPrivate = isTabPBSupported || isWindowPBSupported;
    1.56 +
    1.57 +  let id = Date.now().toString(36);
    1.58 +  let frameUri = "data:text/html;charset=utf-8," + id;
    1.59 +  let uri = "data:text/html;charset=utf-8," +
    1.60 +            encodeURIComponent(id + "<iframe src='" + frameUri + "'><iframe>");
    1.61 +
    1.62 +  let count = 0;
    1.63 +
    1.64 +  openWebpage(uri, setPrivate).then(function(close) {
    1.65 +    PageMod({
    1.66 +      include: [uri, frameUri],
    1.67 +
    1.68 +      onAttach: function(worker) {
    1.69 +        assert.ok(worker.tab.url == uri || worker.tab.url == frameUri,
    1.70 +                  "Got a worker attached to the private window tab");
    1.71 +
    1.72 +        if (setPrivate) {
    1.73 +          assert.ok(isPrivate(worker), "The worker is really private");
    1.74 +          assert.ok(isPrivate(worker.tab), "The document is really private");
    1.75 +        }
    1.76 +        else {
    1.77 +          assert.ok(!isPrivate(worker),
    1.78 +                    "private browsing isn't supported, " +
    1.79 +                    "so that the worker isn't private");
    1.80 +          assert.ok(!isPrivate(worker.tab),
    1.81 +                    "private browsing isn't supported, " +
    1.82 +                    "so that the document isn't private");
    1.83 +        }
    1.84 +
    1.85 +        if (++count == 2) {
    1.86 +          this.destroy();
    1.87 +          close();
    1.88 +          done();
    1.89 +        }
    1.90 +      }
    1.91 +    });
    1.92 +  }).then(null, assert.fail);
    1.93 +};
    1.94 +
    1.95 +exports["test page-mod on non-private tab"] = function (assert, done) {
    1.96 +  let id = Date.now().toString(36);
    1.97 +  let url = "data:text/html;charset=utf-8," + encodeURIComponent(id);
    1.98 +
    1.99 +  openWebpage(url, false).then(function(close){
   1.100 +    PageMod({
   1.101 +      include: url,
   1.102 +      onAttach: function(worker) {
   1.103 +        assert.equal(worker.tab.url, url,
   1.104 +                     "Got a worker attached to the private window tab");
   1.105 +        assert.ok(!isPrivate(worker), "The worker is really non-private");
   1.106 +        assert.ok(!isPrivate(worker.tab), "The document is really non-private");
   1.107 +
   1.108 +        this.destroy();
   1.109 +        close();
   1.110 +        done();
   1.111 +      }
   1.112 +    });
   1.113 +  }).then(null, assert.fail);
   1.114 +}

mercurial