summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/mochitest/test_privatemode_perwindowpb.xul
blob: ae6c9d292f9c6ad4963c837ecf20fa59652d3a12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
                 type="text/css"?>
<window title="NPAPI Private Mode Tests"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
  <script type="application/javascript"
          src="chrome://mochikit/content/chrome-harness.js" />
  <script type="application/javascript" src="plugin-utils.js"></script>
  <script type="application/javascript">
    setTestPluginEnabledState(SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED);
  </script>
<body xmlns="http://www.w3.org/1999/xhtml" onload="runTests()">
<embed id="plugin1" type="application/x-test" width="200" height="200"></embed>
<embed id="plugin2" type="application/x-test" width="200" height="200"></embed>
</body>
<script class="testbody" type="application/javascript">
<![CDATA[
SimpleTest.waitForExplicitFinish();

var Cc = Components.classes;
var Ci = Components.interfaces;

function runTests() {
  // Allow all cookies, then run the actual tests
  SpecialPowers.pushPrefEnv({"set": [["network.cookie.cookieBehavior", 0]]}, runTestsCallback);
}

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");

function whenDelayedStartupFinished(aWindow, aCallback) {
  Services.obs.addObserver(function observer(aSubject, aTopic) {
    if (aWindow == aSubject) {
      Services.obs.removeObserver(observer, aTopic);
      SimpleTest.executeSoon(aCallback);
    }
  }, "browser-delayed-startup-finished", false);
}

function runTestsCallback() {
  var pluginElement1 = document.getElementById("plugin1");
  var pluginElement2 = document.getElementById("plugin2");

  var state1 = false;
  var state2 = false;
  var exceptionThrown = false;

  try {
    state1 = pluginElement1.queryPrivateModeState();
    state2 = pluginElement2.queryPrivateModeState();
  } catch (e) {
    exceptionThrown = true;
  }
  is(exceptionThrown, false, "Exception thrown getting private mode state.");
  is(state1, false, "Browser returned incorrect private mode state.");
  is(state2, false, "Browser returned incorrect private mode state.");

  pluginElement1.setCookie("foo");
  is(pluginElement1.getCookie(), "foo", "Cookie was set and retrieved correctly in public mode.");

  // open a window with private mode and get the references of the elements.
  var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIWebNavigation)
                    .QueryInterface(Ci.nsIDocShellTreeItem)
                    .rootTreeItem
                    .QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIDOMWindow);
  var contentPage = getRootDirectory(window.location.href) + "privatemode_perwindowpb.xul";

  function testOnWindow(aIsPrivate, aCallback) {
    var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
    whenDelayedStartupFinished(win, function () {
      win.addEventListener("DOMContentLoaded", function onInnerLoad() {
        if (win.content.location.href == "about:privatebrowsing") {
          win.gBrowser.loadURI(contentPage);
          return;
        }
        win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
        win.gBrowser.selectedBrowser.focus();
        SimpleTest.executeSoon(function() { aCallback(win); });
      }, true);
      SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
    });
  }

  testOnWindow(true, function(aWin) {
    pluginElement1 = aWin.gBrowser.contentDocument.getElementById("plugin1");
    pluginElement2 = aWin.gBrowser.contentDocument.getElementById("plugin2");

    var officialState1, officialState2;
    try {
      officialState1 = pluginElement1.queryPrivateModeState();
      officialState2 = pluginElement2.queryPrivateModeState();
    } catch (e) {
      exceptionThrown = true;
    }
    is(exceptionThrown, false, "Exception thrown getting private mode state.");
    is(officialState1, true, "Querying private mode reported incorrectly");
    is(officialState2, true, "Querying private mode reported incorrectly");

    // It would be nice to assert that we don't see the public cookie in private mode,
    // but the NPAPI complains when the resulting string is empty.
    // is(pluginElement1.getCookie(), "", "Public cookie was not retrieved in private mode.");
    pluginElement1.setCookie("bar");
    is(pluginElement1.getCookie(), "bar", "Cookie was set and retrieved correctly in private mode.");

    aWin.close();

    pluginElement1 = document.getElementById("plugin1");
    is(pluginElement1.getCookie(), "foo", "Private cookie was not retrieved in public mode.");

    SimpleTest.finish();
  });
}
]]>
</script>
</window>