toolkit/modules/tests/xpcshell/test_FileUtils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/tests/xpcshell/test_FileUtils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,226 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Components.utils.import("resource://gre/modules/FileUtils.jsm");
     1.8 +
     1.9 +function do_check_throws(f, result, stack) {
    1.10 +  if (!stack)
    1.11 +    stack = Components.stack.caller;
    1.12 +
    1.13 +  try {
    1.14 +    f();
    1.15 +  } catch (exc) {
    1.16 +    if (exc.result == result)
    1.17 +      return;
    1.18 +    do_throw("expected result " + result + ", caught " + exc, stack);
    1.19 +  }
    1.20 +  do_throw("expected result " + result + ", none thrown", stack);
    1.21 +}
    1.22 +
    1.23 +const gProfD = do_get_profile();
    1.24 +
    1.25 +add_test(function test_getFile() {
    1.26 +  let file = FileUtils.getFile("ProfD", ["foobar"]);
    1.27 +  do_check_true(file instanceof Components.interfaces.nsIFile);
    1.28 +  do_check_false(file.exists());
    1.29 +
    1.30 +  let other = gProfD.clone();
    1.31 +  other.append("foobar");
    1.32 +  do_check_true(file.equals(other));
    1.33 +
    1.34 +  run_next_test();
    1.35 +});
    1.36 +
    1.37 +add_test(function test_getFile_nonexistentDir() {
    1.38 +  do_check_throws(function () {
    1.39 +    let file = FileUtils.getFile("NonexistentD", ["foobar"]);
    1.40 +  }, Components.results.NS_ERROR_FAILURE);
    1.41 +
    1.42 +  run_next_test();
    1.43 +});
    1.44 +
    1.45 +add_test(function test_getFile_createDirs() {
    1.46 +  let file = FileUtils.getFile("ProfD", ["a", "b", "foobar"]);
    1.47 +  do_check_true(file instanceof Components.interfaces.nsIFile);
    1.48 +  do_check_false(file.exists());
    1.49 +
    1.50 +  let other = gProfD.clone();
    1.51 +  other.append("a");
    1.52 +  do_check_true(other.isDirectory());
    1.53 +  other.append("b");
    1.54 +  do_check_true(other.isDirectory());
    1.55 +  other.append("foobar");
    1.56 +  do_check_true(file.equals(other));
    1.57 +
    1.58 +  run_next_test();
    1.59 +});
    1.60 +
    1.61 +add_test(function test_getDir() {
    1.62 +  let dir = FileUtils.getDir("ProfD", ["foodir"]);
    1.63 +  do_check_true(dir instanceof Components.interfaces.nsIFile);
    1.64 +  do_check_false(dir.exists());
    1.65 +
    1.66 +  let other = gProfD.clone();
    1.67 +  other.append("foodir");
    1.68 +  do_check_true(dir.equals(other));
    1.69 +
    1.70 +  run_next_test();
    1.71 +});
    1.72 +
    1.73 +add_test(function test_getDir_nonexistentDir() {
    1.74 +  do_check_throws(function () {
    1.75 +    let file = FileUtils.getDir("NonexistentD", ["foodir"]);
    1.76 +  }, Components.results.NS_ERROR_FAILURE);
    1.77 +
    1.78 +  run_next_test();
    1.79 +});
    1.80 +
    1.81 +add_test(function test_getDir_shouldCreate() {
    1.82 +  let dir = FileUtils.getDir("ProfD", ["c", "d", "foodir"], true);
    1.83 +  do_check_true(dir instanceof Components.interfaces.nsIFile);
    1.84 +  do_check_true(dir.exists());
    1.85 +
    1.86 +  let other = gProfD.clone();
    1.87 +  other.append("c");
    1.88 +  do_check_true(other.isDirectory());
    1.89 +  other.append("d");
    1.90 +  do_check_true(other.isDirectory());
    1.91 +  other.append("foodir");
    1.92 +  do_check_true(dir.equals(other));
    1.93 +
    1.94 +  run_next_test();
    1.95 +});
    1.96 +
    1.97 +let openFileOutputStream_defaultFlags = function (aKind, aFileName) {
    1.98 +  let file = FileUtils.getFile("ProfD", [aFileName]);
    1.99 +  let fos;
   1.100 +  do_check_true(aKind == "atomic" || aKind == "safe" || aKind == "");
   1.101 +  if (aKind == "atomic") {
   1.102 +    fos = FileUtils.openAtomicFileOutputStream(file);
   1.103 +  } else if (aKind == "safe") {
   1.104 +    fos = FileUtils.openSafeFileOutputStream(file);
   1.105 +  } else {
   1.106 +    fos = FileUtils.openFileOutputStream(file);
   1.107 +  }
   1.108 +  do_check_true(fos instanceof Components.interfaces.nsIFileOutputStream);
   1.109 +  if (aKind == "atomic" || aKind == "safe") {
   1.110 +    do_check_true(fos instanceof Components.interfaces.nsISafeOutputStream);
   1.111 +  }
   1.112 +
   1.113 +  // FileUtils.openFileOutputStream or FileUtils.openAtomicFileOutputStream()
   1.114 +  // or FileUtils.openSafeFileOutputStream() opens the stream with DEFER_OPEN
   1.115 +  // which means the file will not be open until we write to it.
   1.116 +  do_check_false(file.exists());
   1.117 +
   1.118 +  let data = "test_default_flags";
   1.119 +  fos.write(data, data.length);
   1.120 +  do_check_true(file.exists());
   1.121 +
   1.122 +  // No nsIXULRuntime in xpcshell, so use this trick to determine whether we're
   1.123 +  // on Windows.
   1.124 +  if ("@mozilla.org/windows-registry-key;1" in Components.classes) {
   1.125 +    do_check_eq(file.permissions, 0666);
   1.126 +  } else {
   1.127 +    do_check_eq(file.permissions, FileUtils.PERMS_FILE);
   1.128 +  }
   1.129 +
   1.130 +  run_next_test();
   1.131 +};
   1.132 +
   1.133 +let openFileOutputStream_modeFlags = function(aKind, aFileName) {
   1.134 +  let file = FileUtils.getFile("ProfD", [aFileName]);
   1.135 +  let fos;
   1.136 +  do_check_true(aKind == "atomic" || aKind == "safe" || aKind == "");
   1.137 +  if (aKind == "atomic") {
   1.138 +    fos = FileUtils.openAtomicFileOutputStream(file, FileUtils.MODE_WRONLY);
   1.139 +  } else if (aKind == "safe") {
   1.140 +    fos = FileUtils.openSafeFileOutputStream(file, FileUtils.MODE_WRONLY);
   1.141 +  } else {
   1.142 +    fos = FileUtils.openFileOutputStream(file, FileUtils.MODE_WRONLY);
   1.143 +  }
   1.144 +  let data = "test_modeFlags";
   1.145 +  do_check_throws(function () {
   1.146 +    fos.write(data, data.length);
   1.147 +  }, Components.results.NS_ERROR_FILE_NOT_FOUND);
   1.148 +  do_check_false(file.exists());
   1.149 +
   1.150 +  run_next_test();
   1.151 +};
   1.152 +
   1.153 +let closeFileOutputStream = function(aKind, aFileName) {
   1.154 +  let file = FileUtils.getFile("ProfD", [aFileName]);
   1.155 +  let fos;
   1.156 +  do_check_true(aKind == "atomic" || aKind == "safe");
   1.157 +  if (aKind == "atomic") {
   1.158 +    fos = FileUtils.openAtomicFileOutputStream(file);
   1.159 +  } else if (aKind == "safe") {
   1.160 +    fos = FileUtils.openSafeFileOutputStream(file);
   1.161 +  }
   1.162 +
   1.163 +  // We can write data to the stream just fine while it's open.
   1.164 +  let data = "testClose";
   1.165 +  fos.write(data, data.length);
   1.166 +
   1.167 +  // But once we close it, we can't anymore.
   1.168 +  if (aKind == "atomic") {
   1.169 +    FileUtils.closeAtomicFileOutputStream(fos);
   1.170 +  } else if (aKind == "safe"){
   1.171 +    FileUtils.closeSafeFileOutputStream(fos);
   1.172 +  }
   1.173 +  do_check_throws(function () {
   1.174 +    fos.write(data, data.length);
   1.175 +  }, Components.results.NS_BASE_STREAM_CLOSED);
   1.176 +  run_next_test();
   1.177 +};
   1.178 +
   1.179 +add_test(function test_openFileOutputStream_defaultFlags() {
   1.180 +  openFileOutputStream_defaultFlags("", "george");
   1.181 +});
   1.182 +
   1.183 +// openFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE
   1.184 +// as the default mode flags, but we can pass in our own if we want to.
   1.185 +add_test(function test_openFileOutputStream_modeFlags() {
   1.186 +  openFileOutputStream_modeFlags("", "ringo");
   1.187 +});
   1.188 +
   1.189 +add_test(function test_openAtomicFileOutputStream_defaultFlags() {
   1.190 +  openFileOutputStream_defaultFlags("atomic", "peiyong");
   1.191 +});
   1.192 +
   1.193 +// openAtomicFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE
   1.194 +// as the default mode flags, but we can pass in our own if we want to.
   1.195 +add_test(function test_openAtomicFileOutputStream_modeFlags() {
   1.196 +  openFileOutputStream_modeFlags("atomic", "lin");
   1.197 +});
   1.198 +
   1.199 +add_test(function test_closeAtomicFileOutputStream() {
   1.200 +  closeFileOutputStream("atomic", "peiyonglin");
   1.201 +});
   1.202 +
   1.203 +add_test(function test_openSafeFileOutputStream_defaultFlags() {
   1.204 +  openFileOutputStream_defaultFlags("safe", "john");
   1.205 +});
   1.206 +
   1.207 +// openSafeFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE
   1.208 +// as the default mode flags, but we can pass in our own if we want to.
   1.209 +add_test(function test_openSafeFileOutputStream_modeFlags() {
   1.210 +  openFileOutputStream_modeFlags("safe", "paul");
   1.211 +});
   1.212 +
   1.213 +add_test(function test_closeSafeFileOutputStream() {
   1.214 +  closeFileOutputStream("safe", "georgee");
   1.215 +});
   1.216 +
   1.217 +add_test(function test_newFile() {
   1.218 +  let testfile = FileUtils.getFile("ProfD", ["test"]);
   1.219 +  let testpath = testfile.path;
   1.220 +  let file = new FileUtils.File(testpath);
   1.221 +  do_check_true(file instanceof Components.interfaces.nsILocalFile);
   1.222 +  do_check_true(file.equals(testfile));
   1.223 +  do_check_eq(file.path, testpath);
   1.224 +  run_next_test();
   1.225 +});
   1.226 +
   1.227 +function run_test() {
   1.228 +  run_next_test();
   1.229 +}

mercurial