head.js (2005B)
1 ChromeUtils.defineESModuleGetters(this, { 2 Downloads: "resource://gre/modules/Downloads.sys.mjs", 3 DownloadsCommon: 4 "moz-src:///browser/components/downloads/DownloadsCommon.sys.mjs", 5 FileTestUtils: "resource://testing-common/FileTestUtils.sys.mjs", 6 FileUtils: "resource://gre/modules/FileUtils.sys.mjs", 7 NetUtil: "resource://gre/modules/NetUtil.sys.mjs", 8 TestUtils: "resource://testing-common/TestUtils.sys.mjs", 9 }); 10 11 async function createDownloadedFile(pathname, contents) { 12 info("createDownloadedFile: " + pathname); 13 let file = new FileUtils.File(pathname); 14 if (file.exists()) { 15 info(`File at ${pathname} already exists`); 16 if (!contents) { 17 ok( 18 false, 19 `A file already exists at ${pathname}, but createDownloadedFile was asked to create a non-existant file` 20 ); 21 } 22 } 23 if (contents) { 24 await IOUtils.writeUTF8(pathname, contents); 25 ok(file.exists(), `Created ${pathname}`); 26 } 27 // No post-test cleanup necessary; tmp downloads directory is already removed after each test 28 return file; 29 } 30 31 let gDownloadDir; 32 33 async function setDownloadDir() { 34 let tmpDir = Services.dirsvc.get("TmpD", Ci.nsIFile).path; 35 tmpDir = PathUtils.join( 36 tmpDir, 37 "testsavedir" + Math.floor(Math.random() * 2 ** 32) 38 ); 39 // Create this dir if it doesn't exist (ignores existing dirs) 40 await IOUtils.makeDirectory(tmpDir); 41 registerCleanupFunction(async function () { 42 try { 43 await IOUtils.remove(tmpDir, { recursive: true }); 44 } catch (e) { 45 console.error(e); 46 } 47 }); 48 Services.prefs.setIntPref("browser.download.folderList", 2); 49 Services.prefs.setCharPref("browser.download.dir", tmpDir); 50 return tmpDir; 51 } 52 53 /** 54 * All the tests are implemented with add_task, this starts them automatically. 55 */ 56 function run_test() { 57 do_get_profile(); 58 run_next_test(); 59 } 60 61 add_setup(async function test_common_initialize() { 62 gDownloadDir = await setDownloadDir(); 63 Services.prefs.setCharPref("toolkit.download.loglevel", "Debug"); 64 });