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