dom/indexedDB/test/helpers.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 /**
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/publicdomain/zero/1.0/
     4  */
     6 var testGenerator = testSteps();
     7 var archiveReaderEnabled = false;
     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;
    21 function executeSoon(aFun)
    22 {
    23   let comp = SpecialPowers.wrap(Components);
    25   let thread = comp.classes["@mozilla.org/thread-manager;1"]
    26                    .getService(comp.interfaces.nsIThreadManager)
    27                    .mainThread;
    29   thread.dispatch({
    30     run: function() {
    31       aFun();
    32     }
    33   }, Components.interfaces.nsIThread.DISPATCH_NORMAL);
    34 }
    36 function clearAllDatabases(callback) {
    37   function runCallback() {
    38     SimpleTest.executeSoon(function () { callback(); });
    39   }
    41   if (!SpecialPowers.isMainProcess()) {
    42     runCallback();
    43     return;
    44   }
    46   let comp = SpecialPowers.wrap(Components);
    48   let quotaManager =
    49     comp.classes["@mozilla.org/dom/quota/manager;1"]
    50         .getService(comp.interfaces.nsIQuotaManager);
    52   let uri = SpecialPowers.wrap(document).documentURIObject;
    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);
    71   quotaManager.clearStoragesForURI(uri);
    72   quotaManager.getUsageForURI(uri, cb);
    73 }
    75 if (!window.runTest) {
    76   window.runTest = function(limitedQuota)
    77   {
    78     SimpleTest.waitForExplicitFinish();
    80     if (limitedQuota) {
    81       denyUnlimitedQuota();
    82     }
    83     else {
    84       allowUnlimitedQuota();
    85     }
    87     enableExperimental();
    88     enableArchiveReader();
    90     clearAllDatabases(function () { testGenerator.next(); });
    91   }
    92 }
    94 function finishTest()
    95 {
    96   resetUnlimitedQuota();
    97   resetExperimental();
    98   resetArchiveReader();
    99   SpecialPowers.notifyObserversInParentProcess(null, "disk-space-watcher",
   100                                                "free");
   102   SimpleTest.executeSoon(function() {
   103     testGenerator.close();
   104     //clearAllDatabases(function() { SimpleTest.finish(); });
   105     SimpleTest.finish();
   106   });
   107 }
   109 function browserRunTest()
   110 {
   111   testGenerator.next();
   112 }
   114 function browserFinishTest()
   115 {
   116   setTimeout(function() { testGenerator.close(); }, 0);
   117 }
   119 function grabEventAndContinueHandler(event)
   120 {
   121   testGenerator.send(event);
   122 }
   124 function continueToNextStep()
   125 {
   126   SimpleTest.executeSoon(function() {
   127     testGenerator.next();
   128   });
   129 }
   131 function continueToNextStepSync()
   132 {
   133   testGenerator.next();
   134 }
   136 function errorHandler(event)
   137 {
   138   ok(false, "indexedDB error, '" + event.target.error.name + "'");
   139   finishTest();
   140 }
   142 function browserErrorHandler(event)
   143 {
   144   browserFinishTest();
   145   throw new Error("indexedDB error (" + event.code + "): " + event.message);
   146 }
   148 function unexpectedSuccessHandler()
   149 {
   150   ok(false, "Got success, but did not expect it!");
   151   finishTest();
   152 }
   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 }
   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 };
   182 function compareKeys(k1, k2) {
   183   let t = typeof k1;
   184   if (t != typeof k2)
   185     return false;
   187   if (t !== "object")
   188     return k1 === k2;
   190   if (k1 instanceof Date) {
   191     return (k2 instanceof Date) &&
   192       k1.getTime() === k2.getTime();
   193   }
   195   if (k1 instanceof Array) {
   196     if (!(k2 instanceof Array) ||
   197         k1.length != k2.length)
   198       return false;
   200     for (let i = 0; i < k1.length; ++i) {
   201       if (!compareKeys(k1[i], k2[i]))
   202         return false;
   203     }
   205     return true;
   206   }
   208   return false;
   209 }
   211 function addPermission(type, allow, url)
   212 {
   213   if (!url) {
   214     url = window.document;
   215   }
   216   SpecialPowers.addPermission(type, allow, url);
   217 }
   219 function removePermission(type, url)
   220 {
   221   if (!url) {
   222     url = window.document;
   223   }
   224   SpecialPowers.removePermission(type, url);
   225 }
   227 function setQuota(quota)
   228 {
   229   SpecialPowers.setIntPref("dom.indexedDB.warningQuota", quota);
   230 }
   232 function allowUnlimitedQuota(url)
   233 {
   234   addPermission("indexedDB-unlimited", true, url);
   235 }
   237 function denyUnlimitedQuota(url)
   238 {
   239   addPermission("indexedDB-unlimited", false, url);
   240 }
   242 function resetUnlimitedQuota(url)
   243 {
   244   removePermission("indexedDB-unlimited", url);
   245 }
   247 function enableArchiveReader()
   248 {
   249   archiveReaderEnabled = SpecialPowers.getBoolPref("dom.archivereader.enabled");
   250   SpecialPowers.setBoolPref("dom.archivereader.enabled", true);
   251 }
   253 function resetArchiveReader()
   254 {
   255   SpecialPowers.setBoolPref("dom.archivereader.enabled", archiveReaderEnabled);
   256 }
   258 function enableExperimental()
   259 {
   260   SpecialPowers.setBoolPref("dom.indexedDB.experimental", true);
   261 }
   263 function resetExperimental()
   264 {
   265   SpecialPowers.clearUserPref("dom.indexedDB.experimental");
   266 }
   268 function gc()
   269 {
   270   SpecialPowers.forceGC();
   271   SpecialPowers.forceCC();
   272 }
   274 function scheduleGC()
   275 {
   276   SpecialPowers.exactGC(window, continueToNextStep);
   277 }

mercurial