Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: LayoutTests/fast/files/file-constructor.html

Issue 57483002: Implement File constructor. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebased against master, updated patch. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 diffrent 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'");
kinuko 2013/11/14 05:41:06 Maybe also test 'new File([])' case?
pwnall-personal 2013/11/14 14:31:19 Done. Thank you, this is a great idea!
20
21 // Test valid file names.
22 shouldBeTrue("(new File([], null)) instanceof window.File");
23 shouldBeTrue("(new File([], 1)) instanceof window.File");
24 shouldBeTrue("(new File([], document)) instanceof window.File");
25
26 // Test invalid blob parts
kinuko 2013/11/14 05:41:06 nit: please end comment with '.'
pwnall-personal 2013/11/14 14:31:19 Done. I fixed this throughout the file.
27 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."');
28 shouldThrow("new File(0, 'world.html')", '"TypeError: Failed to construct \'File \': The 1st argument is neither an array, nor does it have indexed properties."' );
29
30 // Test valid blob parts.
31 shouldBeTrue("(new File([], 'world.html')) instanceof window.File");
32 shouldBeTrue("(new File(['stringPrimitive'], 'world.html')) instanceof window.Fi le");
33 shouldBeTrue("(new File([String('stringObject')], 'world.html')) instanceof wind ow.File");
34 shouldBeTrue("(new File([new Blob], 'world.html')) instanceof window.File");
35 shouldBeTrue("(new File([new Blob([new Blob])], 'world.html')) instanceof window .File");
36
37 // Test File instances used as blob parts.
38 shouldBeTrue("(new Blob([new File([], 'world.txt')])) instanceof window.Blob");
39 shouldBeTrue("(new Blob([new Blob([new File([new Blob], 'world.txt')])])) instan ceof window.Blob");
40 shouldBeTrue("(new File([new File([], 'world.txt')], 'world.html')) instanceof w indow.File");
41 shouldBeTrue("(new File([new Blob([new File([new Blob], 'world.txt')])], 'world. html')) instanceof window.File");
42
43 // Test some conversions to string in the parts array.
44 shouldBe("(new File([12], 'world.html')).size", "2");
45 shouldBe("(new File([[]], 'world.html')).size", "0"); // [].toString() i s the empty string
46 shouldBe("(new File([{}], 'world.html')).size", "15");; // {}.toString() i s the string "[object Object]"
47 shouldBe("(new File([document], 'world.html')).size", "21"); // document.toStri ng() is the string "[object HTMLDocument]"
48
49 var toStringingObj = { toString: function() { return "A string"; } };
50 shouldBe("(new File([toStringingObj], 'world.html')).size", "8");
51
52 var throwingObj = { toString: function() { throw "Error"; } };
53 shouldThrow("new File([throwingObj], 'world.html')", "'Error'");
54
55 // Test some conversions to string in the file name.
56 shouldBe("(new File([], null)).name", "'null'");
57 shouldBe("(new File([], 12)).name", "'12'");
58 shouldBe("(new File([], {})).name", "'[object Object]'");
59 shouldBe("(new File([], document)).name", "'[object HTMLDocument]'");
60 shouldBe("(new File([], toStringingObj)).name", "'A string'");
61 shouldThrow("(new File([], throwingObj)).name", "'Error'");
62
63 // Test some invalid property bags.
64 shouldBeTrue("(new File([], 'world.html', {unknownKey:'value'})) instanceof wind ow.File"); // Ignore invalid keys
65 shouldThrow("new File([], 'world.html', {endings:'illegalValue'})", "'TypeError: Failed to construct \\'File\\': The \"endings\" property must be either \"trans parent\" or \"native\".'");
66 shouldThrow("new File([], 'world.html', {endings:throwingObj})", "'Error'");
67 shouldThrow("new File([], 'world.html', {type:throwingObj})", "'Error'");
68 shouldThrow("new File([], 'world.html', {type:'hello\u00EE'})", "'SyntaxError: F ailed to construct \\'File\\': The \"type\" property must consist of ASCII chara cters.'");
69
70 // Test various non-object literals being used as property bags.
71 shouldThrow("(new File([], 'world.html', null)) instanceof window.File", "'TypeE rror: Failed to construct \\'File\\': The 3rd argument is not of type Object.'") ;
72 shouldThrow("(new File([], 'world.html', undefined)) instanceof window.File", "' TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Objec t.'");
73 shouldThrow("(new File([], 'world.html', 123)) instanceof window.File", "'TypeEr ror: Failed to construct \\'File\\': The 3rd argument is not of type Object.'");
74 shouldThrow("(new File([], 'world.html', 123.4)) instanceof window.File", "'Type Error: Failed to construct \\'File\\': The 3rd argument is not of type Object.'" );
75 shouldThrow("(new File([], 'world.html', true)) instanceof window.File", "'TypeE rror: Failed to construct \\'File\\': The 3rd argument is not of type Object.'") ;
76 shouldThrow("(new File([], 'world.html', 'abc')) instanceof window.File", "'Type Error: Failed to construct \\'File\\': The 3rd argument is not of type Object.'" );
77 shouldBeTrue("(new File([], 'world.html', [])) instanceof window.File");
78 shouldBeTrue("(new File([], 'world.html', /abc/)) instanceof window.File");
79 shouldBeTrue("(new File([], 'world.html', function () {})) instanceof window.Fil e");
80
81 // Test that the name/type/size are correctly added to the File.
82 shouldBe("(new File([], 'world.html', {type:'text/html'})).name", "'world.html'" );
83 shouldBe("(new File([], 'world.html', {type:'text/html'})).type", "'text/html'") ;
84 shouldBe("(new File([], 'world.html', {type:'text/html'})).size", "0");
85 shouldBe("(new File([], 'world.html', {type:'text/plain;charset=UTF-8'})).type", "'text/plain;charset=utf-8'");
86
87 // Test the number of expected arguments in the File constructor.
88 shouldBe("window.File.length", "2");
89
90 // Test ArrayBufferView Parameters.
91 shouldBe("new File([new DataView(new ArrayBuffer(100))], 'world.html').size", "1 00");
92 shouldBe("new File([new Uint8Array(100)], 'world.html').size", "100");
93 shouldBe("new File([new Uint8ClampedArray(100)], 'world.html').size", "100");
94 shouldBe("new File([new Uint16Array(100)], 'world.html').size", "200");
95 shouldBe("new File([new Uint32Array(100)], 'world.html').size", "400");
96 shouldBe("new File([new Int8Array(100)], 'world.html').size", "100");
97 shouldBe("new File([new Int16Array(100)], 'world.html').size", "200");
98 shouldBe("new File([new Int32Array(100)], 'world.html').size", "400");
99 shouldBe("new File([new Float32Array(100)], 'world.html').size", "400");
100 shouldBe("new File([new Float64Array(100)], 'world.html').size", "800");
101 shouldBe("new File([new Float64Array(100), new Int32Array(100), new Uint8Array(1 00), new DataView(new ArrayBuffer(100))], 'world.html').size", "1400");
102 shouldBe("new File([new Blob([new Int32Array(100)]), new Uint8Array(100), new Fl oat32Array(100), new DataView(new ArrayBuffer(100))], 'world.html').size", "1000 ");
103 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");
104
105 // Test ArrayBuffer Parameters.
106 shouldBe("new File([(new DataView(new ArrayBuffer(100))).buffer], 'world.html'). size", "100");
107 shouldBe("new File([(new Uint8Array(100)).buffer], 'world.html').size", "100");
108 shouldBe("new File([(new Uint8ClampedArray(100)).buffer], 'world.html').size", " 100");
109 shouldBe("new File([(new Uint16Array(100)).buffer], 'world.html').size", "200");
110 shouldBe("new File([(new Uint32Array(100)).buffer], 'world.html').size", "400");
111 shouldBe("new File([(new Int8Array(100)).buffer], 'world.html').size", "100");
112 shouldBe("new File([(new Int16Array(100)).buffer], 'world.html').size", "200");
113 shouldBe("new File([(new Int32Array(100)).buffer], 'world.html').size", "400");
114 shouldBe("new File([(new Float32Array(100)).buffer], 'world.html').size", "400") ;
115 shouldBe("new File([(new Float64Array(100)).buffer], 'world.html').size", "800") ;
116 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");
117 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");
118 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");
119
120 // Test building Blobs with ArrayBuffer / ArrayBufferView parts enclosed in file s.
121 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");
122 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");
123
124 // Test passing blob parts in sequences.
125 shouldBeTrue("new File({length: 0}, 'world.txt') instanceof window.File");
126 shouldBe("new File({length: 0}, 'world.txt').size", "0");
127 shouldBe("new File({length: 1, 0: 'string'}, 'world.txt').size", "6");
128 shouldBe("new File({length: 2, 0: new Uint8Array(100), 1: new Int16Array(100)}, 'world.txt').size", "300");
129 shouldBe("new File({length: 1, 0: 'string'}, 'world.txt', {type: 'text/html'}).t ype", "'text/html'");
130 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\".'");
131
132 </script>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698