2011-05-20 10:18:45 -07:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<!--
|
|
|
|
https://bugzilla.mozilla.org/show_bug.cgi?id=648997
|
|
|
|
-->
|
|
|
|
<head>
|
|
|
|
<title>Test for Bug 648997</title>
|
|
|
|
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
|
|
|
|
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
|
|
|
<script type="text/javascript" src="fileutils.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=648997">Mozilla Bug 648997</a>
|
|
|
|
<p id="display"></p>
|
|
|
|
<div id="content" style="display: none">
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<pre id="test">
|
|
|
|
<script class="testbody" type="text/javascript;version=1.7">
|
2012-03-12 21:45:00 -07:00
|
|
|
"use strict";
|
2011-05-20 10:18:45 -07:00
|
|
|
// We're prefixing still
|
|
|
|
window.BlobBuilder = window.MozBlobBuilder;
|
|
|
|
|
|
|
|
/** Test for Bug 648997 **/
|
|
|
|
var blobBuilder = BlobBuilder();
|
|
|
|
ok(blobBuilder, "BlobBuilder should exist");
|
|
|
|
|
|
|
|
ok(blobBuilder.append, "BlobBuilder should have an append method");
|
|
|
|
ok(blobBuilder.getBlob, "BlobBuilder should have a getBlob method");
|
2011-07-11 19:42:02 -07:00
|
|
|
ok(blobBuilder.getFile, "BlobBuilder should have a getFile method");
|
2011-05-20 10:18:45 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
blobBuilder.append();
|
|
|
|
ok(false, "NOT REACHED");
|
|
|
|
} catch(e) {
|
|
|
|
ok(true, "an empty argument to append should throw");
|
|
|
|
}
|
|
|
|
|
2011-10-24 06:38:41 -07:00
|
|
|
blobBuilder.append(null);
|
|
|
|
// Yay we didn't crash.
|
|
|
|
|
2011-05-20 10:18:45 -07:00
|
|
|
blobBuilder.append("squiggle");
|
|
|
|
let blob1 = blobBuilder.getBlob();
|
2011-07-11 19:42:02 -07:00
|
|
|
ok(blob1 instanceof Blob, "getBlob should produce Blobs");
|
|
|
|
ok(!(blob1 instanceof File), "getBlob should not produce Files");
|
|
|
|
is(blob1.type, "", "getBlob with no argument should return Blob with empty type");
|
|
|
|
is(blob1.size, 8, "getBlob should return Blob with correct size");
|
|
|
|
|
2011-05-20 10:18:45 -07:00
|
|
|
blobBuilder.append("ohai");
|
2011-07-11 19:42:02 -07:00
|
|
|
let blob2 = blobBuilder.getFile("thefilename");
|
|
|
|
ok(blob2 instanceof Blob, "getFile should produce Blobs");
|
|
|
|
ok(blob2 instanceof File, "getFile should produce Files");
|
|
|
|
is(blob2.name, "thefilename", "getFile should produces Files with correct name");
|
|
|
|
is(blob2.type, "", "getFile with no second argument should return File with empty type");
|
|
|
|
is(blob2.size, 4, "getFile should return Blob with correct size");
|
|
|
|
|
|
|
|
blobBuilder.append("steak");
|
|
|
|
let blob3 = blobBuilder.getBlob("content/type");
|
|
|
|
ok(blob3 instanceof Blob, "getBlob should produce Blobs");
|
|
|
|
ok(!(blob3 instanceof File), "getBlob should not produce Files");
|
|
|
|
is(blob3.type, "content/type", "getBlob with no argument should return Blob with empty type");
|
|
|
|
is(blob3.size, 5, "getBlob should return Blob with correct size");
|
|
|
|
|
|
|
|
blobBuilder.append("apples");
|
|
|
|
let blob4 = blobBuilder.getFile("the other filename", "text/plain");
|
|
|
|
ok(blob4 instanceof Blob, "getFile should produce Blobs");
|
|
|
|
ok(blob4 instanceof File, "getFile should produce Files");
|
|
|
|
is(blob4.name, "the other filename", "getFile should produces Files with correct name");
|
|
|
|
is(blob4.type, "text/plain", "getFile with second argument should return File with correct type");
|
|
|
|
is(blob4.size, 6, "getFile should return Blob with correct size");
|
|
|
|
|
|
|
|
blobBuilder.append("boletes");
|
|
|
|
let blob5 = blobBuilder.getFile("");
|
|
|
|
ok(blob5 instanceof Blob, "getFile should produce Blobs");
|
|
|
|
ok(blob5 instanceof File, "getFile should produce Files");
|
|
|
|
is(blob5.name, "", "getFile with empty name should produces Files with empty name");
|
|
|
|
is(blob5.type, "", "getFile with no second argument should return File with correct type");
|
|
|
|
is(blob5.size, 7, "getFile should return Blob with correct size");
|
|
|
|
testFile(blob5, "boletes", "Test empty-named File from BlobBuilder.getFile");
|
|
|
|
|
2011-05-20 10:18:45 -07:00
|
|
|
|
|
|
|
let aB = new ArrayBuffer(16);
|
|
|
|
var int8View = new Int8Array(aB);
|
|
|
|
for (var i = 0; i < 16; i++) {
|
|
|
|
int8View[i] = i+65;
|
|
|
|
}
|
|
|
|
|
|
|
|
let testData =
|
|
|
|
[
|
|
|
|
// Test 3 strings
|
2012-03-12 21:45:00 -07:00
|
|
|
[["foo", "bar", "baz"], undefined,
|
|
|
|
[{start: 0, length: 9, contents: "foobarbaz"},
|
2011-05-20 10:18:45 -07:00
|
|
|
{start: 0, length: 3, contents: "foo"},
|
|
|
|
{start: 3, length:6, contents: "barbaz"},
|
|
|
|
{start: 6, length: 3, contents: "baz"},
|
2011-07-11 19:42:02 -07:00
|
|
|
{start: 6, length: 6, contents: "baz"},
|
2011-05-20 10:18:45 -07:00
|
|
|
{start: 0, length: 9, contents: "foobarbaz"},
|
2011-07-11 19:42:02 -07:00
|
|
|
{start: 0, length: 11, contents: "foobarbaz"},
|
|
|
|
{start: 10, length: 5, contents: ""}]],
|
2011-05-20 10:18:45 -07:00
|
|
|
// Test string, Blob, string
|
2012-03-12 21:45:00 -07:00
|
|
|
[["foo", blob1, "baz"], undefined,
|
|
|
|
[{start: 0, length: 3, contents: "foo"},
|
2011-05-20 10:18:45 -07:00
|
|
|
{start: 3, length: 8, contents: "squiggle"},
|
|
|
|
{start: 2, length: 2, contents: "os"},
|
|
|
|
{start: 10, length: 2, contents: "eb"}]],
|
|
|
|
// Test blob, string, blob
|
2012-03-12 21:45:00 -07:00
|
|
|
[[blob1, "foo", blob1], undefined,
|
|
|
|
[{start: 0, length: 8, contents: "squiggle"},
|
2011-05-20 10:18:45 -07:00
|
|
|
{start: 7, length: 2, contents: "ef"},
|
|
|
|
{start: 10, length: 2, contents: "os"},
|
|
|
|
{start: 1, length: 3, contents: "qui"},
|
|
|
|
{start: 12, length: 3, contents: "qui"},
|
2011-07-11 19:42:02 -07:00
|
|
|
{start: 40, length: 20, contents: ""}]],
|
2011-05-20 10:18:45 -07:00
|
|
|
// Test blobs all the way down
|
2012-03-12 21:45:00 -07:00
|
|
|
[[blob2, blob1, blob2], undefined,
|
|
|
|
[{start: 0, length: 4, contents: "ohai"},
|
2011-05-20 10:18:45 -07:00
|
|
|
{start: 4, length: 8, contents: "squiggle"},
|
|
|
|
{start: 12, length: 4, contents: "ohai"},
|
|
|
|
{start: 1, length: 2, contents: "ha"},
|
|
|
|
{start: 5, length: 4, contents: "quig"}]],
|
|
|
|
// Test an array buffer
|
2012-03-12 21:45:00 -07:00
|
|
|
[[aB, blob1, "foo"], null,
|
|
|
|
[{start: 0, length: 8, contents: "ABCDEFGH"},
|
2011-05-20 10:18:45 -07:00
|
|
|
{start: 8, length:10, contents: "IJKLMNOPsq"},
|
|
|
|
{start: 17, length: 3, contents: "qui"},
|
|
|
|
{start: 4, length: 8, contents: "EFGHIJKL"}]],
|
2012-03-12 21:45:00 -07:00
|
|
|
// Test transparent line endings
|
|
|
|
[["foo\r\n", "bar\r", "baz\n"], { endings: "transparent" },
|
|
|
|
[{start: 0, length: 5, contents: "foo\r\n"},
|
|
|
|
{start: 5, length: 4, contents: "bar\r"},
|
|
|
|
{start: 9, length: 4, contents: "baz\n"}]],
|
|
|
|
// Test transparent line endings when the second argument is omitted
|
|
|
|
[["foo\r\n", "bar\r", "baz\n"], undefined,
|
|
|
|
[{start: 0, length: 5, contents: "foo\r\n"},
|
|
|
|
{start: 5, length: 4, contents: "bar\r"},
|
|
|
|
{start: 9, length: 4, contents: "baz\n"}]],
|
|
|
|
// Test native line endings
|
|
|
|
[["foo\r\n", "bar\r", "baz\n"], { endings: "native" },
|
|
|
|
navigator.platform.indexOf("Win") != -1 ?
|
|
|
|
[{start: 0, length: 5, contents: "foo\r\n"},
|
|
|
|
{start: 5, length: 5, contents: "bar\r\n"},
|
|
|
|
{start: 10, length: 5, contents: "baz\r\n"}] :
|
|
|
|
[{start: 0, length: 4, contents: "foo\n"},
|
|
|
|
{start: 4, length: 4, contents: "bar\n"},
|
|
|
|
{start: 8, length: 4, contents: "baz\n"}]],
|
2011-05-20 10:18:45 -07:00
|
|
|
// Test type coercion of a number
|
2012-03-12 21:45:00 -07:00
|
|
|
[[3, aB, "foo"], {}, [{start: 0, length: 8, contents: "3ABCDEFG"},
|
2011-05-20 10:18:45 -07:00
|
|
|
{start: 8, length:10, contents: "HIJKLMNOPf"},
|
2011-07-11 19:42:02 -07:00
|
|
|
{start: 17, length: 4, contents: "foo"},
|
2011-05-20 10:18:45 -07:00
|
|
|
{start: 4, length: 8, contents: "DEFGHIJK"}]]
|
|
|
|
];
|
|
|
|
|
|
|
|
let testCounter = 0;
|
|
|
|
|
|
|
|
function doTest(data) {
|
|
|
|
testCounter++;
|
|
|
|
|
2012-03-12 21:45:00 -07:00
|
|
|
[blobs, options, tests] = data;
|
2011-05-20 10:18:45 -07:00
|
|
|
|
|
|
|
function runTest(test) {
|
|
|
|
|
|
|
|
let bb = new BlobBuilder();
|
|
|
|
ok(bb, "BlobBuilder should exist");
|
|
|
|
|
|
|
|
function doAppend(blob) {
|
2012-03-12 21:45:00 -07:00
|
|
|
if (options !== undefined) {
|
|
|
|
bb.append(blob, options && options.endings);
|
|
|
|
} else {
|
|
|
|
bb.append(blob);
|
|
|
|
}
|
|
|
|
blob.expando = bb; // Do we leak?
|
2011-05-20 10:18:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
blobs.forEach(doAppend);
|
|
|
|
ok(true, "Test " + testCounter + " appended all successfully");
|
|
|
|
let blob = bb.getBlob();
|
|
|
|
ok(blob, "Test " + testCounter + " got blob");
|
|
|
|
ok(blob instanceof Blob, "Test " + testCounter + " blob is a Blob");
|
2011-07-11 19:42:02 -07:00
|
|
|
ok(!(blob instanceof File), "Test " + testCounter + " blob is not a File");
|
2011-05-20 10:18:45 -07:00
|
|
|
|
2012-02-16 02:22:25 -08:00
|
|
|
let slice = blob.slice(test.start, test.start + test.length);
|
2011-05-20 10:18:45 -07:00
|
|
|
ok(slice, "Test " + testCounter + " got slice");
|
|
|
|
ok(slice instanceof Blob, "Test " + testCounter + " slice is a Blob");
|
2011-07-11 19:42:02 -07:00
|
|
|
ok(!(slice instanceof File), "Test " + testCounter + " slice is not a File");
|
|
|
|
is(slice.size, test.contents.length,
|
2011-05-20 10:18:45 -07:00
|
|
|
"Test " + testCounter + " slice is correct size");
|
|
|
|
|
2011-07-11 19:42:02 -07:00
|
|
|
testFile(slice, test.contents, "Test " + testCounter);
|
2011-05-20 10:18:45 -07:00
|
|
|
}
|
|
|
|
tests.forEach(runTest);
|
|
|
|
SpecialPowers.gc();
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
testData.forEach(doTest);
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|