summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/localstorage/test_localStorageQuotaPrivateBrowsing_perwindowpb.html
blob: 24d3e4ba33d536b091d7082135267dd11c4a73e0 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>localStorage and DOM quota test</title>

<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>

<script type="text/javascript">
SimpleTest.waitForExplicitFinish();

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

const Ci = Components.interfaces;
const CONTENT_PAGE = "http://mochi.test:8888/chrome/dom/tests/mochitest/localstorage/page_blank.html";
const slavePath = "/chrome/dom/tests/mochitest/localstorage/";
var currentTest = 1;
var quota;

try {
  quota = Services.prefs.getIntPref("dom.storage.default_quota");
} catch (ex) {
  quota = 5 * 1024;
}
Services.prefs.setIntPref("browser.startup.page", 0);
Services.prefs.setIntPref("dom.storage.default_quota", 1);

var slaveLoadsPending = 1;
var slaveOrigin = "";
var slave = null;
var failureRegExp = new RegExp("^FAILURE");

function startTest() {
  testOnWindow(true, function(aWindow) {
    info("Private window loaded");
    var frame = aWindow.content.document.createElement("iframe");
    aWindow.content.document.body.appendChild(frame);
    aWindow.content.addEventListener("message", function(aEvent) {
      onMessageReceived(aEvent, aWindow)
    }, false);
    slave = aWindow.content.frames[0];

    SimpleTest.waitForFocus(() => doNextTest(aWindow), aWindow);
  });
}

function doNextTest(aWindow) {
  info("Running test: " + currentTest);
  switch (currentTest) {
    // Initialy setup the quota to testing value of 1024B and
    // set a 500 bytes key with name length 1 (allocate 501 bytes)
    case 1:
      slaveOrigin = "http://example.com";
      slave.location = slaveOrigin + slavePath + "frameQuota.html?add&A&success";
      break;

    // In subdomain now set another key with length 500 bytes, i.e.
    // allocate 501 bytes
    case 2:
      slaveOrigin = "http://test1.example.com";
      slave.location = slaveOrigin + slavePath + "frameQuota.html?add&B&success";
      break;

    // Try to set the same key value again to check we don't fail
    // even 1002 bytes has already been exhausted from the quota
    // We just change the value of an existing key.
    case 3:
      slaveOrigin = "http://test1.example.com";
      slave.location = slaveOrigin + slavePath + "frameQuota.html?add&B&success";
      break;

    // Try to set the same key to a larger value that would lead to
    // quota reach and check that the value is still the old one
    case 4:
      slaveOrigin = "http://test1.example.com";
      slave.location = slaveOrigin + slavePath + "frameQuota.html?add2&B&failure";
      break;

    // In a different subdomain try to set a new 500 bytes key
    // and check we fail because we are over the quota
    case 5:
      slaveOrigin = "https://test2.example.com";
      slave.location = slaveOrigin + slavePath + "frameQuota.html?add&C&failure";
      break;

    // Remove from the second subdomain the second key, it must not fail
    // This should release the allocated space of the quota assigned to
    // example.com.
    case 6:
      slaveOrigin = "http://test1.example.com";
      slave.location = slaveOrigin + slavePath + "frameQuota.html?remove&B&success";
      break;

    // Now try again to set 500 bytes key, it must succeed.
    case 7:
      slaveOrigin = "https://test2.example.com";
      slave.location = slaveOrigin + slavePath + "frameQuota.html?add&C&success";
      break;

    case 8:
      // Do a clean up...
      // TODO Bug 455070, use just ?clear what invokes call
      // of clear() in the target frame. W/o clear method we must
      // call clear implemented as removeItem for each item in
      // the localStorage.
      slaveOrigin = "http://example.com";
      slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&A&";
      break;

    case 9:
      // Do a clean up...
      slaveOrigin = "http://test1.example.com";
      slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&B&";
      break;

    case 10:
      // Do a clean up...
      slaveOrigin = "https://test2.example.com";
      slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&C&";
      break;

    default:
      Services.prefs.clearUserPref("browser.startup.page")
      Services.prefs.setIntPref("dom.storage.default_quota", quota);
      aWindow.close();
      SimpleTest.finish();
  }

  ++currentTest;
}

function onMessageReceived(event, aWindow) {
  info("Message received: " + event.data);
  switch (event.data) {
    // Indication of the frame onload event
    case "frame loaded":
      if (--slaveLoadsPending)
        break;
      // Just fall through...
    // Indication of successfully finished step of a test
    case "perf":
      // postMessage should send to the slaveOrigin. However with the addition of private
      // browsing flags in origin attributes this will cause postMessage to fail. The origin of this
      // window has false privatebrowsing, while the recipient is in a private window.
      // To fix this issue and preserve the integrity of the test a * is passed to get around origin equality.
      slave.postMessage("step", "*");
      break;
    // Indication of all test parts finish (from any of the frames)
    case "done":
      aWindow.content.localStorage.clear();
      slaveLoadsPending = 1;
      doNextTest(aWindow);
      break;
    // Any other message indicates error or succes message of a test
    default:
      SimpleTest.ok(!event.data.match(failureRegExp), event.data);
      break;
  }
}

function whenDelayedStartupFinished(aWindow, aCallback) {
  Services.obs.addObserver(function observer(aSubject, aTopic) {
    if (aWindow == aSubject) {
      Services.obs.removeObserver(observer, aTopic);
    }

    if (aWindow.content == null || aWindow.content.location.href != CONTENT_PAGE ) {
      aWindow.addEventListener("DOMContentLoaded", function onInnerLoad() {
        aWindow.removeEventListener("DOMContentLoaded", onInnerLoad, true);
        SimpleTest.executeSoon(function() { aCallback(aWindow); });
      }, true);

      aWindow.gBrowser.loadURI(CONTENT_PAGE);
    }
  }, "browser-delayed-startup-finished", false);
}

function testOnWindow(aIsPrivate, callback) {
  var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
                         .getInterface(Ci.nsIWebNavigation)
                         .QueryInterface(Ci.nsIDocShellTreeItem)
                         .rootTreeItem
                         .QueryInterface(Ci.nsIInterfaceRequestor)
                         .getInterface(Ci.nsIDOMWindow);

  var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
  whenDelayedStartupFinished(win, function() { callback(win); });
};
</script>
</head>
<body onload="startTest();">
</body>
</html>