test_localStorageBasePrivateBrowsing_perwindowpb.html (14919B)
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <title>localStorage basic test, while in sesison only mode</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 10 const {TestUtils} = ChromeUtils.importESModule( 11 "resource://testing-common/TestUtils.sys.mjs" 12 ); 13 14 var prefBranch = Cc["@mozilla.org/preferences-service;1"] 15 .getService(Ci.nsIPrefBranch); 16 prefBranch.setIntPref("browser.startup.page", 0); 17 prefBranch.setCharPref("browser.startup.homepage_override.mstone", "ignore"); 18 19 var mainWindow = null; 20 function startTest() { 21 mainWindow = window.browsingContext.topChromeWindow; 22 23 doTest(); 24 } 25 26 var contentPage = "http://mochi.test:8888/chrome/dom/tests/mochitest/localstorage/page_blank.html"; 27 28 function testOnWindow(aIsPrivate, aCallback) { 29 var win = mainWindow.OpenBrowserWindow({private: aIsPrivate}); 30 win.addEventListener("load", function() { 31 TestUtils.topicObserved("browser-delayed-startup-finished", 32 subject => subject == win).then(() => { 33 win.addEventListener("DOMContentLoaded", function onInnerLoad() { 34 if (win.content.location.href != contentPage) { 35 win.gBrowser.loadURI(Services.io.newURI(contentPage), { 36 triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({}), 37 }); 38 return; 39 } 40 win.removeEventListener("DOMContentLoaded", onInnerLoad, true); 41 42 win.content.addEventListener("load", function innerLoad2() { 43 win.content.removeEventListener("load", innerLoad2); 44 SimpleTest.executeSoon(function() { aCallback(win); }); 45 }, false, true); 46 }, true); 47 SimpleTest.executeSoon(function() { 48 win.gBrowser.loadURI(Services.io.newURI(contentPage), { 49 triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({}), 50 }); 51 }); 52 }); 53 }, {capture: true, once: true}); 54 } 55 function doTest() { 56 testOnWindow(false, function(aWin) { 57 aWin.content.localStorage.setItem("persistent", "persistent1"); 58 59 testOnWindow(true, function(privateWin) { 60 is(privateWin.content.localStorage.getItem("persistent"), null, "previous values are inaccessible"); 61 62 // Initially check the privateWin.content.localStorage is empty 63 is(privateWin.content.localStorage.length, 0, "The storage is empty [1]"); 64 is(privateWin.content.localStorage.key(0), null, "key() should return null for out-of-bounds access"); 65 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 66 is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); 67 is(privateWin.content.localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())"); 68 is(privateWin.content.localStorage.nonexisting, undefined, "Nonexisting item is null (array access)"); 69 is(privateWin.content.localStorage.nonexisting, undefined, "Nonexisting item is null (property access)"); 70 privateWin.content.localStorage.removeItem("nonexisting"); // Just check there is no exception 71 72 is(typeof privateWin.content.localStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object"); 73 is(typeof privateWin.content.localStorage.nonexisting, "undefined", "['nonexisting'] is undefined"); 74 is(typeof privateWin.content.localStorage.nonexisting, "undefined", "nonexisting is undefined"); 75 is(typeof privateWin.content.localStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object"); 76 is(typeof privateWin.content.localStorage.nonexisting2, "undefined", "['nonexisting2'] is undefined"); 77 is(typeof privateWin.content.localStorage.nonexisting2, "undefined", "nonexisting2 is undefined"); 78 79 // add an empty-value key 80 privateWin.content.localStorage.setItem("empty", ""); 81 is(privateWin.content.localStorage.getItem("empty"), "", "Empty value (getItem())"); 82 is(privateWin.content.localStorage.empty, "", "Empty value (array access)"); 83 is(privateWin.content.localStorage.empty, "", "Empty value (property access)"); 84 is(typeof privateWin.content.localStorage.getItem("empty"), "string", "getItem('empty') is string"); 85 is(typeof privateWin.content.localStorage.empty, "string", "['empty'] is string"); 86 is(typeof privateWin.content.localStorage.empty, "string", "empty is string"); 87 privateWin.content.localStorage.removeItem("empty"); 88 is(privateWin.content.localStorage.length, 0, "The storage has no keys"); 89 is(privateWin.content.localStorage.getItem("empty"), null, "empty item is null (getItem())"); 90 is(privateWin.content.localStorage.empty, undefined, "empty item is undefined (array access)"); 91 is(privateWin.content.localStorage.empty, undefined, "empty item is undefined (property access)"); 92 is(typeof privateWin.content.localStorage.getItem("empty"), "object", "getItem('empty') is object"); 93 is(typeof privateWin.content.localStorage.empty, "undefined", "['empty'] is undefined"); 94 is(typeof privateWin.content.localStorage.empty, "undefined", "empty is undefined"); 95 96 // add one key, check it is there 97 privateWin.content.localStorage.setItem("key1", "value1"); 98 is(privateWin.content.localStorage.length, 1, "The storage has one key-value pair"); 99 is(privateWin.content.localStorage.key(0), "key1"); 100 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 101 is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); 102 103 // check all access method give the correct result 104 // and are of the correct type 105 is(privateWin.content.localStorage.getItem("key1"), "value1", "getItem('key1') == value1"); 106 is(privateWin.content.localStorage.key1, "value1", "['key1'] == value1"); 107 is(privateWin.content.localStorage.key1, "value1", "key1 == value1"); 108 109 is(typeof privateWin.content.localStorage.getItem("key1"), "string", "getItem('key1') is string"); 110 is(typeof privateWin.content.localStorage.key1, "string", "['key1'] is string"); 111 is(typeof privateWin.content.localStorage.key1, "string", "key1 is string"); 112 113 // remove the previously added key and check the storage is empty 114 privateWin.content.localStorage.removeItem("key1"); 115 is(privateWin.content.localStorage.length, 0, "The storage is empty [2]"); 116 is(privateWin.content.localStorage.key(0), null, "key() should return null for out-of-bounds access"); 117 is(privateWin.content.localStorage.getItem("key1"), null, "\'key1\' removed"); 118 119 is(typeof privateWin.content.localStorage.getItem("key1"), "object", "getItem('key1') is object"); 120 is(typeof privateWin.content.localStorage.key1, "undefined", "['key1'] is undefined"); 121 is(typeof privateWin.content.localStorage.key1, "undefined", "key1 is undefined"); 122 123 // add one key, check it is there 124 privateWin.content.localStorage.setItem("key1", "value1"); 125 is(privateWin.content.localStorage.length, 1, "The storage has one key-value pair"); 126 is(privateWin.content.localStorage.key(0), "key1"); 127 is(privateWin.content.localStorage.getItem("key1"), "value1"); 128 129 // add a second key 130 privateWin.content.localStorage.setItem("key2", "value2"); 131 is(privateWin.content.localStorage.length, 2, "The storage has two key-value pairs"); 132 is(privateWin.content.localStorage.getItem("key1"), "value1"); 133 is(privateWin.content.localStorage.getItem("key2"), "value2"); 134 var firstKey = privateWin.content.localStorage.key(0); 135 var secondKey = privateWin.content.localStorage.key(1); 136 ok((firstKey == 'key1' && secondKey == 'key2') || 137 (firstKey == 'key2' && secondKey == 'key1'), 138 'key() API works.'); 139 140 // change the second key 141 privateWin.content.localStorage.setItem("key2", "value2-2"); 142 is(privateWin.content.localStorage.length, 2, "The storage has two key-value pairs"); 143 is(privateWin.content.localStorage.key(0), firstKey); // After key value changes the order must be preserved 144 is(privateWin.content.localStorage.key(1), secondKey); 145 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 146 is(privateWin.content.localStorage.key(2), null, "key() should return null for out-of-bounds access"); 147 is(privateWin.content.localStorage.getItem("key1"), "value1"); 148 is(privateWin.content.localStorage.getItem("key2"), "value2-2"); 149 150 // change the first key 151 privateWin.content.localStorage.setItem("key1", "value1-2"); 152 is(privateWin.content.localStorage.length, 2, "The storage has two key-value pairs"); 153 is(privateWin.content.localStorage.key(0), firstKey); // After key value changes the order must be preserved 154 is(privateWin.content.localStorage.key(1), secondKey); 155 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 156 is(privateWin.content.localStorage.key(2), null, "key() should return null for out-of-bounds access"); 157 is(privateWin.content.localStorage.getItem("key1"), "value1-2"); 158 is(privateWin.content.localStorage.getItem("key2"), "value2-2"); 159 160 // remove the second key 161 privateWin.content.localStorage.removeItem("key2"); 162 is(privateWin.content.localStorage.length, 1, "The storage has one key-value pair"); 163 is(privateWin.content.localStorage.key(0), "key1"); 164 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 165 is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); 166 is(privateWin.content.localStorage.getItem("key1"), "value1-2"); 167 168 // JS property test 169 privateWin.content.localStorage.testA = "valueA"; 170 is(privateWin.content.localStorage.testA, "valueA"); 171 is(privateWin.content.localStorage.testA, "valueA"); 172 is(privateWin.content.localStorage.getItem("testA"), "valueA"); 173 174 privateWin.content.localStorage.testA = "valueA2"; 175 is(privateWin.content.localStorage.testA, "valueA2"); 176 is(privateWin.content.localStorage.testA, "valueA2"); 177 is(privateWin.content.localStorage.getItem("testA"), "valueA2"); 178 179 privateWin.content.localStorage.testB = "valueB"; 180 is(privateWin.content.localStorage.testB, "valueB"); 181 is(privateWin.content.localStorage.testB, "valueB"); 182 is(privateWin.content.localStorage.getItem("testB"), "valueB"); 183 184 privateWin.content.localStorage.testB = "valueB2"; 185 is(privateWin.content.localStorage.testB, "valueB2"); 186 is(privateWin.content.localStorage.testB, "valueB2"); 187 is(privateWin.content.localStorage.getItem("testB"), "valueB2"); 188 189 privateWin.content.localStorage.setItem("testC", "valueC"); 190 is(privateWin.content.localStorage.testC, "valueC"); 191 is(privateWin.content.localStorage.testC, "valueC"); 192 is(privateWin.content.localStorage.getItem("testC"), "valueC"); 193 194 privateWin.content.localStorage.setItem("testC", "valueC2"); 195 is(privateWin.content.localStorage.testC, "valueC2"); 196 is(privateWin.content.localStorage.testC, "valueC2"); 197 is(privateWin.content.localStorage.getItem("testC"), "valueC2"); 198 199 privateWin.content.localStorage.setItem("testC", null); 200 is("testC" in privateWin.content.localStorage, true); 201 is(privateWin.content.localStorage.getItem("testC"), "null"); 202 is(privateWin.content.localStorage.testC, "null"); 203 is(privateWin.content.localStorage.testC, "null"); 204 205 privateWin.content.localStorage.removeItem("testC"); 206 privateWin.content.localStorage.testC = null; 207 is("testC" in privateWin.content.localStorage, true); 208 is(privateWin.content.localStorage.getItem("testC"), "null"); 209 is(privateWin.content.localStorage.testC, "null"); 210 is(privateWin.content.localStorage.testC, "null"); 211 212 privateWin.content.localStorage.setItem(null, "test"); 213 is("null" in privateWin.content.localStorage, true); 214 is(privateWin.content.localStorage.getItem("null"), "test"); 215 is(privateWin.content.localStorage.getItem(null), "test"); 216 is(privateWin.content.localStorage.null, "test"); 217 privateWin.content.localStorage.removeItem(null, "test"); 218 is("null" in privateWin.content.localStorage, false); 219 220 privateWin.content.localStorage.setItem(null, "test"); 221 is("null" in privateWin.content.localStorage, true); 222 privateWin.content.localStorage.removeItem("null", "test"); 223 is("null" in privateWin.content.localStorage, false); 224 225 // Clear the storage 226 privateWin.content.localStorage.clear(); 227 is(privateWin.content.localStorage.length, 0, "The storage is empty [3]"); 228 is(privateWin.content.localStorage.key(0), null, "key() should return null for out-of-bounds access"); 229 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 230 is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); 231 is(privateWin.content.localStorage.getItem("nonexisting"), null, "Nonexisting item is null"); 232 is(privateWin.content.localStorage.getItem("key1"), null, "key1 removed"); 233 is(privateWin.content.localStorage.getItem("key2"), null, "key2 removed"); 234 privateWin.content.localStorage.removeItem("nonexisting"); // Just check there is no exception 235 privateWin.content.localStorage.removeItem("key1"); // Just check there is no exception 236 privateWin.content.localStorage.removeItem("key2"); // Just check there is no exception 237 238 privateWin.content.localStorage.setItem("must disappear", "private browsing value"); 239 240 privateWin.close(); 241 242 // The .close() call above will operate asynchronously, so execute the 243 // code below asynchronously as well. 244 function callback(newPrivateWin) { 245 is(newPrivateWin.content.localStorage.getItem("must disappear"), null, "private browsing values threw away"); 246 is(newPrivateWin.content.localStorage.length, 0, "No items"); 247 248 newPrivateWin.close(); 249 is(aWin.content.localStorage.getItem("persistent"), "persistent1", "back in normal mode"); 250 aWin.content.localStorage.clear(); 251 aWin.close(); 252 253 prefBranch.clearUserPref("browser.startup.page") 254 prefBranch.clearUserPref("browser.startup.homepage_override.mstone"); 255 SimpleTest.finish(); 256 }; 257 SimpleTest.executeSoon(() => testOnWindow(true, callback)); 258 }); 259 }); 260 } 261 262 SimpleTest.waitForExplicitFinish(); 263 264 </script> 265 266 </head> 267 268 <body onload="startTest();"> 269 270 </body> 271 </html>