dom/indexedDB/test/helpers.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:bcfb4f33c7cb
1 /**
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
4 */
5
6 var testGenerator = testSteps();
7 var archiveReaderEnabled = false;
8
9 // The test js is shared between xpcshell (which has no SpecialPowers object)
10 // and content mochitests (where the |Components| object is accessible only as
11 // SpecialPowers.Components). Expose Components if necessary here to make things
12 // work everywhere.
13 //
14 // Even if the real |Components| doesn't exist, we might shim in a simple JS
15 // placebo for compat. An easy way to differentiate this from the real thing
16 // is whether the property is read-only or not.
17 var c = Object.getOwnPropertyDescriptor(this, 'Components');
18 if ((!c.value || c.writable) && typeof SpecialPowers === 'object')
19 Components = SpecialPowers.Components;
20
21 function executeSoon(aFun)
22 {
23 let comp = SpecialPowers.wrap(Components);
24
25 let thread = comp.classes["@mozilla.org/thread-manager;1"]
26 .getService(comp.interfaces.nsIThreadManager)
27 .mainThread;
28
29 thread.dispatch({
30 run: function() {
31 aFun();
32 }
33 }, Components.interfaces.nsIThread.DISPATCH_NORMAL);
34 }
35
36 function clearAllDatabases(callback) {
37 function runCallback() {
38 SimpleTest.executeSoon(function () { callback(); });
39 }
40
41 if (!SpecialPowers.isMainProcess()) {
42 runCallback();
43 return;
44 }
45
46 let comp = SpecialPowers.wrap(Components);
47
48 let quotaManager =
49 comp.classes["@mozilla.org/dom/quota/manager;1"]
50 .getService(comp.interfaces.nsIQuotaManager);
51
52 let uri = SpecialPowers.wrap(document).documentURIObject;
53
54 // We need to pass a JS callback to getUsageForURI. However, that callback
55 // takes an XPCOM URI object, which will cause us to throw when we wrap it
56 // for the content compartment. So we need to define the function in a
57 // privileged scope, which we do using a sandbox.
58 var sysPrin = SpecialPowers.Services.scriptSecurityManager.getSystemPrincipal();
59 var sb = new SpecialPowers.Cu.Sandbox(sysPrin);
60 sb.ok = ok;
61 sb.runCallback = runCallback;
62 var cb = SpecialPowers.Cu.evalInSandbox((function(uri, usage, fileUsage) {
63 if (usage) {
64 ok(false,
65 "getUsageForURI returned non-zero usage after clearing all " +
66 "storages!");
67 }
68 runCallback();
69 }).toSource(), sb);
70
71 quotaManager.clearStoragesForURI(uri);
72 quotaManager.getUsageForURI(uri, cb);
73 }
74
75 if (!window.runTest) {
76 window.runTest = function(limitedQuota)
77 {
78 SimpleTest.waitForExplicitFinish();
79
80 if (limitedQuota) {
81 denyUnlimitedQuota();
82 }
83 else {
84 allowUnlimitedQuota();
85 }
86
87 enableExperimental();
88 enableArchiveReader();
89
90 clearAllDatabases(function () { testGenerator.next(); });
91 }
92 }
93
94 function finishTest()
95 {
96 resetUnlimitedQuota();
97 resetExperimental();
98 resetArchiveReader();
99 SpecialPowers.notifyObserversInParentProcess(null, "disk-space-watcher",
100 "free");
101
102 SimpleTest.executeSoon(function() {
103 testGenerator.close();
104 //clearAllDatabases(function() { SimpleTest.finish(); });
105 SimpleTest.finish();
106 });
107 }
108
109 function browserRunTest()
110 {
111 testGenerator.next();
112 }
113
114 function browserFinishTest()
115 {
116 setTimeout(function() { testGenerator.close(); }, 0);
117 }
118
119 function grabEventAndContinueHandler(event)
120 {
121 testGenerator.send(event);
122 }
123
124 function continueToNextStep()
125 {
126 SimpleTest.executeSoon(function() {
127 testGenerator.next();
128 });
129 }
130
131 function continueToNextStepSync()
132 {
133 testGenerator.next();
134 }
135
136 function errorHandler(event)
137 {
138 ok(false, "indexedDB error, '" + event.target.error.name + "'");
139 finishTest();
140 }
141
142 function browserErrorHandler(event)
143 {
144 browserFinishTest();
145 throw new Error("indexedDB error (" + event.code + "): " + event.message);
146 }
147
148 function unexpectedSuccessHandler()
149 {
150 ok(false, "Got success, but did not expect it!");
151 finishTest();
152 }
153
154 function expectedErrorHandler(name)
155 {
156 return function(event) {
157 is(event.type, "error", "Got an error event");
158 is(event.target.error.name, name, "Expected error was thrown.");
159 event.preventDefault();
160 grabEventAndContinueHandler(event);
161 };
162 }
163
164 function ExpectError(name, preventDefault)
165 {
166 this._name = name;
167 this._preventDefault = preventDefault;
168 }
169 ExpectError.prototype = {
170 handleEvent: function(event)
171 {
172 is(event.type, "error", "Got an error event");
173 is(event.target.error.name, this._name, "Expected error was thrown.");
174 if (this._preventDefault) {
175 event.preventDefault();
176 event.stopPropagation();
177 }
178 grabEventAndContinueHandler(event);
179 }
180 };
181
182 function compareKeys(k1, k2) {
183 let t = typeof k1;
184 if (t != typeof k2)
185 return false;
186
187 if (t !== "object")
188 return k1 === k2;
189
190 if (k1 instanceof Date) {
191 return (k2 instanceof Date) &&
192 k1.getTime() === k2.getTime();
193 }
194
195 if (k1 instanceof Array) {
196 if (!(k2 instanceof Array) ||
197 k1.length != k2.length)
198 return false;
199
200 for (let i = 0; i < k1.length; ++i) {
201 if (!compareKeys(k1[i], k2[i]))
202 return false;
203 }
204
205 return true;
206 }
207
208 return false;
209 }
210
211 function addPermission(type, allow, url)
212 {
213 if (!url) {
214 url = window.document;
215 }
216 SpecialPowers.addPermission(type, allow, url);
217 }
218
219 function removePermission(type, url)
220 {
221 if (!url) {
222 url = window.document;
223 }
224 SpecialPowers.removePermission(type, url);
225 }
226
227 function setQuota(quota)
228 {
229 SpecialPowers.setIntPref("dom.indexedDB.warningQuota", quota);
230 }
231
232 function allowUnlimitedQuota(url)
233 {
234 addPermission("indexedDB-unlimited", true, url);
235 }
236
237 function denyUnlimitedQuota(url)
238 {
239 addPermission("indexedDB-unlimited", false, url);
240 }
241
242 function resetUnlimitedQuota(url)
243 {
244 removePermission("indexedDB-unlimited", url);
245 }
246
247 function enableArchiveReader()
248 {
249 archiveReaderEnabled = SpecialPowers.getBoolPref("dom.archivereader.enabled");
250 SpecialPowers.setBoolPref("dom.archivereader.enabled", true);
251 }
252
253 function resetArchiveReader()
254 {
255 SpecialPowers.setBoolPref("dom.archivereader.enabled", archiveReaderEnabled);
256 }
257
258 function enableExperimental()
259 {
260 SpecialPowers.setBoolPref("dom.indexedDB.experimental", true);
261 }
262
263 function resetExperimental()
264 {
265 SpecialPowers.clearUserPref("dom.indexedDB.experimental");
266 }
267
268 function gc()
269 {
270 SpecialPowers.forceGC();
271 SpecialPowers.forceCC();
272 }
273
274 function scheduleGC()
275 {
276 SpecialPowers.exactGC(window, continueToNextStep);
277 }

mercurial