beidson@apple.com | 07faaf9 | 2016-04-25 17:31:29 +0000 | [diff] [blame] | 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. |
weinig@apple.com | dba59c9 | 2016-12-01 18:04:58 +0000 | [diff] [blame] | 19 | shouldThrow("(new File())", "'TypeError: Not enough arguments'"); |
| 20 | shouldThrow("(new File([]))", "'TypeError: Not enough arguments'"); |
beidson@apple.com | 07faaf9 | 2016-04-25 17:31:29 +0000 | [diff] [blame] | 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: Value is not a sequence"'); |
| 30 | shouldThrow("new File(0, 'world.html')", '"TypeError: Value is not a sequence"'); |
weinig@apple.com | dba59c9 | 2016-12-01 18:04:58 +0000 | [diff] [blame] | 31 | shouldThrow("new File(null, 'world.html')", "'TypeError: Value is not a sequence'"); |
beidson@apple.com | 07faaf9 | 2016-04-25 17:31:29 +0000 | [diff] [blame] | 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"); |
| 69 | shouldThrow("new File([], 'world.html', {type:throwingObj})", "'Error'"); |
| 70 | |
| 71 | // Type with non-ascii characters should become the empty string. |
| 72 | shouldBeTrue("(new File([], 'world.html', {type:'hello\u00EE'})) instanceof window.File"); |
| 73 | shouldBe("(new File([], 'world.html', {type:'hello\u00EE'})).type", "''"); |
| 74 | |
weinig@apple.com | dba59c9 | 2016-12-01 18:04:58 +0000 | [diff] [blame] | 75 | // FilePropertyBag substeps: Type with non-ascii characters should not prevent lastModified from being extracted. |
nael.ouedraogo@crf.canon.fr | 950fe20 | 2016-08-26 08:04:02 +0000 | [diff] [blame] | 76 | shouldBe("(new File([], 'world.html', {lastModified: 555, type:'goodbye'})).lastModified", "555"); |
weinig@apple.com | dba59c9 | 2016-12-01 18:04:58 +0000 | [diff] [blame] | 77 | shouldBe("(new File([], 'world.html', {lastModified: 555, type:'goodbye\u00EE'})).lastModified", "555"); |
beidson@apple.com | 07faaf9 | 2016-04-25 17:31:29 +0000 | [diff] [blame] | 78 | |
| 79 | // Test various non-object literals being used as property bags. |
| 80 | shouldBeTrue("(new File([], 'world.html', null)) instanceof window.File"); |
| 81 | shouldBeTrue("(new File([], 'world.html', undefined)) instanceof window.File"); |
weinig@apple.com | dba59c9 | 2016-12-01 18:04:58 +0000 | [diff] [blame] | 82 | shouldThrow("(new File([], 'world.html', 123)) instanceof window.File", "'TypeError: Type error'"); |
| 83 | shouldThrow("(new File([], 'world.html', 123.4)) instanceof window.File", "'TypeError: Type error'"); |
| 84 | shouldThrow("(new File([], 'world.html', true)) instanceof window.File", "'TypeError: Type error'"); |
| 85 | shouldThrow("(new File([], 'world.html', 'abc')) instanceof window.File", "'TypeError: Type error'"); |
beidson@apple.com | 07faaf9 | 2016-04-25 17:31:29 +0000 | [diff] [blame] | 86 | shouldBeTrue("(new File([], 'world.html', [])) instanceof window.File"); |
commit-queue@webkit.org | ae8fa2a | 2017-07-03 03:42:35 +0000 | [diff] [blame] | 87 | shouldBeTrue("(new File([], 'world.html', /abc/)) instanceof window.File"); |
beidson@apple.com | 07faaf9 | 2016-04-25 17:31:29 +0000 | [diff] [blame] | 88 | shouldBeTrue("(new File([], 'world.html', function () {})) instanceof window.File"); |
| 89 | |
| 90 | // Test that the name/type/size are correctly added to the File. |
| 91 | shouldBe("(new File([], 'world.html')).name", "'world.html'"); |
weinig@apple.com | dba59c9 | 2016-12-01 18:04:58 +0000 | [diff] [blame] | 92 | shouldBe("(new File([], 'w/orld/ht/m.l')).name", "'w/orld/ht/m.l'"); |
beidson@apple.com | 07faaf9 | 2016-04-25 17:31:29 +0000 | [diff] [blame] | 93 | shouldBe("(new File([], 'world.html', {type:'text/html'})).name", "'world.html'"); |
| 94 | shouldBe("(new File([], 'world.html', {type:'text/html'})).type", "'text/html'"); |
| 95 | shouldBe("(new File([], 'world.html', {type:'text/html'})).size", "0"); |
| 96 | shouldBe("(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. |
| 99 | var aDate = new Date(441532800000); |
| 100 | shouldBe("(new File([], 'world.html', {lastModified: 441532800000})).lastModified", "441532800000"); |
| 101 | // Unless specified, lastModified should default to now. |
| 102 | shouldBeNow("(new File([], 'world.html')).lastModified", 20000); |
| 103 | shouldBeNow("(new File([], 'world.html', {})).lastModified", 20000); |
| 104 | shouldBeNow("(new File([], 'world.html', {type: 'text/plain'})).lastModified", 20000); |
| 105 | // Test that Date instances are implicitly converted to numbers. |
| 106 | shouldBe("(new File([], 'world.html', {lastModified: new Date(441532800000)})).lastModified", "441532800000"); |
| 107 | |
| 108 | // Test the number of expected arguments in the File constructor. |
| 109 | shouldBe("window.File.length", "2"); |
| 110 | |
| 111 | // Test ArrayBufferView parameters. |
| 112 | shouldBe("new File([new DataView(new ArrayBuffer(100))], 'world.html').size", "100"); |
| 113 | shouldBe("new File([new Uint8Array(100)], 'world.html').size", "100"); |
| 114 | shouldBe("new File([new Uint8ClampedArray(100)], 'world.html').size", "100"); |
| 115 | shouldBe("new File([new Uint16Array(100)], 'world.html').size", "200"); |
| 116 | shouldBe("new File([new Uint32Array(100)], 'world.html').size", "400"); |
| 117 | shouldBe("new File([new Int8Array(100)], 'world.html').size", "100"); |
| 118 | shouldBe("new File([new Int16Array(100)], 'world.html').size", "200"); |
| 119 | shouldBe("new File([new Int32Array(100)], 'world.html').size", "400"); |
| 120 | shouldBe("new File([new Float32Array(100)], 'world.html').size", "400"); |
| 121 | shouldBe("new File([new Float64Array(100)], 'world.html').size", "800"); |
| 122 | shouldBe("new File([new Float64Array(100), new Int32Array(100), new Uint8Array(100), new DataView(new ArrayBuffer(100))], 'world.html').size", "1400"); |
| 123 | shouldBe("new File([new Blob([new Int32Array(100)]), new Uint8Array(100), new Float32Array(100), new DataView(new ArrayBuffer(100))], 'world.html').size", "1000"); |
| 124 | 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"); |
| 125 | |
| 126 | // Test ArrayBuffer parameters. |
| 127 | shouldBe("new File([(new DataView(new ArrayBuffer(100))).buffer], 'world.html').size", "100"); |
| 128 | shouldBe("new File([(new Uint8Array(100)).buffer], 'world.html').size", "100"); |
| 129 | shouldBe("new File([(new Uint8ClampedArray(100)).buffer], 'world.html').size", "100"); |
| 130 | shouldBe("new File([(new Uint16Array(100)).buffer], 'world.html').size", "200"); |
| 131 | shouldBe("new File([(new Uint32Array(100)).buffer], 'world.html').size", "400"); |
| 132 | shouldBe("new File([(new Int8Array(100)).buffer], 'world.html').size", "100"); |
| 133 | shouldBe("new File([(new Int16Array(100)).buffer], 'world.html').size", "200"); |
| 134 | shouldBe("new File([(new Int32Array(100)).buffer], 'world.html').size", "400"); |
| 135 | shouldBe("new File([(new Float32Array(100)).buffer], 'world.html').size", "400"); |
| 136 | shouldBe("new File([(new Float64Array(100)).buffer], 'world.html').size", "800"); |
| 137 | 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"); |
| 138 | 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"); |
| 139 | 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"); |
| 140 | |
| 141 | // Test building Blobs with ArrayBuffer / ArrayBufferView parts enclosed in files. |
| 142 | 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"); |
| 143 | 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"); |
beidson@apple.com | 07faaf9 | 2016-04-25 17:31:29 +0000 | [diff] [blame] | 144 | </script> |