test_localStorageBaseSessionOnly.html (9158B)
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="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 8 <script type="text/javascript"> 9 10 function startTest() 11 { 12 if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) { 13 ok(true, "Test ignored when the next gen local storage is enabled."); 14 SimpleTest.finish(); 15 return; 16 } 17 18 SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION, 'context': document}], test1); 19 } 20 21 function test1() { 22 // Initially check the localStorage is empty 23 is(localStorage.length, 0, "The storage is empty [1]"); 24 is(localStorage.key(0), null, "key() should return null for out-of-bounds access"); 25 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 26 is(localStorage.key(1), null, "key() should return null for out-of-bounds access"); 27 is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())"); 28 is(localStorage.nonexisting, undefined, "Nonexisting item is undefined (array access)"); 29 is(localStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)"); 30 localStorage.removeItem("nonexisting"); // Just check there is no exception 31 32 is(typeof localStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object"); 33 is(typeof localStorage.nonexisting, "undefined", "['nonexisting'] is undefined"); 34 is(typeof localStorage.nonexisting, "undefined", "nonexisting is undefined"); 35 is(typeof localStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object"); 36 is(typeof localStorage.nonexisting2, "undefined", "['nonexisting2'] is undefined"); 37 is(typeof localStorage.nonexisting2, "undefined", "nonexisting2 is undefined"); 38 39 // add an empty-value key 40 localStorage.setItem("empty", ""); 41 is(localStorage.getItem("empty"), "", "Empty value (getItem())"); 42 is(localStorage.empty, "", "Empty value (array access)"); 43 is(localStorage.empty, "", "Empty value (property access)"); 44 is(typeof localStorage.getItem("empty"), "string", "getItem('empty') is string"); 45 is(typeof localStorage.empty, "string", "['empty'] is string"); 46 is(typeof localStorage.empty, "string", "empty is string"); 47 localStorage.removeItem("empty"); 48 is(localStorage.length, 0, "The storage has no keys"); 49 is(localStorage.getItem("empty"), null, "empty item is null (getItem())"); 50 is(localStorage.empty, undefined, "empty item is undefined (array access)"); 51 is(localStorage.empty, undefined, "empty item is undefined (property access)"); 52 is(typeof localStorage.getItem("empty"), "object", "getItem('empty') is object"); 53 is(typeof localStorage.empty, "undefined", "['empty'] is undefined"); 54 is(typeof localStorage.empty, "undefined", "empty is undefined"); 55 56 // add one key, check it is there 57 localStorage.setItem("key1", "value1"); 58 is(localStorage.length, 1, "The storage has one key-value pair"); 59 is(localStorage.key(0), "key1"); 60 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 61 is(localStorage.key(1), null, "key() should return null for out-of-bounds access"); 62 63 // check all access method give the correct result 64 // and are of the correct type 65 is(localStorage.getItem("key1"), "value1", "getItem('key1') == value1"); 66 is(localStorage.key1, "value1", "['key1'] == value1"); 67 is(localStorage.key1, "value1", "key1 == value1"); 68 69 is(typeof localStorage.getItem("key1"), "string", "getItem('key1') is string"); 70 is(typeof localStorage.key1, "string", "['key1'] is string"); 71 is(typeof localStorage.key1, "string", "key1 is string"); 72 73 // remove the previously added key and check the storage is empty 74 localStorage.removeItem("key1"); 75 is(localStorage.length, 0, "The storage is empty [2]"); 76 is(localStorage.key(0), null, "key() should return null for out-of-bounds access"); 77 is(localStorage.getItem("key1"), null, "\'key1\' removed"); 78 79 is(typeof localStorage.getItem("key1"), "object", "getItem('key1') is object"); 80 is(typeof localStorage.key1, "undefined", "['key1'] is undefined"); 81 is(typeof localStorage.key1, "undefined", "key1 is undefined"); 82 83 // add one key, check it is there 84 localStorage.setItem("key1", "value1"); 85 is(localStorage.length, 1, "The storage has one key-value pair"); 86 is(localStorage.key(0), "key1"); 87 is(localStorage.getItem("key1"), "value1"); 88 89 // add a second key 90 localStorage.setItem("key2", "value2"); 91 is(localStorage.length, 2, "The storage has two key-value pairs"); 92 is(localStorage.getItem("key1"), "value1"); 93 is(localStorage.getItem("key2"), "value2"); 94 var firstKey = localStorage.key(0); 95 var secondKey = localStorage.key(1); 96 ok((firstKey == 'key1' && secondKey == 'key2') || 97 (firstKey == 'key2' && secondKey == 'key1'), 98 'key() API works.'); 99 100 // change the second key 101 localStorage.setItem("key2", "value2-2"); 102 is(localStorage.length, 2, "The storage has two key-value pairs"); 103 is(localStorage.key(0), firstKey); // After key value changes the order must be preserved 104 is(localStorage.key(1), secondKey); 105 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 106 is(localStorage.key(2), null, "key() should return null for out-of-bounds access"); 107 is(localStorage.getItem("key1"), "value1"); 108 is(localStorage.getItem("key2"), "value2-2"); 109 110 // change the first key 111 localStorage.setItem("key1", "value1-2"); 112 is(localStorage.length, 2, "The storage has two key-value pairs"); 113 is(localStorage.key(0), firstKey); // After key value changes the order must be preserved 114 is(localStorage.key(1), secondKey); 115 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 116 is(localStorage.key(2), null, "key() should return null for out-of-bounds access"); 117 is(localStorage.getItem("key1"), "value1-2"); 118 is(localStorage.getItem("key2"), "value2-2"); 119 120 // remove the second key 121 localStorage.removeItem("key2"); 122 is(localStorage.length, 1, "The storage has one key-value pair"); 123 is(localStorage.key(0), "key1"); 124 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 125 is(localStorage.key(1), null, "key() should return null for out-of-bounds access"); 126 is(localStorage.getItem("key1"), "value1-2"); 127 128 // JS property test 129 localStorage.testA = "valueA"; 130 is(localStorage.testA, "valueA"); 131 is(localStorage.testA, "valueA"); 132 is(localStorage.getItem("testA"), "valueA"); 133 134 localStorage.testA = "valueA2"; 135 is(localStorage.testA, "valueA2"); 136 is(localStorage.testA, "valueA2"); 137 is(localStorage.getItem("testA"), "valueA2"); 138 139 localStorage.testB = "valueB"; 140 is(localStorage.testB, "valueB"); 141 is(localStorage.testB, "valueB"); 142 is(localStorage.getItem("testB"), "valueB"); 143 144 localStorage.testB = "valueB2"; 145 is(localStorage.testB, "valueB2"); 146 is(localStorage.testB, "valueB2"); 147 is(localStorage.getItem("testB"), "valueB2"); 148 149 localStorage.setItem("testC", "valueC"); 150 is(localStorage.testC, "valueC"); 151 is(localStorage.testC, "valueC"); 152 is(localStorage.getItem("testC"), "valueC"); 153 154 localStorage.setItem("testC", "valueC2"); 155 is(localStorage.testC, "valueC2"); 156 is(localStorage.testC, "valueC2"); 157 is(localStorage.getItem("testC"), "valueC2"); 158 159 localStorage.setItem("testC", null); 160 is("testC" in localStorage, true); 161 is(localStorage.getItem("testC"), "null"); 162 is(localStorage.testC, "null"); 163 is(localStorage.testC, "null"); 164 165 localStorage.removeItem("testC"); 166 localStorage.testC = null; 167 is("testC" in localStorage, true); 168 is(localStorage.getItem("testC"), "null"); 169 is(localStorage.testC, "null"); 170 is(localStorage.testC, "null"); 171 172 localStorage.setItem(null, "test"); 173 is("null" in localStorage, true); 174 is(localStorage.getItem("null"), "test"); 175 is(localStorage.getItem(null), "test"); 176 is(localStorage.null, "test"); 177 localStorage.removeItem(null, "test"); 178 is("null" in localStorage, false); 179 180 localStorage.setItem(null, "test"); 181 is("null" in localStorage, true); 182 localStorage.removeItem("null", "test"); 183 is("null" in localStorage, false); 184 185 // Clear the storage 186 localStorage.clear(); 187 is(localStorage.length, 0, "The storage is empty [3]"); 188 is(localStorage.key(0), null, "key() should return null for out-of-bounds access"); 189 is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 190 is(localStorage.key(1), null, "key() should return null for out-of-bounds access"); 191 is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null"); 192 is(localStorage.getItem("key1"), null, "key1 removed"); 193 is(localStorage.getItem("key2"), null, "key2 removed"); 194 localStorage.removeItem("nonexisting"); // Just check there is no exception 195 localStorage.removeItem("key1"); // Just check there is no exception 196 localStorage.removeItem("key2"); // Just check there is no exception 197 198 localStorage.clear(); 199 SimpleTest.finish(); 200 } 201 202 SimpleTest.waitForExplicitFinish(); 203 204 </script> 205 206 </head> 207 208 <body onload="startTest();"> 209 210 </body> 211 </html>