FileSystemDirectoryHandle-getFileHandle.js (5573B)
1 'use strict'; 2 3 directory_test(async (t, dir) => { 4 await promise_rejects_dom( 5 t, 'NotFoundError', dir.getFileHandle('non-existing-file')); 6 }, 'getFileHandle(create=false) rejects for non-existing files'); 7 8 directory_test(async (t, dir) => { 9 const handle = await dir.getFileHandle('non-existing-file', {create: true}); 10 11 assert_equals(handle.kind, 'file'); 12 assert_equals(handle.name, 'non-existing-file'); 13 assert_equals(await getFileSize(handle), 0); 14 assert_equals(await getFileContents(handle), ''); 15 }, 'getFileHandle(create=true) creates an empty file for non-existing files'); 16 17 directory_test(async (t, dir) => { 18 var name = ''; 19 // test the ascii characters -- start after the non-character ASCII values, exclude DEL 20 for (let i = 32; i < 127; i++) { 21 // Path separators are disallowed 22 let disallow = false; 23 for (let j = 0; j < kPathSeparators.length; ++j) { 24 if (String.fromCharCode(i) == kPathSeparators[j]) { 25 disallow = true; 26 } 27 } 28 if (!disallow) { 29 name += String.fromCharCode(i); 30 } 31 } 32 // Add in CR, LF, FF, Tab, Vertical Tab 33 for (let i = 9; i < 14; i++) { 34 name += String.fromCharCode(i); 35 } 36 const handle = await dir.getFileHandle(name, {create: true}); 37 38 assert_equals(handle.kind, 'file'); 39 assert_equals(handle.name, name); 40 assert_equals(await getFileSize(handle), 0); 41 assert_equals(await getFileContents(handle), ''); 42 }, 'getFileHandle(create=true) creates an empty file with all valid ASCII characters in the name'); 43 44 directory_test(async (t, dir) => { 45 var name; 46 // A non-ASCII name 47 name = 'Funny cat \u{1F639}' 48 const handle = await dir.getFileHandle(name, {create: true}); 49 50 assert_equals(handle.kind, 'file'); 51 assert_equals(handle.name, name); 52 assert_equals(await getFileSize(handle), 0); 53 assert_equals(await getFileContents(handle), ''); 54 }, 'getFileHandle(create=true) creates an empty file with non-ASCII characters in the name'); 55 56 directory_test(async (t, dir) => { 57 const existing_handle = await createFileWithContents( 58 'existing-file', '1234567890', /*parent=*/ dir); 59 const handle = await dir.getFileHandle('existing-file'); 60 61 assert_equals(handle.kind, 'file'); 62 assert_equals(handle.name, 'existing-file'); 63 assert_equals(await getFileSize(handle), 10); 64 assert_equals(await getFileContents(handle), '1234567890'); 65 }, 'getFileHandle(create=false) returns existing files'); 66 67 directory_test(async (t, dir) => { 68 const existing_handle = await createFileWithContents( 69 'file-with-contents', '1234567890', /*parent=*/ dir); 70 const handle = await dir.getFileHandle('file-with-contents', {create: true}); 71 72 assert_equals(handle.kind, 'file'); 73 assert_equals(handle.name, 'file-with-contents'); 74 assert_equals(await getFileSize(handle), 10); 75 assert_equals(await getFileContents(handle), '1234567890'); 76 }, 'getFileHandle(create=true) returns existing files without erasing'); 77 78 directory_test(async (t, dir) => { 79 const dir_handle = await dir.getDirectoryHandle('dir-name', {create: true}); 80 81 await promise_rejects_dom( 82 t, 'TypeMismatchError', dir.getFileHandle('dir-name')); 83 }, 'getFileHandle(create=false) when a directory already exists with the same name'); 84 85 directory_test(async (t, dir) => { 86 const dir_handle = await dir.getDirectoryHandle('dir-name', {create: true}); 87 88 await promise_rejects_dom( 89 t, 'TypeMismatchError', dir.getFileHandle('dir-name', {create: true})); 90 }, 'getFileHandle(create=true) when a directory already exists with the same name'); 91 92 directory_test(async (t, dir) => { 93 await promise_rejects_js(t, TypeError, dir.getFileHandle('', {create: true})); 94 await promise_rejects_js( 95 t, TypeError, dir.getFileHandle('', {create: false})); 96 }, 'getFileHandle() with empty name'); 97 98 directory_test(async (t, dir) => { 99 await promise_rejects_js(t, TypeError, dir.getFileHandle(kCurrentDirectory)); 100 await promise_rejects_js( 101 t, TypeError, dir.getFileHandle(kCurrentDirectory, {create: true})); 102 }, `getFileHandle() with "${kCurrentDirectory}" name`); 103 104 directory_test(async (t, dir) => { 105 const subdir = await createDirectory('subdir-name', /*parent=*/ dir); 106 107 await promise_rejects_js( 108 t, TypeError, subdir.getFileHandle(kParentDirectory)); 109 await promise_rejects_js( 110 t, TypeError, subdir.getFileHandle(kParentDirectory, {create: true})); 111 }, `getFileHandle() with "${kParentDirectory}" name`); 112 113 directory_test(async (t, dir) => { 114 const subdir_name = 'subdir-name'; 115 const subdir = await createDirectory(subdir_name, /*parent=*/ dir); 116 117 const file_name = 'file-name'; 118 await createEmptyFile(file_name, /*parent=*/ subdir); 119 120 for (let i = 0; i < kPathSeparators.length; ++i) { 121 const path_with_separator = 122 `${subdir_name}${kPathSeparators[i]}${file_name}`; 123 await promise_rejects_js( 124 t, TypeError, dir.getFileHandle(path_with_separator), 125 `getFileHandle() must reject names containing "${kPathSeparators[i]}"`); 126 } 127 }, 'getFileHandle(create=false) with a path separator when the file exists.'); 128 129 directory_test(async (t, dir) => { 130 const subdir_name = 'subdir-name'; 131 const subdir = await createDirectory(subdir_name, /*parent=*/ dir); 132 133 for (let i = 0; i < kPathSeparators.length; ++i) { 134 const path_with_separator = `${subdir_name}${kPathSeparators[i]}file_name`; 135 await promise_rejects_js( 136 t, TypeError, dir.getFileHandle(path_with_separator, {create: true}), 137 `getFileHandle(create=true) must reject names containing "${ 138 kPathSeparators[i]}"`); 139 } 140 }, 'getFileHandle(create=true) with a path separator');