|
1 <html xmlns="http://www.w3.org/1999/xhtml"> |
|
2 <head> |
|
3 <title>sessionStorage basic test</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 var expectedEvents = [ |
|
11 "empty,null,", |
|
12 "empty,,null", |
|
13 "key1,null,value1", |
|
14 "key1,value1,null", |
|
15 "key1,null,value1", |
|
16 "key2,null,value2", |
|
17 "key2,value2,value2-2", |
|
18 "key1,value1,value1-2", |
|
19 "key2,value2-2,null", |
|
20 "null,null,null" |
|
21 ]; |
|
22 |
|
23 function setup() { |
|
24 sessionStorage.clear(); |
|
25 SimpleTest.executeSoon(startTest); |
|
26 } |
|
27 |
|
28 function startTest() |
|
29 { |
|
30 // Initially check the sessionStorage is empty |
|
31 is(sessionStorage.length, 0, "The storage is empty [1]"); |
|
32 is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access"); |
|
33 is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
34 is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access"); |
|
35 is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())"); |
|
36 is(sessionStorage["nonexisting"], undefined, "Nonexisting item is undefined (array access)"); |
|
37 is(sessionStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)"); |
|
38 sessionStorage.removeItem("nonexisting"); // Just check there is no exception |
|
39 |
|
40 is(typeof sessionStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object"); |
|
41 is(typeof sessionStorage["nonexisting"], "undefined", "['nonexisting'] is undefined"); |
|
42 is(typeof sessionStorage.nonexisting, "undefined", "nonexisting is undefined"); |
|
43 is(typeof sessionStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object"); |
|
44 is(typeof sessionStorage["nonexisting2"], "undefined", "['nonexisting2'] is undefined"); |
|
45 is(typeof sessionStorage.nonexisting2, "undefined", "nonexisting2 is undefined"); |
|
46 |
|
47 var mozStorageChangedReceived = 0; |
|
48 var sessionStorageCopy = sessionStorage; |
|
49 |
|
50 function onStorageChanged(e) { |
|
51 if (e.storageArea == sessionStorageCopy) { |
|
52 ok(expectedEvents.length > 0, "Not more then expected events encountered"); |
|
53 var receivedEvent = e.key + "," + e.oldValue + "," + e.newValue; |
|
54 is(receivedEvent, expectedEvents.shift(), "Expected event data: " + receivedEvent); |
|
55 } |
|
56 } |
|
57 |
|
58 // Listen for MozStorageChanged |
|
59 SpecialPowers.addChromeEventListener("MozStorageChanged", onStorageChanged, false); |
|
60 |
|
61 // add an empty-value key |
|
62 sessionStorage.setItem("empty", ""); |
|
63 is(sessionStorage.getItem("empty"), "", "Empty value (getItem())"); |
|
64 is(sessionStorage["empty"], "", "Empty value (array access)"); |
|
65 is(sessionStorage.empty, "", "Empty value (property access)"); |
|
66 is(typeof sessionStorage.getItem("empty"), "string", "getItem('empty') is string"); |
|
67 is(typeof sessionStorage["empty"], "string", "['empty'] is string"); |
|
68 is(typeof sessionStorage.empty, "string", "empty is string"); |
|
69 sessionStorage.removeItem("empty"); |
|
70 is(sessionStorage.length, 0, "The storage has no keys"); |
|
71 is(sessionStorage.getItem("empty"), null, "empty item is null (getItem())"); |
|
72 is(sessionStorage["empty"], undefined, "empty item is undefined (array access)"); |
|
73 is(sessionStorage.empty, undefined, "empty item is undefined (property access)"); |
|
74 is(typeof sessionStorage.getItem("empty"), "object", "getItem('empty') is object"); |
|
75 is(typeof sessionStorage["empty"], "undefined", "['empty'] is undefined"); |
|
76 is(typeof sessionStorage.empty, "undefined", "empty is undefined"); |
|
77 |
|
78 // add one key, check it is there |
|
79 sessionStorage.setItem("key1", "value1"); |
|
80 is(sessionStorage.length, 1, "The storage has one key-value pair"); |
|
81 is(sessionStorage.key(0), "key1"); |
|
82 is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
83 is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access"); |
|
84 |
|
85 // check all access method give the correct result |
|
86 // and are of the correct type |
|
87 is(sessionStorage.getItem("key1"), "value1", "getItem('key1') == value1"); |
|
88 is(sessionStorage["key1"], "value1", "['key1'] == value1"); |
|
89 is(sessionStorage.key1, "value1", "key1 == value1"); |
|
90 |
|
91 is(typeof sessionStorage.getItem("key1"), "string", "getItem('key1') is string"); |
|
92 is(typeof sessionStorage["key1"], "string", "['key1'] is string"); |
|
93 is(typeof sessionStorage.key1, "string", "key1 is string"); |
|
94 |
|
95 // remove the previously added key and check the storage is empty |
|
96 sessionStorage.removeItem("key1"); |
|
97 is(sessionStorage.length, 0, "The storage is empty [2]"); |
|
98 is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access"); |
|
99 is(sessionStorage.getItem("key1"), null, "\'key1\' removed"); |
|
100 |
|
101 is(typeof sessionStorage.getItem("key1"), "object", "getItem('key1') is object"); |
|
102 is(typeof sessionStorage["key1"], "undefined", "['key1'] is undefined"); |
|
103 is(typeof sessionStorage.key1, "undefined", "key1 is undefined"); |
|
104 |
|
105 // add one key, check it is there |
|
106 sessionStorage.setItem("key1", "value1"); |
|
107 is(sessionStorage.length, 1, "The storage has one key-value pair"); |
|
108 is(sessionStorage.key(0), "key1"); |
|
109 is(sessionStorage.getItem("key1"), "value1"); |
|
110 |
|
111 // add a second key |
|
112 sessionStorage.setItem("key2", "value2"); |
|
113 is(sessionStorage.length, 2, "The storage has two key-value pairs"); |
|
114 is(sessionStorage.getItem("key1"), "value1"); |
|
115 is(sessionStorage.getItem("key2"), "value2"); |
|
116 var firstKey = sessionStorage.key(0); |
|
117 var secondKey = sessionStorage.key(1); |
|
118 ok((firstKey == 'key1' && secondKey == 'key2') || |
|
119 (firstKey == 'key2' && secondKey == 'key1'), |
|
120 'key() API works.'); |
|
121 |
|
122 // change the second key |
|
123 sessionStorage.setItem("key2", "value2-2"); |
|
124 is(sessionStorage.length, 2, "The storage has two key-value pairs"); |
|
125 is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved |
|
126 is(sessionStorage.key(1), secondKey); |
|
127 is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
128 is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access"); |
|
129 is(sessionStorage.getItem("key1"), "value1"); |
|
130 is(sessionStorage.getItem("key2"), "value2-2"); |
|
131 |
|
132 // change the first key |
|
133 sessionStorage.setItem("key1", "value1-2"); |
|
134 is(sessionStorage.length, 2, "The storage has two key-value pairs"); |
|
135 is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved |
|
136 is(sessionStorage.key(1), secondKey); |
|
137 is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
138 is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access"); |
|
139 is(sessionStorage.getItem("key1"), "value1-2"); |
|
140 is(sessionStorage.getItem("key2"), "value2-2"); |
|
141 |
|
142 // remove the second key |
|
143 sessionStorage.removeItem("key2"); |
|
144 is(sessionStorage.length, 1, "The storage has one key-value pair"); |
|
145 is(sessionStorage.key(0), "key1"); |
|
146 is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
147 is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access"); |
|
148 is(sessionStorage.getItem("key1"), "value1-2"); |
|
149 |
|
150 // Clear the storage |
|
151 sessionStorage.clear(); |
|
152 is(sessionStorage.length, 0, "The storage is empty [3]"); |
|
153 is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access"); |
|
154 is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
155 is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access"); |
|
156 is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null"); |
|
157 is(sessionStorage.getItem("key1"), null, "key1 removed"); |
|
158 is(sessionStorage.getItem("key2"), null, "key2 removed"); |
|
159 sessionStorage.removeItem("nonexisting"); // Just check there is no exception |
|
160 sessionStorage.removeItem("key1"); // Just check there is no exception |
|
161 sessionStorage.removeItem("key2"); // Just check there is no exception |
|
162 |
|
163 SimpleTest.executeSoon(function () { |
|
164 SpecialPowers.removeChromeEventListener("MozStorageChanged", onStorageChanged, false); |
|
165 is(expectedEvents.length, 0, "received the correct number of events"); |
|
166 |
|
167 sessionStorage.clear(); |
|
168 SimpleTest.finish(); |
|
169 }); |
|
170 } |
|
171 |
|
172 SimpleTest.waitForExplicitFinish(); |
|
173 |
|
174 </script> |
|
175 |
|
176 </head> |
|
177 |
|
178 <body onload="setup();"> |
|
179 |
|
180 </body> |
|
181 </html> |