dom/tests/mochitest/localstorage/test_localStorageQuotaPrivateBrowsing_perwindowpb.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/localstorage/test_localStorageQuotaPrivateBrowsing_perwindowpb.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,181 @@
     1.4 +<html xmlns="http://www.w3.org/1999/xhtml">
     1.5 +<head>
     1.6 +<title>localStorage and DOM quota test</title>
     1.7 +
     1.8 +<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
    1.10 +
    1.11 +<script type="text/javascript">
    1.12 +SimpleTest.waitForExplicitFinish();
    1.13 +
    1.14 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.15 +
    1.16 +const Ci = Components.interfaces;
    1.17 +const CONTENT_PAGE = "http://mochi.test:8888/chrome/dom/tests/mochitest/localstorage/page_blank.html";
    1.18 +const slavePath = "/tests/dom/tests/mochitest/localstorage/";
    1.19 +var currentTest = 1;
    1.20 +var quota;
    1.21 +
    1.22 +try {
    1.23 +  quota = Services.prefs.getIntPref("dom.storage.default_quota");
    1.24 +} catch (ex) {
    1.25 +  quota = 5 * 1024;
    1.26 +}
    1.27 +Services.prefs.setIntPref("browser.startup.page", 0);
    1.28 +Services.prefs.setIntPref("dom.storage.default_quota", 1);
    1.29 +
    1.30 +var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor).
    1.31 +  getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShellTreeItem).
    1.32 +  rootTreeItem.QueryInterface(Ci.nsIInterfaceRequestor).
    1.33 +  getInterface(Ci.nsIDOMWindow);
    1.34 +var slaveLoadsPending = 1;
    1.35 +var slaveOrigin = "";
    1.36 +var slave = null;
    1.37 +var failureRegExp = new RegExp("^FAILURE");
    1.38 +
    1.39 +function startTest() {
    1.40 +  testOnWindow(true, function(aWindow) {
    1.41 +    info("Private window loaded");
    1.42 +    var frame = aWindow.content.document.createElement("iframe");
    1.43 +    aWindow.content.document.body.appendChild(frame);
    1.44 +    aWindow.content.addEventListener("message", function(aEvent) {
    1.45 +      onMessageReceived(aEvent, aWindow)
    1.46 +    }, false);
    1.47 +    slave = aWindow.content.frames[0];
    1.48 +
    1.49 +    SimpleTest.waitForFocus(function() doNextTest(aWindow), aWindow);
    1.50 +  });
    1.51 +}
    1.52 +
    1.53 +function doNextTest(aWindow) {
    1.54 +  info("Running test: " + currentTest);
    1.55 +  switch (currentTest) {
    1.56 +    // Initialy setup the quota to testing value of 1024B and
    1.57 +    // set a 500 bytes key with name length 1 (allocate 501 bytes)
    1.58 +    case 1:
    1.59 +      slaveOrigin = "http://example.com";
    1.60 +      slave.location = slaveOrigin + slavePath + "frameQuota.html?add&A&success";
    1.61 +      break;
    1.62 +
    1.63 +    // In subdomain now set another key with length 500 bytes, i.e.
    1.64 +    // allocate 501 bytes
    1.65 +    case 2:
    1.66 +      slaveOrigin = "http://test1.example.com";
    1.67 +      slave.location = slaveOrigin + slavePath + "frameQuota.html?add&B&success";
    1.68 +      break;
    1.69 +
    1.70 +    // Try to set the same key value again to check we don't fail
    1.71 +    // even 1002 bytes has already been exhausted from the quota
    1.72 +    // We just change the value of an existing key.
    1.73 +    case 3:
    1.74 +      slaveOrigin = "http://test1.example.com";
    1.75 +      slave.location = slaveOrigin + slavePath + "frameQuota.html?add&B&success";
    1.76 +      break;
    1.77 +
    1.78 +    // Try to set the same key to a larger value that would lead to
    1.79 +    // quota reach and check that the value is still the old one
    1.80 +    case 4:
    1.81 +      slaveOrigin = "http://test1.example.com";
    1.82 +      slave.location = slaveOrigin + slavePath + "frameQuota.html?add2&B&failure";
    1.83 +      break;
    1.84 +
    1.85 +    // In a different subdomain try to set a new 500 bytes key
    1.86 +    // and check we fail because we are over the quota
    1.87 +    case 5:
    1.88 +      slaveOrigin = "https://test2.example.com";
    1.89 +      slave.location = slaveOrigin + slavePath + "frameQuota.html?add&C&failure";
    1.90 +      break;
    1.91 +
    1.92 +    // Remove from the second subdomain the second key, it must not fail
    1.93 +    // This should release the allocated space of the quota assigned to
    1.94 +    // example.com.
    1.95 +    case 6:
    1.96 +      slaveOrigin = "http://test1.example.com";
    1.97 +      slave.location = slaveOrigin + slavePath + "frameQuota.html?remove&B&success";
    1.98 +      break;
    1.99 +
   1.100 +    // Now try again to set 500 bytes key, it must succeed.
   1.101 +    case 7:
   1.102 +      slaveOrigin = "https://test2.example.com";
   1.103 +      slave.location = slaveOrigin + slavePath + "frameQuota.html?add&C&success";
   1.104 +      break;
   1.105 +
   1.106 +    case 8:
   1.107 +      // Do a clean up...
   1.108 +      // TODO Bug 455070, use just ?clear what invokes call
   1.109 +      // of clear() in the target frame. W/o clear method we must
   1.110 +      // call clear implemented as removeItem for each item in
   1.111 +      // the localStorage.
   1.112 +      slaveOrigin = "http://example.com";
   1.113 +      slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&A&";
   1.114 +      break;
   1.115 +
   1.116 +    case 9:
   1.117 +      // Do a clean up...
   1.118 +      slaveOrigin = "http://test1.example.com";
   1.119 +      slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&B&";
   1.120 +      break;
   1.121 +
   1.122 +    case 10:
   1.123 +      // Do a clean up...
   1.124 +      slaveOrigin = "https://test2.example.com";
   1.125 +      slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&C&";
   1.126 +      break;
   1.127 +
   1.128 +    default:
   1.129 +      Services.prefs.clearUserPref("browser.startup.page")
   1.130 +      Services.prefs.setIntPref("dom.storage.default_quota", quota);
   1.131 +      aWindow.close();
   1.132 +      SimpleTest.finish();
   1.133 +  }
   1.134 +
   1.135 +  ++currentTest;
   1.136 +}
   1.137 +
   1.138 +function onMessageReceived(event, aWindow) {
   1.139 +  info("Message received: " + event.data);
   1.140 +  switch (event.data) {
   1.141 +    // Indication of the frame onload event
   1.142 +    case "frame loaded":
   1.143 +      if (--slaveLoadsPending)
   1.144 +        break;
   1.145 +      // Just fall through...
   1.146 +    // Indication of successfully finished step of a test
   1.147 +    case "perf":
   1.148 +      slave.postMessage("step", slaveOrigin);
   1.149 +      break;
   1.150 +    // Indication of all test parts finish (from any of the frames)
   1.151 +    case "done":
   1.152 +      aWindow.content.localStorage.clear();
   1.153 +      slaveLoadsPending = 1;
   1.154 +      doNextTest(aWindow);
   1.155 +      break;
   1.156 +    // Any other message indicates error or succes message of a test
   1.157 +    default:
   1.158 +      SimpleTest.ok(!event.data.match(failureRegExp), event.data);
   1.159 +      break;
   1.160 +  }
   1.161 +}
   1.162 +
   1.163 +function testOnWindow(aIsPrivate, aCallback) {
   1.164 +  var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
   1.165 +  win.addEventListener("load", function onLoad() {
   1.166 +    win.removeEventListener("load", onLoad, false);
   1.167 +    win.addEventListener("DOMContentLoaded", function onInnerLoad() {
   1.168 +      if (win.content.location.href != CONTENT_PAGE) {
   1.169 +        win.gBrowser.loadURI(CONTENT_PAGE);
   1.170 +        return;
   1.171 +      }
   1.172 +      win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
   1.173 +      win.gBrowser.selectedBrowser.focus();
   1.174 +      SimpleTest.executeSoon(function() { aCallback(win); });
   1.175 +    }, true);
   1.176 +    win.gBrowser.loadURI(CONTENT_PAGE);
   1.177 +  }, true);
   1.178 +}
   1.179 +
   1.180 +</script>
   1.181 +</head>
   1.182 +<body onload="startTest();">
   1.183 +</body>
   1.184 +</html>

mercurial