summaryrefslogtreecommitdiffstats
path: root/dom/filesystem/tests/script_fileList.js
blob: c1e8bb42a8f2d6a76afeebeb7cacfd8fc15b3a77 (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
/* eslint-env mozilla/frame-script */
// eslint-disable-next-line mozilla/reject-importGlobalProperties
Cu.importGlobalProperties(["File"]);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
function createProfDFile() {
  return Services.dirsvc
    .QueryInterface(Ci.nsIProperties)
    .get("ProfD", Ci.nsIFile);
}

// Creates a parametric arity directory hierarchy as a function of depth.
// Each directory contains one leaf file, and subdirectories of depth [1, depth).
// e.g. for depth 3:
//
// subdir3
// - file.txt
// - subdir2
//   - file.txt
//   - subdir1
//     - file.txt
// - subdir1
//   - file.txt
//
// Returns the parent directory of the subtree.
function createTreeFile(depth, parent) {
  if (!parent) {
    parent = Services.dirsvc
      .QueryInterface(Ci.nsIProperties)
      .get("TmpD", Ci.nsIFile);
    parent.append("dir-tree-test");
    parent.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o700);
  }

  var nextFile = parent.clone();
  if (depth == 0) {
    nextFile.append("file.txt");
    nextFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);
  } else {
    nextFile.append("subdir" + depth);
    nextFile.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o700);
    // Decrement the maximal depth by one for each level of nesting.
    for (var i = 0; i < depth; i++) {
      createTreeFile(i, nextFile);
    }
  }

  return parent;
}

function createRootFile() {
  var testFile = createProfDFile();

  // Let's go back to the root of the FileSystem
  while (true) {
    var parent = testFile.parent;
    if (!parent) {
      break;
    }

    testFile = parent;
  }

  return testFile;
}

function createTestFile() {
  var tmpFile = Services.dirsvc
    .QueryInterface(Ci.nsIProperties)
    .get("TmpD", Ci.nsIFile);
  tmpFile.append("dir-test");
  tmpFile.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o700);

  var file1 = tmpFile.clone();
  file1.append("foo.txt");
  file1.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);

  var dir = tmpFile.clone();
  dir.append("subdir");
  dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o700);

  var file2 = dir.clone();
  file2.append("bar.txt");
  file2.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);

  return tmpFile;
}

addMessageListener("dir.open", function(e) {
  var testFile;

  switch (e.path) {
    case "ProfD":
      // Note that files in the profile directory are not guaranteed to persist-
      // see bug 1284742.
      testFile = createProfDFile();
      break;

    case "root":
      testFile = createRootFile();
      break;

    case "test":
      testFile = createTestFile();
      break;

    case "tree":
      testFile = createTreeFile(3);
      break;
  }

  sendAsyncMessage("dir.opened", {
    dir: testFile.path,
    name: testFile.leafName,
  });
});

addMessageListener("file.open", function(e) {
  var testFile = Services.dirsvc
    .QueryInterface(Ci.nsIProperties)
    .get("ProfD", Ci.nsIFile);
  testFile.append("prefs.js");

  File.createFromNsIFile(testFile).then(function(file) {
    sendAsyncMessage("file.opened", { file });
  });
});