test_basic.html (15995B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test for Blink FileSystem API - subset</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 </head> 8 9 <body> 10 <input id="entries" type="file"></input> 11 <script type="application/javascript"> 12 var fileEntry; 13 var directoryEntry; 14 var script; 15 16 function setup_tests() { 17 SpecialPowers.pushPrefEnv({"set": [["dom.webkitBlink.dirPicker.enabled", true], 18 ["dom.filesystem.pathcheck.disabled", true], 19 ["dom.webkitBlink.filesystem.enabled", true]]}, next); 20 } 21 22 function populate_entries() { 23 var url = SimpleTest.getTestFileURL("script_entries.js"); 24 script = SpecialPowers.loadChromeScript(url); 25 26 function onOpened(message) { 27 var entries = document.getElementById("entries"); 28 SpecialPowers.wrap(entries).mozSetDndFilesAndDirectories(message.data); 29 next(); 30 } 31 32 script.addMessageListener("entries.opened", onOpened); 33 script.sendAsyncMessage("entries.open"); 34 } 35 36 function test_entries() { 37 var entries = document.getElementById("entries"); 38 ok("webkitEntries" in entries, "HTMLInputElement.webkitEntries"); 39 is(entries.webkitEntries.length, 2, "HTMLInputElement.webkitEntries.length == 2"); 40 is(entries.files.length, 1, "HTMLInputElement.files is still populated"); 41 42 for (var i = 0; i < entries.webkitEntries.length; ++i) { 43 if (entries.webkitEntries[i].isFile) { 44 ok(!fileEntry, "We just want 1 fileEntry"); 45 fileEntry = entries.webkitEntries[i]; 46 } else { 47 ok(entries.webkitEntries[i].isDirectory, "If not a file, we have a directory."); 48 ok(!directoryEntry, "We just want 1 directoryEntry"); 49 directoryEntry = entries.webkitEntries[i]; 50 } 51 } 52 53 next(); 54 } 55 56 function test_fileEntry() { 57 ok("name" in fileEntry, "We have a name."); 58 ok("fullPath" in fileEntry, "We have a fullPath."); 59 ok("filesystem" in fileEntry, "We have a filesystem."); 60 61 next(); 62 } 63 64 function test_fileEntry_file() { 65 fileEntry.file(function(file) { 66 ok(file, "We have a file here!"); 67 is(file.name, fileEntry.name, "Same file name."); 68 next(); 69 }, function() { 70 ok(false, "Something when wrong!"); 71 }); 72 } 73 74 function test_fileEntry_getParent() { 75 fileEntry.getParent(function(entry) { 76 is(fileEntry.fullPath, entry.fullPath, "Top level FileEntry should return itself as parent."); 77 next(); 78 }, function() { 79 ok(false, "This is wrong."); 80 }); 81 } 82 83 function test_directoryEntry() { 84 ok("name" in directoryEntry, "We have a name."); 85 ok("fullPath" in directoryEntry, "We have a fullPath."); 86 ok("filesystem" in directoryEntry, "We have a filesystem."); 87 88 next(); 89 } 90 91 function test_directoryEntry_createReader() { 92 var reader = directoryEntry.createReader(); 93 ok(reader, "We have a DirectoryReader"); 94 95 reader.readEntries(function(a) { 96 ok(Array.isArray(a), "We want an array."); 97 is(a.length, 2, "reader.readyEntries returns 2 elements."); 98 99 for (var i = 0; i < 2; ++i) { 100 ok(a[i].name == "subdir" || a[i].name == "foo.txt", "Correct names"); 101 is(a[i].fullPath, directoryEntry.fullPath + "/" + a[i].name, "FullPath is correct"); 102 } 103 104 // Called twice: 105 reader.readEntries(function(a1) { 106 ok(Array.isArray(a1), "We want an array."); 107 is(a1.length, 0, "reader.readyEntries returns 0 elements."); 108 next(); 109 }, function() { 110 ok(false, "Something when wrong!"); 111 }); 112 }, function() { 113 ok(false, "Something when wrong!"); 114 }); 115 } 116 117 function test_directoryEntry_getParent() { 118 directoryEntry.getParent(function(entry) { 119 is(directoryEntry.fullPath, entry.fullPath, "Top level FileEntry should return itself as parent."); 120 next(); 121 }, function() { 122 ok(false, "This is wrong."); 123 }); 124 } 125 126 function test_directoryEntry_getFile_securityError() { 127 directoryEntry.getFile("foo", { create: true }, 128 function() { 129 ok(false, "This should not happen."); 130 }, function(e) { 131 is(e.name, "SecurityError", "This must generate a SecurityError."); 132 next(); 133 }); 134 } 135 136 function test_directoryEntry_getFile_typeMismatchError() { 137 directoryEntry.getFile("subdir", {}, 138 function() { 139 ok(false, "This should not happen."); 140 }, function(e) { 141 is(e.name, "TypeMismatchError", "This must generate a TypeMismatchError."); 142 next(); 143 }); 144 } 145 146 function test_directoryEntry_getFile_nonValidPath() { 147 directoryEntry.getFile("../../", {}, 148 function() { 149 ok(false, "This should not happen."); 150 }, function(e) { 151 is(e.name, "NotFoundError", "This must generate a NotFoundError."); 152 next(); 153 }); 154 } 155 156 function test_directoryEntry_getFile_nonExistingPath() { 157 directoryEntry.getFile("foo_bar.txt", {}, 158 function() { 159 ok(false, "This should not happen."); 160 }, function(e) { 161 is(e.name, "NotFoundError", "This must generate a NotFoundError."); 162 next(); 163 }); 164 } 165 166 function test_directoryEntry_getFile_simple() { 167 directoryEntry.getFile("foo.txt", {}, 168 function(e) { 169 is(e.name, "foo.txt", "We have the right FileEntry."); 170 test_getParent(e, directoryEntry, /* nested */ false); 171 }, function() { 172 ok(false, "This should not happen."); 173 }); 174 } 175 176 function test_directoryEntry_getFile_deep() { 177 directoryEntry.getFile("subdir/bar..txt", {}, 178 function(e) { 179 is(e.name, "bar..txt", "We have the right FileEntry."); 180 test_getParent(e, directoryEntry, /* nested */ true); 181 }, function() { 182 ok(false, "This should not happen."); 183 }); 184 } 185 186 function test_directoryEntry_getDirectory_securityError() { 187 directoryEntry.getDirectory("foo", { create: true }, 188 function() { 189 ok(false, "This should not happen."); 190 }, function(e) { 191 is(e.name, "SecurityError", "This must generate a SecurityError."); 192 next(); 193 }); 194 } 195 196 function test_directoryEntry_getDirectory_typeMismatchError() { 197 directoryEntry.getDirectory("foo.txt", {}, 198 function() { 199 ok(false, "This should not happen."); 200 }, function(e) { 201 is(e.name, "TypeMismatchError", "This must generate a TypeMismatchError."); 202 next(); 203 }); 204 } 205 206 function test_directoryEntry_getDirectory_nonValidPath() { 207 directoryEntry.getDirectory("../../", {}, 208 function() { 209 ok(false, "This should not happen."); 210 }, function(e) { 211 is(e.name, "NotFoundError", "This must generate a NotFoundError."); 212 next(); 213 }); 214 } 215 216 function test_directoryEntry_getDirectory_nonExistingPath() { 217 directoryEntry.getDirectory("non_existing_dir", {}, 218 function() { 219 ok(false, "This should not happen."); 220 }, function(e) { 221 is(e.name, "NotFoundError", "This must generate a NotFoundError."); 222 next(); 223 }); 224 } 225 226 function test_directoryEntry_getDirectory_simple() { 227 directoryEntry.getDirectory("subdir", {}, 228 function(e) { 229 is(e.name, "subdir", "We have the right DirectoryEntry."); 230 test_getParent(e, directoryEntry, /* nested */ false); 231 }, function() { 232 ok(false, "This should not happen."); 233 }); 234 } 235 236 function test_directoryEntry_getDirectory_deep() { 237 directoryEntry.getDirectory("subdir/subsubdir", {}, 238 function(e) { 239 is(e.name, "subsubdir", "We have the right DirectoryEntry."); 240 test_getParent(e, directoryEntry, /* nested */ true); 241 }, function() { 242 ok(false, "This should not happen."); 243 }); 244 } 245 246 function test_filesystem() { 247 is(fileEntry.filesystem, directoryEntry.filesystem, "FileSystem object is shared."); 248 249 var fs = fileEntry.filesystem; 250 ok(fs.name, "FileSystem.name exists."); 251 ok(fs.root, "FileSystem has a root."); 252 253 is(fs.root.name, "", "FileSystem.root.name must be an empty string."); 254 is(fs.root.fullPath, "/", "FileSystem.root.fullPath must be '/'"); 255 256 var reader = fs.root.createReader(); 257 reader.readEntries(function(a) { 258 ok(Array.isArray(a), "We want an array."); 259 is(a.length, 2, "reader.readyEntries returns 2 elements."); 260 next(); 261 }, function() { 262 ok(false, "Something when wrong!"); 263 }); 264 } 265 266 function test_root_getFile_securityError() { 267 fileEntry.filesystem.root.getFile("foo", { create: true }, 268 function() { 269 ok(false, "This should not happen."); 270 }, function(e) { 271 is(e.name, "SecurityError", "This must generate a SecurityError."); 272 next(); 273 }); 274 } 275 276 function test_root_getFile_typeMismatchError() { 277 fileEntry.filesystem.root.getFile(directoryEntry.name, {}, 278 function() { 279 ok(false, "This should not happen."); 280 }, function(e) { 281 is(e.name, "TypeMismatchError", "This must generate a TypeMismatchError."); 282 next(); 283 }); 284 } 285 286 function test_root_getFile_nonValidPath() { 287 fileEntry.filesystem.root.getFile("../../", {}, 288 function() { 289 ok(false, "This should not happen."); 290 }, function(e) { 291 is(e.name, "NotFoundError", "This must generate a NotFoundError."); 292 next(); 293 }); 294 } 295 296 function test_root_getFile_nonExistingPath() { 297 fileEntry.filesystem.root.getFile("existing.txt", {}, 298 function() { 299 ok(false, "This should not happen."); 300 }, function(e) { 301 is(e.name, "NotFoundError", "This must generate a NotFoundError."); 302 next(); 303 }); 304 } 305 306 function test_root_getFile_simple() { 307 fileEntry.filesystem.root.getFile(fileEntry.name, {}, 308 function(e) { 309 is(e.name, fileEntry.name, "We have the right FileEntry."); 310 next(); 311 }, function() { 312 ok(false, "This should not happen."); 313 }); 314 } 315 316 function test_root_getFile_deep() { 317 fileEntry.filesystem.root.getFile(directoryEntry.name + "/subdir/bar..txt", {}, 318 function(e) { 319 is(e.name, "bar..txt", "We have the right FileEntry."); 320 next(); 321 }, function() { 322 ok(false, "This should not happen."); 323 }); 324 } 325 326 function test_root_getDirectory_securityError() { 327 fileEntry.filesystem.root.getDirectory("foo", { create: true }, 328 function() { 329 ok(false, "This should not happen."); 330 }, function(e) { 331 is(e.name, "SecurityError", "This must generate a SecurityError."); 332 next(); 333 }); 334 } 335 336 function test_root_getDirectory_typeMismatchError() { 337 fileEntry.filesystem.root.getDirectory(fileEntry.name, {}, 338 function() { 339 ok(false, "This should not happen."); 340 }, function(e) { 341 is(e.name, "TypeMismatchError", "This must generate a TypeMismatchError."); 342 next(); 343 }); 344 } 345 346 function test_root_getDirectory_nonValidPath() { 347 fileEntry.filesystem.root.getDirectory("../../", {}, 348 function() { 349 ok(false, "This should not happen."); 350 }, function(e) { 351 is(e.name, "NotFoundError", "This must generate a NotFoundError."); 352 next(); 353 }); 354 } 355 356 function test_root_getDirectory_nonExistingPath() { 357 fileEntry.filesystem.root.getDirectory("404", {}, 358 function() { 359 ok(false, "This should not happen."); 360 }, function(e) { 361 is(e.name, "NotFoundError", "This must generate a NotFoundError."); 362 next(); 363 }); 364 } 365 366 function test_root_getDirectory_simple() { 367 fileEntry.filesystem.root.getDirectory(directoryEntry.name, {}, 368 function(e) { 369 is(e.name, directoryEntry.name, "We have the right DirectoryEntry."); 370 next(); 371 }, function() { 372 ok(false, "This should not happen."); 373 }); 374 } 375 376 function test_root_getDirectory_deep() { 377 fileEntry.filesystem.root.getDirectory(directoryEntry.name + "/subdir/subsubdir", {}, 378 function(e) { 379 is(e.name, "subsubdir", "We have the right DirectoryEntry."); 380 next(); 381 }, function() { 382 ok(false, "This should not happen."); 383 }); 384 } 385 386 function cleanUpTestingFiles() { 387 script.addMessageListener("entries.deleted", function onDeleted() { 388 script.removeMessageListener("entries.deleted"); 389 script.destroy(); 390 next(); 391 }); 392 393 script.sendAsyncMessage("entries.delete"); 394 } 395 396 function test_getParent(entry, parentEntry, nested) { 397 entry.getParent(function(e) { 398 ok(e, "We have a parent Entry."); 399 if (!nested) { 400 is(e, parentEntry, "Parent entry matches"); 401 next(); 402 } else { 403 test_getParent(e, parentEntry, false); 404 } 405 }, function() { 406 ok(false, "This should not happen."); 407 }); 408 } 409 410 function test_webkitRelativePath() { 411 fileEntry.file(function(file1) { 412 ok(file1, "We have a file here!"); 413 ok(!file1.webkitRelativePath, "webkitRelativePath is an empty string"); 414 415 fileEntry.file(function(file2) { 416 ok(file2, "We have a file here!"); 417 ok(!file2.webkitRelativePath, "webkitRelativePath is an empty string"); 418 isnot(file1, file2, "The 2 files are not the same"); 419 420 next(); 421 }, function() { 422 ok(false, "Something when wrong!"); 423 }); 424 }, function() { 425 ok(false, "Something when wrong!"); 426 }); 427 } 428 429 function test_deprecatedCallbacks() { 430 try { 431 fileEntry.file({ handleEvent: _ => { ok(false, "This function should not be called!"); }}); 432 ok(false, "fileEntry.file() should throw with wrong arguments"); 433 } catch (e) { 434 ok(true, "fileEntry.file() should throw with wrong arguments"); 435 is(e.name, "TypeError", "Correct exception"); 436 } 437 438 try { 439 fileEntry.getParent({ handleEvent: _ => { ok(false, "This function should not be called!"); }}); 440 ok(false, "fileEntry.getParent() should throw with wrong arguments"); 441 } catch (e) { 442 ok(true, "fileEntry.getParent() should throw with wrong arguments"); 443 is(e.name, "TypeError", "Correct exception"); 444 } 445 446 try { 447 directoryEntry.getFile("file.txt", {}, { handleEvent: _ => { ok(false, "This function should not be called!"); }}); 448 ok(false, "directoryEntry.getFile() should throw with wrong arguments"); 449 } catch (e) { 450 ok(true, "directoryEntry.getFile() should throw with wrong arguments"); 451 is(e.name, "TypeError", "Correct exception"); 452 } 453 454 try { 455 directoryEntry.getDirectory("foo", { create: true }, { handleEvent: _ => { ok(false, "This function should not be called!"); }}); 456 ok(false, "directoryEntry.getDirectory() should throw with wrong arguments"); 457 } catch (e) { 458 ok(true, "directoryEntry.getDirectory() should throw with wrong arguments"); 459 is(e.name, "TypeError", "Correct exception"); 460 } 461 462 try { 463 directoryEntry.getParent({ handleEvent: _ => { ok(false, "This function should not be called!"); }}); 464 ok(false, "directoryEntry.getParent() should throw with wrong arguments"); 465 } catch (e) { 466 ok(true, "directoryEntry.getParent() should throw with wrong arguments"); 467 is(e.name, "TypeError", "Correct exception"); 468 } 469 470 let reader = directoryEntry.createReader(); 471 ok(reader, "We have a DirectoryReader"); 472 473 try { 474 reader.readEntries({ handleEvent: _ => { ok(false, "This function should not be called!"); }}); 475 ok(false, "reader.readEntries() should throw with wrong arguments"); 476 } catch (e) { 477 ok(true, "reader.readEntries() should throw with wrong arguments"); 478 is(e.name, "TypeError", "Correct exception"); 479 } 480 481 next(); 482 } 483 484 var tests = [ 485 setup_tests, 486 populate_entries, 487 488 test_entries, 489 490 test_fileEntry, 491 test_fileEntry_file, 492 test_fileEntry_getParent, 493 494 test_directoryEntry, 495 test_directoryEntry_createReader, 496 test_directoryEntry_getParent, 497 498 test_directoryEntry_getFile_securityError, 499 test_directoryEntry_getFile_typeMismatchError, 500 test_directoryEntry_getFile_nonValidPath, 501 test_directoryEntry_getFile_nonExistingPath, 502 test_directoryEntry_getFile_simple, 503 test_directoryEntry_getFile_deep, 504 505 test_directoryEntry_getDirectory_securityError, 506 test_directoryEntry_getDirectory_typeMismatchError, 507 test_directoryEntry_getDirectory_nonValidPath, 508 test_directoryEntry_getDirectory_nonExistingPath, 509 test_directoryEntry_getDirectory_simple, 510 test_directoryEntry_getDirectory_deep, 511 512 test_filesystem, 513 514 test_root_getFile_securityError, 515 test_root_getFile_typeMismatchError, 516 test_root_getFile_nonValidPath, 517 test_root_getFile_nonExistingPath, 518 test_root_getFile_simple, 519 test_root_getFile_deep, 520 521 test_root_getDirectory_securityError, 522 test_root_getDirectory_typeMismatchError, 523 test_root_getDirectory_nonValidPath, 524 test_root_getDirectory_nonExistingPath, 525 test_root_getDirectory_simple, 526 test_root_getDirectory_deep, 527 528 test_webkitRelativePath, 529 530 test_deprecatedCallbacks, 531 532 cleanUpTestingFiles, 533 ]; 534 535 function next() { 536 if (!tests.length) { 537 SimpleTest.finish(); 538 return; 539 } 540 541 var test = tests.shift(); 542 test(); 543 } 544 545 SimpleTest.waitForExplicitFinish(); 546 next(); 547 </script> 548 </body> 549 </html>