summaryrefslogtreecommitdiffstats
path: root/dom/fs/test/common/test_basics.js
blob: f1cb1c222e5d9512e1663306dd7997e5d1e3c291 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// This test must be first, since we need the actor not to be created already.
exported_symbols.testGetDirectoryTwice = async function () {
  const promise1 = navigator.storage.getDirectory();
  const promise2 = navigator.storage.getDirectory();

  await Promise.all([promise1, promise2]);

  Assert.ok(true, "Should not have thrown");
};

exported_symbols.testGetDirectoryDoesNotThrow = async function () {
  await navigator.storage.getDirectory();

  Assert.ok(true, "Should not have thrown");
};

exported_symbols.testGetDirectoryKindIsDirectory = async function () {
  const root = await navigator.storage.getDirectory();

  Assert.equal(root.kind, "directory");
};

exported_symbols.testDirectoryHandleStringConversion = async function () {
  const root = await navigator.storage.getDirectory();

  Assert.equal(
    "" + root,
    "[object FileSystemDirectoryHandle]",
    "Is directoryHandle convertible to string?"
  );
};

exported_symbols.testNewDirectoryHandleFromPrototype = async function () {
  const root = await navigator.storage.getDirectory();

  try {
    Object.create(root.prototype);
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(true, "Should have thrown");
    Assert.ok(ex instanceof TypeError, "Threw the right error type");
  }
};

exported_symbols.testIsSameEntryRoot = async function () {
  const root = await navigator.storage.getDirectory();
  try {
    await root.move(root);
    Assert.ok(false, "root should not be movable");
  } catch (ex) {
    Assert.ok(true, "root isn't movable");
  }
};

exported_symbols.testDirectoryHandleSupportsKeysIterator = async function () {
  const root = await navigator.storage.getDirectory();

  const it = await root.keys();
  Assert.ok(!!it, "Does root support keys iterator?");
};

exported_symbols.testKeysIteratorNextIsCallable = async function () {
  const root = await navigator.storage.getDirectory();

  const it = await root.keys();
  Assert.ok(!!it, "Does root support keys iterator?");

  const item = await it.next();
  Assert.ok(!!item, "Should return an item");
};

exported_symbols.testDirectoryHandleSupportsValuesIterator = async function () {
  const root = await navigator.storage.getDirectory();

  const it = await root.values();
  Assert.ok(!!it, "Does root support values iterator?");
};

exported_symbols.testValuesIteratorNextIsCallable = async function () {
  const root = await navigator.storage.getDirectory();

  const it = await root.values();
  Assert.ok(!!it, "Does root support values iterator?");

  const item = await it.next();
  Assert.ok(!!item, "Should return an item");
};

exported_symbols.testDirectoryHandleSupportsEntriesIterator =
  async function () {
    const root = await navigator.storage.getDirectory();

    const it = await root.entries();
    Assert.ok(!!it, "Does root support entries iterator?");
  };

exported_symbols.testEntriesIteratorNextIsCallable = async function () {
  const root = await navigator.storage.getDirectory();

  const it = await root.entries();
  Assert.ok(!!it, "Does root support entries iterator?");

  const item = await it.next();
  Assert.ok(!!item, "Should return an item");
};

exported_symbols.testGetFileHandleIsCallable = async function () {
  const root = await navigator.storage.getDirectory();
  const allowCreate = { create: true };

  const item = await root.getFileHandle("fileName", allowCreate);
  Assert.ok(!!item, "Should return an item");

  await root.removeEntry("fileName");
};

exported_symbols.testGetDirectoryHandleIsCallable = async function () {
  const root = await navigator.storage.getDirectory();
  const allowCreate = { create: true };

  const item = await root.getDirectoryHandle("dirName", allowCreate);
  Assert.ok(!!item, "Should return an item");

  await root.removeEntry("dirName");
};

exported_symbols.testRemoveEntryIsCallable = async function () {
  const root = await navigator.storage.getDirectory();
  const removeOptions = { recursive: true };
  const allowCreate = { create: true };

  // Ensure file and directory items exists
  await root.getFileHandle("fileName", allowCreate);
  await root.getDirectoryHandle("dirName", allowCreate);
  await root.removeEntry("fileName", removeOptions);
  await root.removeEntry("dirName", removeOptions);
  try {
    await root.removeEntry("doesNotExist", removeOptions);
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(true, "Should have thrown");
    Assert.equal(
      ex.message,
      "Entry not found",
      "Threw the right error message"
    );
  }
};

exported_symbols.testResolveIsCallable = async function () {
  const root = await navigator.storage.getDirectory();
  const allowCreate = { create: true };
  const item = await root.getFileHandle("fileName", allowCreate);

  let path = await root.resolve(item);
  Assert.equal(path.length, 1);
  Assert.equal(path[0], "fileName", "Resolve got the right path");

  await root.removeEntry("fileName");
};

