FileSystemDirectoryHandle-getDirectoryHandle.js (4571B)
1 'use strict'; 2 3 directory_test(async (t, root) => { 4 await promise_rejects_dom( 5 t, 'NotFoundError', root.getDirectoryHandle('non-existing-dir')); 6 }, 'getDirectoryHandle(create=false) rejects for non-existing directories'); 7 8 directory_test(async (t, root) => { 9 const handle = 10 await root.getDirectoryHandle('non-existing-dir', {create: true}); 11 12 assert_equals(handle.kind, 'directory'); 13 assert_equals(handle.name, 'non-existing-dir'); 14 assert_equals(await getDirectoryEntryCount(handle), 0); 15 assert_array_equals( 16 await getSortedDirectoryEntries(root), ['non-existing-dir/']); 17 }, 'getDirectoryHandle(create=true) creates an empty directory'); 18 19 directory_test(async (t, root) => { 20 const existing_handle = 21 await root.getDirectoryHandle('dir-with-contents', {create: true}); 22 const file_handle = await createEmptyFile('test-file', existing_handle); 23 24 const handle = 25 await root.getDirectoryHandle('dir-with-contents', {create: false}); 26 27 assert_equals(handle.kind, 'directory'); 28 assert_equals(handle.name, 'dir-with-contents'); 29 assert_array_equals(await getSortedDirectoryEntries(handle), ['test-file']); 30 }, 'getDirectoryHandle(create=false) returns existing directories'); 31 32 directory_test(async (t, root) => { 33 const existing_handle = 34 await root.getDirectoryHandle('dir-with-contents', {create: true}); 35 const file_handle = 36 await existing_handle.getFileHandle('test-file', {create: true}); 37 38 const handle = 39 await root.getDirectoryHandle('dir-with-contents', {create: true}); 40 41 assert_equals(handle.kind, 'directory'); 42 assert_equals(handle.name, 'dir-with-contents'); 43 assert_array_equals(await getSortedDirectoryEntries(handle), ['test-file']); 44 }, 'getDirectoryHandle(create=true) returns existing directories without erasing'); 45 46 directory_test(async (t, root) => { 47 await createEmptyFile('file-name', root); 48 49 await promise_rejects_dom( 50 t, 'TypeMismatchError', root.getDirectoryHandle('file-name')); 51 await promise_rejects_dom( 52 t, 'TypeMismatchError', 53 root.getDirectoryHandle('file-name', {create: false})); 54 await promise_rejects_dom( 55 t, 'TypeMismatchError', 56 root.getDirectoryHandle('file-name', {create: true})); 57 }, 'getDirectoryHandle() when a file already exists with the same name'); 58 59 directory_test(async (t, dir) => { 60 await promise_rejects_js( 61 t, TypeError, dir.getDirectoryHandle('', {create: true})); 62 await promise_rejects_js( 63 t, TypeError, dir.getDirectoryHandle('', {create: false})); 64 }, 'getDirectoryHandle() with empty name'); 65 66 directory_test(async (t, dir) => { 67 await promise_rejects_js( 68 t, TypeError, dir.getDirectoryHandle(kCurrentDirectory)); 69 await promise_rejects_js( 70 t, TypeError, dir.getDirectoryHandle(kCurrentDirectory, {create: true})); 71 }, `getDirectoryHandle() with "${kCurrentDirectory}" name`); 72 73 directory_test(async (t, dir) => { 74 const subdir = await createDirectory('subdir-name', /*parent=*/ dir); 75 76 await promise_rejects_js( 77 t, TypeError, subdir.getDirectoryHandle(kParentDirectory)); 78 await promise_rejects_js( 79 t, TypeError, 80 subdir.getDirectoryHandle(kParentDirectory, {create: true})); 81 }, `getDirectoryHandle() with "${kParentDirectory}" name`); 82 83 directory_test(async (t, dir) => { 84 const first_subdir_name = 'first-subdir-name'; 85 const first_subdir = 86 await createDirectory(first_subdir_name, /*parent=*/ dir); 87 88 const second_subdir_name = 'second-subdir-name'; 89 const second_subdir = 90 await createDirectory(second_subdir_name, /*parent=*/ first_subdir); 91 92 for (let i = 0; i < kPathSeparators.length; ++i) { 93 const path_with_separator = 94 `${first_subdir_name}${kPathSeparators[i]}${second_subdir_name}`; 95 await promise_rejects_js( 96 t, TypeError, dir.getDirectoryHandle(path_with_separator), 97 `getDirectoryHandle() must reject names containing "${ 98 kPathSeparators[i]}"`); 99 } 100 }, 'getDirectoryHandle(create=false) with a path separator when the directory exists'); 101 102 directory_test(async (t, dir) => { 103 const subdir_name = 'subdir-name'; 104 const subdir = await createDirectory(subdir_name, /*parent=*/ dir); 105 106 for (let i = 0; i < kPathSeparators.length; ++i) { 107 const path_with_separator = `${subdir_name}${kPathSeparators[i]}file_name`; 108 await promise_rejects_js( 109 t, TypeError, 110 dir.getDirectoryHandle(path_with_separator, {create: true}), 111 `getDirectoryHandle(true) must reject names containing "${ 112 kPathSeparators[i]}"`); 113 } 114 }, 'getDirectoryHandle(create=true) with a path separator');