gecko-dev/content/base/test/test_blobbuilder.html

202 строки
8.5 KiB
HTML

<!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">
"use strict";
// 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");
ok(blobBuilder.getFile, "BlobBuilder should have a getFile method");
try {
blobBuilder.append();
ok(false, "NOT REACHED");
} catch(e) {
ok(true, "an empty argument to append should throw");
}
blobBuilder.append(null);
// Yay we didn't crash.
blobBuilder.append("squiggle");
let blob1 = blobBuilder.getBlob();
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");
blobBuilder.append("ohai");
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");
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
[["foo", "bar", "baz"], undefined,
[{start: 0, length: 9, contents: "foobarbaz"},
{start: 0, length: 3, contents: "foo"},
{start: 3, length:6, contents: "barbaz"},
{start: 6, length: 3, contents: "baz"},
{start: 6, length: 6, contents: "baz"},
{start: 0, length: 9, contents: "foobarbaz"},
{start: 0, length: 11, contents: "foobarbaz"},
{start: 10, length: 5, contents: ""}]],
// Test string, Blob, string
[["foo", blob1, "baz"], undefined,
[{start: 0, length: 3, contents: "foo"},
{start: 3, length: 8, contents: "squiggle"},
{start: 2, length: 2, contents: "os"},
{start: 10, length: 2, contents: "eb"}]],
// Test blob, string, blob
[[blob1, "foo", blob1], undefined,
[{start: 0, length: 8, contents: "squiggle"},
{start: 7, length: 2, contents: "ef"},
{start: 10, length: 2, contents: "os"},
{start: 1, length: 3, contents: "qui"},
{start: 12, length: 3, contents: "qui"},
{start: 40, length: 20, contents: ""}]],
// Test blobs all the way down
[[blob2, blob1, blob2], undefined,
[{start: 0, length: 4, contents: "ohai"},
{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
[[aB, blob1, "foo"], null,
[{start: 0, length: 8, contents: "ABCDEFGH"},
{start: 8, length:10, contents: "IJKLMNOPsq"},
{start: 17, length: 3, contents: "qui"},
{start: 4, length: 8, contents: "EFGHIJKL"}]],
// 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"}]],
// Test type coercion of a number
[[3, aB, "foo"], {}, [{start: 0, length: 8, contents: "3ABCDEFG"},
{start: 8, length:10, contents: "HIJKLMNOPf"},
{start: 17, length: 4, contents: "foo"},
{start: 4, length: 8, contents: "DEFGHIJK"}]]
];
let testCounter = 0;
function doTest(data) {
testCounter++;
[blobs, options, tests] = data;
function runTest(test) {
let bb = new BlobBuilder();
ok(bb, "BlobBuilder should exist");
function doAppend(blob) {
if (options !== undefined) {
bb.append(blob, options && options.endings);
} else {
bb.append(blob);
}
blob.expando = bb; // Do we leak?
}
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");
ok(!(blob instanceof File), "Test " + testCounter + " blob is not a File");
let slice = blob.slice(test.start, test.start + test.length);
ok(slice, "Test " + testCounter + " got slice");
ok(slice instanceof Blob, "Test " + testCounter + " slice is a Blob");
ok(!(slice instanceof File), "Test " + testCounter + " slice is not a File");
is(slice.size, test.contents.length,
"Test " + testCounter + " slice is correct size");
testFile(slice, test.contents, "Test " + testCounter);
}
tests.forEach(runTest);
SpecialPowers.gc();
}
SimpleTest.waitForExplicitFinish();
testData.forEach(doTest);
</script>
</pre>
</body>
</html>