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