dom/tests/mochitest/localstorage/test_localStorageBaseSessionOnly.html

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

mercurial