dom/tests/mochitest/localstorage/test_localStorageBase.html

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 <html xmlns="http://www.w3.org/1999/xhtml">
michael@0 2 <head>
michael@0 3 <title>localStorage basic test</title>
michael@0 4
michael@0 5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 7
michael@0 8 <script type="text/javascript">
michael@0 9
michael@0 10 var expectedEvents = [
michael@0 11 "empty,null,",
michael@0 12 "empty,,null",
michael@0 13 "key1,null,value1",
michael@0 14 "key1,value1,null",
michael@0 15 "key1,null,value1",
michael@0 16 "key2,null,value2",
michael@0 17 "key2,value2,value2-2",
michael@0 18 "key1,value1,value1-2",
michael@0 19 "key2,value2-2,null",
michael@0 20 "testA,null,valueA",
michael@0 21 "testA,valueA,valueA2",
michael@0 22 "testB,null,valueB",
michael@0 23 "testB,valueB,valueB2",
michael@0 24 "testC,null,valueC",
michael@0 25 "testC,valueC,valueC2",
michael@0 26 "testC,valueC2,null",
michael@0 27 "testC,null,null",
michael@0 28 "testC,null,null",
michael@0 29 "null,null,test",
michael@0 30 "null,test,null",
michael@0 31 "null,null,test",
michael@0 32 "null,test,null",
michael@0 33 "null,null,null"
michael@0 34 ];
michael@0 35
michael@0 36 function startTest()
michael@0 37 {
michael@0 38 // Initially check the localStorage is empty
michael@0 39 is(localStorage.length, 0, "The storage is empty [1]");
michael@0 40 is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
michael@0 41 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
michael@0 42 is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
michael@0 43 is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
michael@0 44 is(localStorage["nonexisting"], undefined, "Nonexisting item is undefined (array access)");
michael@0 45 is(localStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)");
michael@0 46 localStorage.removeItem("nonexisting"); // Just check there is no exception
michael@0 47
michael@0 48 is(typeof localStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object");
michael@0 49 is(typeof localStorage["nonexisting"], "undefined", "['nonexisting'] is undefined");
michael@0 50 is(typeof localStorage.nonexisting, "undefined", "nonexisting is undefined");
michael@0 51 is(typeof localStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object");
michael@0 52 is(typeof localStorage["nonexisting2"], "undefined", "['nonexisting2'] is undefined");
michael@0 53 is(typeof localStorage.nonexisting2, "undefined", "nonexisting2 is undefined");
michael@0 54
michael@0 55 var localStorageCopy = localStorage;
michael@0 56
michael@0 57 function onStorageChanged(e) {
michael@0 58 if (e.storageArea == localStorageCopy) {
michael@0 59 ok(expectedEvents.length > 0, "Not more then expected events encountered");
michael@0 60 var receivedEvent = e.key + "," + e.oldValue + "," + e.newValue;
michael@0 61 is(receivedEvent, expectedEvents.shift(), "Expected event data: " + receivedEvent);
michael@0 62 }
michael@0 63 }
michael@0 64
michael@0 65 // Listen for MozStorageChanged
michael@0 66 SpecialPowers.addChromeEventListener("MozStorageChanged", onStorageChanged, false);
michael@0 67
michael@0 68 // add an empty-value key
michael@0 69 localStorage.setItem("empty", "");
michael@0 70 is(localStorage.getItem("empty"), "", "Empty value (getItem())");
michael@0 71 is(localStorage["empty"], "", "Empty value (array access)");
michael@0 72 is(localStorage.empty, "", "Empty value (property access)");
michael@0 73 is(typeof localStorage.getItem("empty"), "string", "getItem('empty') is string");
michael@0 74 is(typeof localStorage["empty"], "string", "['empty'] is string");
michael@0 75 is(typeof localStorage.empty, "string", "empty is string");
michael@0 76 localStorage.removeItem("empty");
michael@0 77 is(localStorage.length, 0, "The storage has no keys");
michael@0 78 is(localStorage.getItem("empty"), null, "empty item is null (getItem())");
michael@0 79 is(localStorage["empty"], undefined, "empty item is undefined (array access)");
michael@0 80 is(localStorage.empty, undefined, "empty item is undefined (property access)");
michael@0 81 is(typeof localStorage.getItem("empty"), "object", "getItem('empty') is object");
michael@0 82 is(typeof localStorage["empty"], "undefined", "['empty'] is undefined");
michael@0 83 is(typeof localStorage.empty, "undefined", "empty is undefined");
michael@0 84
michael@0 85 // add one key, check it is there
michael@0 86 localStorage.setItem("key1", "value1");
michael@0 87 is(localStorage.length, 1, "The storage has one key-value pair");
michael@0 88 is(localStorage.key(0), "key1");
michael@0 89 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
michael@0 90 is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
michael@0 91
michael@0 92 // check all access method give the correct result
michael@0 93 // and are of the correct type
michael@0 94 is(localStorage.getItem("key1"), "value1", "getItem('key1') == value1");
michael@0 95 is(localStorage["key1"], "value1", "['key1'] == value1");
michael@0 96 is(localStorage.key1, "value1", "key1 == value1");
michael@0 97
michael@0 98 is(typeof localStorage.getItem("key1"), "string", "getItem('key1') is string");
michael@0 99 is(typeof localStorage["key1"], "string", "['key1'] is string");
michael@0 100 is(typeof localStorage.key1, "string", "key1 is string");
michael@0 101
michael@0 102 // remove the previously added key and check the storage is empty
michael@0 103 localStorage.removeItem("key1");
michael@0 104 is(localStorage.length, 0, "The storage is empty [2]");
michael@0 105 is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
michael@0 106 is(localStorage.getItem("key1"), null, "\'key1\' removed");
michael@0 107
michael@0 108 is(typeof localStorage.getItem("key1"), "object", "getItem('key1') is object");
michael@0 109 is(typeof localStorage["key1"], "undefined", "['key1'] is object");
michael@0 110 is(typeof localStorage.key1, "undefined", "key1 is object");
michael@0 111
michael@0 112 // add one key, check it is there
michael@0 113 localStorage.setItem("key1", "value1");
michael@0 114 is(localStorage.length, 1, "The storage has one key-value pair");
michael@0 115 is(localStorage.key(0), "key1");
michael@0 116 is(localStorage.getItem("key1"), "value1");
michael@0 117
michael@0 118 // add a second key
michael@0 119 localStorage.setItem("key2", "value2");
michael@0 120 is(localStorage.length, 2, "The storage has two key-value pairs");
michael@0 121 is(localStorage.getItem("key1"), "value1");
michael@0 122 is(localStorage.getItem("key2"), "value2");
michael@0 123 var firstKey = localStorage.key(0);
michael@0 124 var secondKey = localStorage.key(1);
michael@0 125 ok((firstKey == 'key1' && secondKey == 'key2') ||
michael@0 126 (firstKey == 'key2' && secondKey == 'key1'),
michael@0 127 'key() API works.');
michael@0 128
michael@0 129 // change the second key
michael@0 130 localStorage.setItem("key2", "value2-2");
michael@0 131 is(localStorage.length, 2, "The storage has two key-value pairs");
michael@0 132 is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
michael@0 133 is(localStorage.key(1), secondKey);
michael@0 134 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
michael@0 135 is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
michael@0 136 is(localStorage.getItem("key1"), "value1");
michael@0 137 is(localStorage.getItem("key2"), "value2-2");
michael@0 138
michael@0 139 // change the first key
michael@0 140 localStorage.setItem("key1", "value1-2");
michael@0 141 is(localStorage.length, 2, "The storage has two key-value pairs");
michael@0 142 is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
michael@0 143 is(localStorage.key(1), secondKey);
michael@0 144 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
michael@0 145 is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
michael@0 146 is(localStorage.getItem("key1"), "value1-2");
michael@0 147 is(localStorage.getItem("key2"), "value2-2");
michael@0 148
michael@0 149 // remove the second key
michael@0 150 localStorage.removeItem("key2");
michael@0 151 is(localStorage.length, 1, "The storage has one key-value pair");
michael@0 152 is(localStorage.key(0), "key1");
michael@0 153 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
michael@0 154 is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
michael@0 155 is(localStorage.getItem("key1"), "value1-2");
michael@0 156
michael@0 157 // JS property test
michael@0 158 localStorage.testA = "valueA";
michael@0 159 is(localStorage.testA, "valueA");
michael@0 160 is(localStorage["testA"], "valueA");
michael@0 161 is(localStorage.getItem("testA"), "valueA");
michael@0 162
michael@0 163 localStorage.testA = "valueA2";
michael@0 164 is(localStorage.testA, "valueA2");
michael@0 165 is(localStorage["testA"], "valueA2");
michael@0 166 is(localStorage.getItem("testA"), "valueA2");
michael@0 167
michael@0 168 localStorage["testB"] = "valueB";
michael@0 169 is(localStorage.testB, "valueB");
michael@0 170 is(localStorage["testB"], "valueB");
michael@0 171 is(localStorage.getItem("testB"), "valueB");
michael@0 172
michael@0 173 localStorage["testB"] = "valueB2";
michael@0 174 is(localStorage.testB, "valueB2");
michael@0 175 is(localStorage["testB"], "valueB2");
michael@0 176 is(localStorage.getItem("testB"), "valueB2");
michael@0 177
michael@0 178 localStorage.setItem("testC", "valueC");
michael@0 179 is(localStorage.testC, "valueC");
michael@0 180 is(localStorage["testC"], "valueC");
michael@0 181 is(localStorage.getItem("testC"), "valueC");
michael@0 182
michael@0 183 localStorage.setItem("testC", "valueC2");
michael@0 184 is(localStorage.testC, "valueC2");
michael@0 185 is(localStorage["testC"], "valueC2");
michael@0 186 is(localStorage.getItem("testC"), "valueC2");
michael@0 187
michael@0 188 localStorage.setItem("testC", null);
michael@0 189 is("testC" in localStorage, true);
michael@0 190 is(localStorage.getItem("testC"), "null");
michael@0 191 is(localStorage["testC"], "null");
michael@0 192 is(localStorage.testC, "null");
michael@0 193
michael@0 194 localStorage.removeItem("testC");
michael@0 195 localStorage["testC"] = null;
michael@0 196 is("testC" in localStorage, true);
michael@0 197 is(localStorage.getItem("testC"), "null");
michael@0 198 is(localStorage["testC"], "null");
michael@0 199 is(localStorage.testC, "null");
michael@0 200
michael@0 201 localStorage.setItem(null, "test");
michael@0 202 is("null" in localStorage, true);
michael@0 203 is(localStorage.getItem("null"), "test");
michael@0 204 is(localStorage.getItem(null), "test");
michael@0 205 is(localStorage["null"], "test");
michael@0 206 localStorage.removeItem(null, "test");
michael@0 207 // bug 350023
michael@0 208 todo_is("null" in localStorage, false);
michael@0 209
michael@0 210 localStorage.setItem(null, "test");
michael@0 211 is("null" in localStorage, true);
michael@0 212 localStorage.removeItem("null", "test");
michael@0 213 // bug 350023
michael@0 214 todo_is("null" in localStorage, false);
michael@0 215
michael@0 216 // Clear the storage
michael@0 217 localStorage.clear();
michael@0 218 is("testB" in localStorage, false, "Keys are not in the JS scope of the storage");
michael@0 219 is("testC" in localStorage, false, "Keys are not in the JS scope of the storage");
michael@0 220 is(localStorage.length, 0, "The storage is empty [3]");
michael@0 221 is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
michael@0 222 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
michael@0 223 is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
michael@0 224 is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null");
michael@0 225 is(localStorage.getItem("key1"), null, "key1 removed");
michael@0 226 is(localStorage.getItem("key2"), null, "key2 removed");
michael@0 227 localStorage.removeItem("nonexisting"); // Just check there is no exception
michael@0 228 localStorage.removeItem("key1"); // Just check there is no exception
michael@0 229 localStorage.removeItem("key2"); // Just check there is no exception
michael@0 230
michael@0 231 SimpleTest.executeSoon(function () {
michael@0 232 SpecialPowers.removeChromeEventListener("MozStorageChanged", onStorageChanged, false);
michael@0 233 is(expectedEvents.length, 0, "received the correct number of events");
michael@0 234
michael@0 235 localStorage.clear();
michael@0 236 SimpleTest.finish();
michael@0 237 });
michael@0 238 }
michael@0 239
michael@0 240 SimpleTest.waitForExplicitFinish();
michael@0 241
michael@0 242 </script>
michael@0 243
michael@0 244 </head>
michael@0 245
michael@0 246 <body onload="startTest();">
michael@0 247
michael@0 248 </body>
michael@0 249 </html>

mercurial