1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
<html>
<head>
<title>Blob Creation & Slicing</title>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript">
// We create < 3000 bytes of data, as that is the max for the browsertest.
var MAX_BYTES = 3000;
// Number of blobs constructed with raw data.
var NUM_RAW_BLOBS = 100;
// Number of blobs we create by slicing the raw data blobs.
var NUM_SLICES = 100;
// Number of blobs we create by sandwiching sliced blobs in between new data.
var NUM_SANDWICHES = 100;
var totalSize = 0;
var generatePsuedoRandomUint8Array = function(length) {
var array = new Uint8Array(length);
for (let i = 0; i < length; ++i) {
array[i] = (17 + 23 * i) & 0xFF;
}
return array;
}
var test = function() {
var blobs = [];
// This should cause us to go straight to file.
var savedToDiskDataSize = 190;
var savedToDiskData = generatePsuedoRandomUint8Array(savedToDiskDataSize);
// This should require multiple shared memory segments.
var sharedMemoryDataSize = 15;
var sharedMemoryData = generatePsuedoRandomUint8Array(sharedMemoryDataSize);
var sandwichDataSize = 7;
// This should fit in IPC.
var ipcDataSize = 2;
var ipcData = generatePsuedoRandomUint8Array(ipcDataSize);
var expectedBlobData = [];
for (let i = 0; i < NUM_RAW_BLOBS; ++i) {
let data = [];
if (i % 5 == 0) {
data.push(sharedMemoryData);
} else if (i % 13 == 0) {
data.push(savedToDiskData);
} else {
data.push(ipcData);
}
expectedBlobData.push(data[data.length - 1]);
let blob = new Blob(data, { content_type: "text/plain" });
blobs.push(blob);
totalSize += blob.size;
}
for (let i = 0; i < NUM_SLICES; ++i) {
let origSize;
let origData;
let rawIndex = i % NUM_RAW_BLOBS;
if (rawIndex % 5 == 0) {
origSize = sharedMemoryDataSize;
origData = sharedMemoryData;
} else if (rawIndex % 13 == 0) {
origSize = savedToDiskDataSize;
origData = savedToDiskData;
} else {
origSize = ipcDataSize;
origData = ipcData;
}
let blob;
let expectedData;
if (i % 2 == 0) {
blob = blobs[i].slice(origSize / 2);
expectedData = origData.slice(origSize / 2);
} else {
blob = blobs[i].slice(0, origSize / 2);
expectedData = origData.slice(0, origSize / 2);
}
expectedBlobData.push(expectedData);
blobs.push(blob);
}
var getBytes = function(string) {
var bytes = [];
for (let i = 0; i < string.length; ++i) {
bytes.push(string.charCodeAt(i));
}
return bytes;
}
for (let i = 0; i < NUM_SANDWICHES; ++i) {
let sliceIndex = NUM_RAW_BLOBS + (i % NUM_SLICES);
let slicedDataSize = expectedBlobData[sliceIndex].length;
blobs.push(new Blob(['pre', blobs[sliceIndex], 'post'], { content_type: "text/plain" }));
totalSize += sandwichDataSize;
let expectedData = new Uint8Array(sandwichDataSize + slicedDataSize);
expectedData.set(getBytes("pre"), 0);
expectedData.set(expectedBlobData[sliceIndex], 3);
expectedData.set(getBytes("post"), 3 + slicedDataSize);
expectedBlobData.push(expectedData);
}
shouldBeTrue("totalSize <= MAX_BYTES");
var numRead = 0;
for (let i = 0; i < blobs.length; i++) {
(function(index, d) {
var reader = new FileReader();
reader.onloadend = function(e) {
if (reader.error) {
fail('Error when reading blob ' + index + ': ' + reader.error);
return;
}
numRead++;
debug('Finished reading blob ' + index);
shouldBe('event.target.result.byteLength', d.length + '');
shouldBe('new Uint8Array(event.target.result)',
'[' + d + ']');
if (numRead >= blobs.length) {
done('Done!');
}
}
return reader;
})(i, expectedBlobData[i]).readAsArrayBuffer(blobs[i]);
}
};
</script>
</head>
<body onLoad="test()">
<div id="status">Starting...<br/></div>
</body>
</html>
|