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
|
// META: script=resources/test-helpers.js
promise_test(async t => cleanupSandboxedFileSystem(),
'Cleanup to setup test environment');
promise_test(async t => {
const old_handle = await createFileWithContents(t, 'old-file', '12345');
const dir = await old_handle.getParent();
const new_handle = await old_handle.moveTo(dir, 'new-name');
t.add_cleanup(() => new_handle.remove());
// Verify new file.
assert_true(new_handle.isFile);
assert_false(new_handle.isDirectory);
assert_equals(new_handle.name, 'new-name');
assert_equals(await getFileContents(new_handle), '12345');
// And verify old file is gone.
await promise_rejects(t, 'NotFoundError', getFileContents(old_handle));
// Verify directory entries.
assert_array_equals(await getSortedDirectoryEntries(dir), ['new-name']);
}, 'moveTo() to rename a file');
promise_test(async t => {
const dir = await FileSystemDirectoryHandle.getSystemDirectory({ type: 'sandbox' });
const old_handle = await createFileWithContents(t, 'old-file', '12345');
const target_dir = await dir.getDirectory('dir-name', { create: true });
t.add_cleanup(() => target_dir.removeRecursively());
const new_handle = await old_handle.moveTo(target_dir);
// Verify new file.
assert_true(new_handle.isFile);
assert_false(new_handle.isDirectory);
assert_equals(new_handle.name, 'old-file');
assert_equals(await getFileContents(new_handle), '12345');
// And verify old file is gone.
await promise_rejects(t, 'NotFoundError', getFileContents(old_handle));
// Verify directory entries.
assert_array_equals(await getSortedDirectoryEntries(dir), ['dir-name/']);
assert_array_equals(await getSortedDirectoryEntries(target_dir), ['old-file']);
}, 'moveTo() to move a file into a sub-directory');
promise_test(async t => {
const handle = await createFileWithContents(t, 'old-file', '12345');
const dir = await handle.getParent();
await promise_rejects(t, 'InvalidModificationError', handle.moveTo(dir));
await promise_rejects(t, 'InvalidModificationError', handle.moveTo(dir, handle.name));
// Verify file still exists.
assert_equals(await getFileContents(handle), '12345');
assert_array_equals(await getSortedDirectoryEntries(dir), ['old-file']);
}, 'moveTo() with existing name and parent should fail');
promise_test(async t => {
const handle = await createFileWithContents(t, 'old-file', '12345');
const target_handle = await createFileWithContents(t, 'target', 'abc');
const dir = await handle.getParent();
await handle.moveTo(dir, target_handle.name);
// Verify state of files.
await promise_rejects(t, 'NotFoundError', getFileContents(handle));
assert_equals(await getFileContents(target_handle), '12345');
assert_array_equals(await getSortedDirectoryEntries(dir), ['target']);
}, 'moveTo() when target file already exists should overwrite target');
// TODO(mek): Tests to move directories.
|