dom/indexedDB/test/file.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 const DEFAULT_QUOTA = 50 * 1024 * 1024;
michael@0 7
michael@0 8 var bufferCache = [];
michael@0 9 var utils = SpecialPowers.getDOMWindowUtils(window);
michael@0 10
michael@0 11 if (!SpecialPowers.isMainProcess()) {
michael@0 12 window.runTest = function() {
michael@0 13 todo(false, "Test disabled in child processes, for now");
michael@0 14 finishTest();
michael@0 15 }
michael@0 16 }
michael@0 17
michael@0 18 function getView(size)
michael@0 19 {
michael@0 20 let buffer = new ArrayBuffer(size);
michael@0 21 let view = new Uint8Array(buffer);
michael@0 22 is(buffer.byteLength, size, "Correct byte length");
michael@0 23 return view;
michael@0 24 }
michael@0 25
michael@0 26 function getRandomView(size)
michael@0 27 {
michael@0 28 let view = getView(size);
michael@0 29 for (let i = 0; i < size; i++) {
michael@0 30 view[i] = parseInt(Math.random() * 255)
michael@0 31 }
michael@0 32 return view;
michael@0 33 }
michael@0 34
michael@0 35 function compareBuffers(buffer1, buffer2)
michael@0 36 {
michael@0 37 if (buffer1.byteLength != buffer2.byteLength) {
michael@0 38 return false;
michael@0 39 }
michael@0 40 let view1 = new Uint8Array(buffer1);
michael@0 41 let view2 = new Uint8Array(buffer2);
michael@0 42 for (let i = 0; i < buffer1.byteLength; i++) {
michael@0 43 if (view1[i] != view2[i]) {
michael@0 44 return false;
michael@0 45 }
michael@0 46 }
michael@0 47 return true;
michael@0 48 }
michael@0 49
michael@0 50 function getBlob(type, view)
michael@0 51 {
michael@0 52 return SpecialPowers.unwrap(utils.getBlob([view], {type: type}));
michael@0 53 }
michael@0 54
michael@0 55 function getFile(name, type, view)
michael@0 56 {
michael@0 57 return SpecialPowers.unwrap(utils.getFile(name, [view], {type: type}));
michael@0 58 }
michael@0 59
michael@0 60 function getRandomBlob(size)
michael@0 61 {
michael@0 62 return getBlob("binary/random", getRandomView(size));
michael@0 63 }
michael@0 64
michael@0 65 function getRandomFile(name, size)
michael@0 66 {
michael@0 67 return getFile(name, "binary/random", getRandomView(size));
michael@0 68 }
michael@0 69
michael@0 70 function getNullBlob(size)
michael@0 71 {
michael@0 72 return getBlob("binary/null", getView(size));
michael@0 73 }
michael@0 74
michael@0 75 function getNullFile(name, size)
michael@0 76 {
michael@0 77 return getFile(name, "binary/null", getView(size));
michael@0 78 }
michael@0 79
michael@0 80 function verifyBuffers(buffer1, buffer2)
michael@0 81 {
michael@0 82 ok(compareBuffers(buffer1, buffer2), "Correct blob data");
michael@0 83 }
michael@0 84
michael@0 85 function verifyBlob(blob1, blob2, fileId, blobReadHandler)
michael@0 86 {
michael@0 87 is(blob1 instanceof Components.interfaces.nsIDOMBlob, true,
michael@0 88 "Instance of nsIDOMBlob");
michael@0 89 is(blob1 instanceof Components.interfaces.nsIDOMFile,
michael@0 90 blob2 instanceof Components.interfaces.nsIDOMFile,
michael@0 91 "Instance of nsIDOMFile");
michael@0 92 is(blob1.size, blob2.size, "Correct size");
michael@0 93 is(blob1.type, blob2.type, "Correct type");
michael@0 94 if (blob2 instanceof Components.interfaces.nsIDOMFile) {
michael@0 95 is(blob1.name, blob2.name, "Correct name");
michael@0 96 }
michael@0 97 is(utils.getFileId(blob1), fileId, "Correct file id");
michael@0 98
michael@0 99 let buffer1;
michael@0 100 let buffer2;
michael@0 101
michael@0 102 for (let i = 0; i < bufferCache.length; i++) {
michael@0 103 if (bufferCache[i].blob == blob2) {
michael@0 104 buffer2 = bufferCache[i].buffer;
michael@0 105 break;
michael@0 106 }
michael@0 107 }
michael@0 108
michael@0 109 if (!buffer2) {
michael@0 110 let reader = new FileReader();
michael@0 111 reader.readAsArrayBuffer(blob2);
michael@0 112 reader.onload = function(event) {
michael@0 113 buffer2 = event.target.result;
michael@0 114 bufferCache.push({ blob: blob2, buffer: buffer2 });
michael@0 115 if (buffer1) {
michael@0 116 verifyBuffers(buffer1, buffer2);
michael@0 117 if (blobReadHandler) {
michael@0 118 blobReadHandler();
michael@0 119 }
michael@0 120 else {
michael@0 121 testGenerator.next();
michael@0 122 }
michael@0 123 }
michael@0 124 }
michael@0 125 }
michael@0 126
michael@0 127 let reader = new FileReader();
michael@0 128 reader.readAsArrayBuffer(blob1);
michael@0 129 reader.onload = function(event) {
michael@0 130 buffer1 = event.target.result;
michael@0 131 if (buffer2) {
michael@0 132 verifyBuffers(buffer1, buffer2);
michael@0 133 if (blobReadHandler) {
michael@0 134 blobReadHandler();
michael@0 135 }
michael@0 136 else {
michael@0 137 testGenerator.next();
michael@0 138 }
michael@0 139 }
michael@0 140 }
michael@0 141 }
michael@0 142
michael@0 143 function verifyBlobArray(blobs1, blobs2, expectedFileIds)
michael@0 144 {
michael@0 145 is(blobs1 instanceof Array, true, "Got an array object");
michael@0 146 is(blobs1.length, blobs2.length, "Correct length");
michael@0 147
michael@0 148 if (!blobs1.length) {
michael@0 149 return;
michael@0 150 }
michael@0 151
michael@0 152 let verifiedCount = 0;
michael@0 153
michael@0 154 function blobReadHandler() {
michael@0 155 if (++verifiedCount == blobs1.length) {
michael@0 156 testGenerator.next();
michael@0 157 }
michael@0 158 else {
michael@0 159 verifyBlob(blobs1[verifiedCount], blobs2[verifiedCount],
michael@0 160 expectedFileIds[verifiedCount], blobReadHandler);
michael@0 161 }
michael@0 162 }
michael@0 163
michael@0 164 verifyBlob(blobs1[verifiedCount], blobs2[verifiedCount],
michael@0 165 expectedFileIds[verifiedCount], blobReadHandler);
michael@0 166 }
michael@0 167
michael@0 168 function grabFileUsageAndContinueHandler(usage, fileUsage)
michael@0 169 {
michael@0 170 testGenerator.send(fileUsage);
michael@0 171 }
michael@0 172
michael@0 173 function getUsage(usageHandler)
michael@0 174 {
michael@0 175 let comp = SpecialPowers.wrap(Components);
michael@0 176 let quotaManager = comp.classes["@mozilla.org/dom/quota/manager;1"]
michael@0 177 .getService(comp.interfaces.nsIQuotaManager);
michael@0 178
michael@0 179 // We need to pass a JS callback to getUsageForURI. However, that callback
michael@0 180 // takes an XPCOM URI object, which will cause us to throw when we wrap it
michael@0 181 // for the content compartment. So we need to define the function in a
michael@0 182 // privileged scope, which we do using a sandbox.
michael@0 183 var sysPrin = SpecialPowers.Services.scriptSecurityManager.getSystemPrincipal();
michael@0 184 var sb = new SpecialPowers.Cu.Sandbox(sysPrin);
michael@0 185 sb.usageHandler = usageHandler;
michael@0 186 var cb = SpecialPowers.Cu.evalInSandbox((function(uri, usage, fileUsage) {
michael@0 187 usageHandler(usage, fileUsage); }).toSource(), sb);
michael@0 188
michael@0 189 let uri = SpecialPowers.wrap(window).document.documentURIObject;
michael@0 190 quotaManager.getUsageForURI(uri, cb);
michael@0 191 }
michael@0 192
michael@0 193 function getFileId(file)
michael@0 194 {
michael@0 195 return utils.getFileId(file);
michael@0 196 }
michael@0 197
michael@0 198 function hasFileInfo(name, id)
michael@0 199 {
michael@0 200 return utils.getFileReferences(name, id);
michael@0 201 }
michael@0 202
michael@0 203 function getFileRefCount(name, id)
michael@0 204 {
michael@0 205 let count = {};
michael@0 206 utils.getFileReferences(name, id, null, count);
michael@0 207 return count.value;
michael@0 208 }
michael@0 209
michael@0 210 function getFileDBRefCount(name, id)
michael@0 211 {
michael@0 212 let count = {};
michael@0 213 utils.getFileReferences(name, id, null, {}, count);
michael@0 214 return count.value;
michael@0 215 }

mercurial