OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 |
| 3 <script src="../../resources/js-test.js"></script> |
| 4 <script> |
| 5 description("Test the File constructor."); |
| 6 |
| 7 // Test the different ways you can construct a File. |
| 8 shouldBeTrue("(new File([], 'world.html')) instanceof window.File"); |
| 9 shouldBeTrue("(new File(['hello'], 'world.html')) instanceof window.File"); |
| 10 shouldBeTrue("(new File(['hello'], 'world.html', {})) instanceof window.File"); |
| 11 shouldBeTrue("(new File(['hello'], 'world.html', {type:'text/html'})) instanceof
window.File"); |
| 12 shouldBeTrue("(new File(['hello'], 'world.html', {type:'text/html', endings:'nat
ive'})) instanceof window.File"); |
| 13 shouldBeTrue("(new File(['hello'], 'world.html', {type:'text/html', endings:'tra
nsparent'})) instanceof window.File"); |
| 14 |
| 15 // Test that File inherits from File. |
| 16 shouldBeTrue("(new File([], 'world.html')) instanceof window.File") |
| 17 |
| 18 // Verify that the file name argument is required. |
| 19 shouldThrow("(new File())", "'TypeError: File constructor requires at least two
arguments'"); |
| 20 shouldThrow("(new File([]))", "'TypeError: File constructor requires at least tw
o arguments'"); |
| 21 |
| 22 // Test valid file names. |
| 23 shouldBeTrue("(new File([], null)) instanceof window.File"); |
| 24 shouldBeTrue("(new File([], 1)) instanceof window.File"); |
| 25 shouldBeTrue("(new File([], '')) instanceof window.File"); |
| 26 shouldBeTrue("(new File([], document)) instanceof window.File"); |
| 27 |
| 28 // Test invalid blob parts. |
| 29 shouldThrow("new File('hello', 'world.html')", '"TypeError: Failed to construct
\'File\': The 1st argument is neither an array, nor does it have indexed propert
ies."'); |
| 30 shouldThrow("new File(0, 'world.html')", '"TypeError: Failed to construct \'File
\': The 1st argument is neither an array, nor does it have indexed properties."'
); |
| 31 |
| 32 // Test valid blob parts. |
| 33 shouldBeTrue("(new File([], 'world.html')) instanceof window.File"); |
| 34 shouldBeTrue("(new File(['stringPrimitive'], 'world.html')) instanceof window.Fi
le"); |
| 35 shouldBeTrue("(new File([String('stringObject')], 'world.html')) instanceof wind
ow.File"); |
| 36 shouldBeTrue("(new File([new Blob], 'world.html')) instanceof window.File"); |
| 37 shouldBeTrue("(new File([new Blob([new Blob])], 'world.html')) instanceof window
.File"); |
| 38 |
| 39 // Test File instances used as blob parts. |
| 40 shouldBeTrue("(new Blob([new File([], 'world.txt')])) instanceof window.Blob"); |
| 41 shouldBeTrue("(new Blob([new Blob([new File([new Blob], 'world.txt')])])) instan
ceof window.Blob"); |
| 42 shouldBeTrue("(new File([new File([], 'world.txt')], 'world.html')) instanceof w
indow.File"); |
| 43 shouldBeTrue("(new File([new Blob([new File([new Blob], 'world.txt')])], 'world.
html')) instanceof window.File"); |
| 44 |
| 45 // Test some conversions to string in the parts array. |
| 46 shouldBe("(new File([12], 'world.html')).size", "2"); |
| 47 shouldBe("(new File([[]], 'world.html')).size", "0"); // [].toString() i
s the empty string |
| 48 shouldBe("(new File([{}], 'world.html')).size", "15");; // {}.toString() i
s the string "[object Object]" |
| 49 shouldBe("(new File([document], 'world.html')).size", "21"); // document.toStri
ng() is the string "[object HTMLDocument]" |
| 50 |
| 51 var toStringingObj = { toString: function() { return "A string"; } }; |
| 52 shouldBe("(new File([toStringingObj], 'world.html')).size", "8"); |
| 53 |
| 54 var throwingObj = { toString: function() { throw "Error"; } }; |
| 55 shouldThrow("new File([throwingObj], 'world.html')", "'Error'"); |
| 56 |
| 57 // Test some conversions to string in the file name. |
| 58 shouldBe("(new File([], null)).name", "'null'"); |
| 59 shouldBe("(new File([], 12)).name", "'12'"); |
| 60 shouldBe("(new File([], '')).name", "''"); |
| 61 shouldBe("(new File([], {})).name", "'[object Object]'"); |
| 62 shouldBe("(new File([], document)).name", "'[object HTMLDocument]'"); |
| 63 shouldBe("(new File([], toStringingObj)).name", "'A string'"); |
| 64 shouldThrow("(new File([], throwingObj)).name", "'Error'"); |
| 65 |
| 66 // Test some invalid property bags. |
| 67 shouldBeTrue("(new File([], 'world.html', {unknownKey:'value'})) instanceof wind
ow.File"); // Ignore invalid keys |
| 68 shouldThrow("new File([], 'world.html', {endings:'illegalValue'})", "'TypeError:
Failed to construct \\'File\\': The \"endings\" property must be either \"trans
parent\" or \"native\".'"); |
| 69 shouldThrow("new File([], 'world.html', {endings:throwingObj})", "'Error'"); |
| 70 shouldThrow("new File([], 'world.html', {type:throwingObj})", "'Error'"); |
| 71 shouldThrow("new File([], 'world.html', {type:'hello\u00EE'})", "'SyntaxError: F
ailed to construct \\'File\\': The \"type\" property must consist of ASCII chara
cters.'"); |
| 72 |
| 73 // Test various non-object literals being used as property bags. |
| 74 shouldThrow("(new File([], 'world.html', null)) instanceof window.File", "'TypeE
rror: Failed to construct \\'File\\': The 3rd argument is not of type Object.'")
; |
| 75 shouldThrow("(new File([], 'world.html', undefined)) instanceof window.File", "'
TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Objec
t.'"); |
| 76 shouldThrow("(new File([], 'world.html', 123)) instanceof window.File", "'TypeEr
ror: Failed to construct \\'File\\': The 3rd argument is not of type Object.'"); |
| 77 shouldThrow("(new File([], 'world.html', 123.4)) instanceof window.File", "'Type
Error: Failed to construct \\'File\\': The 3rd argument is not of type Object.'"
); |
| 78 shouldThrow("(new File([], 'world.html', true)) instanceof window.File", "'TypeE
rror: Failed to construct \\'File\\': The 3rd argument is not of type Object.'")
; |
| 79 shouldThrow("(new File([], 'world.html', 'abc')) instanceof window.File", "'Type
Error: Failed to construct \\'File\\': The 3rd argument is not of type Object.'"
); |
| 80 shouldBeTrue("(new File([], 'world.html', [])) instanceof window.File"); |
| 81 shouldBeTrue("(new File([], 'world.html', /abc/)) instanceof window.File"); |
| 82 shouldBeTrue("(new File([], 'world.html', function () {})) instanceof window.Fil
e"); |
| 83 |
| 84 // Test that the name/type/size are correctly added to the File. |
| 85 shouldBe("(new File([], 'world.html', {type:'text/html'})).name", "'world.html'"
); |
| 86 shouldBe("(new File([], 'world.html', {type:'text/html'})).type", "'text/html'")
; |
| 87 shouldBe("(new File([], 'world.html', {type:'text/html'})).size", "0"); |
| 88 shouldBe("(new File([], 'world.html', {type:'text/plain;charset=UTF-8'})).type",
"'text/plain;charset=utf-8'"); |
| 89 |
| 90 // Test the number of expected arguments in the File constructor. |
| 91 shouldBe("window.File.length", "2"); |
| 92 |
| 93 // Test ArrayBufferView parameters. |
| 94 shouldBe("new File([new DataView(new ArrayBuffer(100))], 'world.html').size", "1
00"); |
| 95 shouldBe("new File([new Uint8Array(100)], 'world.html').size", "100"); |
| 96 shouldBe("new File([new Uint8ClampedArray(100)], 'world.html').size", "100"); |
| 97 shouldBe("new File([new Uint16Array(100)], 'world.html').size", "200"); |
| 98 shouldBe("new File([new Uint32Array(100)], 'world.html').size", "400"); |
| 99 shouldBe("new File([new Int8Array(100)], 'world.html').size", "100"); |
| 100 shouldBe("new File([new Int16Array(100)], 'world.html').size", "200"); |
| 101 shouldBe("new File([new Int32Array(100)], 'world.html').size", "400"); |
| 102 shouldBe("new File([new Float32Array(100)], 'world.html').size", "400"); |
| 103 shouldBe("new File([new Float64Array(100)], 'world.html').size", "800"); |
| 104 shouldBe("new File([new Float64Array(100), new Int32Array(100), new Uint8Array(1
00), new DataView(new ArrayBuffer(100))], 'world.html').size", "1400"); |
| 105 shouldBe("new File([new Blob([new Int32Array(100)]), new Uint8Array(100), new Fl
oat32Array(100), new DataView(new ArrayBuffer(100))], 'world.html').size", "1000
"); |
| 106 shouldBe("new File([new Blob([new Int32Array(100)]), new File([new Uint16Array(1
00)], 'world.txt'), new Uint8Array(100), new Float32Array(100), new DataView(new
ArrayBuffer(100))], 'world.html').size", "1200"); |
| 107 |
| 108 // Test ArrayBuffer parameters. |
| 109 shouldBe("new File([(new DataView(new ArrayBuffer(100))).buffer], 'world.html').
size", "100"); |
| 110 shouldBe("new File([(new Uint8Array(100)).buffer], 'world.html').size", "100"); |
| 111 shouldBe("new File([(new Uint8ClampedArray(100)).buffer], 'world.html').size", "
100"); |
| 112 shouldBe("new File([(new Uint16Array(100)).buffer], 'world.html').size", "200"); |
| 113 shouldBe("new File([(new Uint32Array(100)).buffer], 'world.html').size", "400"); |
| 114 shouldBe("new File([(new Int8Array(100)).buffer], 'world.html').size", "100"); |
| 115 shouldBe("new File([(new Int16Array(100)).buffer], 'world.html').size", "200"); |
| 116 shouldBe("new File([(new Int32Array(100)).buffer], 'world.html').size", "400"); |
| 117 shouldBe("new File([(new Float32Array(100)).buffer], 'world.html').size", "400")
; |
| 118 shouldBe("new File([(new Float64Array(100)).buffer], 'world.html').size", "800")
; |
| 119 shouldBe("new File([(new Float64Array(100)).buffer, (new Int32Array(100)).buffer
, (new Uint8Array(100)).buffer, (new DataView(new ArrayBuffer(100))).buffer], 'w
orld.html').size", "1400"); |
| 120 shouldBe("new File([new Blob([(new Int32Array(100)).buffer]), (new Uint8Array(10
0)).buffer, (new Float32Array(100)).buffer, (new DataView(new ArrayBuffer(100)))
.buffer], 'world.html').size", "1000"); |
| 121 shouldBe("new File([new Blob([(new Int32Array(100)).buffer]), new File([new Uint
16Array(100).buffer], 'world.txt'), (new Uint8Array(100)).buffer, (new Float32Ar
ray(100)).buffer, (new DataView(new ArrayBuffer(100))).buffer], 'world.html').si
ze", "1200"); |
| 122 |
| 123 // Test building Blobs with ArrayBuffer / ArrayBufferView parts enclosed in file
s. |
| 124 shouldBe("new Blob([new Blob([new Int32Array(100)]), new File([new Uint16Array(1
00)], 'world.txt'), new Uint8Array(100), new Float32Array(100), new DataView(new
ArrayBuffer(100))]).size", "1200"); |
| 125 shouldBe("new Blob([new Blob([(new Int32Array(100)).buffer]), new File([new Uint
16Array(100).buffer], 'world.txt'), (new Uint8Array(100)).buffer, (new Float32Ar
ray(100)).buffer, (new DataView(new ArrayBuffer(100))).buffer]).size", "1200"); |
| 126 |
| 127 // Test passing blob parts in sequences. |
| 128 shouldBeTrue("new File({length: 0}, 'world.txt') instanceof window.File"); |
| 129 shouldBe("new File({length: 0}, 'world.txt').size", "0"); |
| 130 shouldBe("new File({length: 1, 0: 'string'}, 'world.txt').size", "6"); |
| 131 shouldBe("new File({length: 2, 0: new Uint8Array(100), 1: new Int16Array(100)},
'world.txt').size", "300"); |
| 132 shouldBe("new File({length: 1, 0: 'string'}, 'world.txt', {type: 'text/html'}).t
ype", "'text/html'"); |
| 133 shouldThrow("new File({length: 0}, 'world.txt', {endings:'illegal'})", "'TypeErr
or: Failed to construct \\'File\\': The \"endings\" property must be either \"tr
ansparent\" or \"native\".'"); |
| 134 |
| 135 // Test passing blog parts in a sequence-like object that throws on property acc
ess. |
| 136 var throwingSequence = {length: 4, 0: 'hello', 3: 'world'}; |
| 137 Object.defineProperty(throwingSequence, "1", { |
| 138 get: function() { throw new Error("Misbehaving property"); }, |
| 139 enumerable: true, configurable: true |
| 140 }); |
| 141 Object.defineProperty(throwingSequence, "2", { |
| 142 get: function() { throw new Error("This should not be thrown"); }, |
| 143 enumerable: true, configurable: true |
| 144 }); |
| 145 shouldThrow("new File(throwingSequence, 'world.txt')", "'Error: Misbehaving prop
erty'"); |
| 146 </script> |
OLD | NEW |