11cb0ef41Sopenharmony_ci// META: title=File constructor 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst to_string_obj = { toString: () => 'a string' }; 41cb0ef41Sopenharmony_ciconst to_string_throws = { toString: () => { throw new Error('expected'); } }; 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_citest(function() { 71cb0ef41Sopenharmony_ci assert_true("File" in globalThis, "globalThis should have a File property."); 81cb0ef41Sopenharmony_ci}, "File interface object exists"); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_citest(t => { 111cb0ef41Sopenharmony_ci assert_throws_js(TypeError, () => new File(), 121cb0ef41Sopenharmony_ci 'Bits argument is required'); 131cb0ef41Sopenharmony_ci assert_throws_js(TypeError, () => new File([]), 141cb0ef41Sopenharmony_ci 'Name argument is required'); 151cb0ef41Sopenharmony_ci}, 'Required arguments'); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cifunction test_first_argument(arg1, expectedSize, testName) { 181cb0ef41Sopenharmony_ci test(function() { 191cb0ef41Sopenharmony_ci var file = new File(arg1, "dummy"); 201cb0ef41Sopenharmony_ci assert_true(file instanceof File); 211cb0ef41Sopenharmony_ci assert_equals(file.name, "dummy"); 221cb0ef41Sopenharmony_ci assert_equals(file.size, expectedSize); 231cb0ef41Sopenharmony_ci assert_equals(file.type, ""); 241cb0ef41Sopenharmony_ci // assert_false(file.isClosed); XXX: File.isClosed doesn't seem to be implemented 251cb0ef41Sopenharmony_ci assert_not_equals(file.lastModified, ""); 261cb0ef41Sopenharmony_ci }, testName); 271cb0ef41Sopenharmony_ci} 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_citest_first_argument([], 0, "empty fileBits"); 301cb0ef41Sopenharmony_citest_first_argument(["bits"], 4, "DOMString fileBits"); 311cb0ef41Sopenharmony_citest_first_argument([""], 16, "Unicode DOMString fileBits"); 321cb0ef41Sopenharmony_citest_first_argument([new String('string object')], 13, "String object fileBits"); 331cb0ef41Sopenharmony_citest_first_argument([new Blob()], 0, "Empty Blob fileBits"); 341cb0ef41Sopenharmony_citest_first_argument([new Blob(["bits"])], 4, "Blob fileBits"); 351cb0ef41Sopenharmony_citest_first_argument([new File([], 'world.txt')], 0, "Empty File fileBits"); 361cb0ef41Sopenharmony_citest_first_argument([new File(["bits"], 'world.txt')], 4, "File fileBits"); 371cb0ef41Sopenharmony_citest_first_argument([new ArrayBuffer(8)], 8, "ArrayBuffer fileBits"); 381cb0ef41Sopenharmony_citest_first_argument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4, "Typed array fileBits"); 391cb0ef41Sopenharmony_citest_first_argument(["bits", new Blob(["bits"]), new Blob(), new Uint8Array([0x50, 0x41]), 401cb0ef41Sopenharmony_ci new Uint16Array([0x5353]), new Uint32Array([0x53534150])], 16, "Various fileBits"); 411cb0ef41Sopenharmony_citest_first_argument([12], 2, "Number in fileBits"); 421cb0ef41Sopenharmony_citest_first_argument([[1,2,3]], 5, "Array in fileBits"); 431cb0ef41Sopenharmony_citest_first_argument([{}], 15, "Object in fileBits"); // "[object Object]" 441cb0ef41Sopenharmony_ciif (globalThis.document !== undefined) { 451cb0ef41Sopenharmony_ci test_first_argument([document.body], 24, "HTMLBodyElement in fileBits"); // "[object HTMLBodyElement]" 461cb0ef41Sopenharmony_ci} 471cb0ef41Sopenharmony_citest_first_argument([to_string_obj], 8, "Object with toString in fileBits"); 481cb0ef41Sopenharmony_citest_first_argument({[Symbol.iterator]() { 491cb0ef41Sopenharmony_ci let i = 0; 501cb0ef41Sopenharmony_ci return {next: () => [ 511cb0ef41Sopenharmony_ci {done:false, value:'ab'}, 521cb0ef41Sopenharmony_ci {done:false, value:'cde'}, 531cb0ef41Sopenharmony_ci {done:true} 541cb0ef41Sopenharmony_ci ][i++]}; 551cb0ef41Sopenharmony_ci}}, 5, 'Custom @@iterator'); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci[ 581cb0ef41Sopenharmony_ci 'hello', 591cb0ef41Sopenharmony_ci 0, 601cb0ef41Sopenharmony_ci null 611cb0ef41Sopenharmony_ci].forEach(arg => { 621cb0ef41Sopenharmony_ci test(t => { 631cb0ef41Sopenharmony_ci assert_throws_js(TypeError, () => new File(arg, 'world.html'), 641cb0ef41Sopenharmony_ci 'Constructor should throw for invalid bits argument'); 651cb0ef41Sopenharmony_ci }, `Invalid bits argument: ${JSON.stringify(arg)}`); 661cb0ef41Sopenharmony_ci}); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_citest(t => { 691cb0ef41Sopenharmony_ci assert_throws_js(Error, () => new File([to_string_throws], 'name.txt'), 701cb0ef41Sopenharmony_ci 'Constructor should propagate exceptions'); 711cb0ef41Sopenharmony_ci}, 'Bits argument: object that throws'); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_cifunction test_second_argument(arg2, expectedFileName, testName) { 751cb0ef41Sopenharmony_ci test(function() { 761cb0ef41Sopenharmony_ci var file = new File(["bits"], arg2); 771cb0ef41Sopenharmony_ci assert_true(file instanceof File); 781cb0ef41Sopenharmony_ci assert_equals(file.name, expectedFileName); 791cb0ef41Sopenharmony_ci }, testName); 801cb0ef41Sopenharmony_ci} 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_citest_second_argument("dummy", "dummy", "Using fileName"); 831cb0ef41Sopenharmony_citest_second_argument("dummy/foo", "dummy/foo", 841cb0ef41Sopenharmony_ci "No replacement when using special character in fileName"); 851cb0ef41Sopenharmony_citest_second_argument(null, "null", "Using null fileName"); 861cb0ef41Sopenharmony_citest_second_argument(1, "1", "Using number fileName"); 871cb0ef41Sopenharmony_citest_second_argument('', '', "Using empty string fileName"); 881cb0ef41Sopenharmony_ciif (globalThis.document !== undefined) { 891cb0ef41Sopenharmony_ci test_second_argument(document.body, '[object HTMLBodyElement]', "Using object fileName"); 901cb0ef41Sopenharmony_ci} 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci// testing the third argument 931cb0ef41Sopenharmony_ci[ 941cb0ef41Sopenharmony_ci {type: 'text/plain', expected: 'text/plain'}, 951cb0ef41Sopenharmony_ci {type: 'text/plain;charset=UTF-8', expected: 'text/plain;charset=utf-8'}, 961cb0ef41Sopenharmony_ci {type: 'TEXT/PLAIN', expected: 'text/plain'}, 971cb0ef41Sopenharmony_ci {type: '/', expected: ''}, 981cb0ef41Sopenharmony_ci {type: 'ascii/nonprintable\u001F', expected: ''}, 991cb0ef41Sopenharmony_ci {type: 'ascii/nonprintable\u007F', expected: ''}, 1001cb0ef41Sopenharmony_ci {type: 'nonascii\u00EE', expected: ''}, 1011cb0ef41Sopenharmony_ci {type: 'nonascii\u1234', expected: ''}, 1021cb0ef41Sopenharmony_ci {type: 'nonparsable', expected: 'nonparsable'} 1031cb0ef41Sopenharmony_ci].forEach(testCase => { 1041cb0ef41Sopenharmony_ci test(t => { 1051cb0ef41Sopenharmony_ci var file = new File(["bits"], "dummy", { type: testCase.type}); 1061cb0ef41Sopenharmony_ci assert_true(file instanceof File); 1071cb0ef41Sopenharmony_ci assert_equals(file.type, testCase.expected); 1081cb0ef41Sopenharmony_ci }, `Using type in File constructor: ${testCase.type}`); 1091cb0ef41Sopenharmony_ci}); 1101cb0ef41Sopenharmony_citest(function() { 1111cb0ef41Sopenharmony_ci var file = new File(["bits"], "dummy", { lastModified: 42 }); 1121cb0ef41Sopenharmony_ci assert_true(file instanceof File); 1131cb0ef41Sopenharmony_ci assert_equals(file.lastModified, 42); 1141cb0ef41Sopenharmony_ci}, "Using lastModified"); 1151cb0ef41Sopenharmony_citest(function() { 1161cb0ef41Sopenharmony_ci var file = new File(["bits"], "dummy", { name: "foo" }); 1171cb0ef41Sopenharmony_ci assert_true(file instanceof File); 1181cb0ef41Sopenharmony_ci assert_equals(file.name, "dummy"); 1191cb0ef41Sopenharmony_ci}, "Misusing name"); 1201cb0ef41Sopenharmony_citest(function() { 1211cb0ef41Sopenharmony_ci var file = new File(["bits"], "dummy", { unknownKey: "value" }); 1221cb0ef41Sopenharmony_ci assert_true(file instanceof File); 1231cb0ef41Sopenharmony_ci assert_equals(file.name, "dummy"); 1241cb0ef41Sopenharmony_ci}, "Unknown properties are ignored"); 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci[ 1271cb0ef41Sopenharmony_ci 123, 1281cb0ef41Sopenharmony_ci 123.4, 1291cb0ef41Sopenharmony_ci true, 1301cb0ef41Sopenharmony_ci 'abc' 1311cb0ef41Sopenharmony_ci].forEach(arg => { 1321cb0ef41Sopenharmony_ci test(t => { 1331cb0ef41Sopenharmony_ci assert_throws_js(TypeError, () => new File(['bits'], 'name.txt', arg), 1341cb0ef41Sopenharmony_ci 'Constructor should throw for invalid property bag type'); 1351cb0ef41Sopenharmony_ci }, `Invalid property bag: ${JSON.stringify(arg)}`); 1361cb0ef41Sopenharmony_ci}); 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci[ 1391cb0ef41Sopenharmony_ci null, 1401cb0ef41Sopenharmony_ci undefined, 1411cb0ef41Sopenharmony_ci [1,2,3], 1421cb0ef41Sopenharmony_ci /regex/, 1431cb0ef41Sopenharmony_ci function() {} 1441cb0ef41Sopenharmony_ci].forEach(arg => { 1451cb0ef41Sopenharmony_ci test(t => { 1461cb0ef41Sopenharmony_ci assert_equals(new File(['bits'], 'name.txt', arg).size, 4, 1471cb0ef41Sopenharmony_ci 'Constructor should accept object-ish property bag type'); 1481cb0ef41Sopenharmony_ci }, `Unusual but valid property bag: ${arg}`); 1491cb0ef41Sopenharmony_ci}); 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_citest(t => { 1521cb0ef41Sopenharmony_ci assert_throws_js(Error, 1531cb0ef41Sopenharmony_ci () => new File(['bits'], 'name.txt', {type: to_string_throws}), 1541cb0ef41Sopenharmony_ci 'Constructor should propagate exceptions'); 1551cb0ef41Sopenharmony_ci}, 'Property bag propagates exceptions'); 156