1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/localstorage/test_localStorageBase.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,249 @@ 1.4 +<html xmlns="http://www.w3.org/1999/xhtml"> 1.5 +<head> 1.6 +<title>localStorage basic test</title> 1.7 + 1.8 +<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.9 +<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.10 + 1.11 +<script type="text/javascript"> 1.12 + 1.13 +var expectedEvents = [ 1.14 + "empty,null,", 1.15 + "empty,,null", 1.16 + "key1,null,value1", 1.17 + "key1,value1,null", 1.18 + "key1,null,value1", 1.19 + "key2,null,value2", 1.20 + "key2,value2,value2-2", 1.21 + "key1,value1,value1-2", 1.22 + "key2,value2-2,null", 1.23 + "testA,null,valueA", 1.24 + "testA,valueA,valueA2", 1.25 + "testB,null,valueB", 1.26 + "testB,valueB,valueB2", 1.27 + "testC,null,valueC", 1.28 + "testC,valueC,valueC2", 1.29 + "testC,valueC2,null", 1.30 + "testC,null,null", 1.31 + "testC,null,null", 1.32 + "null,null,test", 1.33 + "null,test,null", 1.34 + "null,null,test", 1.35 + "null,test,null", 1.36 + "null,null,null" 1.37 +]; 1.38 + 1.39 +function startTest() 1.40 +{ 1.41 + // Initially check the localStorage is empty 1.42 + is(localStorage.length, 0, "The storage is empty [1]"); 1.43 + is(localStorage.key(0), null, "key() should return null for out-of-bounds access"); 1.44 + is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.45 + is(localStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.46 + is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())"); 1.47 + is(localStorage["nonexisting"], undefined, "Nonexisting item is undefined (array access)"); 1.48 + is(localStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)"); 1.49 + localStorage.removeItem("nonexisting"); // Just check there is no exception 1.50 + 1.51 + is(typeof localStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object"); 1.52 + is(typeof localStorage["nonexisting"], "undefined", "['nonexisting'] is undefined"); 1.53 + is(typeof localStorage.nonexisting, "undefined", "nonexisting is undefined"); 1.54 + is(typeof localStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object"); 1.55 + is(typeof localStorage["nonexisting2"], "undefined", "['nonexisting2'] is undefined"); 1.56 + is(typeof localStorage.nonexisting2, "undefined", "nonexisting2 is undefined"); 1.57 + 1.58 + var localStorageCopy = localStorage; 1.59 + 1.60 + function onStorageChanged(e) { 1.61 + if (e.storageArea == localStorageCopy) { 1.62 + ok(expectedEvents.length > 0, "Not more then expected events encountered"); 1.63 + var receivedEvent = e.key + "," + e.oldValue + "," + e.newValue; 1.64 + is(receivedEvent, expectedEvents.shift(), "Expected event data: " + receivedEvent); 1.65 + } 1.66 + } 1.67 + 1.68 + // Listen for MozStorageChanged 1.69 + SpecialPowers.addChromeEventListener("MozStorageChanged", onStorageChanged, false); 1.70 + 1.71 + // add an empty-value key 1.72 + localStorage.setItem("empty", ""); 1.73 + is(localStorage.getItem("empty"), "", "Empty value (getItem())"); 1.74 + is(localStorage["empty"], "", "Empty value (array access)"); 1.75 + is(localStorage.empty, "", "Empty value (property access)"); 1.76 + is(typeof localStorage.getItem("empty"), "string", "getItem('empty') is string"); 1.77 + is(typeof localStorage["empty"], "string", "['empty'] is string"); 1.78 + is(typeof localStorage.empty, "string", "empty is string"); 1.79 + localStorage.removeItem("empty"); 1.80 + is(localStorage.length, 0, "The storage has no keys"); 1.81 + is(localStorage.getItem("empty"), null, "empty item is null (getItem())"); 1.82 + is(localStorage["empty"], undefined, "empty item is undefined (array access)"); 1.83 + is(localStorage.empty, undefined, "empty item is undefined (property access)"); 1.84 + is(typeof localStorage.getItem("empty"), "object", "getItem('empty') is object"); 1.85 + is(typeof localStorage["empty"], "undefined", "['empty'] is undefined"); 1.86 + is(typeof localStorage.empty, "undefined", "empty is undefined"); 1.87 + 1.88 + // add one key, check it is there 1.89 + localStorage.setItem("key1", "value1"); 1.90 + is(localStorage.length, 1, "The storage has one key-value pair"); 1.91 + is(localStorage.key(0), "key1"); 1.92 + is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.93 + is(localStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.94 + 1.95 + // check all access method give the correct result 1.96 + // and are of the correct type 1.97 + is(localStorage.getItem("key1"), "value1", "getItem('key1') == value1"); 1.98 + is(localStorage["key1"], "value1", "['key1'] == value1"); 1.99 + is(localStorage.key1, "value1", "key1 == value1"); 1.100 + 1.101 + is(typeof localStorage.getItem("key1"), "string", "getItem('key1') is string"); 1.102 + is(typeof localStorage["key1"], "string", "['key1'] is string"); 1.103 + is(typeof localStorage.key1, "string", "key1 is string"); 1.104 + 1.105 + // remove the previously added key and check the storage is empty 1.106 + localStorage.removeItem("key1"); 1.107 + is(localStorage.length, 0, "The storage is empty [2]"); 1.108 + is(localStorage.key(0), null, "key() should return null for out-of-bounds access"); 1.109 + is(localStorage.getItem("key1"), null, "\'key1\' removed"); 1.110 + 1.111 + is(typeof localStorage.getItem("key1"), "object", "getItem('key1') is object"); 1.112 + is(typeof localStorage["key1"], "undefined", "['key1'] is object"); 1.113 + is(typeof localStorage.key1, "undefined", "key1 is object"); 1.114 + 1.115 + // add one key, check it is there 1.116 + localStorage.setItem("key1", "value1"); 1.117 + is(localStorage.length, 1, "The storage has one key-value pair"); 1.118 + is(localStorage.key(0), "key1"); 1.119 + is(localStorage.getItem("key1"), "value1"); 1.120 + 1.121 + // add a second key 1.122 + localStorage.setItem("key2", "value2"); 1.123 + is(localStorage.length, 2, "The storage has two key-value pairs"); 1.124 + is(localStorage.getItem("key1"), "value1"); 1.125 + is(localStorage.getItem("key2"), "value2"); 1.126 + var firstKey = localStorage.key(0); 1.127 + var secondKey = localStorage.key(1); 1.128 + ok((firstKey == 'key1' && secondKey == 'key2') || 1.129 + (firstKey == 'key2' && secondKey == 'key1'), 1.130 + 'key() API works.'); 1.131 + 1.132 + // change the second key 1.133 + localStorage.setItem("key2", "value2-2"); 1.134 + is(localStorage.length, 2, "The storage has two key-value pairs"); 1.135 + is(localStorage.key(0), firstKey); // After key value changes the order must be preserved 1.136 + is(localStorage.key(1), secondKey); 1.137 + is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.138 + is(localStorage.key(2), null, "key() should return null for out-of-bounds access"); 1.139 + is(localStorage.getItem("key1"), "value1"); 1.140 + is(localStorage.getItem("key2"), "value2-2"); 1.141 + 1.142 + // change the first key 1.143 + localStorage.setItem("key1", "value1-2"); 1.144 + is(localStorage.length, 2, "The storage has two key-value pairs"); 1.145 + is(localStorage.key(0), firstKey); // After key value changes the order must be preserved 1.146 + is(localStorage.key(1), secondKey); 1.147 + is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.148 + is(localStorage.key(2), null, "key() should return null for out-of-bounds access"); 1.149 + is(localStorage.getItem("key1"), "value1-2"); 1.150 + is(localStorage.getItem("key2"), "value2-2"); 1.151 + 1.152 + // remove the second key 1.153 + localStorage.removeItem("key2"); 1.154 + is(localStorage.length, 1, "The storage has one key-value pair"); 1.155 + is(localStorage.key(0), "key1"); 1.156 + is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.157 + is(localStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.158 + is(localStorage.getItem("key1"), "value1-2"); 1.159 + 1.160 + // JS property test 1.161 + localStorage.testA = "valueA"; 1.162 + is(localStorage.testA, "valueA"); 1.163 + is(localStorage["testA"], "valueA"); 1.164 + is(localStorage.getItem("testA"), "valueA"); 1.165 + 1.166 + localStorage.testA = "valueA2"; 1.167 + is(localStorage.testA, "valueA2"); 1.168 + is(localStorage["testA"], "valueA2"); 1.169 + is(localStorage.getItem("testA"), "valueA2"); 1.170 + 1.171 + localStorage["testB"] = "valueB"; 1.172 + is(localStorage.testB, "valueB"); 1.173 + is(localStorage["testB"], "valueB"); 1.174 + is(localStorage.getItem("testB"), "valueB"); 1.175 + 1.176 + localStorage["testB"] = "valueB2"; 1.177 + is(localStorage.testB, "valueB2"); 1.178 + is(localStorage["testB"], "valueB2"); 1.179 + is(localStorage.getItem("testB"), "valueB2"); 1.180 + 1.181 + localStorage.setItem("testC", "valueC"); 1.182 + is(localStorage.testC, "valueC"); 1.183 + is(localStorage["testC"], "valueC"); 1.184 + is(localStorage.getItem("testC"), "valueC"); 1.185 + 1.186 + localStorage.setItem("testC", "valueC2"); 1.187 + is(localStorage.testC, "valueC2"); 1.188 + is(localStorage["testC"], "valueC2"); 1.189 + is(localStorage.getItem("testC"), "valueC2"); 1.190 + 1.191 + localStorage.setItem("testC", null); 1.192 + is("testC" in localStorage, true); 1.193 + is(localStorage.getItem("testC"), "null"); 1.194 + is(localStorage["testC"], "null"); 1.195 + is(localStorage.testC, "null"); 1.196 + 1.197 + localStorage.removeItem("testC"); 1.198 + localStorage["testC"] = null; 1.199 + is("testC" in localStorage, true); 1.200 + is(localStorage.getItem("testC"), "null"); 1.201 + is(localStorage["testC"], "null"); 1.202 + is(localStorage.testC, "null"); 1.203 + 1.204 + localStorage.setItem(null, "test"); 1.205 + is("null" in localStorage, true); 1.206 + is(localStorage.getItem("null"), "test"); 1.207 + is(localStorage.getItem(null), "test"); 1.208 + is(localStorage["null"], "test"); 1.209 + localStorage.removeItem(null, "test"); 1.210 + // bug 350023 1.211 + todo_is("null" in localStorage, false); 1.212 + 1.213 + localStorage.setItem(null, "test"); 1.214 + is("null" in localStorage, true); 1.215 + localStorage.removeItem("null", "test"); 1.216 + // bug 350023 1.217 + todo_is("null" in localStorage, false); 1.218 + 1.219 + // Clear the storage 1.220 + localStorage.clear(); 1.221 + is("testB" in localStorage, false, "Keys are not in the JS scope of the storage"); 1.222 + is("testC" in localStorage, false, "Keys are not in the JS scope of the storage"); 1.223 + is(localStorage.length, 0, "The storage is empty [3]"); 1.224 + is(localStorage.key(0), null, "key() should return null for out-of-bounds access"); 1.225 + is(localStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.226 + is(localStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.227 + is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null"); 1.228 + is(localStorage.getItem("key1"), null, "key1 removed"); 1.229 + is(localStorage.getItem("key2"), null, "key2 removed"); 1.230 + localStorage.removeItem("nonexisting"); // Just check there is no exception 1.231 + localStorage.removeItem("key1"); // Just check there is no exception 1.232 + localStorage.removeItem("key2"); // Just check there is no exception 1.233 + 1.234 + SimpleTest.executeSoon(function () { 1.235 + SpecialPowers.removeChromeEventListener("MozStorageChanged", onStorageChanged, false); 1.236 + is(expectedEvents.length, 0, "received the correct number of events"); 1.237 + 1.238 + localStorage.clear(); 1.239 + SimpleTest.finish(); 1.240 + }); 1.241 +} 1.242 + 1.243 +SimpleTest.waitForExplicitFinish(); 1.244 + 1.245 +</script> 1.246 + 1.247 +</head> 1.248 + 1.249 +<body onload="startTest();"> 1.250 + 1.251 +</body> 1.252 +</html>