Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: LayoutTests/imported/web-platform-tests/FileAPI/blob/Blob-constructor.html

Issue 1236713002: Import FileAPI tests from web-platform-tests (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 <!DOCTYPE html>
2 <meta charset=utf-8>
3 <title>Blob constructor</title>
4 <link rel=help href="http://dev.w3.org/2006/webapi/FileAPI/#constructorBlob">
5 <link rel=help href="https://heycam.github.io/webidl/#es-union">
6 <link rel=help href="https://heycam.github.io/webidl/#es-dictionary">
7 <link rel=help href="https://heycam.github.io/webidl/#es-sequence">
8 <script src="../../../../resources/testharness.js"></script>
9 <script src="../../../../resources/testharnessreport.js"></script>
10 <script src="../support/Blob.js"></script>
11 <p><strong><a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=23683">Discus sion</a>
12 is ongoing that will affect a number of the following tests.</strong>
13 <div id="log"></div>
14 <!-- used by "platform object that supports indexed properties" tests -->
15 <iframe style="display:none"></iframe>
16 <script>
17 test(function() {
18 assert_true("Blob" in window, "window should have a Blob property.");
19 assert_equals(Blob.length, 0, "Blob.length should be 0.");
20 assert_true(Blob instanceof Function, "Blob should be a function.");
21 }, "Blob interface object");
22
23 // Step 1.
24 test(function() {
25 var blob = new Blob();
26 assert_true(blob instanceof Blob);
27 assert_equals(String(blob), '[object Blob]');
28 assert_equals(blob.size, 0);
29 assert_equals(blob.type, "");
30 }, "no-argument Blob constructor");
31 test(function() {
32 var blob = Blob();
33 assert_true(blob instanceof Blob);
34 assert_equals(blob.size, 0);
35 assert_equals(blob.type, "");
36 }, "no-argument Blob constructor without 'new'");
37 test(function() {
38 var blob = new Blob;
39 assert_true(blob instanceof Blob);
40 assert_equals(blob.size, 0);
41 assert_equals(blob.type, "");
42 }, "no-argument Blob constructor without brackets");
43
44 // blobParts argument (WebIDL).
45 test(function() {
46 var args = [
47 null,
48 undefined,
49 true,
50 false,
51 0,
52 1,
53 1.5,
54 "FAIL",
55 new Date(),
56 new RegExp(),
57 ];
58 args.forEach(function(arg) {
59 assert_throws(new TypeError(), function() {
60 new Blob(arg);
61 }, "Should throw for argument " + format_value(arg) + ".");
62 });
63 }, "Passing non-objects, Dates and RegExps for blobParts should throw a TypeErro r.");
64
65 test_blob(function() {
66 return new Blob({});
67 }, {
68 expected: "",
69 type: "",
70 desc: "A plain object should be treated as a sequence for the blobParts argume nt."
71 });
72 test_blob(function() {
73 return new Blob({ 0: "PASS", length: 1 });
74 }, {
75 expected: "PASS",
76 type: "",
77 desc: "A plain object with a length property should be treated as a sequence f or the blobParts argument."
78 });
79 test_blob(function() {
80 return new Blob(new String("xyz"));
81 }, {
82 expected: "xyz",
83 type: "",
84 desc: "A String object should be treated as a sequence for the blobParts argum ent."
85 });
86 test_blob(function() {
87 return new Blob(new Uint8Array([1, 2, 3]));
88 }, {
89 expected: "123",
90 type: "",
91 desc: "A Uint8Array object should be treated as a sequence for the blobParts a rgument."
92 });
93
94 var test_error = { name: "test" };
95
96 test(function() {
97 var obj = {
98 get length() { throw test_error; }
99 };
100 assert_throws(test_error, function() {
101 new Blob(obj);
102 });
103 }, "The length getter should be invoked and any exceptions should be propagated. ");
104
105 test_blob(function() {
106 var element = document.createElement("div");
107 element.appendChild(document.createElement("div"));
108 element.appendChild(document.createElement("p"));
109 var list = element.children;
110 Object.defineProperty(list, "length", {
111 get: function() { throw test_error; }
112 });
113 return new Blob(list);
114 }, {
115 expected: "[object HTMLDivElement][object HTMLParagraphElement]",
116 type: "",
117 desc: "A platform object that supports indexed properties should be treated as a sequence for the blobParts argument (overwritten 'length'.)"
118 });
119
120 test(function() {
121 assert_throws(test_error, function() {
122 var obj = {
123 length: {
124 valueOf: null,
125 toString: function() { throw test_error; }
126 }
127 };
128 new Blob(obj);
129 });
130 assert_throws(test_error, function() {
131 var obj = {
132 length: { valueOf: function() { throw test_error; } }
133 };
134 new Blob(obj);
135 });
136 }, "ToUint32 should be applied to the length and any exceptions should be propag ated.");
137
138 test(function() {
139 var received = [];
140 var obj = {
141 get length() {
142 received.push("length getter");
143 return {
144 valueOf: function() {
145 received.push("length valueOf");
146 return 3;
147 }
148 };
149 },
150 get 0() {
151 received.push("0 getter");
152 return {
153 toString: function() {
154 received.push("0 toString");
155 return "a";
156 }
157 };
158 },
159 get 1() {
160 received.push("1 getter");
161 throw test_error;
162 },
163 get 2() {
164 received.push("2 getter");
165 assert_unreached("Should not call the getter for 2 if the getter for 1 thr ew.");
166 }
167 };
168 assert_throws(test_error, function() {
169 new Blob(obj);
170 });
171 assert_array_equals(received, [
172 "length getter",
173 "length valueOf",
174 "0 getter",
175 "0 toString",
176 "1 getter"
177 ]);
178 }, "Getters and value conversions should happen in order until an exception is t hrown.");
179
180 // XXX should add tests edge cases of ToUint32(length)
181
182 test(function() {
183 assert_throws(test_error, function() {
184 new Blob([{ toString: function() { throw test_error; } }]);
185 }, "Throwing toString");
186 assert_throws(test_error, function() {
187 new Blob([{ toString: undefined, valueOf: function() { throw test_error; } } ]);
188 }, "Throwing valueOf");
189 assert_throws(test_error, function() {
190 new Blob([{
191 toString: function() { throw test_error; },
192 valueOf: function() { assert_unreached("Should not call valueOf if toStrin g is present."); }
193 }]);
194 }, "Throwing toString and valueOf");
195 assert_throws(new TypeError(), function() {
196 new Blob([{toString: null, valueOf: null}]);
197 }, "Null toString and valueOf");
198 }, "ToString should be called on elements of the blobParts array and any excepti ons should be propagated.");
199
200 test_blob(function() {
201 var arr = [
202 { toString: function() { arr.pop(); return "PASS"; } },
203 { toString: function() { assert_unreached("Should have removed the second el ement of the array rather than called toString() on it."); } }
204 ];
205 return new Blob(arr);
206 }, {
207 expected: "PASSundefined",
208 type: "",
209 desc: "Changes to the blobParts array should be reflected in the returned Blob (pop)."
210 });
211
212 test_blob(function() {
213 var arr = [
214 {
215 toString: function() {
216 if (arr.length === 3) {
217 return "SS";
218 }
219 arr.unshift({
220 toString: function() {
221 assert_unreached("Should only access index 0 once.");
222 }
223 });
224 return "PA";
225 }
226 },
227 {
228 toString: function() {
229 assert_unreached("Should not access the final element.");
230 }
231 }
232 ];
233 return new Blob(arr);
234 }, {
235 expected: "PASS",
236 type: "",
237 desc: "Changes to the blobParts array should be reflected in the returned Blob (unshift)."
238 });
239
240 test_blob(function() {
241 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17652
242 return new Blob([
243 null,
244 undefined,
245 true,
246 false,
247 0,
248 1,
249 new String("stringobject"),
250 [],
251 ['x', 'y'],
252 {},
253 { 0: "FAIL", length: 1 },
254 { toString: function() { return "stringA"; } },
255 { toString: undefined, valueOf: function() { return "stringB"; } },
256 { valueOf: function() { assert_unreached("Should not call valueOf if toStrin g is present on the prototype."); } }
257 ]);
258 }, {
259 expected: "nullundefinedtruefalse01stringobjectx,y[object Object][object Objec t]stringAstringB[object Object]",
260 type: "",
261 desc: "ToString should be called on elements of the blobParts array."
262 });
263
264 test_blob(function() {
265 return new Blob([
266 new ArrayBuffer(8)
267 ]);
268 }, {
269 expected: "\0\0\0\0\0\0\0\0",
270 type: "",
271 desc: "ArrayBuffer elements of the blobParts array should be supported."
272 });
273
274 test_blob(function() {
275 return new Blob([
276 new Uint8Array([0x50, 0x41, 0x53, 0x53]),
277 new Int8Array([0x50, 0x41, 0x53, 0x53]),
278 new Uint16Array([0x4150, 0x5353]),
279 new Int16Array([0x4150, 0x5353]),
280 new Uint32Array([0x53534150]),
281 new Int32Array([0x53534150]),
282 new Float32Array([0xD341500000])
283 ]);
284 }, {
285 expected: "PASSPASSPASSPASSPASSPASSPASS",
286 type: "",
287 desc: "Passing typed arrays as elements of the blobParts array should work."
288 });
289 test_blob(function() {
290 return new Blob([
291 // 0x535 3415053534150
292 // 0x535 = 0b010100110101 -> Sign = +, Exponent = 1333 - 1023 = 310
293 // 0x13415053534150 * 2**(-52)
294 // ==> 0x13415053534150 * 2**258 = 25102973727670367250052675631218218749219 13208671273727396467555337665343087229079989707079680
295 new Float64Array([2510297372767036725005267563121821874921913208671273727396 467555337665343087229079989707079680])
296 ]);
297 }, {
298 expected: "PASSPASS",
299 type: "",
300 desc: "Passing a Float64Array as element of the blobParts array should work."
301 });
302
303 test_blob(function() {
304 return new Blob(document.createElement("div"));
305 }, {
306 expected: "",
307 type: "",
308 desc: "Passing an element as the blobParts array should work."
309 });
310
311 test_blob(function() {
312 return new Blob(window);
313 }, {
314 expected: "[object Window]",
315 type: "",
316 desc: "Passing an platform object that supports indexed properties as the blob Parts array should work (window)."
317 });
318 test_blob(function() {
319 window[0].toString = function() { return "foo"; };
320 return new Blob(window);
321 }, {
322 expected: "foo",
323 type: "",
324 desc: "Passing an platform object that supports indexed properties as the blob Parts array should work (window with custom toString)."
325 });
326 test_blob(function() {
327 var select = document.createElement("select");
328 select.appendChild(document.createElement("option"));
329 return new Blob(select);
330 }, {
331 expected: "[object HTMLOptionElement]",
332 type: "",
333 desc: "Passing an platform object that supports indexed properties as the blob Parts array should work (select)."
334 });
335
336 var t_ports = async_test("Passing a platform array object as the blobParts array should work (MessagePort[]).");
337 t_ports.step(function() {
338 var channel = new MessageChannel();
339 channel.port2.onmessage = this.step_func(function(e) {
340 var b_ports = new Blob(e.ports);
341 assert_equals(b_ports.size, "[object MessagePort]".length);
342 this.done();
343 });
344 var channel2 = new MessageChannel();
345 channel.port1.postMessage('', [channel2.port1]);
346 });
347
348 test_blob(function() {
349 var elm = document.createElement("div");
350 elm.setAttribute("foo", "bar");
351 return new Blob(elm.attributes);
352 }, {
353 expected: "[object Attr]",
354 type: "",
355 desc: "Passing a platform array object as the blobParts array should work (Att r[])."
356 });
357
358 test_blob(function() {
359 var blob = new Blob(['foo']);
360 return new Blob([blob, blob]);
361 }, {
362 expected: "foofoo",
363 type: "",
364 desc: "Array with two blobs"
365 });
366
367 test_blob_binary(function() {
368 var view = new Uint8Array([0, 255, 0]);
369 return new Blob([view.buffer, view.buffer]);
370 }, {
371 expected: [0, 255, 0, 0, 255, 0],
372 type: "",
373 desc: "Array with two buffers"
374 });
375
376 test_blob_binary(function() {
377 var view = new Uint8Array([0, 255, 0, 4]);
378 var blob = new Blob([view, view]);
379 assert_equals(blob.size, 8);
380 var view1 = new Uint16Array(view.buffer, 2);
381 return new Blob([view1, view.buffer, view1]);
382 }, {
383 expected: [0, 4, 0, 255, 0, 4, 0, 4],
384 type: "",
385 desc: "Array with two bufferviews"
386 });
387
388 test_blob(function() {
389 var view = new Uint8Array([0]);
390 var blob = new Blob(["fo"]);
391 return new Blob([view.buffer, blob, "foo"]);
392 }, {
393 expected: "\0fofoo",
394 type: "",
395 desc: "Array with mixed types"
396 });
397
398 // options argument
399 test(function() {
400 new Blob([], { endings: "invalidEnumValue" });
401 new Blob([], { endings: null });
402 new Blob([], { endings: undefined });
403 new Blob([], { endings: 0 });
404 new Blob([], { get endings() { assert_unreached("Should not call getter"); } } );
405 }, "The 'endings' property should be ignored.");
406
407 test(function() {
408 assert_throws(test_error, function() {
409 new Blob([], {
410 get type() { throw test_error; }
411 });
412 });
413 assert_throws(test_error, function() {
414 new Blob([], {
415 type: { toString: function() { throw test_error; } }
416 });
417 });
418 }, "options properties should be accessed in lexicographic order.");
419
420 test(function() {
421 assert_throws(test_error, function() {
422 new Blob(
423 [{ toString: function() { throw test_error } }],
424 {
425 get type() { assert_unreached("type getter should not be called."); }
426 }
427 );
428 });
429 }, "Arguments should be evaluated from left to right.");
430
431 [
432 null,
433 undefined,
434 {},
435 { unrecognized: true },
436 /regex/,
437 function() {}
438 ].forEach(function(arg, idx) {
439 test_blob(function() {
440 return new Blob([], arg);
441 }, {
442 expected: "",
443 type: "",
444 desc: "Passing " + format_value(arg) + " (index " + idx + ") for options sho uld use the defaults."
445 });
446 test_blob(function() {
447 return new Blob(["\na\r\nb\n\rc\r"], arg);
448 }, {
449 expected: "\na\r\nb\n\rc\r",
450 type: "",
451 desc: "Passing " + format_value(arg) + " (index " + idx + ") for options sho uld use the defaults (with newlines)."
452 });
453 });
454
455 test_blob(function() {
456 return new Blob(["\na\r\nb\n\rc\r"], { endings: "transparent" });
457 }, {
458 expected: "\na\r\nb\n\rc\r",
459 type: "",
460 desc: "Newlines should not change when endings is 'transparent'."
461 });
462 test_blob(function() {
463 return new Blob(["\na\r\nb\n\rc\r"], { endings: "native" });
464 }, {
465 expected: "\na\r\nb\n\rc\r",
466 type: "",
467 desc: "Newlines should not change when endings is 'native'."
468 });
469
470 var type_tests = [
471 // blobParts, type, expected type
472 [[], '', ''],
473 [[], 'a', 'a'],
474 [[], 'A', 'a'],
475 [[], 'text/html', 'text/html'],
476 [[], 'TEXT/HTML', 'text/html'],
477 [[], '\u00E5', ''],
478 [[], '\uD801\uDC7E', ''], // U+1047E
479 [[], ' image/gif ', ' image/gif '],
480 [[], '\timage/gif\t', ''],
481 [[], 'image/gif;\u007f', ''],
482 [[], '\u0130mage/gif', ''], // uppercase i with dot
483 [[], '\u0131mage/gif', ''], // lowercase dotless i
484 [[], 'image/gif\u0000', ''],
485 // check that type isn't changed based on sniffing
486 [[0x3C, 0x48, 0x54, 0x4D, 0x4C, 0x3E], 'unknown/unknown', 'unknown/unknown'], // "<HTML>"
487 [[0x00, 0xFF], 'text/plain', 'text/plain'],
488 [[0x47, 0x49, 0x46, 0x38, 0x39, 0x61], 'image/png', 'image/png'], // "GIF89a"
489 ];
490
491 type_tests.forEach(function(t) {
492 test(function() {
493 var arr = new Uint8Array([t[0]]).buffer;
494 var b = new Blob([arr], {type:t[1]});
495 assert_equals(b.type, t[2]);
496 }, "Blob with type " + format_value(t[1]));
497 });
498 </script>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698