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