summaryrefslogtreecommitdiffstats
path: root/dom/filesystem/tests/filesystem_commons.js
blob: af0ce36339e647e093f5a04820daaaa6eb84206f (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
function createPath(parentDir, dirOrFile) {
  return parentDir.path + (parentDir.path == "/" ? "" : "/") + dirOrFile.name;
}

function createRelativePath(parentDir, dirOrFile) {
  let path = createPath(parentDir, dirOrFile);
  is(path[0], "/", "The full path should start with '/'");
  return path.substring(1);
}

function setup_tests(aNext) {
  SimpleTest.requestLongerTimeout(2);
  SpecialPowers.pushPrefEnv(
    {
      set: [
        ["dom.filesystem.pathcheck.disabled", true],
        ["dom.webkitBlink.dirPicker.enabled", true],
      ],
    },
    aNext
  );
}

function test_basic(aDirectory, aNext) {
  ok(aDirectory, "Directory exists.");
  ok(aDirectory instanceof Directory, "We have a directory.");
  is(aDirectory.path, "/" + aDirectory.name, "directory.path must be '/'+name");
  aNext();
}

function test_getFilesAndDirectories(aDirectory, aRecursive, aNext) {
  function checkSubDir(dir) {
    return dir.getFilesAndDirectories().then(function (data) {
      for (var i = 0; i < data.length; ++i) {
        ok(
          data[i] instanceof File || data[i] instanceof Directory,
          "Just Files or Directories"
        );
        if (data[i] instanceof Directory) {
          isnot(
            data[i].name,
            "/",
            "Subdirectory should be called with the leafname"
          );
          isnot(
            data[i].path,
            "/",
            "Subdirectory path should be called with the leafname"
          );
          isnot(
            data[i].path,
            dir.path,
            "Subdirectory path should contain the parent path."
          );
          is(
            data[i].path,
            createPath(dir, data[i]),
            "Subdirectory path should be called parentdir.path + '/' + leafname: " +
              data[i].path
          );
        }

        if (data[i] instanceof File) {
          is(
            data[i].webkitRelativePath,
            createRelativePath(dir, data[i]),
            "File.webkitRelativePath should be called: parentdir.path + '/' + file.name: " +
              data[i].webkitRelativePath
          );
          ok(
            !data[i].webkitRelativePath.endsWith("symlink.txt"),
            "We should never see a path ending with symlink.txt, our symlink sentinel."
          );
        }
      }
    });
  }

  aDirectory
    .getFilesAndDirectories()
    .then(
      function (data) {
        ok(data.length, "We should have some data.");
        var promises = [];
        for (var i = 0; i < data.length; ++i) {
          ok(
            data[i] instanceof File || data[i] instanceof Directory,
            "Just Files or Directories: " + data[i].name
          );
          if (data[i] instanceof Directory) {
            isnot(
              data[i].name,
              "/",
              "Subdirectory should be called with the leafname"
            );
            is(
              data[i].path,
              createPath(aDirectory, data[i]),
              "Subdirectory path should be called parentdir.path + '/' + leafname: " +
                data[i].path
            );
            if (aRecursive) {
              promises.push(checkSubDir(data[i]));
            }
          }

          if (data[i] instanceof File) {
            is(
              data[i].webkitRelativePath,
              createRelativePath(aDirectory, data[i]),
              "File.webkitRelativePath should be called file.name: " +
                data[i].webkitRelativePath
            );
          }
        }

        return Promise.all(promises);
      },
      function () {
        ok(false, "Something when wrong");
      }
    )
    .then(aNext);
}

function test_getFiles(aDirectory, aRecursive, aNext) {
  aDirectory
    .getFiles(aRecursive)
    .then(
      function (data) {
        for (var i = 0; i < data.length; ++i) {
          ok(data[i] instanceof File, "File: " + data[i].name);
          is(aDirectory.path[0], "/", "Directory path must start with '/'");
          ok(
            data[i].webkitRelativePath.indexOf(aDirectory.path.substring(1)) ==
              0 &&
              data[i].webkitRelativePath.indexOf("/" + data[i].name) +
                ("/" + data[i].name).length ==
                data[i].webkitRelativePath.length,
            "File.webkitRelativePath should be called dir.path + '/' + file.name: " +
              data[i].webkitRelativePath
          );
        }
      },
      function () {
        ok(false, "Something when wrong");
      }
    )
    .then(aNext);
}

function test_getFiles_recursiveComparison(aDirectory, aNext) {
  aDirectory
    .getFiles(true)
    .then(function (data) {
      is(data.length, 2, "Only 2 files for this test.");
      ok(
        data[0].name == "foo.txt" || data[0].name == "bar.txt",
        "First filename matches"
      );
      ok(
        data[1].name == "foo.txt" || data[1].name == "bar.txt",
        "Second filename matches"
      );
    })
    .then(function () {
      return aDirectory.getFiles(false);
    })
    .then(function (data) {
      is(data.length, 1, "Only 1 file for this test.");
      ok(
        data[0].name == "foo.txt" || data[0].name == "bar.txt",
        "First filename matches"
      );
    })
    .catch(function () {
      ok(false, "Something when wrong");
    })
    .then(aNext);
}