2011-12-16 11:34:24 +04:00
|
|
|
/**
|
|
|
|
* Any copyright is dedicated to the Public Domain.
|
|
|
|
* http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
*/
|
|
|
|
|
|
|
|
var bufferCache = [];
|
|
|
|
var utils = SpecialPowers.getDOMWindowUtils(window);
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
function getBuffer(size)
|
|
|
|
{
|
|
|
|
let buffer = new ArrayBuffer(size);
|
|
|
|
is(buffer.byteLength, size, "Correct byte length");
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomBuffer(size)
|
|
|
|
{
|
|
|
|
let buffer = getBuffer(size);
|
|
|
|
let view = new Uint8Array(buffer);
|
|
|
|
for (let i = 0; i < size; i++) {
|
|
|
|
view[i] = parseInt(Math.random() * 255)
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2012-05-18 01:53:44 +04:00
|
|
|
function getView(size)
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
|
|
|
let buffer = new ArrayBuffer(size);
|
2012-05-18 01:53:44 +04:00
|
|
|
let view = new Uint8Array(buffer);
|
2011-12-16 11:34:24 +04:00
|
|
|
is(buffer.byteLength, size, "Correct byte length");
|
2012-05-18 01:53:44 +04:00
|
|
|
return view;
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-05-18 01:53:44 +04:00
|
|
|
function getRandomView(size)
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
2012-05-18 01:53:44 +04:00
|
|
|
let view = getView(size);
|
2011-12-16 11:34:24 +04:00
|
|
|
for (let i = 0; i < size; i++) {
|
|
|
|
view[i] = parseInt(Math.random() * 255)
|
|
|
|
}
|
2012-05-18 01:53:44 +04:00
|
|
|
return view;
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function compareBuffers(buffer1, buffer2)
|
|
|
|
{
|
|
|
|
if (buffer1.byteLength != buffer2.byteLength) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let view1 = new Uint8Array(buffer1);
|
|
|
|
let view2 = new Uint8Array(buffer2);
|
|
|
|
for (let i = 0; i < buffer1.byteLength; i++) {
|
|
|
|
if (view1[i] != view2[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-18 01:53:44 +04:00
|
|
|
function getBlob(type, view)
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
2014-06-27 10:05:25 +04:00
|
|
|
return new Blob([view], {type: type});
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-05-18 01:53:44 +04:00
|
|
|
function getFile(name, type, view)
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
2014-06-27 10:05:25 +04:00
|
|
|
return new File([view], name, {type: type});
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomBlob(size)
|
|
|
|
{
|
2012-05-18 01:53:44 +04:00
|
|
|
return getBlob("binary/random", getRandomView(size));
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomFile(name, size)
|
|
|
|
{
|
2012-05-18 01:53:44 +04:00
|
|
|
return getFile(name, "binary/random", getRandomView(size));
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getNullBlob(size)
|
|
|
|
{
|
2012-05-18 01:53:44 +04:00
|
|
|
return getBlob("binary/null", getView(size));
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getNullFile(name, size)
|
|
|
|
{
|
2012-05-18 01:53:44 +04:00
|
|
|
return getFile(name, "binary/null", getView(size));
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function verifyBuffers(buffer1, buffer2)
|
|
|
|
{
|
|
|
|
ok(compareBuffers(buffer1, buffer2), "Correct blob data");
|
|
|
|
}
|
|
|
|
|
|
|
|
function verifyBlob(blob1, blob2, fileId, blobReadHandler)
|
|
|
|
{
|
|
|
|
is(blob1 instanceof Components.interfaces.nsIDOMBlob, true,
|
|
|
|
"Instance of nsIDOMBlob");
|
2015-05-18 16:52:26 +03:00
|
|
|
is(blob1 instanceof File, blob2 instanceof File,
|
|
|
|
"Instance of DOM File");
|
2011-12-16 11:34:24 +04:00
|
|
|
is(blob1.size, blob2.size, "Correct size");
|
|
|
|
is(blob1.type, blob2.type, "Correct type");
|
2015-05-18 16:52:26 +03:00
|
|
|
if (blob2 instanceof File) {
|
2011-12-16 11:34:24 +04:00
|
|
|
is(blob1.name, blob2.name, "Correct name");
|
|
|
|
}
|
|
|
|
is(utils.getFileId(blob1), fileId, "Correct file id");
|
|
|
|
|
|
|
|
let buffer1;
|
|
|
|
let buffer2;
|
|
|
|
|
|
|
|
for (let i = 0; i < bufferCache.length; i++) {
|
|
|
|
if (bufferCache[i].blob == blob2) {
|
|
|
|
buffer2 = bufferCache[i].buffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!buffer2) {
|
|
|
|
let reader = new FileReader();
|
|
|
|
reader.readAsArrayBuffer(blob2);
|
|
|
|
reader.onload = function(event) {
|
|
|
|
buffer2 = event.target.result;
|
|
|
|
bufferCache.push({ blob: blob2, buffer: buffer2 });
|
|
|
|
if (buffer1) {
|
|
|
|
verifyBuffers(buffer1, buffer2);
|
|
|
|
if (blobReadHandler) {
|
|
|
|
blobReadHandler();
|
2011-12-20 15:23:27 +04:00
|
|
|
}
|
|
|
|
else {
|
2011-12-16 11:34:24 +04:00
|
|
|
testGenerator.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let reader = new FileReader();
|
|
|
|
reader.readAsArrayBuffer(blob1);
|
|
|
|
reader.onload = function(event) {
|
|
|
|
buffer1 = event.target.result;
|
|
|
|
if (buffer2) {
|
|
|
|
verifyBuffers(buffer1, buffer2);
|
|
|
|
if (blobReadHandler) {
|
|
|
|
blobReadHandler();
|
2011-12-20 15:23:27 +04:00
|
|
|
}
|
|
|
|
else {
|
2011-12-16 11:34:24 +04:00
|
|
|
testGenerator.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function verifyBlobArray(blobs1, blobs2, expectedFileIds)
|
|
|
|
{
|
|
|
|
is(blobs1 instanceof Array, true, "Got an array object");
|
|
|
|
is(blobs1.length, blobs2.length, "Correct length");
|
|
|
|
|
|
|
|
if (!blobs1.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let verifiedCount = 0;
|
|
|
|
|
|
|
|
function blobReadHandler() {
|
|
|
|
if (++verifiedCount == blobs1.length) {
|
|
|
|
testGenerator.next();
|
2011-12-20 15:23:27 +04:00
|
|
|
}
|
|
|
|
else {
|
2011-12-16 11:34:24 +04:00
|
|
|
verifyBlob(blobs1[verifiedCount], blobs2[verifiedCount],
|
|
|
|
expectedFileIds[verifiedCount], blobReadHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyBlob(blobs1[verifiedCount], blobs2[verifiedCount],
|
|
|
|
expectedFileIds[verifiedCount], blobReadHandler);
|
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
function verifyMutableFile(mutableFile1, file2)
|
|
|
|
{
|
|
|
|
ok(mutableFile1 instanceof IDBMutableFile, "Instance of IDBMutableFile");
|
|
|
|
is(mutableFile1.name, file2.name, "Correct name");
|
|
|
|
is(mutableFile1.type, file2.type, "Correct type");
|
|
|
|
executeSoon(function() {
|
|
|
|
testGenerator.next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-22 12:44:24 +03:00
|
|
|
function grabFileUsageAndContinueHandler(request)
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
2015-11-22 12:44:24 +03:00
|
|
|
testGenerator.send(request.fileUsage);
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getUsage(usageHandler)
|
|
|
|
{
|
2015-11-22 12:44:24 +03:00
|
|
|
let qms = SpecialPowers.Services.qms;
|
|
|
|
let principal = SpecialPowers.wrap(document).nodePrincipal;
|
|
|
|
let cb = SpecialPowers.wrapCallback(usageHandler);
|
|
|
|
qms.getUsageForPrincipal(principal, cb);
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-06-03 20:33:52 +04:00
|
|
|
function getFileId(file)
|
|
|
|
{
|
|
|
|
return utils.getFileId(file);
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
function getFilePath(file)
|
|
|
|
{
|
|
|
|
return utils.getFilePath(file);
|
|
|
|
}
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
function hasFileInfo(name, id)
|
|
|
|
{
|
|
|
|
return utils.getFileReferences(name, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFileRefCount(name, id)
|
|
|
|
{
|
|
|
|
let count = {};
|
2013-09-11 08:18:36 +04:00
|
|
|
utils.getFileReferences(name, id, null, count);
|
2011-12-16 11:34:24 +04:00
|
|
|
return count.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFileDBRefCount(name, id)
|
|
|
|
{
|
|
|
|
let count = {};
|
2013-09-11 08:18:36 +04:00
|
|
|
utils.getFileReferences(name, id, null, {}, count);
|
2011-12-16 11:34:24 +04:00
|
|
|
return count.value;
|
|
|
|
}
|
2015-06-30 15:59:27 +03:00
|
|
|
|
|
|
|
function flushPendingFileDeletions()
|
|
|
|
{
|
|
|
|
utils.flushPendingFileDeletions();
|
|
|
|
}
|