|
6 | 6 | <script src="/resources/testharnessreport.js"></script>
|
7 | 7 | <div id="log"></div>
|
8 | 8 | <script>
|
| 9 | +const to_string_obj = { toString: () => 'a string' }; |
| 10 | +const to_string_throws = { toString: () => { throw new Error('expected'); } }; |
| 11 | + |
9 | 12 | test(function() {
|
10 | 13 | assert_true("File" in window, "window should have a File property.");
|
11 | 14 | }, "File interface object exists");
|
12 | 15 |
|
| 16 | +test(t => { |
| 17 | + assert_throws(new TypeError(), () => new File(), |
| 18 | + 'Bits argument is required'); |
| 19 | + assert_throws(new TypeError(), () => new File([]), |
| 20 | + 'Name argument is required'); |
| 21 | +}, 'Required arguments'); |
| 22 | + |
13 | 23 | function test_first_argument(arg1, expectedSize, testName) {
|
14 | 24 | test(function() {
|
15 | 25 | var file = new File(arg1, "dummy");
|
|
22 | 32 | }, testName);
|
23 | 33 | }
|
24 | 34 |
|
| 35 | +test_first_argument([], 0, "empty fileBits"); |
25 | 36 | test_first_argument(["bits"], 4, "DOMString fileBits");
|
26 | 37 | test_first_argument(["𝓽𝓮𝔁𝓽"], 16, "Unicode DOMString fileBits");
|
| 38 | +test_first_argument([new String('string object')], 13, "String object fileBits"); |
27 | 39 | test_first_argument([new Blob()], 0, "Empty Blob fileBits");
|
28 | 40 | test_first_argument([new Blob(["bits"])], 4, "Blob fileBits");
|
| 41 | +test_first_argument([new File([], 'world.txt')], 0, "Empty File fileBits"); |
| 42 | +test_first_argument([new File(["bits"], 'world.txt')], 4, "File fileBits"); |
29 | 43 | test_first_argument([new ArrayBuffer(8)], 8, "ArrayBuffer fileBits");
|
30 | 44 | test_first_argument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4, "Typed array fileBits");
|
31 | 45 | test_first_argument(["bits", new Blob(["bits"]), new Blob(), new Uint8Array([0x50, 0x41]),
|
32 | 46 | new Uint16Array([0x5353]), new Uint32Array([0x53534150])], 16, "Various fileBits");
|
| 47 | +test_first_argument([12], 2, "Number in fileBits"); |
| 48 | +test_first_argument([[1,2,3]], 5, "Array in fileBits"); |
| 49 | +test_first_argument([{}], 15, "Object in fileBits"); // "[object Object]" |
| 50 | +test_first_argument([document], 21, "HTMLDocument in fileBits"); // "[object HTMLDocument]" |
| 51 | +test_first_argument([to_string_obj], 8, "Object with toString in fileBits"); |
| 52 | +test_first_argument({[Symbol.iterator]() { |
| 53 | + let i = 0; |
| 54 | + return {next: () => [ |
| 55 | + {done:false, value:'ab'}, |
| 56 | + {done:false, value:'cde'}, |
| 57 | + {done:true} |
| 58 | + ][i++]}; |
| 59 | +}}, 5, 'Custom @@iterator'); |
| 60 | + |
| 61 | +[ |
| 62 | + 'hello', |
| 63 | + 0, |
| 64 | + null |
| 65 | +].forEach(arg => { |
| 66 | + test(t => { |
| 67 | + assert_throws(new TypeError(), () => new File(arg, 'world.html'), |
| 68 | + 'Constructor should throw for invalid bits argument'); |
| 69 | + }, `Invalid bits argument: ${JSON.stringify(arg)}`); |
| 70 | +}); |
| 71 | + |
| 72 | +test(t => { |
| 73 | + assert_throws(new Error(), () => new File([to_string_throws], 'name.txt'), |
| 74 | + 'Constructor should propagate exceptions'); |
| 75 | +}, 'Bits argument: object that throws'); |
| 76 | + |
33 | 77 |
|
34 | 78 | function test_second_argument(arg2, expectedFileName, testName) {
|
35 | 79 | test(function() {
|
|
41 | 85 |
|
42 | 86 | test_second_argument("dummy", "dummy", "Using fileName");
|
43 | 87 | test_second_argument("dummy/foo", "dummy:foo", "Using special character in fileName");
|
| 88 | +test_second_argument(null, "null", "Using null fileName"); |
| 89 | +test_second_argument(1, "1", "Using number fileName"); |
| 90 | +test_second_argument('', '', "Using empty string fileName"); |
| 91 | +test_second_argument(document, '[object HTMLDocument]', "Using object fileName"); |
44 | 92 |
|
45 | 93 | // testing the third argument
|
46 |
| -test(function() { |
47 |
| - var file = new File(["bits"], "dummy", { type: "text/plain"}); |
48 |
| - assert_true(file instanceof File); |
49 |
| - assert_equals(file.type, "text/plain"); |
50 |
| -}, "Using type on the File constructor"); |
51 |
| -test(function() { |
52 |
| - var file = new File(["bits"], "dummy", { type: "TEXT/PLAIN"}); |
53 |
| - assert_true(file instanceof File); |
54 |
| - assert_equals(file.type, "text/plain"); |
55 |
| -}, "Using uppercase characters in type"); |
56 |
| -test(function() { |
57 |
| - var file = new File(["bits"], "dummy", { type: "𝓽𝓮𝔁𝓽/𝔭𝔩𝔞𝔦𝔫"}); |
58 |
| - assert_true(file instanceof File); |
59 |
| - assert_equals(file.type, ""); |
60 |
| -}, "Using illegal character for type"); |
| 94 | +[ |
| 95 | + {type: 'text/plain', expected: 'text/plain'}, |
| 96 | + {type: 'text/plain;charset=UTF-8', expected: 'text/plain;charset=utf-8'}, |
| 97 | + {type: 'TEXT/PLAIN', expected: 'text/plain'}, |
| 98 | + {type: '𝓽𝓮𝔁𝓽/𝔭𝔩𝔞𝔦𝔫', expected: ''}, |
| 99 | + {type: 'ascii/nonprintable\u001F', expected: ''}, |
| 100 | + {type: 'ascii/nonprintable\u007F', expected: ''}, |
| 101 | + {type: 'nonascii\u00EE', expected: ''}, |
| 102 | + {type: 'nonascii\u1234', expected: ''}, |
| 103 | + {type: 'nonparsable', expected: 'nonparsable'} |
| 104 | +].forEach(testCase => { |
| 105 | + test(t => { |
| 106 | + var file = new File(["bits"], "dummy", { type: testCase.type}); |
| 107 | + assert_true(file instanceof File); |
| 108 | + assert_equals(file.type, testCase.expected); |
| 109 | + }, `Using type in File constructor: ${testCase.type}`); |
| 110 | +}); |
61 | 111 | test(function() {
|
62 | 112 | var file = new File(["bits"], "dummy", { lastModified: 42 });
|
63 | 113 | assert_true(file instanceof File);
|
|
68 | 118 | assert_true(file instanceof File);
|
69 | 119 | assert_equals(file.name, "dummy");
|
70 | 120 | }, "Misusing name");
|
| 121 | +test(function() { |
| 122 | + var file = new File(["bits"], "dummy", { unknownKey: "value" }); |
| 123 | + assert_true(file instanceof File); |
| 124 | + assert_equals(file.name, "dummy"); |
| 125 | +}, "Unknown properties are ignored"); |
| 126 | + |
| 127 | +[ |
| 128 | + 123, |
| 129 | + 123.4, |
| 130 | + true, |
| 131 | + 'abc' |
| 132 | +].forEach(arg => { |
| 133 | + test(t => { |
| 134 | + assert_throws(new TypeError(), () => new File(['bits'], 'name.txt', arg), |
| 135 | + 'Constructor should throw for invalid property bag type'); |
| 136 | + }, `Invalid property bag: ${JSON.stringify(arg)}`); |
| 137 | +}); |
| 138 | + |
| 139 | +[ |
| 140 | + null, |
| 141 | + undefined, |
| 142 | + [1,2,3], |
| 143 | + /regex/, |
| 144 | + function() {} |
| 145 | +].forEach(arg => { |
| 146 | + test(t => { |
| 147 | + assert_equals(new File(['bits'], 'name.txt', arg).size, 4, |
| 148 | + 'Constructor should accept object-ish property bag type'); |
| 149 | + }, `Unusual but valid property bag: ${arg}`); |
| 150 | +}); |
| 151 | + |
| 152 | +test(t => { |
| 153 | + assert_throws(new Error(), |
| 154 | + () => new File(['bits'], 'name.txt', {type: to_string_throws}), |
| 155 | + 'Constructor should propagate exceptions'); |
| 156 | +}, 'Property bag propagates exceptions'); |
71 | 157 |
|
72 | 158 | </script>
|
0 commit comments