dom/plugins/test/mochitest/test_privatemode_perwindowpb.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/test/mochitest/test_privatemode_perwindowpb.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,120 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
     1.7 +                 type="text/css"?>
     1.8 +<window title="NPAPI Private Mode Tests"
     1.9 +  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    1.10 +  <script type="application/javascript"
    1.11 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
    1.12 +  <script type="application/javascript"
    1.13 +          src="chrome://mochikit/content/chrome-harness.js" />
    1.14 +  <script type="application/javascript" src="utils.js"></script>
    1.15 +  <script type="application/javascript">
    1.16 +    setTestPluginEnabledState(SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED);
    1.17 +  </script>
    1.18 +<body xmlns="http://www.w3.org/1999/xhtml" onload="runTests()">
    1.19 +<embed id="plugin1" type="application/x-test" width="200" height="200"></embed>
    1.20 +<embed id="plugin2" type="application/x-test" width="200" height="200"></embed>
    1.21 +</body>
    1.22 +<script class="testbody" type="application/javascript">
    1.23 +<![CDATA[
    1.24 +SimpleTest.waitForExplicitFinish();
    1.25 +
    1.26 +const Cc = Components.classes;
    1.27 +const Ci = Components.interfaces;
    1.28 +
    1.29 +function runTests() {
    1.30 +  // Allow all cookies, then run the actual tests
    1.31 +  SpecialPowers.pushPrefEnv({"set": [["network.cookie.cookieBehavior", 0]]}, runTestsCallback);
    1.32 +}
    1.33 +
    1.34 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.35 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.36 +
    1.37 +function whenDelayedStartupFinished(aWindow, aCallback) {
    1.38 +  Services.obs.addObserver(function observer(aSubject, aTopic) {
    1.39 +    if (aWindow == aSubject) {
    1.40 +      Services.obs.removeObserver(observer, aTopic);
    1.41 +      SimpleTest.executeSoon(aCallback);
    1.42 +    }
    1.43 +  }, "browser-delayed-startup-finished", false);
    1.44 +}
    1.45 +
    1.46 +function runTestsCallback() {
    1.47 +  var pluginElement1 = document.getElementById("plugin1");
    1.48 +  var pluginElement2 = document.getElementById("plugin2");
    1.49 +
    1.50 +  var state1 = false;
    1.51 +  var state2 = false;
    1.52 +  var exceptionThrown = false;
    1.53 +
    1.54 +  try {
    1.55 +    state1 = pluginElement1.queryPrivateModeState();
    1.56 +    state2 = pluginElement2.queryPrivateModeState();
    1.57 +  } catch (e) {
    1.58 +    exceptionThrown = true;
    1.59 +  }
    1.60 +  is(exceptionThrown, false, "Exception thrown getting private mode state.");
    1.61 +  is(state1, false, "Browser returned incorrect private mode state.");
    1.62 +  is(state2, false, "Browser returned incorrect private mode state.");
    1.63 +
    1.64 +  pluginElement1.setCookie("foo");
    1.65 +  is(pluginElement1.getCookie(), "foo", "Cookie was set and retrieved correctly in public mode.");
    1.66 +
    1.67 +  // open a window with private mode and get the references of the elements.
    1.68 +  var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
    1.69 +                    .getInterface(Ci.nsIWebNavigation)
    1.70 +                    .QueryInterface(Ci.nsIDocShellTreeItem)
    1.71 +                    .rootTreeItem
    1.72 +                    .QueryInterface(Ci.nsIInterfaceRequestor)
    1.73 +                    .getInterface(Ci.nsIDOMWindow);
    1.74 +  var contentPage = getRootDirectory(window.location.href) + "privatemode_perwindowpb.xul";
    1.75 +
    1.76 +  function testOnWindow(aIsPrivate, aCallback) {
    1.77 +    var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
    1.78 +    whenDelayedStartupFinished(win, function () {
    1.79 +      win.addEventListener("DOMContentLoaded", function onInnerLoad() {
    1.80 +        if (win.content.location.href == "about:privatebrowsing") {
    1.81 +          win.gBrowser.loadURI(contentPage);
    1.82 +          return;
    1.83 +        }
    1.84 +        win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
    1.85 +        win.gBrowser.selectedBrowser.focus();
    1.86 +        SimpleTest.executeSoon(function() { aCallback(win); });
    1.87 +      }, true);
    1.88 +      SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
    1.89 +    });
    1.90 +  }
    1.91 +
    1.92 +  testOnWindow(true, function(aWin) {
    1.93 +    pluginElement1 = aWin.gBrowser.contentDocument.getElementById("plugin1");
    1.94 +    pluginElement2 = aWin.gBrowser.contentDocument.getElementById("plugin2");
    1.95 +
    1.96 +    var officialState1, officialState2;
    1.97 +    try {
    1.98 +      officialState1 = pluginElement1.queryPrivateModeState();
    1.99 +      officialState2 = pluginElement2.queryPrivateModeState();
   1.100 +    } catch (e) {
   1.101 +      exceptionThrown = true;
   1.102 +    }
   1.103 +    is(exceptionThrown, false, "Exception thrown getting private mode state.");
   1.104 +    is(officialState1, true, "Querying private mode reported incorrectly");
   1.105 +    is(officialState2, true, "Querying private mode reported incorrectly");
   1.106 +
   1.107 +    // It would be nice to assert that we don't see the public cookie in private mode,
   1.108 +    // but the NPAPI complains when the resulting string is empty.
   1.109 +    // is(pluginElement1.getCookie(), "", "Public cookie was not retrieved in private mode.");
   1.110 +    pluginElement1.setCookie("bar");
   1.111 +    is(pluginElement1.getCookie(), "bar", "Cookie was set and retrieved correctly in private mode.");
   1.112 +
   1.113 +    aWin.close();
   1.114 +
   1.115 +    pluginElement1 = document.getElementById("plugin1");
   1.116 +    is(pluginElement1.getCookie(), "foo", "Private cookie was not retrieved in public mode.");
   1.117 +
   1.118 +    SimpleTest.finish();
   1.119 +  });
   1.120 +}
   1.121 +]]>
   1.122 +</script>
   1.123 +</window>

mercurial