1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/indexedDB/test/unit/head.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,275 @@ 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 +const { 'classes': Cc, 'interfaces': Ci, 'utils': Cu } = Components; 1.10 + 1.11 +const DOMException = Ci.nsIDOMDOMException; 1.12 + 1.13 +function is(a, b, msg) { 1.14 + dump("is(" + a + ", " + b + ", \"" + msg + "\")"); 1.15 + do_check_eq(a, b, Components.stack.caller); 1.16 +} 1.17 + 1.18 +function ok(cond, msg) { 1.19 + dump("ok(" + cond + ", \"" + msg + "\")"); 1.20 + do_check_true(!!cond, Components.stack.caller); 1.21 +} 1.22 + 1.23 +function isnot(a, b, msg) { 1.24 + dump("isnot(" + a + ", " + b + ", \"" + msg + "\")"); 1.25 + do_check_neq(a, b, Components.stack.caller); 1.26 +} 1.27 + 1.28 +function executeSoon(fun) { 1.29 + do_execute_soon(fun); 1.30 +} 1.31 + 1.32 +function todo(condition, name, diag) { 1.33 + dump("TODO: ", diag); 1.34 +} 1.35 + 1.36 +function info(name, message) { 1.37 + do_print(name); 1.38 +} 1.39 + 1.40 +function run_test() { 1.41 + runTest(); 1.42 +}; 1.43 + 1.44 +if (!this.runTest) { 1.45 + this.runTest = function() 1.46 + { 1.47 + // XPCShell does not get a profile by default. 1.48 + do_get_profile(); 1.49 + 1.50 + enableExperimental(); 1.51 + 1.52 + Cu.importGlobalProperties(["indexedDB"]); 1.53 + 1.54 + do_test_pending(); 1.55 + testGenerator.next(); 1.56 + } 1.57 +} 1.58 + 1.59 +function finishTest() 1.60 +{ 1.61 + resetExperimental(); 1.62 + SpecialPowers.notifyObserversInParentProcess(null, "disk-space-watcher", 1.63 + "free"); 1.64 + 1.65 + do_execute_soon(function(){ 1.66 + testGenerator.close(); 1.67 + do_test_finished(); 1.68 + }) 1.69 +} 1.70 + 1.71 +function grabEventAndContinueHandler(event) 1.72 +{ 1.73 + testGenerator.send(event); 1.74 +} 1.75 + 1.76 +function continueToNextStep() 1.77 +{ 1.78 + do_execute_soon(function() { 1.79 + testGenerator.next(); 1.80 + }); 1.81 +} 1.82 + 1.83 +function errorHandler(event) 1.84 +{ 1.85 + dump("indexedDB error: " + event.target.error.name); 1.86 + do_check_true(false); 1.87 + finishTest(); 1.88 +} 1.89 + 1.90 +function unexpectedSuccessHandler() 1.91 +{ 1.92 + do_check_true(false); 1.93 + finishTest(); 1.94 +} 1.95 + 1.96 +function expectedErrorHandler(name) 1.97 +{ 1.98 + return function(event) { 1.99 + do_check_eq(event.type, "error"); 1.100 + do_check_eq(event.target.error.name, name); 1.101 + event.preventDefault(); 1.102 + grabEventAndContinueHandler(event); 1.103 + }; 1.104 +} 1.105 + 1.106 +function ExpectError(name) 1.107 +{ 1.108 + this._name = name; 1.109 +} 1.110 +ExpectError.prototype = { 1.111 + handleEvent: function(event) 1.112 + { 1.113 + do_check_eq(event.type, "error"); 1.114 + do_check_eq(this._name, event.target.error.name); 1.115 + event.preventDefault(); 1.116 + grabEventAndContinueHandler(event); 1.117 + } 1.118 +}; 1.119 + 1.120 +function continueToNextStepSync() 1.121 +{ 1.122 + testGenerator.next(); 1.123 +} 1.124 + 1.125 +function compareKeys(k1, k2) { 1.126 + let t = typeof k1; 1.127 + if (t != typeof k2) 1.128 + return false; 1.129 + 1.130 + if (t !== "object") 1.131 + return k1 === k2; 1.132 + 1.133 + if (k1 instanceof Date) { 1.134 + return (k2 instanceof Date) && 1.135 + k1.getTime() === k2.getTime(); 1.136 + } 1.137 + 1.138 + if (k1 instanceof Array) { 1.139 + if (!(k2 instanceof Array) || 1.140 + k1.length != k2.length) 1.141 + return false; 1.142 + 1.143 + for (let i = 0; i < k1.length; ++i) { 1.144 + if (!compareKeys(k1[i], k2[i])) 1.145 + return false; 1.146 + } 1.147 + 1.148 + return true; 1.149 + } 1.150 + 1.151 + return false; 1.152 +} 1.153 + 1.154 +function addPermission(permission, url) 1.155 +{ 1.156 + throw "addPermission"; 1.157 +} 1.158 + 1.159 +function removePermission(permission, url) 1.160 +{ 1.161 + throw "removePermission"; 1.162 +} 1.163 + 1.164 +function setQuota(quota) 1.165 +{ 1.166 + throw "setQuota"; 1.167 +} 1.168 + 1.169 +function allowIndexedDB(url) 1.170 +{ 1.171 + throw "allowIndexedDB"; 1.172 +} 1.173 + 1.174 +function disallowIndexedDB(url) 1.175 +{ 1.176 + throw "disallowIndexedDB"; 1.177 +} 1.178 + 1.179 +function allowUnlimitedQuota(url) 1.180 +{ 1.181 + throw "allowUnlimitedQuota"; 1.182 +} 1.183 + 1.184 +function disallowUnlimitedQuota(url) 1.185 +{ 1.186 + throw "disallowUnlimitedQuota"; 1.187 +} 1.188 + 1.189 +function enableExperimental() 1.190 +{ 1.191 + SpecialPowers.setBoolPref("dom.indexedDB.experimental", true); 1.192 +} 1.193 + 1.194 +function resetExperimental() 1.195 +{ 1.196 + SpecialPowers.clearUserPref("dom.indexedDB.experimental"); 1.197 +} 1.198 + 1.199 +function gc() 1.200 +{ 1.201 + Components.utils.forceGC(); 1.202 + Components.utils.forceCC(); 1.203 +} 1.204 + 1.205 +function scheduleGC() 1.206 +{ 1.207 + SpecialPowers.exactGC(null, continueToNextStep); 1.208 +} 1.209 + 1.210 +function setTimeout(fun, timeout) { 1.211 + let timer = Components.classes["@mozilla.org/timer;1"] 1.212 + .createInstance(Components.interfaces.nsITimer); 1.213 + var event = { 1.214 + notify: function (timer) { 1.215 + fun(); 1.216 + } 1.217 + }; 1.218 + timer.initWithCallback(event, timeout, 1.219 + Components.interfaces.nsITimer.TYPE_ONE_SHOT); 1.220 + return timer; 1.221 +} 1.222 + 1.223 +var SpecialPowers = { 1.224 + isMainProcess: function() { 1.225 + return Components.classes["@mozilla.org/xre/app-info;1"] 1.226 + .getService(Components.interfaces.nsIXULRuntime) 1.227 + .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; 1.228 + }, 1.229 + notifyObservers: function(subject, topic, data) { 1.230 + var obsvc = Cc['@mozilla.org/observer-service;1'] 1.231 + .getService(Ci.nsIObserverService); 1.232 + obsvc.notifyObservers(subject, topic, data); 1.233 + }, 1.234 + notifyObserversInParentProcess: function(subject, topic, data) { 1.235 + if (subject) { 1.236 + throw new Error("Can't send subject to another process!"); 1.237 + } 1.238 + return this.notifyObservers(subject, topic, data); 1.239 + }, 1.240 + getBoolPref: function(prefName) { 1.241 + return this._getPrefs().getBoolPref(prefName); 1.242 + }, 1.243 + setBoolPref: function(prefName, value) { 1.244 + this._getPrefs().setBoolPref(prefName, value); 1.245 + }, 1.246 + setIntPref: function(prefName, value) { 1.247 + this._getPrefs().setIntPref(prefName, value); 1.248 + }, 1.249 + clearUserPref: function(prefName) { 1.250 + this._getPrefs().clearUserPref(prefName); 1.251 + }, 1.252 + // Copied (and slightly adjusted) from specialpowersAPI.js 1.253 + exactGC: function(win, callback) { 1.254 + let count = 0; 1.255 + 1.256 + function doPreciseGCandCC() { 1.257 + function scheduledGCCallback() { 1.258 + Components.utils.forceCC(); 1.259 + 1.260 + if (++count < 2) { 1.261 + doPreciseGCandCC(); 1.262 + } else { 1.263 + callback(); 1.264 + } 1.265 + } 1.266 + 1.267 + Components.utils.schedulePreciseGC(scheduledGCCallback); 1.268 + } 1.269 + 1.270 + doPreciseGCandCC(); 1.271 + }, 1.272 + 1.273 + _getPrefs: function() { 1.274 + var prefService = 1.275 + Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService); 1.276 + return prefService.getBranch(null); 1.277 + } 1.278 +};