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
|
// Any copyright is dedicated to the Public Domain.
// - http://creativecommons.org/publicdomain/zero/1.0/
/* eslint-env mozilla/chrome-worker, node */
"use strict";
/* import-globals-from /testing/mochitest/tests/SimpleTest/WorkerSimpleTest.js */
importScripts("chrome://mochikit/content/tests/SimpleTest/WorkerSimpleTest.js");
/* import-globals-from /toolkit/modules/ObjectUtils.jsm */
importScripts("resource://gre/modules/ObjectUtils.jsm");
importScripts("file_ioutils_test_fixtures.js");
self.onmessage = async function (msg) {
const tmpDir = await PathUtils.getTempDir();
// IOUtils functionality is the same when called from the main thread, or a
// web worker. These tests are a modified subset of the main thread tests, and
// serve as a confidence check that the implementation is thread-safe.
await test_api_is_available_on_worker();
await test_full_read_and_write();
await test_move_file();
await test_copy_file();
await test_make_directory();
finish();
info("test_ioutils_worker.xhtml: Test finished");
async function test_api_is_available_on_worker() {
ok(self.IOUtils, "IOUtils is present in web workers");
}
async function test_full_read_and_write() {
// Write a file.
const tmpFileName = PathUtils.join(tmpDir, "test_ioutils_numbers.tmp");
const bytes = Uint8Array.of(...new Array(50).keys());
const bytesWritten = await IOUtils.write(tmpFileName, bytes);
is(bytesWritten, 50, "IOUtils::write can write entire byte array to file");
// Read it back.
let fileContents = await IOUtils.read(tmpFileName);
ok(
ObjectUtils.deepEqual(bytes, fileContents) &&
bytes.length == fileContents.length,
"IOUtils::read can read back entire file"
);
const tooManyBytes = bytes.length + 1;
fileContents = await IOUtils.read(tmpFileName, { maxBytes: tooManyBytes });
ok(
ObjectUtils.deepEqual(bytes, fileContents) &&
fileContents.length == bytes.length,
"IOUtils::read can read entire file when requested maxBytes is too large"
);
await cleanup(tmpFileName);
}
async function test_move_file() {
const src = PathUtils.join(tmpDir, "test_move_file_src.tmp");
const dest = PathUtils.join(tmpDir, "test_move_file_dest.tmp");
const bytes = Uint8Array.of(...new Array(50).keys());
await IOUtils.write(src, bytes);
await IOUtils.move(src, dest);
ok(
!(await fileExists(src)) && (await fileExists(dest)),
"IOUtils::move can move files from a worker"
);
await cleanup(dest);
}
async function test_copy_file() {
const tmpFileName = PathUtils.join(tmpDir, "test_ioutils_orig.tmp");
const destFileName = PathUtils.join(tmpDir, "test_ioutils_copy.tmp");
await createFile(tmpFileName, "original");
await IOUtils.copy(tmpFileName, destFileName);
ok(
(await fileExists(tmpFileName)) &&
(await fileHasTextContents(destFileName, "original")),
"IOUtils::copy can copy source to dest in same directory"
);
await cleanup(tmpFileName, destFileName);
}
async function test_make_directory() {
const dir = PathUtils.join(tmpDir, "test_make_dir.tmp.d");
await IOUtils.makeDirectory(dir);
const stat = await IOUtils.stat(dir);
is(
stat.type,
"directory",
"IOUtils::makeDirectory can make a new directory from a worker"
);
await cleanup(dir);
}
};
|