1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/localstorage/test_localStorageBasePrivateBrowsing_perwindowpb.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,265 @@ 1.4 +<html xmlns="http://www.w3.org/1999/xhtml"> 1.5 +<head> 1.6 +<title>localStorage basic test, while in sesison only mode</title> 1.7 + 1.8 +<script type="application/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 + 1.13 +const Ci = Components.interfaces; 1.14 +var mainWindow; 1.15 + 1.16 +var prefBranch = Components.classes["@mozilla.org/preferences-service;1"] 1.17 + .getService(Components.interfaces.nsIPrefBranch); 1.18 +prefBranch.setIntPref("browser.startup.page", 0); 1.19 +prefBranch.setCharPref("browser.startup.homepage_override.mstone", "ignore"); 1.20 + 1.21 +function startTest() { 1.22 + mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor) 1.23 + .getInterface(Ci.nsIWebNavigation) 1.24 + .QueryInterface(Ci.nsIDocShellTreeItem) 1.25 + .rootTreeItem 1.26 + .QueryInterface(Ci.nsIInterfaceRequestor) 1.27 + .getInterface(Ci.nsIDOMWindow); 1.28 + 1.29 + doTest(); 1.30 +} 1.31 + 1.32 +var contentPage = "http://mochi.test:8888/chrome/dom/tests/mochitest/localstorage/page_blank.html"; 1.33 + 1.34 +function testOnWindow(aIsPrivate, aCallback) { 1.35 + var win = mainWindow.OpenBrowserWindow({private: aIsPrivate}); 1.36 + win.addEventListener("load", function onLoad() { 1.37 + win.removeEventListener("load", onLoad, false); 1.38 + win.addEventListener("DOMContentLoaded", function onInnerLoad() { 1.39 + if (win.content.location.href == "about:privatebrowsing") { 1.40 + win.gBrowser.loadURI(contentPage); 1.41 + return; 1.42 + } 1.43 + win.removeEventListener("DOMContentLoaded", onInnerLoad, true); 1.44 + SimpleTest.executeSoon(function() { aCallback(win); }); 1.45 + }, true); 1.46 + win.gBrowser.loadURI(contentPage); 1.47 + }, true); 1.48 +} 1.49 + 1.50 +function doTest() { 1.51 + testOnWindow(false, function(aWin) { 1.52 + aWin.content.localStorage.setItem("persistent", "persistent1"); 1.53 + 1.54 + testOnWindow(true, function(privateWin) { 1.55 + is(privateWin.content.localStorage.getItem("persistent"), null, "previous values are inaccessible"); 1.56 + 1.57 + // Initially check the privateWin.content.localStorage is empty 1.58 + is(privateWin.content.localStorage.length, 0, "The storage is empty [1]"); 1.59 + is(privateWin.content.localStorage.key(0), null, "key() should return null for out-of-bounds access"); 1.60 + is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.61 + is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.62 + is(privateWin.content.localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())"); 1.63 + is(privateWin.content.localStorage["nonexisting"], undefined, "Nonexisting item is null (array access)"); 1.64 + is(privateWin.content.localStorage.nonexisting, undefined, "Nonexisting item is null (property access)"); 1.65 + privateWin.content.localStorage.removeItem("nonexisting"); // Just check there is no exception 1.66 + 1.67 + is(typeof privateWin.content.localStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object"); 1.68 + is(typeof privateWin.content.localStorage["nonexisting"], "undefined", "['nonexisting'] is undefined"); 1.69 + is(typeof privateWin.content.localStorage.nonexisting, "undefined", "nonexisting is undefined"); 1.70 + is(typeof privateWin.content.localStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object"); 1.71 + is(typeof privateWin.content.localStorage["nonexisting2"], "undefined", "['nonexisting2'] is undefined"); 1.72 + is(typeof privateWin.content.localStorage.nonexisting2, "undefined", "nonexisting2 is undefined"); 1.73 + 1.74 + // add an empty-value key 1.75 + privateWin.content.localStorage.setItem("empty", ""); 1.76 + is(privateWin.content.localStorage.getItem("empty"), "", "Empty value (getItem())"); 1.77 + is(privateWin.content.localStorage["empty"], "", "Empty value (array access)"); 1.78 + is(privateWin.content.localStorage.empty, "", "Empty value (property access)"); 1.79 + is(typeof privateWin.content.localStorage.getItem("empty"), "string", "getItem('empty') is string"); 1.80 + is(typeof privateWin.content.localStorage["empty"], "string", "['empty'] is string"); 1.81 + is(typeof privateWin.content.localStorage.empty, "string", "empty is string"); 1.82 + privateWin.content.localStorage.removeItem("empty"); 1.83 + is(privateWin.content.localStorage.length, 0, "The storage has no keys"); 1.84 + is(privateWin.content.localStorage.getItem("empty"), null, "empty item is null (getItem())"); 1.85 + is(privateWin.content.localStorage["empty"], null, "empty item is undefined (array access)"); 1.86 + is(privateWin.content.localStorage.empty, null, "empty item is undefined (property access)"); 1.87 + is(typeof privateWin.content.localStorage.getItem("empty"), "object", "getItem('empty') is object"); 1.88 + is(typeof privateWin.content.localStorage["empty"], "undefined", "['empty'] is undefined"); 1.89 + is(typeof privateWin.content.localStorage.empty, "undefined", "empty is undefined"); 1.90 + 1.91 + // add one key, check it is there 1.92 + privateWin.content.localStorage.setItem("key1", "value1"); 1.93 + is(privateWin.content.localStorage.length, 1, "The storage has one key-value pair"); 1.94 + is(privateWin.content.localStorage.key(0), "key1"); 1.95 + is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.96 + is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.97 + 1.98 + // check all access method give the correct result 1.99 + // and are of the correct type 1.100 + is(privateWin.content.localStorage.getItem("key1"), "value1", "getItem('key1') == value1"); 1.101 + is(privateWin.content.localStorage["key1"], "value1", "['key1'] == value1"); 1.102 + is(privateWin.content.localStorage.key1, "value1", "key1 == value1"); 1.103 + 1.104 + is(typeof privateWin.content.localStorage.getItem("key1"), "string", "getItem('key1') is string"); 1.105 + is(typeof privateWin.content.localStorage["key1"], "string", "['key1'] is string"); 1.106 + is(typeof privateWin.content.localStorage.key1, "string", "key1 is string"); 1.107 + 1.108 + // remove the previously added key and check the storage is empty 1.109 + privateWin.content.localStorage.removeItem("key1"); 1.110 + is(privateWin.content.localStorage.length, 0, "The storage is empty [2]"); 1.111 + is(privateWin.content.localStorage.key(0), null, "key() should return null for out-of-bounds access"); 1.112 + is(privateWin.content.localStorage.getItem("key1"), null, "\'key1\' removed"); 1.113 + 1.114 + is(typeof privateWin.content.localStorage.getItem("key1"), "object", "getItem('key1') is object"); 1.115 + is(typeof privateWin.content.localStorage["key1"], "undefined", "['key1'] is undefined"); 1.116 + is(typeof privateWin.content.localStorage.key1, "undefined", "key1 is undefined"); 1.117 + 1.118 + // add one key, check it is there 1.119 + privateWin.content.localStorage.setItem("key1", "value1"); 1.120 + is(privateWin.content.localStorage.length, 1, "The storage has one key-value pair"); 1.121 + is(privateWin.content.localStorage.key(0), "key1"); 1.122 + is(privateWin.content.localStorage.getItem("key1"), "value1"); 1.123 + 1.124 + // add a second key 1.125 + privateWin.content.localStorage.setItem("key2", "value2"); 1.126 + is(privateWin.content.localStorage.length, 2, "The storage has two key-value pairs"); 1.127 + is(privateWin.content.localStorage.getItem("key1"), "value1"); 1.128 + is(privateWin.content.localStorage.getItem("key2"), "value2"); 1.129 + var firstKey = privateWin.content.localStorage.key(0); 1.130 + var secondKey = privateWin.content.localStorage.key(1); 1.131 + ok((firstKey == 'key1' && secondKey == 'key2') || 1.132 + (firstKey == 'key2' && secondKey == 'key1'), 1.133 + 'key() API works.'); 1.134 + 1.135 + // change the second key 1.136 + privateWin.content.localStorage.setItem("key2", "value2-2"); 1.137 + is(privateWin.content.localStorage.length, 2, "The storage has two key-value pairs"); 1.138 + is(privateWin.content.localStorage.key(0), firstKey); // After key value changes the order must be preserved 1.139 + is(privateWin.content.localStorage.key(1), secondKey); 1.140 + is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.141 + is(privateWin.content.localStorage.key(2), null, "key() should return null for out-of-bounds access"); 1.142 + is(privateWin.content.localStorage.getItem("key1"), "value1"); 1.143 + is(privateWin.content.localStorage.getItem("key2"), "value2-2"); 1.144 + 1.145 + // change the first key 1.146 + privateWin.content.localStorage.setItem("key1", "value1-2"); 1.147 + is(privateWin.content.localStorage.length, 2, "The storage has two key-value pairs"); 1.148 + is(privateWin.content.localStorage.key(0), firstKey); // After key value changes the order must be preserved 1.149 + is(privateWin.content.localStorage.key(1), secondKey); 1.150 + is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.151 + is(privateWin.content.localStorage.key(2), null, "key() should return null for out-of-bounds access"); 1.152 + is(privateWin.content.localStorage.getItem("key1"), "value1-2"); 1.153 + is(privateWin.content.localStorage.getItem("key2"), "value2-2"); 1.154 + 1.155 + // remove the second key 1.156 + privateWin.content.localStorage.removeItem("key2"); 1.157 + is(privateWin.content.localStorage.length, 1, "The storage has one key-value pair"); 1.158 + is(privateWin.content.localStorage.key(0), "key1"); 1.159 + is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.160 + is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.161 + is(privateWin.content.localStorage.getItem("key1"), "value1-2"); 1.162 + 1.163 + // JS property test 1.164 + privateWin.content.localStorage.testA = "valueA"; 1.165 + is(privateWin.content.localStorage.testA, "valueA"); 1.166 + is(privateWin.content.localStorage["testA"], "valueA"); 1.167 + is(privateWin.content.localStorage.getItem("testA"), "valueA"); 1.168 + 1.169 + privateWin.content.localStorage.testA = "valueA2"; 1.170 + is(privateWin.content.localStorage.testA, "valueA2"); 1.171 + is(privateWin.content.localStorage["testA"], "valueA2"); 1.172 + is(privateWin.content.localStorage.getItem("testA"), "valueA2"); 1.173 + 1.174 + privateWin.content.localStorage["testB"] = "valueB"; 1.175 + is(privateWin.content.localStorage.testB, "valueB"); 1.176 + is(privateWin.content.localStorage["testB"], "valueB"); 1.177 + is(privateWin.content.localStorage.getItem("testB"), "valueB"); 1.178 + 1.179 + privateWin.content.localStorage["testB"] = "valueB2"; 1.180 + is(privateWin.content.localStorage.testB, "valueB2"); 1.181 + is(privateWin.content.localStorage["testB"], "valueB2"); 1.182 + is(privateWin.content.localStorage.getItem("testB"), "valueB2"); 1.183 + 1.184 + privateWin.content.localStorage.setItem("testC", "valueC"); 1.185 + is(privateWin.content.localStorage.testC, "valueC"); 1.186 + is(privateWin.content.localStorage["testC"], "valueC"); 1.187 + is(privateWin.content.localStorage.getItem("testC"), "valueC"); 1.188 + 1.189 + privateWin.content.localStorage.setItem("testC", "valueC2"); 1.190 + is(privateWin.content.localStorage.testC, "valueC2"); 1.191 + is(privateWin.content.localStorage["testC"], "valueC2"); 1.192 + is(privateWin.content.localStorage.getItem("testC"), "valueC2"); 1.193 + 1.194 + privateWin.content.localStorage.setItem("testC", null); 1.195 + is("testC" in privateWin.content.localStorage, true); 1.196 + is(privateWin.content.localStorage.getItem("testC"), "null"); 1.197 + is(privateWin.content.localStorage["testC"], "null"); 1.198 + is(privateWin.content.localStorage.testC, "null"); 1.199 + 1.200 + privateWin.content.localStorage.removeItem("testC"); 1.201 + privateWin.content.localStorage["testC"] = null; 1.202 + is("testC" in privateWin.content.localStorage, true); 1.203 + is(privateWin.content.localStorage.getItem("testC"), "null"); 1.204 + is(privateWin.content.localStorage["testC"], "null"); 1.205 + is(privateWin.content.localStorage.testC, "null"); 1.206 + 1.207 + privateWin.content.localStorage.setItem(null, "test"); 1.208 + is("null" in privateWin.content.localStorage, true); 1.209 + is(privateWin.content.localStorage.getItem("null"), "test"); 1.210 + is(privateWin.content.localStorage.getItem(null), "test"); 1.211 + is(privateWin.content.localStorage["null"], "test"); 1.212 + privateWin.content.localStorage.removeItem(null, "test"); 1.213 + // bug 350023 1.214 + todo_is("null" in privateWin.content.localStorage, false); 1.215 + 1.216 + privateWin.content.localStorage.setItem(null, "test"); 1.217 + is("null" in privateWin.content.localStorage, true); 1.218 + privateWin.content.localStorage.removeItem("null", "test"); 1.219 + // bug 350023 1.220 + todo_is("null" in privateWin.content.localStorage, false); 1.221 + 1.222 + // Clear the storage 1.223 + privateWin.content.localStorage.clear(); 1.224 + is(privateWin.content.localStorage.length, 0, "The storage is empty [3]"); 1.225 + is(privateWin.content.localStorage.key(0), null, "key() should return null for out-of-bounds access"); 1.226 + is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.227 + is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.228 + is(privateWin.content.localStorage.getItem("nonexisting"), null, "Nonexisting item is null"); 1.229 + is(privateWin.content.localStorage.getItem("key1"), null, "key1 removed"); 1.230 + is(privateWin.content.localStorage.getItem("key2"), null, "key2 removed"); 1.231 + privateWin.content.localStorage.removeItem("nonexisting"); // Just check there is no exception 1.232 + privateWin.content.localStorage.removeItem("key1"); // Just check there is no exception 1.233 + privateWin.content.localStorage.removeItem("key2"); // Just check there is no exception 1.234 + 1.235 + privateWin.content.localStorage.setItem("must disappear", "private browsing value"); 1.236 + 1.237 + privateWin.close(); 1.238 + 1.239 + // The .close() call above will operate asynchronously, so execute the 1.240 + // code below asynchronously as well. 1.241 + function callback(newPrivateWin) { 1.242 + is(newPrivateWin.content.localStorage.getItem("must disappear"), null, "private browsing values threw away"); 1.243 + is(newPrivateWin.content.localStorage.length, 0, "No items"); 1.244 + 1.245 + newPrivateWin.close(); 1.246 + is(aWin.content.localStorage.getItem("persistent"), "persistent1", "back in normal mode"); 1.247 + aWin.content.localStorage.clear(); 1.248 + aWin.close(); 1.249 + 1.250 + prefBranch.clearUserPref("browser.startup.page") 1.251 + prefBranch.clearUserPref("browser.startup.homepage_override.mstone"); 1.252 + SimpleTest.finish(); 1.253 + }; 1.254 + SimpleTest.executeSoon(function() testOnWindow(true, callback)); 1.255 + }); 1.256 + }); 1.257 +} 1.258 + 1.259 +SimpleTest.waitForExplicitFinish(); 1.260 + 1.261 +</script> 1.262 + 1.263 +</head> 1.264 + 1.265 +<body onload="startTest();"> 1.266 + 1.267 +</body> 1.268 +</html>