diff options
author | costan@gmail.com <costan@gmail.com@bbb929c8-8fbe-4397-9dbb-9b2b20218538> | 2013-11-15 13:24:42 +0000 |
---|---|---|
committer | costan@gmail.com <costan@gmail.com@bbb929c8-8fbe-4397-9dbb-9b2b20218538> | 2013-11-15 13:24:42 +0000 |
commit | 83dc38cd16e7cc8dec3d0f9224a8d4a2e453459f (patch) | |
tree | a4b52e7b1a6c1709431a6745dfb2371711311b4a /third_party/WebKit/LayoutTests/fast/files/file-constructor.html | |
parent | 4a2309898062a594325174ab56c5018cf1be2c95 (diff) | |
download | chromium_src-83dc38cd16e7cc8dec3d0f9224a8d4a2e453459f.zip chromium_src-83dc38cd16e7cc8dec3d0f9224a8d4a2e453459f.tar.gz chromium_src-83dc38cd16e7cc8dec3d0f9224a8d4a2e453459f.tar.bz2 |
Implement File constructor.
The feature is behind the FileConstructor RuntimeEnabledFeature with
status=experimental.
BUG=164933
Review URL: https://codereview.chromium.org/57483002
git-svn-id: svn://svn.chromium.org/blink/trunk@162116 bbb929c8-8fbe-4397-9dbb-9b2b20218538
Diffstat (limited to 'third_party/WebKit/LayoutTests/fast/files/file-constructor.html')
-rw-r--r-- | third_party/WebKit/LayoutTests/fast/files/file-constructor.html | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/third_party/WebKit/LayoutTests/fast/files/file-constructor.html b/third_party/WebKit/LayoutTests/fast/files/file-constructor.html new file mode 100644 index 0000000..f615864 --- /dev/null +++ b/third_party/WebKit/LayoutTests/fast/files/file-constructor.html @@ -0,0 +1,146 @@ +<!DOCTYPE html> + +<script src="../../resources/js-test.js"></script> +<script> +description("Test the File constructor."); + +// Test the different ways you can construct a File. +shouldBeTrue("(new File([], 'world.html')) instanceof window.File"); +shouldBeTrue("(new File(['hello'], 'world.html')) instanceof window.File"); +shouldBeTrue("(new File(['hello'], 'world.html', {})) instanceof window.File"); +shouldBeTrue("(new File(['hello'], 'world.html', {type:'text/html'})) instanceof window.File"); +shouldBeTrue("(new File(['hello'], 'world.html', {type:'text/html', endings:'native'})) instanceof window.File"); +shouldBeTrue("(new File(['hello'], 'world.html', {type:'text/html', endings:'transparent'})) instanceof window.File"); + +// Test that File inherits from File. +shouldBeTrue("(new File([], 'world.html')) instanceof window.File") + +// Verify that the file name argument is required. +shouldThrow("(new File())", "'TypeError: File constructor requires at least two arguments'"); +shouldThrow("(new File([]))", "'TypeError: File constructor requires at least two arguments'"); + +// Test valid file names. +shouldBeTrue("(new File([], null)) instanceof window.File"); +shouldBeTrue("(new File([], 1)) instanceof window.File"); +shouldBeTrue("(new File([], '')) instanceof window.File"); +shouldBeTrue("(new File([], document)) instanceof window.File"); + +// Test invalid blob parts. +shouldThrow("new File('hello', 'world.html')", '"TypeError: Failed to construct \'File\': The 1st argument is neither an array, nor does it have indexed properties."'); +shouldThrow("new File(0, 'world.html')", '"TypeError: Failed to construct \'File\': The 1st argument is neither an array, nor does it have indexed properties."'); + +// Test valid blob parts. +shouldBeTrue("(new File([], 'world.html')) instanceof window.File"); +shouldBeTrue("(new File(['stringPrimitive'], 'world.html')) instanceof window.File"); +shouldBeTrue("(new File([String('stringObject')], 'world.html')) instanceof window.File"); +shouldBeTrue("(new File([new Blob], 'world.html')) instanceof window.File"); +shouldBeTrue("(new File([new Blob([new Blob])], 'world.html')) instanceof window.File"); + +// Test File instances used as blob parts. +shouldBeTrue("(new Blob([new File([], 'world.txt')])) instanceof window.Blob"); +shouldBeTrue("(new Blob([new Blob([new File([new Blob], 'world.txt')])])) instanceof window.Blob"); +shouldBeTrue("(new File([new File([], 'world.txt')], 'world.html')) instanceof window.File"); +shouldBeTrue("(new File([new Blob([new File([new Blob], 'world.txt')])], 'world.html')) instanceof window.File"); + +// Test some conversions to string in the parts array. +shouldBe("(new File([12], 'world.html')).size", "2"); +shouldBe("(new File([[]], 'world.html')).size", "0"); // [].toString() is the empty string +shouldBe("(new File([{}], 'world.html')).size", "15");; // {}.toString() is the string "[object Object]" +shouldBe("(new File([document], 'world.html')).size", "21"); // document.toString() is the string "[object HTMLDocument]" + +var toStringingObj = { toString: function() { return "A string"; } }; +shouldBe("(new File([toStringingObj], 'world.html')).size", "8"); + +var throwingObj = { toString: function() { throw "Error"; } }; +shouldThrow("new File([throwingObj], 'world.html')", "'Error'"); + +// Test some conversions to string in the file name. +shouldBe("(new File([], null)).name", "'null'"); +shouldBe("(new File([], 12)).name", "'12'"); +shouldBe("(new File([], '')).name", "''"); +shouldBe("(new File([], {})).name", "'[object Object]'"); +shouldBe("(new File([], document)).name", "'[object HTMLDocument]'"); +shouldBe("(new File([], toStringingObj)).name", "'A string'"); +shouldThrow("(new File([], throwingObj)).name", "'Error'"); + +// Test some invalid property bags. +shouldBeTrue("(new File([], 'world.html', {unknownKey:'value'})) instanceof window.File"); // Ignore invalid keys +shouldThrow("new File([], 'world.html', {endings:'illegalValue'})", "'TypeError: Failed to construct \\'File\\': The \"endings\" property must be either \"transparent\" or \"native\".'"); +shouldThrow("new File([], 'world.html', {endings:throwingObj})", "'Error'"); +shouldThrow("new File([], 'world.html', {type:throwingObj})", "'Error'"); +shouldThrow("new File([], 'world.html', {type:'hello\u00EE'})", "'SyntaxError: Failed to construct \\'File\\': The \"type\" property must consist of ASCII characters.'"); + +// Test various non-object literals being used as property bags. +shouldThrow("(new File([], 'world.html', null)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Object.'"); +shouldThrow("(new File([], 'world.html', undefined)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Object.'"); +shouldThrow("(new File([], 'world.html', 123)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Object.'"); +shouldThrow("(new File([], 'world.html', 123.4)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Object.'"); +shouldThrow("(new File([], 'world.html', true)) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Object.'"); +shouldThrow("(new File([], 'world.html', 'abc')) instanceof window.File", "'TypeError: Failed to construct \\'File\\': The 3rd argument is not of type Object.'"); +shouldBeTrue("(new File([], 'world.html', [])) instanceof window.File"); +shouldBeTrue("(new File([], 'world.html', /abc/)) instanceof window.File"); +shouldBeTrue("(new File([], 'world.html', function () {})) instanceof window.File"); + +// Test that the name/type/size are correctly added to the File. +shouldBe("(new File([], 'world.html', {type:'text/html'})).name", "'world.html'"); +shouldBe("(new File([], 'world.html', {type:'text/html'})).type", "'text/html'"); +shouldBe("(new File([], 'world.html', {type:'text/html'})).size", "0"); +shouldBe("(new File([], 'world.html', {type:'text/plain;charset=UTF-8'})).type", "'text/plain;charset=utf-8'"); + +// Test the number of expected arguments in the File constructor. +shouldBe("window.File.length", "2"); + +// Test ArrayBufferView parameters. +shouldBe("new File([new DataView(new ArrayBuffer(100))], 'world.html').size", "100"); +shouldBe("new File([new Uint8Array(100)], 'world.html').size", "100"); +shouldBe("new File([new Uint8ClampedArray(100)], 'world.html').size", "100"); +shouldBe("new File([new Uint16Array(100)], 'world.html').size", "200"); +shouldBe("new File([new Uint32Array(100)], 'world.html').size", "400"); +shouldBe("new File([new Int8Array(100)], 'world.html').size", "100"); +shouldBe("new File([new Int16Array(100)], 'world.html').size", "200"); +shouldBe("new File([new Int32Array(100)], 'world.html').size", "400"); +shouldBe("new File([new Float32Array(100)], 'world.html').size", "400"); +shouldBe("new File([new Float64Array(100)], 'world.html').size", "800"); +shouldBe("new File([new Float64Array(100), new Int32Array(100), new Uint8Array(100), new DataView(new ArrayBuffer(100))], 'world.html').size", "1400"); +shouldBe("new File([new Blob([new Int32Array(100)]), new Uint8Array(100), new Float32Array(100), new DataView(new ArrayBuffer(100))], 'world.html').size", "1000"); +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"); + +// Test ArrayBuffer parameters. +shouldBe("new File([(new DataView(new ArrayBuffer(100))).buffer], 'world.html').size", "100"); +shouldBe("new File([(new Uint8Array(100)).buffer], 'world.html').size", "100"); +shouldBe("new File([(new Uint8ClampedArray(100)).buffer], 'world.html').size", "100"); +shouldBe("new File([(new Uint16Array(100)).buffer], 'world.html').size", "200"); +shouldBe("new File([(new Uint32Array(100)).buffer], 'world.html').size", "400"); +shouldBe("new File([(new Int8Array(100)).buffer], 'world.html').size", "100"); +shouldBe("new File([(new Int16Array(100)).buffer], 'world.html').size", "200"); +shouldBe("new File([(new Int32Array(100)).buffer], 'world.html').size", "400"); +shouldBe("new File([(new Float32Array(100)).buffer], 'world.html').size", "400"); +shouldBe("new File([(new Float64Array(100)).buffer], 'world.html').size", "800"); +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"); +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"); +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"); + +// Test building Blobs with ArrayBuffer / ArrayBufferView parts enclosed in files. +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"); +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"); + +// Test passing blob parts in sequences. +shouldBeTrue("new File({length: 0}, 'world.txt') instanceof window.File"); +shouldBe("new File({length: 0}, 'world.txt').size", "0"); +shouldBe("new File({length: 1, 0: 'string'}, 'world.txt').size", "6"); +shouldBe("new File({length: 2, 0: new Uint8Array(100), 1: new Int16Array(100)}, 'world.txt').size", "300"); +shouldBe("new File({length: 1, 0: 'string'}, 'world.txt', {type: 'text/html'}).type", "'text/html'"); +shouldThrow("new File({length: 0}, 'world.txt', {endings:'illegal'})", "'TypeError: Failed to construct \\'File\\': The \"endings\" property must be either \"transparent\" or \"native\".'"); + +// Test passing blog parts in a sequence-like object that throws on property access. +var throwingSequence = {length: 4, 0: 'hello', 3: 'world'}; +Object.defineProperty(throwingSequence, "1", { + get: function() { throw new Error("Misbehaving property"); }, + enumerable: true, configurable: true +}); +Object.defineProperty(throwingSequence, "2", { + get: function() { throw new Error("This should not be thrown"); }, + enumerable: true, configurable: true +}); +shouldThrow("new File(throwingSequence, 'world.txt')", "'Error: Misbehaving property'"); +</script> |