|
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="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
|
6 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?> |
|
7 |
|
8 <script type="text/javascript"> |
|
9 |
|
10 const Ci = Components.interfaces; |
|
11 var mainWindow; |
|
12 |
|
13 var prefBranch = Components.classes["@mozilla.org/preferences-service;1"] |
|
14 .getService(Components.interfaces.nsIPrefBranch); |
|
15 prefBranch.setIntPref("browser.startup.page", 0); |
|
16 prefBranch.setCharPref("browser.startup.homepage_override.mstone", "ignore"); |
|
17 |
|
18 function startTest() { |
|
19 mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor) |
|
20 .getInterface(Ci.nsIWebNavigation) |
|
21 .QueryInterface(Ci.nsIDocShellTreeItem) |
|
22 .rootTreeItem |
|
23 .QueryInterface(Ci.nsIInterfaceRequestor) |
|
24 .getInterface(Ci.nsIDOMWindow); |
|
25 |
|
26 doTest(); |
|
27 } |
|
28 |
|
29 var contentPage = "http://mochi.test:8888/chrome/dom/tests/mochitest/localstorage/page_blank.html"; |
|
30 |
|
31 function testOnWindow(aIsPrivate, aCallback) { |
|
32 var win = mainWindow.OpenBrowserWindow({private: aIsPrivate}); |
|
33 win.addEventListener("load", function onLoad() { |
|
34 win.removeEventListener("load", onLoad, false); |
|
35 win.addEventListener("DOMContentLoaded", function onInnerLoad() { |
|
36 if (win.content.location.href == "about:privatebrowsing") { |
|
37 win.gBrowser.loadURI(contentPage); |
|
38 return; |
|
39 } |
|
40 win.removeEventListener("DOMContentLoaded", onInnerLoad, true); |
|
41 SimpleTest.executeSoon(function() { aCallback(win); }); |
|
42 }, true); |
|
43 win.gBrowser.loadURI(contentPage); |
|
44 }, true); |
|
45 } |
|
46 |
|
47 function doTest() { |
|
48 testOnWindow(false, function(aWin) { |
|
49 aWin.content.localStorage.setItem("persistent", "persistent1"); |
|
50 |
|
51 testOnWindow(true, function(privateWin) { |
|
52 is(privateWin.content.localStorage.getItem("persistent"), null, "previous values are inaccessible"); |
|
53 |
|
54 // Initially check the privateWin.content.localStorage is empty |
|
55 is(privateWin.content.localStorage.length, 0, "The storage is empty [1]"); |
|
56 is(privateWin.content.localStorage.key(0), null, "key() should return null for out-of-bounds access"); |
|
57 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
58 is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); |
|
59 is(privateWin.content.localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())"); |
|
60 is(privateWin.content.localStorage["nonexisting"], undefined, "Nonexisting item is null (array access)"); |
|
61 is(privateWin.content.localStorage.nonexisting, undefined, "Nonexisting item is null (property access)"); |
|
62 privateWin.content.localStorage.removeItem("nonexisting"); // Just check there is no exception |
|
63 |
|
64 is(typeof privateWin.content.localStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object"); |
|
65 is(typeof privateWin.content.localStorage["nonexisting"], "undefined", "['nonexisting'] is undefined"); |
|
66 is(typeof privateWin.content.localStorage.nonexisting, "undefined", "nonexisting is undefined"); |
|
67 is(typeof privateWin.content.localStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object"); |
|
68 is(typeof privateWin.content.localStorage["nonexisting2"], "undefined", "['nonexisting2'] is undefined"); |
|
69 is(typeof privateWin.content.localStorage.nonexisting2, "undefined", "nonexisting2 is undefined"); |
|
70 |
|
71 // add an empty-value key |
|
72 privateWin.content.localStorage.setItem("empty", ""); |
|
73 is(privateWin.content.localStorage.getItem("empty"), "", "Empty value (getItem())"); |
|
74 is(privateWin.content.localStorage["empty"], "", "Empty value (array access)"); |
|
75 is(privateWin.content.localStorage.empty, "", "Empty value (property access)"); |
|
76 is(typeof privateWin.content.localStorage.getItem("empty"), "string", "getItem('empty') is string"); |
|
77 is(typeof privateWin.content.localStorage["empty"], "string", "['empty'] is string"); |
|
78 is(typeof privateWin.content.localStorage.empty, "string", "empty is string"); |
|
79 privateWin.content.localStorage.removeItem("empty"); |
|
80 is(privateWin.content.localStorage.length, 0, "The storage has no keys"); |
|
81 is(privateWin.content.localStorage.getItem("empty"), null, "empty item is null (getItem())"); |
|
82 is(privateWin.content.localStorage["empty"], null, "empty item is undefined (array access)"); |
|
83 is(privateWin.content.localStorage.empty, null, "empty item is undefined (property access)"); |
|
84 is(typeof privateWin.content.localStorage.getItem("empty"), "object", "getItem('empty') is object"); |
|
85 is(typeof privateWin.content.localStorage["empty"], "undefined", "['empty'] is undefined"); |
|
86 is(typeof privateWin.content.localStorage.empty, "undefined", "empty is undefined"); |
|
87 |
|
88 // add one key, check it is there |
|
89 privateWin.content.localStorage.setItem("key1", "value1"); |
|
90 is(privateWin.content.localStorage.length, 1, "The storage has one key-value pair"); |
|
91 is(privateWin.content.localStorage.key(0), "key1"); |
|
92 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
93 is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); |
|
94 |
|
95 // check all access method give the correct result |
|
96 // and are of the correct type |
|
97 is(privateWin.content.localStorage.getItem("key1"), "value1", "getItem('key1') == value1"); |
|
98 is(privateWin.content.localStorage["key1"], "value1", "['key1'] == value1"); |
|
99 is(privateWin.content.localStorage.key1, "value1", "key1 == value1"); |
|
100 |
|
101 is(typeof privateWin.content.localStorage.getItem("key1"), "string", "getItem('key1') is string"); |
|
102 is(typeof privateWin.content.localStorage["key1"], "string", "['key1'] is string"); |
|
103 is(typeof privateWin.content.localStorage.key1, "string", "key1 is string"); |
|
104 |
|
105 // remove the previously added key and check the storage is empty |
|
106 privateWin.content.localStorage.removeItem("key1"); |
|
107 is(privateWin.content.localStorage.length, 0, "The storage is empty [2]"); |
|
108 is(privateWin.content.localStorage.key(0), null, "key() should return null for out-of-bounds access"); |
|
109 is(privateWin.content.localStorage.getItem("key1"), null, "\'key1\' removed"); |
|
110 |
|
111 is(typeof privateWin.content.localStorage.getItem("key1"), "object", "getItem('key1') is object"); |
|
112 is(typeof privateWin.content.localStorage["key1"], "undefined", "['key1'] is undefined"); |
|
113 is(typeof privateWin.content.localStorage.key1, "undefined", "key1 is undefined"); |
|
114 |
|
115 // add one key, check it is there |
|
116 privateWin.content.localStorage.setItem("key1", "value1"); |
|
117 is(privateWin.content.localStorage.length, 1, "The storage has one key-value pair"); |
|
118 is(privateWin.content.localStorage.key(0), "key1"); |
|
119 is(privateWin.content.localStorage.getItem("key1"), "value1"); |
|
120 |
|
121 // add a second key |
|
122 privateWin.content.localStorage.setItem("key2", "value2"); |
|
123 is(privateWin.content.localStorage.length, 2, "The storage has two key-value pairs"); |
|
124 is(privateWin.content.localStorage.getItem("key1"), "value1"); |
|
125 is(privateWin.content.localStorage.getItem("key2"), "value2"); |
|
126 var firstKey = privateWin.content.localStorage.key(0); |
|
127 var secondKey = privateWin.content.localStorage.key(1); |
|
128 ok((firstKey == 'key1' && secondKey == 'key2') || |
|
129 (firstKey == 'key2' && secondKey == 'key1'), |
|
130 'key() API works.'); |
|
131 |
|
132 // change the second key |
|
133 privateWin.content.localStorage.setItem("key2", "value2-2"); |
|
134 is(privateWin.content.localStorage.length, 2, "The storage has two key-value pairs"); |
|
135 is(privateWin.content.localStorage.key(0), firstKey); // After key value changes the order must be preserved |
|
136 is(privateWin.content.localStorage.key(1), secondKey); |
|
137 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
138 is(privateWin.content.localStorage.key(2), null, "key() should return null for out-of-bounds access"); |
|
139 is(privateWin.content.localStorage.getItem("key1"), "value1"); |
|
140 is(privateWin.content.localStorage.getItem("key2"), "value2-2"); |
|
141 |
|
142 // change the first key |
|
143 privateWin.content.localStorage.setItem("key1", "value1-2"); |
|
144 is(privateWin.content.localStorage.length, 2, "The storage has two key-value pairs"); |
|
145 is(privateWin.content.localStorage.key(0), firstKey); // After key value changes the order must be preserved |
|
146 is(privateWin.content.localStorage.key(1), secondKey); |
|
147 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
148 is(privateWin.content.localStorage.key(2), null, "key() should return null for out-of-bounds access"); |
|
149 is(privateWin.content.localStorage.getItem("key1"), "value1-2"); |
|
150 is(privateWin.content.localStorage.getItem("key2"), "value2-2"); |
|
151 |
|
152 // remove the second key |
|
153 privateWin.content.localStorage.removeItem("key2"); |
|
154 is(privateWin.content.localStorage.length, 1, "The storage has one key-value pair"); |
|
155 is(privateWin.content.localStorage.key(0), "key1"); |
|
156 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
157 is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); |
|
158 is(privateWin.content.localStorage.getItem("key1"), "value1-2"); |
|
159 |
|
160 // JS property test |
|
161 privateWin.content.localStorage.testA = "valueA"; |
|
162 is(privateWin.content.localStorage.testA, "valueA"); |
|
163 is(privateWin.content.localStorage["testA"], "valueA"); |
|
164 is(privateWin.content.localStorage.getItem("testA"), "valueA"); |
|
165 |
|
166 privateWin.content.localStorage.testA = "valueA2"; |
|
167 is(privateWin.content.localStorage.testA, "valueA2"); |
|
168 is(privateWin.content.localStorage["testA"], "valueA2"); |
|
169 is(privateWin.content.localStorage.getItem("testA"), "valueA2"); |
|
170 |
|
171 privateWin.content.localStorage["testB"] = "valueB"; |
|
172 is(privateWin.content.localStorage.testB, "valueB"); |
|
173 is(privateWin.content.localStorage["testB"], "valueB"); |
|
174 is(privateWin.content.localStorage.getItem("testB"), "valueB"); |
|
175 |
|
176 privateWin.content.localStorage["testB"] = "valueB2"; |
|
177 is(privateWin.content.localStorage.testB, "valueB2"); |
|
178 is(privateWin.content.localStorage["testB"], "valueB2"); |
|
179 is(privateWin.content.localStorage.getItem("testB"), "valueB2"); |
|
180 |
|
181 privateWin.content.localStorage.setItem("testC", "valueC"); |
|
182 is(privateWin.content.localStorage.testC, "valueC"); |
|
183 is(privateWin.content.localStorage["testC"], "valueC"); |
|
184 is(privateWin.content.localStorage.getItem("testC"), "valueC"); |
|
185 |
|
186 privateWin.content.localStorage.setItem("testC", "valueC2"); |
|
187 is(privateWin.content.localStorage.testC, "valueC2"); |
|
188 is(privateWin.content.localStorage["testC"], "valueC2"); |
|
189 is(privateWin.content.localStorage.getItem("testC"), "valueC2"); |
|
190 |
|
191 privateWin.content.localStorage.setItem("testC", null); |
|
192 is("testC" in privateWin.content.localStorage, true); |
|
193 is(privateWin.content.localStorage.getItem("testC"), "null"); |
|
194 is(privateWin.content.localStorage["testC"], "null"); |
|
195 is(privateWin.content.localStorage.testC, "null"); |
|
196 |
|
197 privateWin.content.localStorage.removeItem("testC"); |
|
198 privateWin.content.localStorage["testC"] = null; |
|
199 is("testC" in privateWin.content.localStorage, true); |
|
200 is(privateWin.content.localStorage.getItem("testC"), "null"); |
|
201 is(privateWin.content.localStorage["testC"], "null"); |
|
202 is(privateWin.content.localStorage.testC, "null"); |
|
203 |
|
204 privateWin.content.localStorage.setItem(null, "test"); |
|
205 is("null" in privateWin.content.localStorage, true); |
|
206 is(privateWin.content.localStorage.getItem("null"), "test"); |
|
207 is(privateWin.content.localStorage.getItem(null), "test"); |
|
208 is(privateWin.content.localStorage["null"], "test"); |
|
209 privateWin.content.localStorage.removeItem(null, "test"); |
|
210 // bug 350023 |
|
211 todo_is("null" in privateWin.content.localStorage, false); |
|
212 |
|
213 privateWin.content.localStorage.setItem(null, "test"); |
|
214 is("null" in privateWin.content.localStorage, true); |
|
215 privateWin.content.localStorage.removeItem("null", "test"); |
|
216 // bug 350023 |
|
217 todo_is("null" in privateWin.content.localStorage, false); |
|
218 |
|
219 // Clear the storage |
|
220 privateWin.content.localStorage.clear(); |
|
221 is(privateWin.content.localStorage.length, 0, "The storage is empty [3]"); |
|
222 is(privateWin.content.localStorage.key(0), null, "key() should return null for out-of-bounds access"); |
|
223 is(privateWin.content.localStorage.key(-1), null, "key() should return null for out-of-bounds access"); |
|
224 is(privateWin.content.localStorage.key(1), null, "key() should return null for out-of-bounds access"); |
|
225 is(privateWin.content.localStorage.getItem("nonexisting"), null, "Nonexisting item is null"); |
|
226 is(privateWin.content.localStorage.getItem("key1"), null, "key1 removed"); |
|
227 is(privateWin.content.localStorage.getItem("key2"), null, "key2 removed"); |
|
228 privateWin.content.localStorage.removeItem("nonexisting"); // Just check there is no exception |
|
229 privateWin.content.localStorage.removeItem("key1"); // Just check there is no exception |
|
230 privateWin.content.localStorage.removeItem("key2"); // Just check there is no exception |
|
231 |
|
232 privateWin.content.localStorage.setItem("must disappear", "private browsing value"); |
|
233 |
|
234 privateWin.close(); |
|
235 |
|
236 // The .close() call above will operate asynchronously, so execute the |
|
237 // code below asynchronously as well. |
|
238 function callback(newPrivateWin) { |
|
239 is(newPrivateWin.content.localStorage.getItem("must disappear"), null, "private browsing values threw away"); |
|
240 is(newPrivateWin.content.localStorage.length, 0, "No items"); |
|
241 |
|
242 newPrivateWin.close(); |
|
243 is(aWin.content.localStorage.getItem("persistent"), "persistent1", "back in normal mode"); |
|
244 aWin.content.localStorage.clear(); |
|
245 aWin.close(); |
|
246 |
|
247 prefBranch.clearUserPref("browser.startup.page") |
|
248 prefBranch.clearUserPref("browser.startup.homepage_override.mstone"); |
|
249 SimpleTest.finish(); |
|
250 }; |
|
251 SimpleTest.executeSoon(function() testOnWindow(true, callback)); |
|
252 }); |
|
253 }); |
|
254 } |
|
255 |
|
256 SimpleTest.waitForExplicitFinish(); |
|
257 |
|
258 </script> |
|
259 |
|
260 </head> |
|
261 |
|
262 <body onload="startTest();"> |
|
263 |
|
264 </body> |
|
265 </html> |