blob: 0ef610c789d86f1820cb64a904be539240befc72 [file] [log] [blame]
beidson@apple.com07faaf92016-04-25 17:31:29 +00001<!DOCTYPE html>
2
3<script src="../../resources/js-test.js"></script>
4<script>
5description("Test the File constructor.");
6
7// Test the different ways you can construct a File.
8shouldBeTrue("(new File([], 'world.html')) instanceof window.File");
9shouldBeTrue("(new File(['hello'], 'world.html')) instanceof window.File");
10shouldBeTrue("(new File(['hello'], 'world.html', {})) instanceof window.File");
11shouldBeTrue("(new File(['hello'], 'world.html', {type:'text/html'})) instanceof window.File");
12shouldBeTrue("(new File(['hello'], 'world.html', {type:'text/html', endings:'native'})) instanceof window.File");
13shouldBeTrue("(new File(['hello'], 'world.html', {type:'text/html', endings:'transparent'})) instanceof window.File");
14
15// Test that File inherits from File.
16shouldBeTrue("(new File([], 'world.html')) instanceof window.File")
17
18// Verify that the file name argument is required.
weinig@apple.comdba59c92016-12-01 18:04:58 +000019shouldThrow("(new File())", "'TypeError: Not enough arguments'");
20shouldThrow("(new File([]))", "'TypeError: Not enough arguments'");
beidson@apple.com07faaf92016-04-25 17:31:29 +000021
22// Test valid file names.
23shouldBeTrue("(new File([], null)) instanceof window.File");
24shouldBeTrue("(new File([], 1)) instanceof window.File");
25shouldBeTrue("(new File([], '')) instanceof window.File");
26shouldBeTrue("(new File([], document)) instanceof window.File");
27
28// Test invalid file parts.
29shouldThrow("new File('hello', 'world.html')", '"TypeError: Value is not a sequence"');
30shouldThrow("new File(0, 'world.html')", '"TypeError: Value is not a sequence"');
weinig@apple.comdba59c92016-12-01 18:04:58 +000031shouldThrow("new File(null, 'world.html')", "'TypeError: Value is not a sequence'");
beidson@apple.com07faaf92016-04-25 17:31:29 +000032
33// Test valid file parts.
34shouldBeTrue("(new File([], 'world.html')) instanceof window.File");
35shouldBeTrue("(new File(['stringPrimitive'], 'world.html')) instanceof window.File");
36shouldBeTrue("(new File([String('stringObject')], 'world.html')) instanceof window.File");
37shouldBeTrue("(new File([new Blob], 'world.html')) instanceof window.File");
38shouldBeTrue("(new File([new Blob([new Blob])], 'world.html')) instanceof window.File");
39
40// Test File instances used as blob parts.
41shouldBeTrue("(new Blob([new File([], 'world.txt')])) instanceof window.Blob");
42shouldBeTrue("(new Blob([new Blob([new File([new Blob], 'world.txt')])])) instanceof window.Blob");
43shouldBeTrue("(new File([new File([], 'world.txt')], 'world.html')) instanceof window.File");
44shouldBeTrue("(new File([new Blob([new File([new Blob], 'world.txt')])], 'world.html')) instanceof window.File");
45
46// Test some conversions to string in the parts array.
47shouldBe("(new File([12], 'world.html')).size", "2");
48shouldBe("(new File([[]], 'world.html')).size", "0"); // [].toString() is the empty string
49shouldBe("(new File([{}], 'world.html')).size", "15");; // {}.toString() is the string "[object Object]"
50shouldBe("(new File([document], 'world.html')).size", "21"); // document.toString() is the string "[object HTMLDocument]"
51
52var toStringingObj = { toString: function() { return "A string"; } };
53shouldBe("(new File([toStringingObj], 'world.html')).size", "8");
54
55var throwingObj = { toString: function() { throw "Error"; } };
56shouldThrow("new File([throwingObj], 'world.html')", "'Error'");
57
58// Test some conversions to string in the file name.
59shouldBe("(new File([], null)).name", "'null'");
60shouldBe("(new File([], 12)).name", "'12'");
61shouldBe("(new File([], '')).name", "''");
62shouldBe("(new File([], {})).name", "'[object Object]'");
63shouldBe("(new File([], document)).name", "'[object HTMLDocument]'");
64shouldBe("(new File([], toStringingObj)).name", "'A string'");
65shouldThrow("(new File([], throwingObj)).name", "'Error'");
66
67// Test some invalid property bags.
68shouldBeTrue("(new File([], 'world.html', {unknownKey:'value'})) instanceof window.File");
69shouldThrow("new File([], 'world.html', {type:throwingObj})", "'Error'");
70
71// Type with non-ascii characters should become the empty string.
72shouldBeTrue("(new File([], 'world.html', {type:'hello\u00EE'})) instanceof window.File");
73shouldBe("(new File([], 'world.html', {type:'hello\u00EE'})).type", "''");
74
weinig@apple.comdba59c92016-12-01 18:04:58 +000075// FilePropertyBag substeps: Type with non-ascii characters should not prevent lastModified from being extracted.
nael.ouedraogo@crf.canon.fr950fe202016-08-26 08:04:02 +000076shouldBe("(new File([], 'world.html', {lastModified: 555, type:'goodbye'})).lastModified", "555");
weinig@apple.comdba59c92016-12-01 18:04:58 +000077shouldBe("(new File([], 'world.html', {lastModified: 555, type:'goodbye\u00EE'})).lastModified", "555");
beidson@apple.com07faaf92016-04-25 17:31:29 +000078
79// Test various non-object literals being used as property bags.
80shouldBeTrue("(new File([], 'world.html', null)) instanceof window.File");
81shouldBeTrue("(new File([], 'world.html', undefined)) instanceof window.File");
weinig@apple.comdba59c92016-12-01 18:04:58 +000082shouldThrow("(new File([], 'world.html', 123)) instanceof window.File", "'TypeError: Type error'");
83shouldThrow("(new File([], 'world.html', 123.4)) instanceof window.File", "'TypeError: Type error'");
84shouldThrow("(new File([], 'world.html', true)) instanceof window.File", "'TypeError: Type error'");
85shouldThrow("(new File([], 'world.html', 'abc')) instanceof window.File", "'TypeError: Type error'");
beidson@apple.com07faaf92016-04-25 17:31:29 +000086shouldBeTrue("(new File([], 'world.html', [])) instanceof window.File");
commit-queue@webkit.orgae8fa2a2017-07-03 03:42:35 +000087shouldBeTrue("(new File([], 'world.html', /abc/)) instanceof window.File");
beidson@apple.com07faaf92016-04-25 17:31:29 +000088shouldBeTrue("(new File([], 'world.html', function () {})) instanceof window.File");
89
90// Test that the name/type/size are correctly added to the File.
91shouldBe("(new File([], 'world.html')).name", "'world.html'");
weinig@apple.comdba59c92016-12-01 18:04:58 +000092shouldBe("(new File([], 'w/orld/ht/m.l')).name", "'w/orld/ht/m.l'");
beidson@apple.com07faaf92016-04-25 17:31:29 +000093shouldBe("(new File([], 'world.html', {type:'text/html'})).name", "'world.html'");
94shouldBe("(new File([], 'world.html', {type:'text/html'})).type", "'text/html'");
95shouldBe("(new File([], 'world.html', {type:'text/html'})).size", "0");
96shouldBe("(new File([], 'world.html', {type:'text/plain;charset=UTF-8'})).type", "'text/plain;charset=utf-8'");
97
98// Test that the lastModified is correctly added to the File.
99var aDate = new Date(441532800000);
100shouldBe("(new File([], 'world.html', {lastModified: 441532800000})).lastModified", "441532800000");
101// Unless specified, lastModified should default to now.
102shouldBeNow("(new File([], 'world.html')).lastModified", 20000);
103shouldBeNow("(new File([], 'world.html', {})).lastModified", 20000);
104shouldBeNow("(new File([], 'world.html', {type: 'text/plain'})).lastModified", 20000);
105// Test that Date instances are implicitly converted to numbers.
106shouldBe("(new File([], 'world.html', {lastModified: new Date(441532800000)})).lastModified", "441532800000");
107
108// Test the number of expected arguments in the File constructor.
109shouldBe("window.File.length", "2");
110
111// Test ArrayBufferView parameters.
112shouldBe("new File([new DataView(new ArrayBuffer(100))], 'world.html').size", "100");
113shouldBe("new File([new Uint8Array(100)], 'world.html').size", "100");
114shouldBe("new File([new Uint8ClampedArray(100)], 'world.html').size", "100");
115shouldBe("new File([new Uint16Array(100)], 'world.html').size", "200");
116shouldBe("new File([new Uint32Array(100)], 'world.html').size", "400");
117shouldBe("new File([new Int8Array(100)], 'world.html').size", "100");
118shouldBe("new File([new Int16Array(100)], 'world.html').size", "200");
119shouldBe("new File([new Int32Array(100)], 'world.html').size", "400");
120shouldBe("new File([new Float32Array(100)], 'world.html').size", "400");
121shouldBe("new File([new Float64Array(100)], 'world.html').size", "800");
122shouldBe("new File([new Float64Array(100), new Int32Array(100), new Uint8Array(100), new DataView(new ArrayBuffer(100))], 'world.html').size", "1400");
123shouldBe("new File([new Blob([new Int32Array(100)]), new Uint8Array(100), new Float32Array(100), new DataView(new ArrayBuffer(100))], 'world.html').size", "1000");
124shouldBe("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");
125
126// Test ArrayBuffer parameters.
127shouldBe("new File([(new DataView(new ArrayBuffer(100))).buffer], 'world.html').size", "100");
128shouldBe("new File([(new Uint8Array(100)).buffer], 'world.html').size", "100");
129shouldBe("new File([(new Uint8ClampedArray(100)).buffer], 'world.html').size", "100");
130shouldBe("new File([(new Uint16Array(100)).buffer], 'world.html').size", "200");
131shouldBe("new File([(new Uint32Array(100)).buffer], 'world.html').size", "400");
132shouldBe("new File([(new Int8Array(100)).buffer], 'world.html').size", "100");
133shouldBe("new File([(new Int16Array(100)).buffer], 'world.html').size", "200");
134shouldBe("new File([(new Int32Array(100)).buffer], 'world.html').size", "400");
135shouldBe("new File([(new Float32Array(100)).buffer], 'world.html').size", "400");
136shouldBe("new File([(new Float64Array(100)).buffer], 'world.html').size", "800");
137shouldBe("new File([(new Float64Array(100)).buffer, (new Int32Array(100)).buffer, (new Uint8Array(100)).buffer, (new DataView(new ArrayBuffer(100))).buffer], 'world.html').size", "1400");
138shouldBe("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");
139shouldBe("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");
140
141// Test building Blobs with ArrayBuffer / ArrayBufferView parts enclosed in files.
142shouldBe("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");
143shouldBe("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");
beidson@apple.com07faaf92016-04-25 17:31:29 +0000144</script>