dom/indexedDB/test/unit/test_temporary_storage.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

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
michael@0 8 function testSteps()
michael@0 9 {
michael@0 10 const name = this.window ? window.location.pathname : "Splendid Test";
michael@0 11
michael@0 12 const urls = [
michael@0 13 { url: "http://www.alpha.com", flags: [true, true, false, false] },
michael@0 14 { url: "http://www.beta.com", flags: [true, false, false, false] },
michael@0 15 { url: "http://www.gamma.com", flags: [true, true, false, false] },
michael@0 16 { url: "http://www.delta.com", flags: [true, true, false, false] },
michael@0 17 { url: "http://www.epsilon.com", flags: [true, true, false, false] },
michael@0 18 { url: "http://www2.alpha.com", flags: [true, true, false, false] },
michael@0 19 { url: "http://www2.beta.com", flags: [true, true, false, false] },
michael@0 20 { url: "http://www2.gamma.com", flags: [true, true, true, false] },
michael@0 21 { url: "http://www2.delta.com", flags: [true, true, true, true] },
michael@0 22 { url: "http://www2.epsilon.com", flags: [true, true, true, true] },
michael@0 23 { url: "http://joe.blog.alpha.com", flags: [true, true, true, true] },
michael@0 24 { url: "http://joe.blog.beta.com", flags: [true, true, true, true] },
michael@0 25 { url: "http://joe.blog.gamma.com", flags: [true, true, true, true] },
michael@0 26 { url: "http://joe.blog.delta.com", flags: [true, true, true, true] },
michael@0 27 { url: "http://joe.blog.epsilon.com", flags: [true, true, true, true] },
michael@0 28 { url: "http://www.rudolf.org", flags: [true, true, true, true] },
michael@0 29 { url: "http://www.pauline.org", flags: [true, true, true, true] },
michael@0 30 { url: "http://www.marie.org", flags: [true, true, true, true] },
michael@0 31 { url: "http://www.john.org", flags: [true, true, true, true] },
michael@0 32 { url: "http://www.ema.org", flags: [true, true, true, true] },
michael@0 33 { url: "http://www.trigger.com", flags: [false, true, true, true] }
michael@0 34 ];
michael@0 35 const lastIndex = urls.length - 1;
michael@0 36 const lastUrl = urls[lastIndex].url;
michael@0 37
michael@0 38 let quotaManager =
michael@0 39 Components.classes["@mozilla.org/dom/quota/manager;1"]
michael@0 40 .getService(Components.interfaces.nsIQuotaManager);
michael@0 41
michael@0 42 let ioService = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 43 .getService(Components.interfaces.nsIIOService);
michael@0 44
michael@0 45 let dbSize = 0;
michael@0 46
michael@0 47 let databases = [];
michael@0 48
michael@0 49 function setLimit(limit) {
michael@0 50 if (limit) {
michael@0 51 SpecialPowers.setIntPref("dom.quotaManager.temporaryStorage.fixedLimit",
michael@0 52 limit);
michael@0 53 return;
michael@0 54 }
michael@0 55 SpecialPowers.clearUserPref("dom.quotaManager.temporaryStorage.fixedLimit");
michael@0 56 }
michael@0 57
michael@0 58 function getPrincipal(url) {
michael@0 59 let uri = ioService.newURI(url, null, null);
michael@0 60 return Components.classes["@mozilla.org/scriptsecuritymanager;1"]
michael@0 61 .getService(Components.interfaces.nsIScriptSecurityManager)
michael@0 62 .getNoAppCodebasePrincipal(uri);
michael@0 63 }
michael@0 64
michael@0 65 function getUsageForUrl(url, usageHandler) {
michael@0 66 let uri = ioService.newURI(url, null, null);
michael@0 67 function callback(uri, usage, fileUsage) {
michael@0 68 usageHandler(usage, fileUsage);
michael@0 69 }
michael@0 70 quotaManager.getUsageForURI(uri, callback);
michael@0 71 }
michael@0 72
michael@0 73 function grabUsageAndContinueHandler(usage, fileUsage) {
michael@0 74 testGenerator.send(usage);
michael@0 75 }
michael@0 76
michael@0 77 function checkUsage(stageIndex) {
michael@0 78 let handledIndex = 0;
michael@0 79
michael@0 80 function usageHandler(usage, fileUsage) {
michael@0 81 if (urls[handledIndex].flags[stageIndex - 1]) {
michael@0 82 ok(usage > 0, "Correct usage");
michael@0 83 }
michael@0 84 else {
michael@0 85 ok(usage == 0, "Correct usage");
michael@0 86 }
michael@0 87 if (++handledIndex == urls.length) {
michael@0 88 continueToNextStep();
michael@0 89 }
michael@0 90 }
michael@0 91
michael@0 92 for (let i = 0; i < urls.length; i++) {
michael@0 93 getUsageForUrl(urls[i].url, usageHandler);
michael@0 94 }
michael@0 95 }
michael@0 96
michael@0 97 // Enable clear() and test()
michael@0 98 let testingEnabled =
michael@0 99 SpecialPowers.getBoolPref("dom.quotaManager.testing");
michael@0 100 SpecialPowers.setBoolPref("dom.quotaManager.testing", true)
michael@0 101
michael@0 102 // Calibration
michael@0 103 let request = indexedDB.openForPrincipal(getPrincipal(lastUrl), name,
michael@0 104 { storage: "temporary" });
michael@0 105 request.onerror = errorHandler;
michael@0 106 request.onsuccess = grabEventAndContinueHandler;
michael@0 107 let event = yield undefined;
michael@0 108
michael@0 109 getUsageForUrl(lastUrl, grabUsageAndContinueHandler);
michael@0 110 dbSize = yield undefined;
michael@0 111
michael@0 112 setLimit(lastIndex * dbSize / 1024);
michael@0 113 quotaManager.clear();
michael@0 114
michael@0 115 // Stage 1
michael@0 116 for (let i = 0; i < lastIndex; i++) {
michael@0 117 let data = urls[i];
michael@0 118
michael@0 119 request = indexedDB.openForPrincipal(getPrincipal(data.url), name,
michael@0 120 { storage: "temporary" });
michael@0 121 request.onerror = errorHandler;
michael@0 122 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 123 request.onsuccess = grabEventAndContinueHandler;
michael@0 124 event = yield undefined;
michael@0 125
michael@0 126 is(event.type, "upgradeneeded", "Got correct event type");
michael@0 127
michael@0 128 let db = event.target.result;
michael@0 129 db.createObjectStore("foo", { });
michael@0 130
michael@0 131 event = yield undefined;
michael@0 132
michael@0 133 is(event.type, "success", "Got correct event type");
michael@0 134
michael@0 135 databases.push(event.target.result);
michael@0 136 }
michael@0 137
michael@0 138 request = indexedDB.openForPrincipal(getPrincipal(lastUrl), name,
michael@0 139 { storage: "temporary" });
michael@0 140 request.addEventListener("error", new ExpectError("QuotaExceededError"));
michael@0 141 request.onsuccess = unexpectedSuccessHandler;
michael@0 142 event = yield undefined;
michael@0 143
michael@0 144 checkUsage(1);
michael@0 145 yield undefined;
michael@0 146
michael@0 147 // Stage 2
michael@0 148 for (let i = 1; i < urls.length; i++) {
michael@0 149 databases[i] = null;
michael@0 150
michael@0 151 scheduleGC();
michael@0 152 yield undefined;
michael@0 153
michael@0 154 // The origin access time is set to the current system time when the first
michael@0 155 // database for an origin is registered or the last one is unregistered.
michael@0 156 // The registration happens when the database object is being created and
michael@0 157 // the unregistration when it is unlinked/garbage collected.
michael@0 158 // Some older windows systems have the system time limited to a maximum
michael@0 159 // resolution of 10 or 15 milliseconds, so without a pause here we would
michael@0 160 // end up with origins with the same access time which would cause random
michael@0 161 // failures.
michael@0 162 setTimeout(function() { testGenerator.next(); }, 20);
michael@0 163 yield undefined;
michael@0 164 }
michael@0 165
michael@0 166 request = indexedDB.openForPrincipal(getPrincipal(lastUrl), name,
michael@0 167 { storage: "temporary" });
michael@0 168 request.onerror = errorHandler;
michael@0 169 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 170 request.onsuccess = grabEventAndContinueHandler;
michael@0 171 event = yield undefined;
michael@0 172
michael@0 173 is(event.type, "upgradeneeded", "Got correct event type");
michael@0 174
michael@0 175 let db = event.target.result;
michael@0 176 db.createObjectStore("foo", { });
michael@0 177
michael@0 178 event = yield undefined;
michael@0 179
michael@0 180 is(event.type, "success", "Got correct event type");
michael@0 181
michael@0 182 checkUsage(2);
michael@0 183 yield undefined;
michael@0 184
michael@0 185 // Stage 3
michael@0 186 setLimit(14 * dbSize / 1024);
michael@0 187 quotaManager.reset();
michael@0 188
michael@0 189 request = indexedDB.openForPrincipal(getPrincipal(lastUrl), name,
michael@0 190 { storage: "temporary" });
michael@0 191 request.onerror = errorHandler;
michael@0 192 request.onsuccess = grabEventAndContinueHandler;
michael@0 193 event = yield undefined;
michael@0 194
michael@0 195 is(event.type, "success", "Got correct event type");
michael@0 196
michael@0 197 let db = event.target.result;
michael@0 198
michael@0 199 checkUsage(3);
michael@0 200 yield undefined;
michael@0 201
michael@0 202 // Stage 4
michael@0 203 let trans = db.transaction(["foo"], "readwrite");
michael@0 204
michael@0 205 let blob = Blob(["bar"]);
michael@0 206 request = trans.objectStore("foo").add(blob, 42);
michael@0 207 request.onerror = errorHandler;
michael@0 208 request.onsuccess = grabEventAndContinueHandler;
michael@0 209 event = yield undefined;
michael@0 210
michael@0 211 trans.oncomplete = grabEventAndContinueHandler;
michael@0 212 event = yield undefined;
michael@0 213
michael@0 214 checkUsage(4);
michael@0 215 yield undefined;
michael@0 216
michael@0 217 // Cleanup
michael@0 218 setLimit();
michael@0 219 quotaManager.reset();
michael@0 220
michael@0 221 SpecialPowers.setBoolPref("dom.quotaManager.testing", testingEnabled);
michael@0 222
michael@0 223 finishTest();
michael@0 224 yield undefined;
michael@0 225 }

mercurial