exported_symbols.testFileType = async function () {
  const root = await navigator.storage.getDirectory();
  const allowCreate = { create: true };
  const nameStem = "testFileType";
  const empty = "";

  const extensions = [
    "txt",
    "jS",
    "JSON",
    "css",
    "html",
    "htm",
    "xhtml",
    "xml",
    "xhtml+xml",
    "png",
    "apng",
    "jPg",
    "Jpeg",
    "pdF",
    "out",
    "sh",
    "ExE",
    "psid",
    "EXE ",
    " EXE",
    "EX\uff65",
    "\udbff\udbff\udbff",
    // XXX: Invalid surrogate combos like "\udc00\udc00\udc00" may map to the same names impacting cleanup.
    "js\udbff",
    "\udc00js",
    "???",
    "\root",
    empty,
    "AXS",
    "dll",
    "ocx",
    "1",
    "ps1",
    "cmd",
    "xpi",
    "swf",
  ];

  const expectedTypes = [
    "text/plain",
    "application/javascript",
    "application/json",
    "text/css",
    "text/html",
    "text/html",
    "application/xhtml+xml",
    "text/xml",
    empty,
    "image/png",
    "image/apng",
    "image/jpeg",
    "image/jpeg",
    "application/pdf",
    empty,
    "application/x-sh",
    "application/octet-stream",
    empty,
    empty,
    empty,
    empty,
    empty,
    empty,
    empty,
    empty,
    empty,
    empty,
    "application/olescript",
    "application/x-msdownload",
    "application/octet-stream",
    empty,
    empty,
    "text/plain",
    "application/x-xpinstall",
    "application/x-shockwave-flash",
  ];

  Assert.equal(extensions.length, expectedTypes.length);

  await Promise.all(
    extensions.map(async (ext, i) => {
      const fileName = nameStem + "." + ext;
      const fileHandle = await root.getFileHandle(fileName, allowCreate);
      const fileObject = await fileHandle.getFile();
      Assert.equal(fileObject.name, fileHandle.name);
      Assert.equal(fileObject.type, expectedTypes[i]);
      await root.removeEntry(fileName);
    })
  );
};

exported_symbols.testContentTypeChangesOnFileMove = async function () {
  const allowCreate = { create: true };
  const root = await navigator.storage.getDirectory();
  const oldName = "testFile.txt";
  const oldType = "text/plain";
  const subdir = await root.getDirectoryHandle("subdir", allowCreate);

  const fileHandle = await root.getFileHandle(oldName, allowCreate);

  async function checkMove(newName, newType) {
    Assert.equal(fileHandle.name, newName, "Has filename changed?");
    {
      const fileObject = await fileHandle.getFile();
      Assert.equal(fileObject.name, newName, "Is the fileobject renamed?");
      Assert.equal(fileObject.type, newType, "Is the fileobject type updated?");
    }
  }

  async function restoreTest() {
    await fileHandle.move(root, oldName);
    await checkMove(oldName, oldType);
  }

  // No name change
  await checkMove(oldName, oldType);
  await fileHandle.move(subdir);
  await checkMove(oldName, oldType);
  await restoreTest();

  // With name change

  async function testMoveWithParams(testName, testType) {
    async function testFileMoveCall(...combo) {
      await fileHandle.move(...combo);
      await checkMove(testName, testType);
      await restoreTest();
    }

    await testFileMoveCall(subdir, testName);
    await testFileMoveCall(root, testName);
    await testFileMoveCall(testName);
  }

  const testParams = {
    "testFile.json": "application/json",
    testFile: oldType,
    "testFile.äüö": "",
  };

  for (const [aName, aType] of Object.entries(testParams)) {
    await testMoveWithParams(aName, aType);
  }
};

exported_symbols.testContentTypeChangesOnDirMove = async function () {
  const allowCreate = { create: true };
  const root = await navigator.storage.getDirectory();
  const oldName = "testFile.txt";
  const oldType = "text/plain";
  const subDirOrig = await root.getDirectoryHandle("subDirOrig", allowCreate);
  const subDirOther = await root.getDirectoryHandle("subDirOther", allowCreate);
  const subSubDir = await subDirOrig.getDirectoryHandle(
    "subSubDir",
    allowCreate
  );

  const testName = "testFile.json";
  const testType = "application/json";

  async function checkMove(newName, newType) {
    const fileHandle = await subSubDir.getFileHandle(newName, allowCreate);

    Assert.equal(fileHandle.name, newName, "Has filename changed?");
    {
      const fileObject = await fileHandle.getFile();
      Assert.equal(fileObject.name, newName, "Is the fileobject renamed?");
      Assert.equal(fileObject.type, newType, "Is the fileobject type updated?");
    }
  }

  async function restoreTest() {
    await subSubDir.move(subDirOrig, "subSubDir");
    await checkMove(oldName, oldType);
  }

  await checkMove(oldName, oldType);

  // No name change
  await subSubDir.move(subDirOther, "other");
  await checkMove(oldName, oldType);
  await restoreTest();

  // With name change

  async function testDirMoveCall(...combo) {
    await subSubDir.move(...combo);
    await checkMove(testName, testType);
    await restoreTest();
  }

  await testDirMoveCall(subDirOther);
  await testDirMoveCall(subDirOther, testName);
  await testDirMoveCall(subDirOrig, testName);
  await testDirMoveCall(subDirOrig);
};

for (const [key, value] of Object.entries(exported_symbols)) {
  Object.defineProperty(value, "name", {
    value: key,
    writable: false,
  });
}