michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Components.utils.import("resource://gre/modules/FileUtils.jsm"); michael@0: michael@0: function do_check_throws(f, result, stack) { michael@0: if (!stack) michael@0: stack = Components.stack.caller; michael@0: michael@0: try { michael@0: f(); michael@0: } catch (exc) { michael@0: if (exc.result == result) michael@0: return; michael@0: do_throw("expected result " + result + ", caught " + exc, stack); michael@0: } michael@0: do_throw("expected result " + result + ", none thrown", stack); michael@0: } michael@0: michael@0: const gProfD = do_get_profile(); michael@0: michael@0: add_test(function test_getFile() { michael@0: let file = FileUtils.getFile("ProfD", ["foobar"]); michael@0: do_check_true(file instanceof Components.interfaces.nsIFile); michael@0: do_check_false(file.exists()); michael@0: michael@0: let other = gProfD.clone(); michael@0: other.append("foobar"); michael@0: do_check_true(file.equals(other)); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_getFile_nonexistentDir() { michael@0: do_check_throws(function () { michael@0: let file = FileUtils.getFile("NonexistentD", ["foobar"]); michael@0: }, Components.results.NS_ERROR_FAILURE); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_getFile_createDirs() { michael@0: let file = FileUtils.getFile("ProfD", ["a", "b", "foobar"]); michael@0: do_check_true(file instanceof Components.interfaces.nsIFile); michael@0: do_check_false(file.exists()); michael@0: michael@0: let other = gProfD.clone(); michael@0: other.append("a"); michael@0: do_check_true(other.isDirectory()); michael@0: other.append("b"); michael@0: do_check_true(other.isDirectory()); michael@0: other.append("foobar"); michael@0: do_check_true(file.equals(other)); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_getDir() { michael@0: let dir = FileUtils.getDir("ProfD", ["foodir"]); michael@0: do_check_true(dir instanceof Components.interfaces.nsIFile); michael@0: do_check_false(dir.exists()); michael@0: michael@0: let other = gProfD.clone(); michael@0: other.append("foodir"); michael@0: do_check_true(dir.equals(other)); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_getDir_nonexistentDir() { michael@0: do_check_throws(function () { michael@0: let file = FileUtils.getDir("NonexistentD", ["foodir"]); michael@0: }, Components.results.NS_ERROR_FAILURE); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_getDir_shouldCreate() { michael@0: let dir = FileUtils.getDir("ProfD", ["c", "d", "foodir"], true); michael@0: do_check_true(dir instanceof Components.interfaces.nsIFile); michael@0: do_check_true(dir.exists()); michael@0: michael@0: let other = gProfD.clone(); michael@0: other.append("c"); michael@0: do_check_true(other.isDirectory()); michael@0: other.append("d"); michael@0: do_check_true(other.isDirectory()); michael@0: other.append("foodir"); michael@0: do_check_true(dir.equals(other)); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: let openFileOutputStream_defaultFlags = function (aKind, aFileName) { michael@0: let file = FileUtils.getFile("ProfD", [aFileName]); michael@0: let fos; michael@0: do_check_true(aKind == "atomic" || aKind == "safe" || aKind == ""); michael@0: if (aKind == "atomic") { michael@0: fos = FileUtils.openAtomicFileOutputStream(file); michael@0: } else if (aKind == "safe") { michael@0: fos = FileUtils.openSafeFileOutputStream(file); michael@0: } else { michael@0: fos = FileUtils.openFileOutputStream(file); michael@0: } michael@0: do_check_true(fos instanceof Components.interfaces.nsIFileOutputStream); michael@0: if (aKind == "atomic" || aKind == "safe") { michael@0: do_check_true(fos instanceof Components.interfaces.nsISafeOutputStream); michael@0: } michael@0: michael@0: // FileUtils.openFileOutputStream or FileUtils.openAtomicFileOutputStream() michael@0: // or FileUtils.openSafeFileOutputStream() opens the stream with DEFER_OPEN michael@0: // which means the file will not be open until we write to it. michael@0: do_check_false(file.exists()); michael@0: michael@0: let data = "test_default_flags"; michael@0: fos.write(data, data.length); michael@0: do_check_true(file.exists()); michael@0: michael@0: // No nsIXULRuntime in xpcshell, so use this trick to determine whether we're michael@0: // on Windows. michael@0: if ("@mozilla.org/windows-registry-key;1" in Components.classes) { michael@0: do_check_eq(file.permissions, 0666); michael@0: } else { michael@0: do_check_eq(file.permissions, FileUtils.PERMS_FILE); michael@0: } michael@0: michael@0: run_next_test(); michael@0: }; michael@0: michael@0: let openFileOutputStream_modeFlags = function(aKind, aFileName) { michael@0: let file = FileUtils.getFile("ProfD", [aFileName]); michael@0: let fos; michael@0: do_check_true(aKind == "atomic" || aKind == "safe" || aKind == ""); michael@0: if (aKind == "atomic") { michael@0: fos = FileUtils.openAtomicFileOutputStream(file, FileUtils.MODE_WRONLY); michael@0: } else if (aKind == "safe") { michael@0: fos = FileUtils.openSafeFileOutputStream(file, FileUtils.MODE_WRONLY); michael@0: } else { michael@0: fos = FileUtils.openFileOutputStream(file, FileUtils.MODE_WRONLY); michael@0: } michael@0: let data = "test_modeFlags"; michael@0: do_check_throws(function () { michael@0: fos.write(data, data.length); michael@0: }, Components.results.NS_ERROR_FILE_NOT_FOUND); michael@0: do_check_false(file.exists()); michael@0: michael@0: run_next_test(); michael@0: }; michael@0: michael@0: let closeFileOutputStream = function(aKind, aFileName) { michael@0: let file = FileUtils.getFile("ProfD", [aFileName]); michael@0: let fos; michael@0: do_check_true(aKind == "atomic" || aKind == "safe"); michael@0: if (aKind == "atomic") { michael@0: fos = FileUtils.openAtomicFileOutputStream(file); michael@0: } else if (aKind == "safe") { michael@0: fos = FileUtils.openSafeFileOutputStream(file); michael@0: } michael@0: michael@0: // We can write data to the stream just fine while it's open. michael@0: let data = "testClose"; michael@0: fos.write(data, data.length); michael@0: michael@0: // But once we close it, we can't anymore. michael@0: if (aKind == "atomic") { michael@0: FileUtils.closeAtomicFileOutputStream(fos); michael@0: } else if (aKind == "safe"){ michael@0: FileUtils.closeSafeFileOutputStream(fos); michael@0: } michael@0: do_check_throws(function () { michael@0: fos.write(data, data.length); michael@0: }, Components.results.NS_BASE_STREAM_CLOSED); michael@0: run_next_test(); michael@0: }; michael@0: michael@0: add_test(function test_openFileOutputStream_defaultFlags() { michael@0: openFileOutputStream_defaultFlags("", "george"); michael@0: }); michael@0: michael@0: // openFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE michael@0: // as the default mode flags, but we can pass in our own if we want to. michael@0: add_test(function test_openFileOutputStream_modeFlags() { michael@0: openFileOutputStream_modeFlags("", "ringo"); michael@0: }); michael@0: michael@0: add_test(function test_openAtomicFileOutputStream_defaultFlags() { michael@0: openFileOutputStream_defaultFlags("atomic", "peiyong"); michael@0: }); michael@0: michael@0: // openAtomicFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE michael@0: // as the default mode flags, but we can pass in our own if we want to. michael@0: add_test(function test_openAtomicFileOutputStream_modeFlags() { michael@0: openFileOutputStream_modeFlags("atomic", "lin"); michael@0: }); michael@0: michael@0: add_test(function test_closeAtomicFileOutputStream() { michael@0: closeFileOutputStream("atomic", "peiyonglin"); michael@0: }); michael@0: michael@0: add_test(function test_openSafeFileOutputStream_defaultFlags() { michael@0: openFileOutputStream_defaultFlags("safe", "john"); michael@0: }); michael@0: michael@0: // openSafeFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE michael@0: // as the default mode flags, but we can pass in our own if we want to. michael@0: add_test(function test_openSafeFileOutputStream_modeFlags() { michael@0: openFileOutputStream_modeFlags("safe", "paul"); michael@0: }); michael@0: michael@0: add_test(function test_closeSafeFileOutputStream() { michael@0: closeFileOutputStream("safe", "georgee"); michael@0: }); michael@0: michael@0: add_test(function test_newFile() { michael@0: let testfile = FileUtils.getFile("ProfD", ["test"]); michael@0: let testpath = testfile.path; michael@0: let file = new FileUtils.File(testpath); michael@0: do_check_true(file instanceof Components.interfaces.nsILocalFile); michael@0: do_check_true(file.equals(testfile)); michael@0: do_check_eq(file.path, testpath); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: }