test_createFiles.html (3366B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test for SpecialPowers.createFiles</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 </head> 8 <body> 9 10 <div id="content" class="testbody"> 11 <script type="text/javascript"> 12 // Creating one file, followed by failing to create a file. 13 function test1() { 14 const fileType = "some file type"; 15 let fdata = "this is same data for a file"; 16 SpecialPowers.createFiles([{name: "test1.txt", data:fdata, options:{type:fileType}}], 17 function (files) { 18 is(files.length, 1, "Created 1 file"); 19 let f = files[0]; 20 is("[object File]", f.toString(), "first thing in array is a file"); 21 is(f.size, fdata.length, "test1 size of first file should be length of its data"); 22 is("test1.txt", f.name, "test1 test file should have the right name"); 23 is(f.type, fileType, "File should have the specified type"); 24 test2(); 25 }, 26 function () { ok(false, "Should be able to create a file without an error"); test2(); } 27 ); 28 } 29 30 // Failing to create a file, followed by creating a file. 31 function test2() { 32 function test3Check(passed) { 33 ok(passed, "Should trigger the error handler for a bad file name."); 34 test3(); 35 }; 36 37 SpecialPowers.createFiles([{name: "/\/\/\/\/\/\/\/\/\/\/\invalidname",}], 38 function () { test3Check(false); }, 39 function () { test3Check(true); } 40 ); 41 } 42 43 // Creating two files at the same time. 44 function test3() { 45 let f1data = "hello"; 46 SpecialPowers.createFiles([{name: "test3_file.txt", data:f1data}, {name: "emptyfile.txt"}], 47 function (files) { 48 is(files.length, 2, "Expected two files to be created"); 49 let f1 = files[0]; 50 let f2 = files[1]; 51 is("[object File]", f1.toString(), "first thing in array is a file"); 52 is("[object File]", f2.toString(), "second thing in array is a file"); 53 is("test3_file.txt", f1.name, "first test3 test file should have the right name"); 54 is("emptyfile.txt", f2.name, "second test3 test file should have the right name"); 55 is(f1.size, f1data.length, "size of first file should be length of its data"); 56 is(f2.size, 0, "size of second file should be 0"); 57 test4(); 58 }, 59 function (msg) { 60 ok(false, "Failed to create files: " + msg); 61 test4(); 62 } 63 ); 64 }; 65 66 // Creating a file without specifying a name should work. 67 function test4() { 68 let fdata = "this is same data for a file"; 69 SpecialPowers.createFiles([{data:fdata}], 70 function (files) { 71 is(files.length, 1, "Created 1 file"); 72 let f = files[0]; 73 is("[object File]", f.toString(), "first thing in array is a file"); 74 is(f.size, fdata.length, "test4 size of first file should be length of its data"); 75 ok(f.name, "test4 test file should have a name"); 76 SimpleTest.finish(); 77 }, 78 function () { 79 ok(false, "Should be able to create a file without a name without an error"); 80 SimpleTest.finish(); 81 } 82 ); 83 } 84 85 SimpleTest.waitForExplicitFinish(); 86 test1(); 87 88 </script> 89 </div> 90 </body> 91 </html>