dom/tests/mochitest/localstorage/test_localStorageBaseSessionOnly.html

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

mercurial