f7c8eb8e21ae3f3e94258e1dcbca474f18e69053
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / fast / files / file-constructor.html
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:'native'})) instanceof window.File");
13 shouldBeTrue("(new File(['hello'], 'world.html', {type:'text/html', endings:'transparent'})) 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: 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."');
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 file parts.
29 shouldThrow("new File('hello', 'world.html')", '"TypeError: Failed to construct \'File\': The 1st argument provided is either null, or an invalid Array object."');
30 shouldThrow("new File(0, 'world.html')", '"TypeError: Failed to construct \'File\': The 1st argument provided is either null, or an invalid Array object."');
31 shouldThrow("new File(null, 'world.html')", '"TypeError: Failed to construct \'File\': The 1st argument provided is either null, or an invalid Array object."');
32
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");
39
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");
45
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]"
51
52 var toStringingObj = { toString: function() { return "A string"; } };
53 shouldBe("(new File([toStringingObj], 'world.html')).size", "8");
54
55 var throwingObj = { toString: function() { throw "Error"; } };
56 shouldThrow("new File([throwingObj], 'world.html')", "'Error'");
57
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'");
66
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 \\'endings\\' property must be either \\'transparent\\' or \\'native\\'.'");
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.'");
73
74 // Test various non-object literals being used as property bags.
75 shouldThrow("(new File([], 'world.html', null)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Object.'");
76 shouldThrow("(new File([], 'world.html', undefined)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Object.'");
77 shouldThrow("(new File([], 'world.html', 123)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Object.'");
78 shouldThrow("(new File([], 'world.html', 123.4)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Object.'");
79 shouldThrow("(new File([], 'world.html', true)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Object.'");
80 shouldThrow("(new File([], 'world.html', 'abc')) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type 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");
84
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'");
90
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");
100
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");
104
105 // Test the number of expected arguments in the File constructor.
106 shouldBe("window.File.length", "2");
107
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");
122
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");
137
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");
141
142 // Test passing file parts in (deprecated and unsupported) objects with indexed properties.
143 shouldThrow("new File({length: 0}, 'world.txt')", "'TypeError: Failed to construct \\'File\\': The 1st argument provided is either null, or an invalid Array object.'");
144 shouldThrow("new File({length: 1, 0: 'string'}, 'world.txt')", "'TypeError: Failed to construct \\'File\\': The 1st argument provided is either null, or an invalid Array object.'");
145 </script>