aboutsummaryrefslogtreecommitdiffstats
path: root/tests/wpt/web-platform-tests/native-file-system/FileSystemBaseHandle-copyTo.tentative.window.js
blob: 2019024363ab5f4c17b3d8a8cef3eddf9008c948 (plain) (blame)
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.copyTo(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 still around as well.
    assert_equals(await getFileContents(old_handle), '12345');

    // Verify directory entries.
    assert_array_equals(await getSortedDirectoryEntries(dir), ['new-name', 'old-file']);
}, 'copyTo() into the same parent directory');

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.copyTo(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 still around as well.
    assert_equals(await getFileContents(old_handle), '12345');

    // Verify directory entries.
    assert_array_equals(await getSortedDirectoryEntries(dir), ['dir-name/', 'old-file']);
    assert_array_equals(await getSortedDirectoryEntries(target_dir), ['old-file']);
}, 'copyTo() to copy 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.copyTo(dir));
    await promise_rejects(t, 'InvalidModificationError', handle.copyTo(dir, handle.name));

    // Verify file still exists.
    assert_equals(await getFileContents(handle), '12345');
    assert_array_equals(await getSortedDirectoryEntries(dir), ['old-file']);
}, 'copyTo() 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.copyTo(dir, target_handle.name);

    // Verify state of files.
    assert_equals(await getFileContents(handle), '12345');
    assert_equals(await getFileContents(target_handle), '12345');
    assert_array_equals(await getSortedDirectoryEntries(dir), ['old-file', 'target']);
}, 'copyTo() when target file already exists should overwrite target');

// TODO(mek): Tests to copy directories.