3 <script src=
"../../resources/js-test.js"></script>
5 description("Test the File constructor.");
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:'native'})) instanceof window.File");
13 shouldBeTrue("(new File(['hello'], 'world.html', {type:'text/html', endings:'transparent'})) instanceof window.File");
15 // Test that File inherits from File.
16 shouldBeTrue("(new File([], 'world.html')) instanceof window.File")
18 // Verify that the file name argument is required.
19 shouldThrow("(new File())", '"TypeError: Failed to construct \'File\': 2 arguments required, but only 0 present."');
20 shouldThrow("(new File([]))", '"TypeError: Failed to construct \'File\': 2 arguments required, but only 1 present."');
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");
28 // Test invalid file 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 properties."');
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 shouldThrow("new File(null, 'world.html')", '"TypeError: Failed to construct \'File\': The 1st argument is neither an array, nor does it have indexed properties."');
33 // Test valid file parts.
34 shouldBeTrue("(new File([], 'world.html')) instanceof window.File");
35 shouldBeTrue("(new File(['stringPrimitive'], 'world.html')) instanceof window.File");
36 shouldBeTrue("(new File([String('stringObject')], 'world.html')) instanceof window.File");
37 shouldBeTrue("(new File([new Blob], 'world.html')) instanceof window.File");
38 shouldBeTrue("(new File([new Blob([new Blob])], 'world.html')) instanceof window.File");
40 // Test File instances used as blob parts.
41 shouldBeTrue("(new Blob([new File([], 'world.txt')])) instanceof window.Blob");
42 shouldBeTrue("(new Blob([new Blob([new File([new Blob], 'world.txt')])])) instanceof window.Blob");
43 shouldBeTrue("(new File([new File([], 'world.txt')], 'world.html')) instanceof window.File");
44 shouldBeTrue("(new File([new Blob([new File([new Blob], 'world.txt')])], 'world.html')) instanceof window.File");
46 // Test some conversions to string in the parts array.
47 shouldBe("(new File([12], 'world.html')).size", "2");
48 shouldBe("(new File([[]], 'world.html')).size", "0"); // [].toString() is the empty string
49 shouldBe("(new File([{}], 'world.html')).size", "15");; // {}.toString() is the string "[object Object]"
50 shouldBe("(new File([document], 'world.html')).size", "21"); // document.toString() is the string "[object HTMLDocument]"
52 var toStringingObj
= { toString: function() { return "A string"; } };
53 shouldBe("(new File([toStringingObj], 'world.html')).size", "8");
55 var throwingObj
= { toString: function() { throw "Error"; } };
56 shouldThrow("new File([throwingObj], 'world.html')", "'Error'");
58 // Test some conversions to string in the file name.
59 shouldBe("(new File([], null)).name", "'null'");
60 shouldBe("(new File([], 12)).name", "'12'");
61 shouldBe("(new File([], '')).name", "''");
62 shouldBe("(new File([], {})).name", "'[object Object]'");
63 shouldBe("(new File([], document)).name", "'[object HTMLDocument]'");
64 shouldBe("(new File([], toStringingObj)).name", "'A string'");
65 shouldThrow("(new File([], throwingObj)).name", "'Error'");
67 // Test some invalid property bags.
68 shouldBeTrue("(new File([], 'world.html', {unknownKey:'value'})) instanceof window.File"); // Ignore invalid keys
69 shouldThrow("new File([], 'world.html', {endings:'illegalValue'})", "'TypeError: Failed to construct \\'File\\': The provided value \\'illegalValue\\' is not a valid enum value of type NormalizeLineEndings.'");
70 shouldThrow("new File([], 'world.html', {endings:throwingObj})", "'Error'");
71 shouldThrow("new File([], 'world.html', {type:throwingObj})", "'Error'");
72 shouldThrow("new File([], 'world.html', {type:'hello\u00EE'})", "'SyntaxError: Failed to construct \\'File\\': The \\'type\\' property must consist of ASCII characters.'");
74 // Test various non-object literals being used as property bags.
75 shouldBeTrue("(new File([], 'world.html', null)) instanceof window.File");
76 shouldBeTrue("(new File([], 'world.html', undefined)) instanceof window.File");
77 shouldThrow("(new File([], 'world.html', 123)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': parameter 3 (\\'options\\') is not an object.'");
78 shouldThrow("(new File([], 'world.html', 123.4)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': parameter 3 (\\'options\\') is not an object.'");
79 shouldThrow("(new File([], 'world.html', true)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': parameter 3 (\\'options\\') is not an object.'");
80 shouldThrow("(new File([], 'world.html', 'abc')) instanceof window.File", "'TypeError: Failed to construct \\'File\\': parameter 3 (\\'options\\') is not an object.'");
81 shouldBeTrue("(new File([], 'world.html', [])) instanceof window.File");
82 shouldBeTrue("(new File([], 'world.html', /abc/)) instanceof window.File");
83 shouldBeTrue("(new File([], 'world.html', function () {})) instanceof window.File");
85 // Test that the name/type/size are correctly added to the File.
86 shouldBe("(new File([], 'world.html', {type:'text/html'})).name", "'world.html'");
87 shouldBe("(new File([], 'world.html', {type:'text/html'})).type", "'text/html'");
88 shouldBe("(new File([], 'world.html', {type:'text/html'})).size", "0");
89 shouldBe("(new File([], 'world.html', {type:'text/plain;charset=UTF-8'})).type", "'text/plain;charset=utf-8'");
91 // Test that the lastModified is correctly added to the File.
92 var aDate
= new Date(441532800000);
93 shouldBe("(new File([], 'world.html', {lastModified: 441532800000})).lastModified", "441532800000");
94 // Unless specified, lastModified should default to now.
95 shouldBeNow("(new File([], 'world.html')).lastModified", 20000);
96 shouldBeNow("(new File([], 'world.html', {})).lastModified", 20000);
97 shouldBeNow("(new File([], 'world.html', {type: 'text/plain'})).lastModified", 20000);
98 // Test that Date instances are implicitly converted to numbers.
99 shouldBe("(new File([], 'world.html', {lastModified: new Date(441532800000)})).lastModified", "441532800000");
101 // Test the deprecated attribute lastModifiedDate.
102 shouldBeTrue("(new File([], 'world.html', {lastModified: 441532800000})).lastModifiedDate instanceof Date");
103 shouldBe("(new File([], 'world.html', {lastModified: 441532800000})).lastModifiedDate.valueOf()", "441532800000");
105 // Test the number of expected arguments in the File constructor.
106 shouldBe("window.File.length", "2");
108 // Test ArrayBufferView parameters.
109 shouldBe("new File([new DataView(new ArrayBuffer(100))], 'world.html').size", "100");
110 shouldBe("new File([new Uint8Array(100)], 'world.html').size", "100");
111 shouldBe("new File([new Uint8ClampedArray(100)], 'world.html').size", "100");
112 shouldBe("new File([new Uint16Array(100)], 'world.html').size", "200");
113 shouldBe("new File([new Uint32Array(100)], 'world.html').size", "400");
114 shouldBe("new File([new Int8Array(100)], 'world.html').size", "100");
115 shouldBe("new File([new Int16Array(100)], 'world.html').size", "200");
116 shouldBe("new File([new Int32Array(100)], 'world.html').size", "400");
117 shouldBe("new File([new Float32Array(100)], 'world.html').size", "400");
118 shouldBe("new File([new Float64Array(100)], 'world.html').size", "800");
119 shouldBe("new File([new Float64Array(100), new Int32Array(100), new Uint8Array(100), new DataView(new ArrayBuffer(100))], 'world.html').size", "1400");
120 shouldBe("new File([new Blob([new Int32Array(100)]), new Uint8Array(100), new Float32Array(100), new DataView(new ArrayBuffer(100))], 'world.html').size", "1000");
121 shouldBe("new File([new Blob([new Int32Array(100)]), new File([new Uint16Array(100)], 'world.txt'), new Uint8Array(100), new Float32Array(100), new DataView(new ArrayBuffer(100))], 'world.html').size", "1200");
123 // Test ArrayBuffer parameters.
124 shouldBe("new File([(new DataView(new ArrayBuffer(100))).buffer], 'world.html').size", "100");
125 shouldBe("new File([(new Uint8Array(100)).buffer], 'world.html').size", "100");
126 shouldBe("new File([(new Uint8ClampedArray(100)).buffer], 'world.html').size", "100");
127 shouldBe("new File([(new Uint16Array(100)).buffer], 'world.html').size", "200");
128 shouldBe("new File([(new Uint32Array(100)).buffer], 'world.html').size", "400");
129 shouldBe("new File([(new Int8Array(100)).buffer], 'world.html').size", "100");
130 shouldBe("new File([(new Int16Array(100)).buffer], 'world.html').size", "200");
131 shouldBe("new File([(new Int32Array(100)).buffer], 'world.html').size", "400");
132 shouldBe("new File([(new Float32Array(100)).buffer], 'world.html').size", "400");
133 shouldBe("new File([(new Float64Array(100)).buffer], 'world.html').size", "800");
134 shouldBe("new File([(new Float64Array(100)).buffer, (new Int32Array(100)).buffer, (new Uint8Array(100)).buffer, (new DataView(new ArrayBuffer(100))).buffer], 'world.html').size", "1400");
135 shouldBe("new File([new Blob([(new Int32Array(100)).buffer]), (new Uint8Array(100)).buffer, (new Float32Array(100)).buffer, (new DataView(new ArrayBuffer(100))).buffer], 'world.html').size", "1000");
136 shouldBe("new File([new Blob([(new Int32Array(100)).buffer]), new File([new Uint16Array(100).buffer], 'world.txt'), (new Uint8Array(100)).buffer, (new Float32Array(100)).buffer, (new DataView(new ArrayBuffer(100))).buffer], 'world.html').size", "1200");
138 // Test building Blobs with ArrayBuffer / ArrayBufferView parts enclosed in files.
139 shouldBe("new Blob([new Blob([new Int32Array(100)]), new File([new Uint16Array(100)], 'world.txt'), new Uint8Array(100), new Float32Array(100), new DataView(new ArrayBuffer(100))]).size", "1200");
140 shouldBe("new Blob([new Blob([(new Int32Array(100)).buffer]), new File([new Uint16Array(100).buffer], 'world.txt'), (new Uint8Array(100)).buffer, (new Float32Array(100)).buffer, (new DataView(new ArrayBuffer(100))).buffer]).size", "1200");
142 // Test passing blob parts in objects with indexed properties.
143 // (This depends on the bindings code handling of sequence<T>)
144 shouldBe("new File({length: 0}, 'world.txt').size", "0");
145 shouldBe("new File({length: 1, 0: 'string'}, 'world.txt').size", "6");