1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/sessionstorage/test_sessionStorageBaseSessionOnly.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,233 @@ 1.4 +<html xmlns="http://www.w3.org/1999/xhtml"> 1.5 +<head> 1.6 +<title>sessionStorage basic test, while in sesison only mode</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 testframe; 1.14 +function iframeOnload(aValue) { 1.15 + switch (aValue) { 1.16 + case 1: 1.17 + testframe.onload = test1; 1.18 + break; 1.19 + case 2: 1.20 + testframe.onload = test2; 1.21 + break; 1.22 + default: 1.23 + of(false, 'should not be reached'); 1.24 + SimpleTest.finish(); 1.25 + return; 1.26 + } 1.27 + /* After every permission change, an iframe has to be reloaded, 1.28 + otherwise this test causes failures in b2g (oop) mochitest, because 1.29 + the permission changes don't seem to be always picked up 1.30 + by the code that excercises it */ 1.31 + testframe.contentWindow.location.reload(); 1.32 +} 1.33 + 1.34 +function startTest() { 1.35 + testframe = document.getElementById('testframe'); 1.36 + SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION, 'context': document}], function() { iframeOnload(1); }); 1.37 +} 1.38 + 1.39 +function test1() { 1.40 + 1.41 + // Initially check the sessionStorage is empty 1.42 + is(sessionStorage.length, 0, "The storage is empty [1]"); 1.43 + is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access"); 1.44 + is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.45 + is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.46 + is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())"); 1.47 + is(sessionStorage["nonexisting"], undefined, "Nonexisting item is undefined (array access)"); 1.48 + is(sessionStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)"); 1.49 + sessionStorage.removeItem("nonexisting"); // Just check there is no exception 1.50 + 1.51 + is(typeof sessionStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object"); 1.52 + is(typeof sessionStorage["nonexisting"], "undefined", "['nonexisting'] is undefined"); 1.53 + is(typeof sessionStorage.nonexisting, "undefined", "nonexisting is undefined"); 1.54 + is(typeof sessionStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object"); 1.55 + is(typeof sessionStorage["nonexisting2"], "undefined", "['nonexisting2'] is undefined"); 1.56 + is(typeof sessionStorage.nonexisting2, "undefined", "nonexisting2 is undefined"); 1.57 + 1.58 + // add an empty-value key 1.59 + sessionStorage.setItem("empty", ""); 1.60 + is(sessionStorage.getItem("empty"), "", "Empty value (getItem())"); 1.61 + is(sessionStorage["empty"], "", "Empty value (array access)"); 1.62 + is(sessionStorage.empty, "", "Empty value (property access)"); 1.63 + is(typeof sessionStorage.getItem("empty"), "string", "getItem('empty') is string"); 1.64 + is(typeof sessionStorage["empty"], "string", "['empty'] is string"); 1.65 + is(typeof sessionStorage.empty, "string", "empty is string"); 1.66 + sessionStorage.removeItem("empty"); 1.67 + is(sessionStorage.length, 0, "The storage has no keys"); 1.68 + is(sessionStorage.getItem("empty"), null, "empty item is null (getItem())"); 1.69 + is(sessionStorage["empty"], undefined, "empty item is undefined (array access)"); 1.70 + is(sessionStorage.empty, undefined, "empty item is undefined (property access)"); 1.71 + is(typeof sessionStorage.getItem("empty"), "object", "getItem('empty') is object"); 1.72 + is(typeof sessionStorage["empty"], "undefined", "['empty'] is undefined"); 1.73 + is(typeof sessionStorage.empty, "undefined", "empty is undefined"); 1.74 + 1.75 + // add one key, check it is there 1.76 + sessionStorage.setItem("key1", "value1"); 1.77 + is(sessionStorage.length, 1, "The storage has one key-value pair"); 1.78 + is(sessionStorage.key(0), "key1"); 1.79 + is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.80 + is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.81 + 1.82 + // check all access method give the correct result 1.83 + // and are of the correct type 1.84 + is(sessionStorage.getItem("key1"), "value1", "getItem('key1') == value1"); 1.85 + is(sessionStorage["key1"], "value1", "['key1'] == value1"); 1.86 + is(sessionStorage.key1, "value1", "key1 == value1"); 1.87 + 1.88 + is(typeof sessionStorage.getItem("key1"), "string", "getItem('key1') is string"); 1.89 + is(typeof sessionStorage["key1"], "string", "['key1'] is string"); 1.90 + is(typeof sessionStorage.key1, "string", "key1 is string"); 1.91 + 1.92 + // remove the previously added key and check the storage is empty 1.93 + sessionStorage.removeItem("key1"); 1.94 + is(sessionStorage.length, 0, "The storage is empty [2]"); 1.95 + is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access"); 1.96 + is(sessionStorage.getItem("key1"), null, "\'key1\' removed"); 1.97 + 1.98 + is(typeof sessionStorage.getItem("key1"), "object", "getItem('key1') is object"); 1.99 + is(typeof sessionStorage["key1"], "undefined", "['key1'] is undefined"); 1.100 + is(typeof sessionStorage.key1, "undefined", "key1 is undefined"); 1.101 + 1.102 + // add one key, check it is there 1.103 + sessionStorage.setItem("key1", "value1"); 1.104 + is(sessionStorage.length, 1, "The storage has one key-value pair"); 1.105 + is(sessionStorage.key(0), "key1"); 1.106 + is(sessionStorage.getItem("key1"), "value1"); 1.107 + 1.108 + // add a second key 1.109 + sessionStorage.setItem("key2", "value2"); 1.110 + is(sessionStorage.length, 2, "The storage has two key-value pairs"); 1.111 + is(sessionStorage.getItem("key1"), "value1"); 1.112 + is(sessionStorage.getItem("key2"), "value2"); 1.113 + var firstKey = sessionStorage.key(0); 1.114 + var secondKey = sessionStorage.key(1); 1.115 + ok((firstKey == 'key1' && secondKey == 'key2') || 1.116 + (firstKey == 'key2' && secondKey == 'key1'), 1.117 + 'key() API works.'); 1.118 + 1.119 + // change the second key 1.120 + sessionStorage.setItem("key2", "value2-2"); 1.121 + is(sessionStorage.length, 2, "The storage has two key-value pairs"); 1.122 + is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved 1.123 + is(sessionStorage.key(1), secondKey); 1.124 + is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.125 + is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access"); 1.126 + is(sessionStorage.getItem("key1"), "value1"); 1.127 + is(sessionStorage.getItem("key2"), "value2-2"); 1.128 + 1.129 + // change the first key 1.130 + sessionStorage.setItem("key1", "value1-2"); 1.131 + is(sessionStorage.length, 2, "The storage has two key-value pairs"); 1.132 + is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved 1.133 + is(sessionStorage.key(1), secondKey); 1.134 + is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.135 + is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access"); 1.136 + is(sessionStorage.getItem("key1"), "value1-2"); 1.137 + is(sessionStorage.getItem("key2"), "value2-2"); 1.138 + 1.139 + // remove the second key 1.140 + sessionStorage.removeItem("key2"); 1.141 + is(sessionStorage.length, 1, "The storage has one key-value pair"); 1.142 + is(sessionStorage.key(0), "key1"); 1.143 + is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.144 + is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.145 + is(sessionStorage.getItem("key1"), "value1-2"); 1.146 + 1.147 + // JS property test 1.148 + sessionStorage.testA = "valueA"; 1.149 + is(sessionStorage.testA, "valueA"); 1.150 + is(sessionStorage["testA"], "valueA"); 1.151 + is(sessionStorage.getItem("testA"), "valueA"); 1.152 + 1.153 + sessionStorage.testA = "valueA2"; 1.154 + is(sessionStorage.testA, "valueA2"); 1.155 + is(sessionStorage["testA"], "valueA2"); 1.156 + is(sessionStorage.getItem("testA"), "valueA2"); 1.157 + 1.158 + sessionStorage["testB"] = "valueB"; 1.159 + is(sessionStorage.testB, "valueB"); 1.160 + is(sessionStorage["testB"], "valueB"); 1.161 + is(sessionStorage.getItem("testB"), "valueB"); 1.162 + 1.163 + sessionStorage["testB"] = "valueB2"; 1.164 + is(sessionStorage.testB, "valueB2"); 1.165 + is(sessionStorage["testB"], "valueB2"); 1.166 + is(sessionStorage.getItem("testB"), "valueB2"); 1.167 + 1.168 + sessionStorage.setItem("testC", "valueC"); 1.169 + is(sessionStorage.testC, "valueC"); 1.170 + is(sessionStorage["testC"], "valueC"); 1.171 + is(sessionStorage.getItem("testC"), "valueC"); 1.172 + 1.173 + sessionStorage.setItem("testC", "valueC2"); 1.174 + is(sessionStorage.testC, "valueC2"); 1.175 + is(sessionStorage["testC"], "valueC2"); 1.176 + is(sessionStorage.getItem("testC"), "valueC2"); 1.177 + 1.178 + sessionStorage.setItem("testC", null); 1.179 + is("testC" in sessionStorage, true); 1.180 + is(sessionStorage.getItem("testC"), "null"); 1.181 + is(sessionStorage["testC"], "null"); 1.182 + is(sessionStorage.testC, "null"); 1.183 + 1.184 + sessionStorage.removeItem("testC"); 1.185 + sessionStorage["testC"] = null; 1.186 + is("testC" in sessionStorage, true); 1.187 + is(sessionStorage.getItem("testC"), "null"); 1.188 + is(sessionStorage["testC"], "null"); 1.189 + is(sessionStorage.testC, "null"); 1.190 + 1.191 + sessionStorage.setItem(null, "test"); 1.192 + is("null" in sessionStorage, true); 1.193 + is(sessionStorage.getItem("null"), "test"); 1.194 + is(sessionStorage.getItem(null), "test"); 1.195 + is(sessionStorage["null"], "test"); 1.196 + sessionStorage.removeItem(null, "test"); 1.197 + // bug 350023 1.198 + todo_is("null" in sessionStorage, false); 1.199 + 1.200 + sessionStorage.setItem(null, "test"); 1.201 + is("null" in sessionStorage, true); 1.202 + sessionStorage.removeItem("null", "test"); 1.203 + // bug 350023 1.204 + todo_is("null" in sessionStorage, false); 1.205 + 1.206 + // Clear the storage 1.207 + sessionStorage.clear(); 1.208 + is(sessionStorage.length, 0, "The storage is empty [3]"); 1.209 + is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access"); 1.210 + is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); 1.211 + is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access"); 1.212 + is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null"); 1.213 + is(sessionStorage.getItem("key1"), null, "key1 removed"); 1.214 + is(sessionStorage.getItem("key2"), null, "key2 removed"); 1.215 + sessionStorage.removeItem("nonexisting"); // Just check there is no exception 1.216 + sessionStorage.removeItem("key1"); // Just check there is no exception 1.217 + sessionStorage.removeItem("key2"); // Just check there is no exception 1.218 + 1.219 + SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': SpecialPowers.Ci.nsICookiePermission.ACCESS_DEFAULT, 'context': document}], function() { iframeOnload(2); }); 1.220 +} 1.221 + 1.222 +function test2() { 1.223 + sessionStorage.clear(); 1.224 + SimpleTest.finish(); 1.225 +} 1.226 + 1.227 +SimpleTest.waitForExplicitFinish(); 1.228 + 1.229 +</script> 1.230 + 1.231 +</head> 1.232 + 1.233 +<body onload="startTest();"> 1.234 +<iframe id="testframe" src="data:text/html;charset=utf-8,"></iframe> 1.235 +</body> 1.236 +</html>