summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/Harness_sanity/test_createFiles.html
blob: 502592acc9e01317b5150db57e79d14c9cec1ffe (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for SpecialPowers.createFiles</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>

<div id="content" class="testbody">
  <script type="text/javascript">
    // Creating one file, followed by failing to create a file.
    function test1() {
      const fileType = "some file type";
      let fdata = "this is same data for a file";
      SpecialPowers.createFiles([{name: "test1.txt", data:fdata, options:{type:fileType}}],
        function (files) {
          is(files.length, 1, "Created 1 file");
          let f = files[0];
          is("[object File]", f.toString(), "first thing in array is a file");
          is(f.size, fdata.length, "test1 size of first file should be length of its data");
          is("test1.txt", f.name, "test1 test file should have the right name");
          is(f.type, fileType, "File should have the specified type");
          test2();
        },
        function (msg) { ok(false, "Should be able to create a file without an error"); test2(); }
      );
    }

    // Failing to create a file, followed by creating a file.
    function test2() {
      function test3Check(passed) {
        ok(passed, "Should trigger the error handler for a bad file name.");
        test3();
      };

      SpecialPowers.createFiles([{name: "/\/\/\/\/\/\/\/\/\/\/\invalidname",}],
        function () { test3Check(false); },
        function (msg) { test3Check(true); }
      );
    }

    // Creating two files at the same time.
    function test3() {
      let f1data = "hello";
      SpecialPowers.createFiles([{name: "test3_file.txt", data:f1data}, {name: "emptyfile.txt"}],
        function (files) {
          is(files.length, 2, "Expected two files to be created");
          let f1 = files[0];
          let f2 = files[1];
          is("[object File]", f1.toString(), "first thing in array is a file");
          is("[object File]", f2.toString(), "second thing in array is a file");
          is("test3_file.txt", f1.name, "first test3 test file should have the right name");
          is("emptyfile.txt", f2.name, "second test3 test file should have the right name");
          is(f1.size, f1data.length, "size of first file should be length of its data");
          is(f2.size, 0, "size of second file should be 0");
          test4();
        },
        function (msg) {
          ok(false, "Failed to create files: " + msg);
          test4();
        }
      );
    };

    // Creating a file without specifying a name should work.
    function test4() {
      let fdata = "this is same data for a file";
      SpecialPowers.createFiles([{data:fdata}],
        function (files) {
          is(files.length, 1, "Created 1 file");
          let f = files[0];
          is("[object File]", f.toString(), "first thing in array is a file");
          is(f.size, fdata.length, "test4 size of first file should be length of its data");
          ok(f.name, "test4 test file should have a name");
          SimpleTest.finish();
        },
        function (msg) {
          ok(false, "Should be able to create a file without a name without an error");
          SimpleTest.finish();
        }
      );
    }

    SimpleTest.waitForExplicitFinish();
    test1();

  </script>
</div>
</body>
</html>