dom/indexedDB/test/unit/head.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /**
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/publicdomain/zero/1.0/
     4  */
     6 const { 'classes': Cc, 'interfaces': Ci, 'utils': Cu } = Components;
     8 const DOMException = Ci.nsIDOMDOMException;
    10 function is(a, b, msg) {
    11   dump("is(" + a + ", " + b + ", \"" + msg + "\")");
    12   do_check_eq(a, b, Components.stack.caller);
    13 }
    15 function ok(cond, msg) {
    16   dump("ok(" + cond + ", \"" + msg + "\")");
    17   do_check_true(!!cond, Components.stack.caller); 
    18 }
    20 function isnot(a, b, msg) {
    21   dump("isnot(" + a + ", " + b + ", \"" + msg + "\")");
    22   do_check_neq(a, b, Components.stack.caller); 
    23 }
    25 function executeSoon(fun) {
    26   do_execute_soon(fun);
    27 }
    29 function todo(condition, name, diag) {
    30   dump("TODO: ", diag);
    31 }
    33 function info(name, message) {
    34   do_print(name);
    35 }
    37 function run_test() {
    38   runTest();
    39 };
    41 if (!this.runTest) {
    42   this.runTest = function()
    43   {
    44     // XPCShell does not get a profile by default.
    45     do_get_profile();
    47     enableExperimental();
    49     Cu.importGlobalProperties(["indexedDB"]);
    51     do_test_pending();
    52     testGenerator.next();
    53   }
    54 }
    56 function finishTest()
    57 {
    58   resetExperimental();
    59   SpecialPowers.notifyObserversInParentProcess(null, "disk-space-watcher",
    60                                                "free");
    62   do_execute_soon(function(){
    63     testGenerator.close();
    64     do_test_finished();
    65   })
    66 }
    68 function grabEventAndContinueHandler(event)
    69 {
    70   testGenerator.send(event);
    71 }
    73 function continueToNextStep()
    74 {
    75   do_execute_soon(function() {
    76     testGenerator.next();
    77   });
    78 }
    80 function errorHandler(event)
    81 {
    82   dump("indexedDB error: " + event.target.error.name);
    83   do_check_true(false);
    84   finishTest();
    85 }
    87 function unexpectedSuccessHandler()
    88 {
    89   do_check_true(false);
    90   finishTest();
    91 }
    93 function expectedErrorHandler(name)
    94 {
    95   return function(event) {
    96     do_check_eq(event.type, "error");
    97     do_check_eq(event.target.error.name, name);
    98     event.preventDefault();
    99     grabEventAndContinueHandler(event);
   100   };
   101 }
   103 function ExpectError(name)
   104 {
   105   this._name = name;
   106 }
   107 ExpectError.prototype = {
   108   handleEvent: function(event)
   109   {
   110     do_check_eq(event.type, "error");
   111     do_check_eq(this._name, event.target.error.name);
   112     event.preventDefault();
   113     grabEventAndContinueHandler(event);
   114   }
   115 };
   117 function continueToNextStepSync()
   118 {
   119   testGenerator.next();
   120 }
   122 function compareKeys(k1, k2) {
   123   let t = typeof k1;
   124   if (t != typeof k2)
   125     return false;
   127   if (t !== "object")
   128     return k1 === k2;
   130   if (k1 instanceof Date) {
   131     return (k2 instanceof Date) &&
   132       k1.getTime() === k2.getTime();
   133   }
   135   if (k1 instanceof Array) {
   136     if (!(k2 instanceof Array) ||
   137         k1.length != k2.length)
   138       return false;
   140     for (let i = 0; i < k1.length; ++i) {
   141       if (!compareKeys(k1[i], k2[i]))
   142         return false;
   143     }
   145     return true;
   146   }
   148   return false;
   149 }
   151 function addPermission(permission, url)
   152 {
   153   throw "addPermission";
   154 }
   156 function removePermission(permission, url)
   157 {
   158   throw "removePermission";
   159 }
   161 function setQuota(quota)
   162 {
   163   throw "setQuota";
   164 }
   166 function allowIndexedDB(url)
   167 {
   168   throw "allowIndexedDB";
   169 }
   171 function disallowIndexedDB(url)
   172 {
   173   throw "disallowIndexedDB";
   174 }
   176 function allowUnlimitedQuota(url)
   177 {
   178   throw "allowUnlimitedQuota";
   179 }
   181 function disallowUnlimitedQuota(url)
   182 {
   183   throw "disallowUnlimitedQuota";
   184 }
   186 function enableExperimental()
   187 {
   188   SpecialPowers.setBoolPref("dom.indexedDB.experimental", true);
   189 }
   191 function resetExperimental()
   192 {
   193   SpecialPowers.clearUserPref("dom.indexedDB.experimental");
   194 }
   196 function gc()
   197 {
   198   Components.utils.forceGC();
   199   Components.utils.forceCC();
   200 }
   202 function scheduleGC()
   203 {
   204   SpecialPowers.exactGC(null, continueToNextStep);
   205 }
   207 function setTimeout(fun, timeout) {
   208   let timer = Components.classes["@mozilla.org/timer;1"]
   209                         .createInstance(Components.interfaces.nsITimer);
   210   var event = {
   211     notify: function (timer) {
   212       fun();
   213     }
   214   };
   215   timer.initWithCallback(event, timeout,
   216                          Components.interfaces.nsITimer.TYPE_ONE_SHOT);
   217   return timer;
   218 }
   220 var SpecialPowers = {
   221   isMainProcess: function() {
   222     return Components.classes["@mozilla.org/xre/app-info;1"]
   223                      .getService(Components.interfaces.nsIXULRuntime)
   224                      .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
   225   },
   226   notifyObservers: function(subject, topic, data) {
   227     var obsvc = Cc['@mozilla.org/observer-service;1']
   228                    .getService(Ci.nsIObserverService);
   229     obsvc.notifyObservers(subject, topic, data);
   230   },
   231   notifyObserversInParentProcess: function(subject, topic, data) {
   232     if (subject) {
   233       throw new Error("Can't send subject to another process!");
   234     }
   235     return this.notifyObservers(subject, topic, data);
   236   },
   237   getBoolPref: function(prefName) {
   238     return this._getPrefs().getBoolPref(prefName);
   239   },
   240   setBoolPref: function(prefName, value) {
   241     this._getPrefs().setBoolPref(prefName, value);
   242   },
   243   setIntPref: function(prefName, value) {
   244     this._getPrefs().setIntPref(prefName, value);
   245   },
   246   clearUserPref: function(prefName) {
   247     this._getPrefs().clearUserPref(prefName);
   248   },
   249   // Copied (and slightly adjusted) from specialpowersAPI.js
   250   exactGC: function(win, callback) {
   251     let count = 0;
   253     function doPreciseGCandCC() {
   254       function scheduledGCCallback() {
   255         Components.utils.forceCC();
   257         if (++count < 2) {
   258           doPreciseGCandCC();
   259         } else {
   260           callback();
   261         }
   262       }
   264       Components.utils.schedulePreciseGC(scheduledGCCallback);
   265     }
   267     doPreciseGCandCC();
   268   },
   270   _getPrefs: function() {
   271     var prefService =
   272       Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);
   273     return prefService.getBranch(null);
   274   }
   275 };

mercurial