test_localStorageQuotaPrivateBrowsing_perwindowpb.html (6898B)
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <title>localStorage and DOM quota test</title> 4 5 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?> 7 8 <script type="text/javascript"> 9 SimpleTest.waitForExplicitFinish(); 10 11 const CONTENT_PAGE = "http://mochi.test:8888/chrome/dom/tests/mochitest/localstorage/page_blank.html"; 12 const slavePath = "/chrome/dom/tests/mochitest/localstorage/"; 13 var currentTest = 1; 14 var quota = Services.prefs.getIntPref("dom.storage.default_quota", 5 * 1024); 15 var siteQuota = Services.prefs.getIntPref("dom.storage.default_site_quota", 25 * 1024); 16 Services.prefs.setIntPref("browser.startup.page", 0); 17 Services.prefs.setIntPref("dom.storage.default_quota", 1); 18 Services.prefs.setIntPref("dom.storage.default_site_quota", 1); 19 20 var slaveLoadsPending = 1; 21 var slaveOrigin = ""; 22 var slave = null; 23 var failureRegExp = new RegExp("^FAILURE"); 24 25 function startTest() { 26 testOnWindow(true, function(aWindow) { 27 info("Private window loaded"); 28 var frame = aWindow.content.document.createElement("iframe"); 29 aWindow.content.document.body.appendChild(frame); 30 aWindow.content.addEventListener("message", function(aEvent) { 31 onMessageReceived(aEvent, aWindow) 32 }); 33 slave = aWindow.content.frames[0]; 34 35 SimpleTest.waitForFocus(() => doNextTest(aWindow), aWindow); 36 }); 37 } 38 39 function doNextTest(aWindow) { 40 info("Running test: " + currentTest); 41 switch (currentTest) { 42 // Initialy setup the quota to testing value of 1024B and 43 // set a 500 bytes key with name length 1 (allocate 501 bytes) 44 case 1: 45 if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) { 46 slaveOrigin = "http://test1.example.com"; 47 } else { 48 slaveOrigin = "http://example.com"; 49 } 50 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&A&success"; 51 break; 52 53 // Now set another key with length 500 bytes, i.e. allocate 501 bytes 54 case 2: 55 slaveOrigin = "http://test1.example.com"; 56 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&B&success"; 57 break; 58 59 // Try to set the same key value again to check we don't fail 60 // even 1002 bytes has already been exhausted from the quota 61 // We just change the value of an existing key. 62 case 3: 63 slaveOrigin = "http://test1.example.com"; 64 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&B&success"; 65 break; 66 67 // Try to set the same key to a larger value that would lead to 68 // quota reach and check that the value is still the old one 69 case 4: 70 slaveOrigin = "http://test1.example.com"; 71 slave.location = slaveOrigin + slavePath + "frameQuota.html?add2&B&failure"; 72 break; 73 74 // Try to set a new 500 bytes key and check we fail because we are over the 75 // quota. 76 case 5: 77 if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) { 78 slaveOrigin = "http://test1.example.com"; 79 } else { 80 slaveOrigin = "https://test2.example.com"; 81 } 82 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&C&failure"; 83 break; 84 85 // Remove the second key, it must not fail. This should release the 86 // allocated space of the quota assigned to test1.example.com. 87 case 6: 88 slaveOrigin = "http://test1.example.com"; 89 slave.location = slaveOrigin + slavePath + "frameQuota.html?remove&B&success"; 90 break; 91 92 // Now try again to set 500 bytes key, it must succeed. 93 case 7: 94 if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) { 95 slaveOrigin = "http://test1.example.com"; 96 } else { 97 slaveOrigin = "https://test2.example.com"; 98 } 99 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&C&success"; 100 break; 101 102 case 8: 103 if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) { 104 SimpleTest.executeSoon(() => doNextTest(aWindow)); 105 } else { 106 // Do a clean up... 107 slaveOrigin = "http://example.com"; 108 slave.location = slaveOrigin + slavePath + "frameQuota.html?clear"; 109 } 110 break; 111 112 case 9: 113 // Do a clean up... 114 slaveOrigin = "http://test1.example.com"; 115 slave.location = slaveOrigin + slavePath + "frameQuota.html?clear"; 116 break; 117 118 case 10: 119 if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) { 120 SimpleTest.executeSoon(() => doNextTest(aWindow)); 121 } else { 122 // Do a clean up... 123 slaveOrigin = "https://test2.example.com"; 124 slave.location = slaveOrigin + slavePath + "frameQuota.html?clear"; 125 } 126 break; 127 128 default: 129 Services.prefs.clearUserPref("browser.startup.page") 130 Services.prefs.setIntPref("dom.storage.default_quota", quota); 131 Services.prefs.setIntPref("dom.storage.default_site_quota", siteQuota); 132 aWindow.close(); 133 SimpleTest.finish(); 134 } 135 136 ++currentTest; 137 } 138 139 function onMessageReceived(event, aWindow) { 140 info("Message received: " + event.data); 141 switch (event.data) { 142 // Indication of the frame onload event 143 case "frame loaded": 144 if (--slaveLoadsPending) 145 break; 146 // Indication of successfully finished step of a test 147 // Just fall through... 148 case "perf": 149 // postMessage should send to the slaveOrigin. However with the addition of private 150 // browsing flags in origin attributes this will cause postMessage to fail. The origin of this 151 // window has false privatebrowsing, while the recipient is in a private window. 152 // To fix this issue and preserve the integrity of the test a * is passed to get around origin equality. 153 slave.postMessage("step", "*"); 154 break; 155 // Indication of all test parts finish (from any of the frames) 156 case "done": 157 aWindow.content.localStorage.clear(); 158 slaveLoadsPending = 1; 159 doNextTest(aWindow); 160 break; 161 // Any other message indicates error or succes message of a test 162 default: 163 SimpleTest.ok(!event.data.match(failureRegExp), event.data); 164 break; 165 } 166 } 167 168 function whenDelayedStartupFinished(aCallback) { 169 Services.obs.addObserver(function observer(aSubject, aTopic) { 170 Services.obs.removeObserver(observer, aTopic); 171 172 aSubject.addEventListener("DOMContentLoaded", function() { 173 SimpleTest.executeSoon(function() { aCallback(aSubject); }); 174 }, {capture: true, once: true}); 175 }, "browser-delayed-startup-finished"); 176 } 177 178 function testOnWindow(aIsPrivate, callback) { 179 var mainWindow = window.browsingContext.topChromeWindow; 180 181 mainWindow.openWebLinkIn(CONTENT_PAGE, "window", { 182 private: aIsPrivate }); 183 whenDelayedStartupFinished(callback); 184 }; 185 </script> 186 </head> 187 <body onload="startTest();"> 188 </body> 189 </html>