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