filesystem_commons.js (5302B)
1 function createPath(parentDir, dirOrFile) { 2 return parentDir.path + (parentDir.path == "/" ? "" : "/") + dirOrFile.name; 3 } 4 5 function createRelativePath(parentDir, dirOrFile) { 6 let path = createPath(parentDir, dirOrFile); 7 is(path[0], "/", "The full path should start with '/'"); 8 return path.substring(1); 9 } 10 11 function setup_tests(aNext) { 12 SimpleTest.requestLongerTimeout(2); 13 SpecialPowers.pushPrefEnv( 14 { 15 set: [ 16 ["dom.filesystem.pathcheck.disabled", true], 17 ["dom.webkitBlink.dirPicker.enabled", true], 18 ], 19 }, 20 aNext 21 ); 22 } 23 24 function test_basic(aDirectory, aNext) { 25 ok(aDirectory, "Directory exists."); 26 ok(aDirectory instanceof Directory, "We have a directory."); 27 is(aDirectory.path, "/" + aDirectory.name, "directory.path must be '/'+name"); 28 aNext(); 29 } 30 31 function test_getFilesAndDirectories(aDirectory, aRecursive, aNext) { 32 function checkSubDir(dir) { 33 return dir.getFilesAndDirectories().then(function (data) { 34 for (var i = 0; i < data.length; ++i) { 35 ok( 36 data[i] instanceof File || data[i] instanceof Directory, 37 "Just Files or Directories" 38 ); 39 if (data[i] instanceof Directory) { 40 isnot( 41 data[i].name, 42 "/", 43 "Subdirectory should be called with the leafname" 44 ); 45 isnot( 46 data[i].path, 47 "/", 48 "Subdirectory path should be called with the leafname" 49 ); 50 isnot( 51 data[i].path, 52 dir.path, 53 "Subdirectory path should contain the parent path." 54 ); 55 is( 56 data[i].path, 57 createPath(dir, data[i]), 58 "Subdirectory path should be called parentdir.path + '/' + leafname: " + 59 data[i].path 60 ); 61 } 62 63 if (data[i] instanceof File) { 64 is( 65 data[i].webkitRelativePath, 66 createRelativePath(dir, data[i]), 67 "File.webkitRelativePath should be called: parentdir.path + '/' + file.name: " + 68 data[i].webkitRelativePath 69 ); 70 ok( 71 !data[i].webkitRelativePath.endsWith("symlink.txt"), 72 "We should never see a path ending with symlink.txt, our symlink sentinel." 73 ); 74 } 75 } 76 }); 77 } 78 79 aDirectory 80 .getFilesAndDirectories() 81 .then( 82 function (data) { 83 ok(data.length, "We should have some data."); 84 var promises = []; 85 for (var i = 0; i < data.length; ++i) { 86 ok( 87 data[i] instanceof File || data[i] instanceof Directory, 88 "Just Files or Directories: " + data[i].name 89 ); 90 if (data[i] instanceof Directory) { 91 isnot( 92 data[i].name, 93 "/", 94 "Subdirectory should be called with the leafname" 95 ); 96 is( 97 data[i].path, 98 createPath(aDirectory, data[i]), 99 "Subdirectory path should be called parentdir.path + '/' + leafname: " + 100 data[i].path 101 ); 102 if (aRecursive) { 103 promises.push(checkSubDir(data[i])); 104 } 105 } 106 107 if (data[i] instanceof File) { 108 is( 109 data[i].webkitRelativePath, 110 createRelativePath(aDirectory, data[i]), 111 "File.webkitRelativePath should be called file.name: " + 112 data[i].webkitRelativePath 113 ); 114 } 115 } 116 117 return Promise.all(promises); 118 }, 119 function () { 120 ok(false, "Something when wrong"); 121 } 122 ) 123 .then(aNext); 124 } 125 126 function test_getFiles(aDirectory, aRecursive, aNext) { 127 aDirectory 128 .getFiles(aRecursive) 129 .then( 130 function (data) { 131 for (var i = 0; i < data.length; ++i) { 132 ok(data[i] instanceof File, "File: " + data[i].name); 133 is(aDirectory.path[0], "/", "Directory path must start with '/'"); 134 ok( 135 data[i].webkitRelativePath.indexOf(aDirectory.path.substring(1)) == 136 0 && 137 data[i].webkitRelativePath.indexOf("/" + data[i].name) + 138 ("/" + data[i].name).length == 139 data[i].webkitRelativePath.length, 140 "File.webkitRelativePath should be called dir.path + '/' + file.name: " + 141 data[i].webkitRelativePath 142 ); 143 } 144 }, 145 function () { 146 ok(false, "Something when wrong"); 147 } 148 ) 149 .then(aNext); 150 } 151 152 function test_getFiles_recursiveComparison(aDirectory, aNext) { 153 aDirectory 154 .getFiles(true) 155 .then(function (data) { 156 is(data.length, 2, "Only 2 files for this test."); 157 ok( 158 data[0].name == "foo.txt" || data[0].name == "bar.txt", 159 "First filename matches" 160 ); 161 ok( 162 data[1].name == "foo.txt" || data[1].name == "bar.txt", 163 "Second filename matches" 164 ); 165 }) 166 .then(function () { 167 return aDirectory.getFiles(false); 168 }) 169 .then(function (data) { 170 is(data.length, 1, "Only 1 file for this test."); 171 ok( 172 data[0].name == "foo.txt" || data[0].name == "bar.txt", 173 "First filename matches" 174 ); 175 }) 176 .catch(function () { 177 ok(false, "Something when wrong"); 178 }) 179 .then(aNext); 180 }