forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_blobbuilder.html
201 lines (177 loc) · 8.53 KB
/
test_blobbuilder.html
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!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>