summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/FileAPI/file/File-constructor.html
blob: 97c08b6546ecc17b63af46bc31453569cfa3b6a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<meta charset=utf-8>
<title>File constructor</title>
<link rel=help href="http://dev.w3.org/2006/webapi/FileAPI/#dfn-file">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
test(function() {
  assert_true("File" in window, "window should have a File property.");
}, "File interface object exists");

function test_first_argument(arg1, expectedSize, testName) {
  test(function() {
    var file = new File(arg1, "dummy");
    assert_true(file instanceof File);
    assert_equals(file.name, "dummy");
    assert_equals(file.size, expectedSize);
    assert_equals(file.type, "");
    // assert_false(file.isClosed); XXX: File.isClosed doesn't seem to be implemented
    assert_not_equals(file.lastModified, "");
  }, testName);
}

test_first_argument(["bits"], 4, "DOMString fileBits");
test_first_argument(["𝓽𝓮𝔁𝓽"], 16, "Unicode DOMString fileBits");
test_first_argument([new Blob()], 0, "Empty Blob fileBits");
test_first_argument([new Blob(["bits"])], 4, "Blob fileBits");
test_first_argument([new ArrayBuffer(8)], 8, "ArrayBuffer fileBits");
test_first_argument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4, "Typed array fileBits");
test_first_argument(["bits", new Blob(["bits"]), new Blob(), new Uint8Array([0x50, 0x41]),
                     new Uint16Array([0x5353]), new Uint32Array([0x53534150])], 16, "Various fileBits");

function test_second_argument(arg2, expectedFileName, testName) {
  test(function() {
    var file = new File(["bits"], arg2);
    assert_true(file instanceof File);
    assert_equals(file.name, expectedFileName);
  }, testName);
}

test_second_argument("dummy", "dummy", "Using fileName");
test_second_argument("dummy/foo", "dummy:foo", "Using special character in fileName");

// testing the third argument
test(function() {
  var file = new File(["bits"], "dummy", { type: "text/plain"});
  assert_true(file instanceof File);
  assert_equals(file.type, "text/plain");
}, "Using type on the File constructor");
test(function() {
  var file = new File(["bits"], "dummy", { type: "TEXT/PLAIN"});
  assert_true(file instanceof File);
  assert_equals(file.type, "text/plain");
}, "Using uppercase characters in type");
test(function() {
  var file = new File(["bits"], "dummy", { type: "𝓽𝓮𝔁𝓽/𝔭𝔩𝔞𝔦𝔫"});
  assert_true(file instanceof File);
  assert_equals(file.type, "");
}, "Using illegal character for type");
test(function() {
  var file = new File(["bits"], "dummy", { lastModified: 42 });
  assert_true(file instanceof File);
  assert_equals(file.lastModified, 42);
}, "Using lastModified");
test(function() {
  var file = new File(["bits"], "dummy", { name: "foo" });
  assert_true(file instanceof File);
  assert_equals(file.name, "dummy");
}, "Misusing name");

</script>