Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Components.utils.import("resource://gre/modules/FileUtils.jsm"); |
michael@0 | 5 | |
michael@0 | 6 | function do_check_throws(f, result, stack) { |
michael@0 | 7 | if (!stack) |
michael@0 | 8 | stack = Components.stack.caller; |
michael@0 | 9 | |
michael@0 | 10 | try { |
michael@0 | 11 | f(); |
michael@0 | 12 | } catch (exc) { |
michael@0 | 13 | if (exc.result == result) |
michael@0 | 14 | return; |
michael@0 | 15 | do_throw("expected result " + result + ", caught " + exc, stack); |
michael@0 | 16 | } |
michael@0 | 17 | do_throw("expected result " + result + ", none thrown", stack); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | const gProfD = do_get_profile(); |
michael@0 | 21 | |
michael@0 | 22 | add_test(function test_getFile() { |
michael@0 | 23 | let file = FileUtils.getFile("ProfD", ["foobar"]); |
michael@0 | 24 | do_check_true(file instanceof Components.interfaces.nsIFile); |
michael@0 | 25 | do_check_false(file.exists()); |
michael@0 | 26 | |
michael@0 | 27 | let other = gProfD.clone(); |
michael@0 | 28 | other.append("foobar"); |
michael@0 | 29 | do_check_true(file.equals(other)); |
michael@0 | 30 | |
michael@0 | 31 | run_next_test(); |
michael@0 | 32 | }); |
michael@0 | 33 | |
michael@0 | 34 | add_test(function test_getFile_nonexistentDir() { |
michael@0 | 35 | do_check_throws(function () { |
michael@0 | 36 | let file = FileUtils.getFile("NonexistentD", ["foobar"]); |
michael@0 | 37 | }, Components.results.NS_ERROR_FAILURE); |
michael@0 | 38 | |
michael@0 | 39 | run_next_test(); |
michael@0 | 40 | }); |
michael@0 | 41 | |
michael@0 | 42 | add_test(function test_getFile_createDirs() { |
michael@0 | 43 | let file = FileUtils.getFile("ProfD", ["a", "b", "foobar"]); |
michael@0 | 44 | do_check_true(file instanceof Components.interfaces.nsIFile); |
michael@0 | 45 | do_check_false(file.exists()); |
michael@0 | 46 | |
michael@0 | 47 | let other = gProfD.clone(); |
michael@0 | 48 | other.append("a"); |
michael@0 | 49 | do_check_true(other.isDirectory()); |
michael@0 | 50 | other.append("b"); |
michael@0 | 51 | do_check_true(other.isDirectory()); |
michael@0 | 52 | other.append("foobar"); |
michael@0 | 53 | do_check_true(file.equals(other)); |
michael@0 | 54 | |
michael@0 | 55 | run_next_test(); |
michael@0 | 56 | }); |
michael@0 | 57 | |
michael@0 | 58 | add_test(function test_getDir() { |
michael@0 | 59 | let dir = FileUtils.getDir("ProfD", ["foodir"]); |
michael@0 | 60 | do_check_true(dir instanceof Components.interfaces.nsIFile); |
michael@0 | 61 | do_check_false(dir.exists()); |
michael@0 | 62 | |
michael@0 | 63 | let other = gProfD.clone(); |
michael@0 | 64 | other.append("foodir"); |
michael@0 | 65 | do_check_true(dir.equals(other)); |
michael@0 | 66 | |
michael@0 | 67 | run_next_test(); |
michael@0 | 68 | }); |
michael@0 | 69 | |
michael@0 | 70 | add_test(function test_getDir_nonexistentDir() { |
michael@0 | 71 | do_check_throws(function () { |
michael@0 | 72 | let file = FileUtils.getDir("NonexistentD", ["foodir"]); |
michael@0 | 73 | }, Components.results.NS_ERROR_FAILURE); |
michael@0 | 74 | |
michael@0 | 75 | run_next_test(); |
michael@0 | 76 | }); |
michael@0 | 77 | |
michael@0 | 78 | add_test(function test_getDir_shouldCreate() { |
michael@0 | 79 | let dir = FileUtils.getDir("ProfD", ["c", "d", "foodir"], true); |
michael@0 | 80 | do_check_true(dir instanceof Components.interfaces.nsIFile); |
michael@0 | 81 | do_check_true(dir.exists()); |
michael@0 | 82 | |
michael@0 | 83 | let other = gProfD.clone(); |
michael@0 | 84 | other.append("c"); |
michael@0 | 85 | do_check_true(other.isDirectory()); |
michael@0 | 86 | other.append("d"); |
michael@0 | 87 | do_check_true(other.isDirectory()); |
michael@0 | 88 | other.append("foodir"); |
michael@0 | 89 | do_check_true(dir.equals(other)); |
michael@0 | 90 | |
michael@0 | 91 | run_next_test(); |
michael@0 | 92 | }); |
michael@0 | 93 | |
michael@0 | 94 | let openFileOutputStream_defaultFlags = function (aKind, aFileName) { |
michael@0 | 95 | let file = FileUtils.getFile("ProfD", [aFileName]); |
michael@0 | 96 | let fos; |
michael@0 | 97 | do_check_true(aKind == "atomic" || aKind == "safe" || aKind == ""); |
michael@0 | 98 | if (aKind == "atomic") { |
michael@0 | 99 | fos = FileUtils.openAtomicFileOutputStream(file); |
michael@0 | 100 | } else if (aKind == "safe") { |
michael@0 | 101 | fos = FileUtils.openSafeFileOutputStream(file); |
michael@0 | 102 | } else { |
michael@0 | 103 | fos = FileUtils.openFileOutputStream(file); |
michael@0 | 104 | } |
michael@0 | 105 | do_check_true(fos instanceof Components.interfaces.nsIFileOutputStream); |
michael@0 | 106 | if (aKind == "atomic" || aKind == "safe") { |
michael@0 | 107 | do_check_true(fos instanceof Components.interfaces.nsISafeOutputStream); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | // FileUtils.openFileOutputStream or FileUtils.openAtomicFileOutputStream() |
michael@0 | 111 | // or FileUtils.openSafeFileOutputStream() opens the stream with DEFER_OPEN |
michael@0 | 112 | // which means the file will not be open until we write to it. |
michael@0 | 113 | do_check_false(file.exists()); |
michael@0 | 114 | |
michael@0 | 115 | let data = "test_default_flags"; |
michael@0 | 116 | fos.write(data, data.length); |
michael@0 | 117 | do_check_true(file.exists()); |
michael@0 | 118 | |
michael@0 | 119 | // No nsIXULRuntime in xpcshell, so use this trick to determine whether we're |
michael@0 | 120 | // on Windows. |
michael@0 | 121 | if ("@mozilla.org/windows-registry-key;1" in Components.classes) { |
michael@0 | 122 | do_check_eq(file.permissions, 0666); |
michael@0 | 123 | } else { |
michael@0 | 124 | do_check_eq(file.permissions, FileUtils.PERMS_FILE); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | run_next_test(); |
michael@0 | 128 | }; |
michael@0 | 129 | |
michael@0 | 130 | let openFileOutputStream_modeFlags = function(aKind, aFileName) { |
michael@0 | 131 | let file = FileUtils.getFile("ProfD", [aFileName]); |
michael@0 | 132 | let fos; |
michael@0 | 133 | do_check_true(aKind == "atomic" || aKind == "safe" || aKind == ""); |
michael@0 | 134 | if (aKind == "atomic") { |
michael@0 | 135 | fos = FileUtils.openAtomicFileOutputStream(file, FileUtils.MODE_WRONLY); |
michael@0 | 136 | } else if (aKind == "safe") { |
michael@0 | 137 | fos = FileUtils.openSafeFileOutputStream(file, FileUtils.MODE_WRONLY); |
michael@0 | 138 | } else { |
michael@0 | 139 | fos = FileUtils.openFileOutputStream(file, FileUtils.MODE_WRONLY); |
michael@0 | 140 | } |
michael@0 | 141 | let data = "test_modeFlags"; |
michael@0 | 142 | do_check_throws(function () { |
michael@0 | 143 | fos.write(data, data.length); |
michael@0 | 144 | }, Components.results.NS_ERROR_FILE_NOT_FOUND); |
michael@0 | 145 | do_check_false(file.exists()); |
michael@0 | 146 | |
michael@0 | 147 | run_next_test(); |
michael@0 | 148 | }; |
michael@0 | 149 | |
michael@0 | 150 | let closeFileOutputStream = function(aKind, aFileName) { |
michael@0 | 151 | let file = FileUtils.getFile("ProfD", [aFileName]); |
michael@0 | 152 | let fos; |
michael@0 | 153 | do_check_true(aKind == "atomic" || aKind == "safe"); |
michael@0 | 154 | if (aKind == "atomic") { |
michael@0 | 155 | fos = FileUtils.openAtomicFileOutputStream(file); |
michael@0 | 156 | } else if (aKind == "safe") { |
michael@0 | 157 | fos = FileUtils.openSafeFileOutputStream(file); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | // We can write data to the stream just fine while it's open. |
michael@0 | 161 | let data = "testClose"; |
michael@0 | 162 | fos.write(data, data.length); |
michael@0 | 163 | |
michael@0 | 164 | // But once we close it, we can't anymore. |
michael@0 | 165 | if (aKind == "atomic") { |
michael@0 | 166 | FileUtils.closeAtomicFileOutputStream(fos); |
michael@0 | 167 | } else if (aKind == "safe"){ |
michael@0 | 168 | FileUtils.closeSafeFileOutputStream(fos); |
michael@0 | 169 | } |
michael@0 | 170 | do_check_throws(function () { |
michael@0 | 171 | fos.write(data, data.length); |
michael@0 | 172 | }, Components.results.NS_BASE_STREAM_CLOSED); |
michael@0 | 173 | run_next_test(); |
michael@0 | 174 | }; |
michael@0 | 175 | |
michael@0 | 176 | add_test(function test_openFileOutputStream_defaultFlags() { |
michael@0 | 177 | openFileOutputStream_defaultFlags("", "george"); |
michael@0 | 178 | }); |
michael@0 | 179 | |
michael@0 | 180 | // openFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE |
michael@0 | 181 | // as the default mode flags, but we can pass in our own if we want to. |
michael@0 | 182 | add_test(function test_openFileOutputStream_modeFlags() { |
michael@0 | 183 | openFileOutputStream_modeFlags("", "ringo"); |
michael@0 | 184 | }); |
michael@0 | 185 | |
michael@0 | 186 | add_test(function test_openAtomicFileOutputStream_defaultFlags() { |
michael@0 | 187 | openFileOutputStream_defaultFlags("atomic", "peiyong"); |
michael@0 | 188 | }); |
michael@0 | 189 | |
michael@0 | 190 | // openAtomicFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE |
michael@0 | 191 | // as the default mode flags, but we can pass in our own if we want to. |
michael@0 | 192 | add_test(function test_openAtomicFileOutputStream_modeFlags() { |
michael@0 | 193 | openFileOutputStream_modeFlags("atomic", "lin"); |
michael@0 | 194 | }); |
michael@0 | 195 | |
michael@0 | 196 | add_test(function test_closeAtomicFileOutputStream() { |
michael@0 | 197 | closeFileOutputStream("atomic", "peiyonglin"); |
michael@0 | 198 | }); |
michael@0 | 199 | |
michael@0 | 200 | add_test(function test_openSafeFileOutputStream_defaultFlags() { |
michael@0 | 201 | openFileOutputStream_defaultFlags("safe", "john"); |
michael@0 | 202 | }); |
michael@0 | 203 | |
michael@0 | 204 | // openSafeFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE |
michael@0 | 205 | // as the default mode flags, but we can pass in our own if we want to. |
michael@0 | 206 | add_test(function test_openSafeFileOutputStream_modeFlags() { |
michael@0 | 207 | openFileOutputStream_modeFlags("safe", "paul"); |
michael@0 | 208 | }); |
michael@0 | 209 | |
michael@0 | 210 | add_test(function test_closeSafeFileOutputStream() { |
michael@0 | 211 | closeFileOutputStream("safe", "georgee"); |
michael@0 | 212 | }); |
michael@0 | 213 | |
michael@0 | 214 | add_test(function test_newFile() { |
michael@0 | 215 | let testfile = FileUtils.getFile("ProfD", ["test"]); |
michael@0 | 216 | let testpath = testfile.path; |
michael@0 | 217 | let file = new FileUtils.File(testpath); |
michael@0 | 218 | do_check_true(file instanceof Components.interfaces.nsILocalFile); |
michael@0 | 219 | do_check_true(file.equals(testfile)); |
michael@0 | 220 | do_check_eq(file.path, testpath); |
michael@0 | 221 | run_next_test(); |
michael@0 | 222 | }); |
michael@0 | 223 | |
michael@0 | 224 | function run_test() { |
michael@0 | 225 | run_next_test(); |
michael@0 | 226 | } |