dom/indexedDB/test/helpers.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/test/helpers.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,277 @@
     1.4 +/**
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.7 + */
     1.8 +
     1.9 +var testGenerator = testSteps();
    1.10 +var archiveReaderEnabled = false;
    1.11 +
    1.12 +// The test js is shared between xpcshell (which has no SpecialPowers object)
    1.13 +// and content mochitests (where the |Components| object is accessible only as
    1.14 +// SpecialPowers.Components). Expose Components if necessary here to make things
    1.15 +// work everywhere.
    1.16 +//
    1.17 +// Even if the real |Components| doesn't exist, we might shim in a simple JS
    1.18 +// placebo for compat. An easy way to differentiate this from the real thing
    1.19 +// is whether the property is read-only or not.
    1.20 +var c = Object.getOwnPropertyDescriptor(this, 'Components');
    1.21 +if ((!c.value || c.writable) && typeof SpecialPowers === 'object')
    1.22 +  Components = SpecialPowers.Components;
    1.23 +
    1.24 +function executeSoon(aFun)
    1.25 +{
    1.26 +  let comp = SpecialPowers.wrap(Components);
    1.27 +
    1.28 +  let thread = comp.classes["@mozilla.org/thread-manager;1"]
    1.29 +                   .getService(comp.interfaces.nsIThreadManager)
    1.30 +                   .mainThread;
    1.31 +
    1.32 +  thread.dispatch({
    1.33 +    run: function() {
    1.34 +      aFun();
    1.35 +    }
    1.36 +  }, Components.interfaces.nsIThread.DISPATCH_NORMAL);
    1.37 +}
    1.38 +
    1.39 +function clearAllDatabases(callback) {
    1.40 +  function runCallback() {
    1.41 +    SimpleTest.executeSoon(function () { callback(); });
    1.42 +  }
    1.43 +
    1.44 +  if (!SpecialPowers.isMainProcess()) {
    1.45 +    runCallback();
    1.46 +    return;
    1.47 +  }
    1.48 +
    1.49 +  let comp = SpecialPowers.wrap(Components);
    1.50 +
    1.51 +  let quotaManager =
    1.52 +    comp.classes["@mozilla.org/dom/quota/manager;1"]
    1.53 +        .getService(comp.interfaces.nsIQuotaManager);
    1.54 +
    1.55 +  let uri = SpecialPowers.wrap(document).documentURIObject;
    1.56 +
    1.57 +  // We need to pass a JS callback to getUsageForURI. However, that callback
    1.58 +  // takes an XPCOM URI object, which will cause us to throw when we wrap it
    1.59 +  // for the content compartment. So we need to define the function in a
    1.60 +  // privileged scope, which we do using a sandbox.
    1.61 +  var sysPrin = SpecialPowers.Services.scriptSecurityManager.getSystemPrincipal();
    1.62 +  var sb = new SpecialPowers.Cu.Sandbox(sysPrin);
    1.63 +  sb.ok = ok;
    1.64 +  sb.runCallback = runCallback;
    1.65 +  var cb = SpecialPowers.Cu.evalInSandbox((function(uri, usage, fileUsage) {
    1.66 +    if (usage) {
    1.67 +      ok(false,
    1.68 +         "getUsageForURI returned non-zero usage after clearing all " +
    1.69 +         "storages!");
    1.70 +    }
    1.71 +    runCallback();
    1.72 +  }).toSource(), sb);
    1.73 +
    1.74 +  quotaManager.clearStoragesForURI(uri);
    1.75 +  quotaManager.getUsageForURI(uri, cb);
    1.76 +}
    1.77 +
    1.78 +if (!window.runTest) {
    1.79 +  window.runTest = function(limitedQuota)
    1.80 +  {
    1.81 +    SimpleTest.waitForExplicitFinish();
    1.82 +
    1.83 +    if (limitedQuota) {
    1.84 +      denyUnlimitedQuota();
    1.85 +    }
    1.86 +    else {
    1.87 +      allowUnlimitedQuota();
    1.88 +    }
    1.89 +
    1.90 +    enableExperimental();
    1.91 +    enableArchiveReader();
    1.92 +
    1.93 +    clearAllDatabases(function () { testGenerator.next(); });
    1.94 +  }
    1.95 +}
    1.96 +
    1.97 +function finishTest()
    1.98 +{
    1.99 +  resetUnlimitedQuota();
   1.100 +  resetExperimental();
   1.101 +  resetArchiveReader();
   1.102 +  SpecialPowers.notifyObserversInParentProcess(null, "disk-space-watcher",
   1.103 +                                               "free");
   1.104 +
   1.105 +  SimpleTest.executeSoon(function() {
   1.106 +    testGenerator.close();
   1.107 +    //clearAllDatabases(function() { SimpleTest.finish(); });
   1.108 +    SimpleTest.finish();
   1.109 +  });
   1.110 +}
   1.111 +
   1.112 +function browserRunTest()
   1.113 +{
   1.114 +  testGenerator.next();
   1.115 +}
   1.116 +
   1.117 +function browserFinishTest()
   1.118 +{
   1.119 +  setTimeout(function() { testGenerator.close(); }, 0);
   1.120 +}
   1.121 +
   1.122 +function grabEventAndContinueHandler(event)
   1.123 +{
   1.124 +  testGenerator.send(event);
   1.125 +}
   1.126 +
   1.127 +function continueToNextStep()
   1.128 +{
   1.129 +  SimpleTest.executeSoon(function() {
   1.130 +    testGenerator.next();
   1.131 +  });
   1.132 +}
   1.133 +
   1.134 +function continueToNextStepSync()
   1.135 +{
   1.136 +  testGenerator.next();
   1.137 +}
   1.138 +
   1.139 +function errorHandler(event)
   1.140 +{
   1.141 +  ok(false, "indexedDB error, '" + event.target.error.name + "'");
   1.142 +  finishTest();
   1.143 +}
   1.144 +
   1.145 +function browserErrorHandler(event)
   1.146 +{
   1.147 +  browserFinishTest();
   1.148 +  throw new Error("indexedDB error (" + event.code + "): " + event.message);
   1.149 +}
   1.150 +
   1.151 +function unexpectedSuccessHandler()
   1.152 +{
   1.153 +  ok(false, "Got success, but did not expect it!");
   1.154 +  finishTest();
   1.155 +}
   1.156 +
   1.157 +function expectedErrorHandler(name)
   1.158 +{
   1.159 +  return function(event) {
   1.160 +    is(event.type, "error", "Got an error event");
   1.161 +    is(event.target.error.name, name, "Expected error was thrown.");
   1.162 +    event.preventDefault();
   1.163 +    grabEventAndContinueHandler(event);
   1.164 +  };
   1.165 +}
   1.166 +
   1.167 +function ExpectError(name, preventDefault)
   1.168 +{
   1.169 +  this._name = name;
   1.170 +  this._preventDefault = preventDefault;
   1.171 +}
   1.172 +ExpectError.prototype = {
   1.173 +  handleEvent: function(event)
   1.174 +  {
   1.175 +    is(event.type, "error", "Got an error event");
   1.176 +    is(event.target.error.name, this._name, "Expected error was thrown.");
   1.177 +    if (this._preventDefault) {
   1.178 +      event.preventDefault();
   1.179 +      event.stopPropagation();
   1.180 +    }
   1.181 +    grabEventAndContinueHandler(event);
   1.182 +  }
   1.183 +};
   1.184 +
   1.185 +function compareKeys(k1, k2) {
   1.186 +  let t = typeof k1;
   1.187 +  if (t != typeof k2)
   1.188 +    return false;
   1.189 +
   1.190 +  if (t !== "object")
   1.191 +    return k1 === k2;
   1.192 +
   1.193 +  if (k1 instanceof Date) {
   1.194 +    return (k2 instanceof Date) &&
   1.195 +      k1.getTime() === k2.getTime();
   1.196 +  }
   1.197 +
   1.198 +  if (k1 instanceof Array) {
   1.199 +    if (!(k2 instanceof Array) ||
   1.200 +        k1.length != k2.length)
   1.201 +      return false;
   1.202 +
   1.203 +    for (let i = 0; i < k1.length; ++i) {
   1.204 +      if (!compareKeys(k1[i], k2[i]))
   1.205 +        return false;
   1.206 +    }
   1.207 +
   1.208 +    return true;
   1.209 +  }
   1.210 +
   1.211 +  return false;
   1.212 +}
   1.213 +
   1.214 +function addPermission(type, allow, url)
   1.215 +{
   1.216 +  if (!url) {
   1.217 +    url = window.document;
   1.218 +  }
   1.219 +  SpecialPowers.addPermission(type, allow, url);
   1.220 +}
   1.221 +
   1.222 +function removePermission(type, url)
   1.223 +{
   1.224 +  if (!url) {
   1.225 +    url = window.document;
   1.226 +  }
   1.227 +  SpecialPowers.removePermission(type, url);
   1.228 +}
   1.229 +
   1.230 +function setQuota(quota)
   1.231 +{
   1.232 +  SpecialPowers.setIntPref("dom.indexedDB.warningQuota", quota);
   1.233 +}
   1.234 +
   1.235 +function allowUnlimitedQuota(url)
   1.236 +{
   1.237 +  addPermission("indexedDB-unlimited", true, url);
   1.238 +}
   1.239 +
   1.240 +function denyUnlimitedQuota(url)
   1.241 +{
   1.242 +  addPermission("indexedDB-unlimited", false, url);
   1.243 +}
   1.244 +
   1.245 +function resetUnlimitedQuota(url)
   1.246 +{
   1.247 +  removePermission("indexedDB-unlimited", url);
   1.248 +}
   1.249 +
   1.250 +function enableArchiveReader()
   1.251 +{
   1.252 +  archiveReaderEnabled = SpecialPowers.getBoolPref("dom.archivereader.enabled");
   1.253 +  SpecialPowers.setBoolPref("dom.archivereader.enabled", true);
   1.254 +}
   1.255 +
   1.256 +function resetArchiveReader()
   1.257 +{
   1.258 +  SpecialPowers.setBoolPref("dom.archivereader.enabled", archiveReaderEnabled);
   1.259 +}
   1.260 +
   1.261 +function enableExperimental()
   1.262 +{
   1.263 +  SpecialPowers.setBoolPref("dom.indexedDB.experimental", true);
   1.264 +}
   1.265 +
   1.266 +function resetExperimental()
   1.267 +{
   1.268 +  SpecialPowers.clearUserPref("dom.indexedDB.experimental");
   1.269 +}
   1.270 +
   1.271 +function gc()
   1.272 +{
   1.273 +  SpecialPowers.forceGC();
   1.274 +  SpecialPowers.forceCC();
   1.275 +}
   1.276 +
   1.277 +function scheduleGC()
   1.278 +{
   1.279 +  SpecialPowers.exactGC(window, continueToNextStep);
   1.280 +}

mercurial