|
1 <html xmlns="http://www.w3.org/1999/xhtml"> |
|
2 <head> |
|
3 <title>localStorage and DOM quota test</title> |
|
4 |
|
5 <script type="text/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 SimpleTest.waitForExplicitFinish(); |
|
10 |
|
11 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
12 |
|
13 const Ci = Components.interfaces; |
|
14 const CONTENT_PAGE = "http://mochi.test:8888/chrome/dom/tests/mochitest/localstorage/page_blank.html"; |
|
15 const slavePath = "/tests/dom/tests/mochitest/localstorage/"; |
|
16 var currentTest = 1; |
|
17 var quota; |
|
18 |
|
19 try { |
|
20 quota = Services.prefs.getIntPref("dom.storage.default_quota"); |
|
21 } catch (ex) { |
|
22 quota = 5 * 1024; |
|
23 } |
|
24 Services.prefs.setIntPref("browser.startup.page", 0); |
|
25 Services.prefs.setIntPref("dom.storage.default_quota", 1); |
|
26 |
|
27 var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor). |
|
28 getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShellTreeItem). |
|
29 rootTreeItem.QueryInterface(Ci.nsIInterfaceRequestor). |
|
30 getInterface(Ci.nsIDOMWindow); |
|
31 var slaveLoadsPending = 1; |
|
32 var slaveOrigin = ""; |
|
33 var slave = null; |
|
34 var failureRegExp = new RegExp("^FAILURE"); |
|
35 |
|
36 function startTest() { |
|
37 testOnWindow(true, function(aWindow) { |
|
38 info("Private window loaded"); |
|
39 var frame = aWindow.content.document.createElement("iframe"); |
|
40 aWindow.content.document.body.appendChild(frame); |
|
41 aWindow.content.addEventListener("message", function(aEvent) { |
|
42 onMessageReceived(aEvent, aWindow) |
|
43 }, false); |
|
44 slave = aWindow.content.frames[0]; |
|
45 |
|
46 SimpleTest.waitForFocus(function() doNextTest(aWindow), aWindow); |
|
47 }); |
|
48 } |
|
49 |
|
50 function doNextTest(aWindow) { |
|
51 info("Running test: " + currentTest); |
|
52 switch (currentTest) { |
|
53 // Initialy setup the quota to testing value of 1024B and |
|
54 // set a 500 bytes key with name length 1 (allocate 501 bytes) |
|
55 case 1: |
|
56 slaveOrigin = "http://example.com"; |
|
57 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&A&success"; |
|
58 break; |
|
59 |
|
60 // In subdomain now set another key with length 500 bytes, i.e. |
|
61 // allocate 501 bytes |
|
62 case 2: |
|
63 slaveOrigin = "http://test1.example.com"; |
|
64 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&B&success"; |
|
65 break; |
|
66 |
|
67 // Try to set the same key value again to check we don't fail |
|
68 // even 1002 bytes has already been exhausted from the quota |
|
69 // We just change the value of an existing key. |
|
70 case 3: |
|
71 slaveOrigin = "http://test1.example.com"; |
|
72 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&B&success"; |
|
73 break; |
|
74 |
|
75 // Try to set the same key to a larger value that would lead to |
|
76 // quota reach and check that the value is still the old one |
|
77 case 4: |
|
78 slaveOrigin = "http://test1.example.com"; |
|
79 slave.location = slaveOrigin + slavePath + "frameQuota.html?add2&B&failure"; |
|
80 break; |
|
81 |
|
82 // In a different subdomain try to set a new 500 bytes key |
|
83 // and check we fail because we are over the quota |
|
84 case 5: |
|
85 slaveOrigin = "https://test2.example.com"; |
|
86 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&C&failure"; |
|
87 break; |
|
88 |
|
89 // Remove from the second subdomain the second key, it must not fail |
|
90 // This should release the allocated space of the quota assigned to |
|
91 // example.com. |
|
92 case 6: |
|
93 slaveOrigin = "http://test1.example.com"; |
|
94 slave.location = slaveOrigin + slavePath + "frameQuota.html?remove&B&success"; |
|
95 break; |
|
96 |
|
97 // Now try again to set 500 bytes key, it must succeed. |
|
98 case 7: |
|
99 slaveOrigin = "https://test2.example.com"; |
|
100 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&C&success"; |
|
101 break; |
|
102 |
|
103 case 8: |
|
104 // Do a clean up... |
|
105 // TODO Bug 455070, use just ?clear what invokes call |
|
106 // of clear() in the target frame. W/o clear method we must |
|
107 // call clear implemented as removeItem for each item in |
|
108 // the localStorage. |
|
109 slaveOrigin = "http://example.com"; |
|
110 slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&A&"; |
|
111 break; |
|
112 |
|
113 case 9: |
|
114 // Do a clean up... |
|
115 slaveOrigin = "http://test1.example.com"; |
|
116 slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&B&"; |
|
117 break; |
|
118 |
|
119 case 10: |
|
120 // Do a clean up... |
|
121 slaveOrigin = "https://test2.example.com"; |
|
122 slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&C&"; |
|
123 break; |
|
124 |
|
125 default: |
|
126 Services.prefs.clearUserPref("browser.startup.page") |
|
127 Services.prefs.setIntPref("dom.storage.default_quota", quota); |
|
128 aWindow.close(); |
|
129 SimpleTest.finish(); |
|
130 } |
|
131 |
|
132 ++currentTest; |
|
133 } |
|
134 |
|
135 function onMessageReceived(event, aWindow) { |
|
136 info("Message received: " + event.data); |
|
137 switch (event.data) { |
|
138 // Indication of the frame onload event |
|
139 case "frame loaded": |
|
140 if (--slaveLoadsPending) |
|
141 break; |
|
142 // Just fall through... |
|
143 // Indication of successfully finished step of a test |
|
144 case "perf": |
|
145 slave.postMessage("step", slaveOrigin); |
|
146 break; |
|
147 // Indication of all test parts finish (from any of the frames) |
|
148 case "done": |
|
149 aWindow.content.localStorage.clear(); |
|
150 slaveLoadsPending = 1; |
|
151 doNextTest(aWindow); |
|
152 break; |
|
153 // Any other message indicates error or succes message of a test |
|
154 default: |
|
155 SimpleTest.ok(!event.data.match(failureRegExp), event.data); |
|
156 break; |
|
157 } |
|
158 } |
|
159 |
|
160 function testOnWindow(aIsPrivate, aCallback) { |
|
161 var win = mainWindow.OpenBrowserWindow({private: aIsPrivate}); |
|
162 win.addEventListener("load", function onLoad() { |
|
163 win.removeEventListener("load", onLoad, false); |
|
164 win.addEventListener("DOMContentLoaded", function onInnerLoad() { |
|
165 if (win.content.location.href != CONTENT_PAGE) { |
|
166 win.gBrowser.loadURI(CONTENT_PAGE); |
|
167 return; |
|
168 } |
|
169 win.removeEventListener("DOMContentLoaded", onInnerLoad, true); |
|
170 win.gBrowser.selectedBrowser.focus(); |
|
171 SimpleTest.executeSoon(function() { aCallback(win); }); |
|
172 }, true); |
|
173 win.gBrowser.loadURI(CONTENT_PAGE); |
|
174 }, true); |
|
175 } |
|
176 |
|
177 </script> |
|
178 </head> |
|
179 <body onload="startTest();"> |
|
180 </body> |
|
181 </html> |