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 | "use strict"; |
michael@0 | 2 | |
michael@0 | 3 | Components.utils.import("resource://gre/modules/osfile.jsm"); |
michael@0 | 4 | Components.utils.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 5 | Components.utils.import("resource://gre/modules/Task.jsm"); |
michael@0 | 6 | Components.utils.import("resource://gre/modules/AsyncShutdown.jsm"); |
michael@0 | 7 | |
michael@0 | 8 | // The following are used to compare against a well-tested reference |
michael@0 | 9 | // implementation of file I/O. |
michael@0 | 10 | Components.utils.import("resource://gre/modules/NetUtil.jsm"); |
michael@0 | 11 | Components.utils.import("resource://gre/modules/FileUtils.jsm"); |
michael@0 | 12 | Components.utils.import("resource://gre/modules/Services.jsm"); |
michael@0 | 13 | |
michael@0 | 14 | let myok = ok; |
michael@0 | 15 | let myis = is; |
michael@0 | 16 | let myinfo = info; |
michael@0 | 17 | let myisnot = isnot; |
michael@0 | 18 | |
michael@0 | 19 | let isPromise = function ispromise(value) { |
michael@0 | 20 | return value != null && typeof value == "object" && "then" in value; |
michael@0 | 21 | }; |
michael@0 | 22 | |
michael@0 | 23 | let maketest = function(prefix, test) { |
michael@0 | 24 | let utils = { |
michael@0 | 25 | ok: function ok(t, m) { |
michael@0 | 26 | myok(t, prefix + ": " + m); |
michael@0 | 27 | }, |
michael@0 | 28 | is: function is(l, r, m) { |
michael@0 | 29 | myis(l, r, prefix + ": " + m); |
michael@0 | 30 | }, |
michael@0 | 31 | isnot: function isnot(l, r, m) { |
michael@0 | 32 | myisnot(l, r, prefix + ": " + m); |
michael@0 | 33 | }, |
michael@0 | 34 | info: function info(m) { |
michael@0 | 35 | myinfo(prefix + ": " + m); |
michael@0 | 36 | }, |
michael@0 | 37 | fail: function fail(m) { |
michael@0 | 38 | utils.ok(false, m); |
michael@0 | 39 | }, |
michael@0 | 40 | okpromise: function okpromise(t, m) { |
michael@0 | 41 | return t.then( |
michael@0 | 42 | function onSuccess() { |
michael@0 | 43 | util.ok(true, m); |
michael@0 | 44 | }, |
michael@0 | 45 | function onFailure() { |
michael@0 | 46 | util.ok(false, m); |
michael@0 | 47 | } |
michael@0 | 48 | ); |
michael@0 | 49 | } |
michael@0 | 50 | }; |
michael@0 | 51 | return function runtest() { |
michael@0 | 52 | utils.info("Entering"); |
michael@0 | 53 | try { |
michael@0 | 54 | let result = test.call(this, utils); |
michael@0 | 55 | if (!isPromise(result)) { |
michael@0 | 56 | throw new TypeError("The test did not return a promise"); |
michael@0 | 57 | } |
michael@0 | 58 | utils.info("This was a promise"); |
michael@0 | 59 | // The test returns a promise |
michael@0 | 60 | result = result.then(function test_complete() { |
michael@0 | 61 | utils.info("Complete"); |
michael@0 | 62 | }, function catch_uncaught_errors(err) { |
michael@0 | 63 | utils.fail("Uncaught error " + err); |
michael@0 | 64 | if (err && typeof err == "object" && "message" in err) { |
michael@0 | 65 | utils.fail("(" + err.message + ")"); |
michael@0 | 66 | } |
michael@0 | 67 | if (err && typeof err == "object" && "stack" in err) { |
michael@0 | 68 | utils.fail("at " + err.stack); |
michael@0 | 69 | } |
michael@0 | 70 | }); |
michael@0 | 71 | return result; |
michael@0 | 72 | } catch (x) { |
michael@0 | 73 | utils.fail("Error " + x + " at " + x.stack); |
michael@0 | 74 | return null; |
michael@0 | 75 | } |
michael@0 | 76 | }; |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | /** |
michael@0 | 80 | * Fetch asynchronously the contents of a file using xpcom. |
michael@0 | 81 | * |
michael@0 | 82 | * Used for comparing xpcom-based results to os.file-based results. |
michael@0 | 83 | * |
michael@0 | 84 | * @param {string} path The _absolute_ path to the file. |
michael@0 | 85 | * @return {promise} |
michael@0 | 86 | * @resolves {string} The contents of the file. |
michael@0 | 87 | */ |
michael@0 | 88 | let reference_fetch_file = function reference_fetch_file(path, test) { |
michael@0 | 89 | test.info("Fetching file " + path); |
michael@0 | 90 | let promise = Promise.defer(); |
michael@0 | 91 | let file = new FileUtils.File(path); |
michael@0 | 92 | NetUtil.asyncFetch(file, |
michael@0 | 93 | function(stream, status) { |
michael@0 | 94 | if (!Components.isSuccessCode(status)) { |
michael@0 | 95 | promise.reject(status); |
michael@0 | 96 | return; |
michael@0 | 97 | } |
michael@0 | 98 | let result, reject; |
michael@0 | 99 | try { |
michael@0 | 100 | result = NetUtil.readInputStreamToString(stream, stream.available()); |
michael@0 | 101 | } catch (x) { |
michael@0 | 102 | reject = x; |
michael@0 | 103 | } |
michael@0 | 104 | stream.close(); |
michael@0 | 105 | if (reject) { |
michael@0 | 106 | promise.reject(reject); |
michael@0 | 107 | } else { |
michael@0 | 108 | promise.resolve(result); |
michael@0 | 109 | } |
michael@0 | 110 | }); |
michael@0 | 111 | return promise.promise; |
michael@0 | 112 | }; |
michael@0 | 113 | |
michael@0 | 114 | /** |
michael@0 | 115 | * Compare asynchronously the contents two files using xpcom. |
michael@0 | 116 | * |
michael@0 | 117 | * Used for comparing xpcom-based results to os.file-based results. |
michael@0 | 118 | * |
michael@0 | 119 | * @param {string} a The _absolute_ path to the first file. |
michael@0 | 120 | * @param {string} b The _absolute_ path to the second file. |
michael@0 | 121 | * |
michael@0 | 122 | * @resolves {null} |
michael@0 | 123 | */ |
michael@0 | 124 | let reference_compare_files = function reference_compare_files(a, b, test) { |
michael@0 | 125 | test.info("Comparing files " + a + " and " + b); |
michael@0 | 126 | let a_contents = yield reference_fetch_file(a, test); |
michael@0 | 127 | let b_contents = yield reference_fetch_file(b, test); |
michael@0 | 128 | is(a_contents, b_contents, "Contents of files " + a + " and " + b + " match"); |
michael@0 | 129 | }; |
michael@0 | 130 | |
michael@0 | 131 | let reference_dir_contents = function reference_dir_contents(path) { |
michael@0 | 132 | let result = []; |
michael@0 | 133 | let entries = new FileUtils.File(path).directoryEntries; |
michael@0 | 134 | while (entries.hasMoreElements()) { |
michael@0 | 135 | let entry = entries.getNext().QueryInterface(Components.interfaces.nsILocalFile); |
michael@0 | 136 | result.push(entry.path); |
michael@0 | 137 | } |
michael@0 | 138 | return result; |
michael@0 | 139 | }; |
michael@0 | 140 | |
michael@0 | 141 | // Set/Unset OS.Shared.DEBUG, OS.Shared.TEST and a console listener. |
michael@0 | 142 | function toggleDebugTest (pref, consoleListener) { |
michael@0 | 143 | Services.prefs.setBoolPref("toolkit.osfile.log", pref); |
michael@0 | 144 | Services.prefs.setBoolPref("toolkit.osfile.log.redirect", pref); |
michael@0 | 145 | Services.console[pref ? "registerListener" : "unregisterListener"]( |
michael@0 | 146 | consoleListener); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | let test = maketest("Main", function main(test) { |
michael@0 | 150 | return Task.spawn(function() { |
michael@0 | 151 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 152 | yield test_constants(); |
michael@0 | 153 | yield test_path(); |
michael@0 | 154 | yield test_stat(); |
michael@0 | 155 | yield test_debug(); |
michael@0 | 156 | yield test_info_features_detect(); |
michael@0 | 157 | yield test_read_write(); |
michael@0 | 158 | yield test_position(); |
michael@0 | 159 | yield test_iter(); |
michael@0 | 160 | yield test_exists(); |
michael@0 | 161 | yield test_debug_test(); |
michael@0 | 162 | info("Test is over"); |
michael@0 | 163 | SimpleTest.finish(); |
michael@0 | 164 | }); |
michael@0 | 165 | }); |
michael@0 | 166 | |
michael@0 | 167 | /** |
michael@0 | 168 | * A file that we know exists and that can be used for reading. |
michael@0 | 169 | */ |
michael@0 | 170 | let EXISTING_FILE = OS.Path.join("chrome", "toolkit", "components", |
michael@0 | 171 | "osfile", "tests", "mochi", "main_test_osfile_async.js"); |
michael@0 | 172 | |
michael@0 | 173 | /** |
michael@0 | 174 | * Test that OS.Constants is defined correctly. |
michael@0 | 175 | */ |
michael@0 | 176 | let test_constants = maketest("constants", function constants(test) { |
michael@0 | 177 | return Task.spawn(function() { |
michael@0 | 178 | test.isnot(OS.Constants, null, "OS.Constants exists"); |
michael@0 | 179 | test.ok(OS.Constants.Win || OS.Constants.libc, "OS.Constants.Win exists or OS.Constants.Unix exists"); |
michael@0 | 180 | test.isnot(OS.Constants.Path, null, "OS.Constants.Path exists"); |
michael@0 | 181 | test.isnot(OS.Constants.Sys, null, "OS.Constants.Sys exists"); |
michael@0 | 182 | }); |
michael@0 | 183 | }); |
michael@0 | 184 | |
michael@0 | 185 | /** |
michael@0 | 186 | * Test that OS.Constants.Path paths are consistent. |
michael@0 | 187 | */ |
michael@0 | 188 | let test_path = maketest("path", function path(test) { |
michael@0 | 189 | return Task.spawn(function() { |
michael@0 | 190 | test.ok(OS.Path, "OS.Path exists"); |
michael@0 | 191 | test.ok(OS.Constants.Path, "OS.Constants.Path exists"); |
michael@0 | 192 | test.is(OS.Constants.Path.tmpDir, Services.dirsvc.get("TmpD", Components.interfaces.nsIFile).path, "OS.Constants.Path.tmpDir is correct"); |
michael@0 | 193 | test.is(OS.Constants.Path.profileDir, Services.dirsvc.get("ProfD", Components.interfaces.nsIFile).path, "OS.Constants.Path.profileDir is correct"); |
michael@0 | 194 | test.is(OS.Constants.Path.localProfileDir, Services.dirsvc.get("ProfLD", Components.interfaces.nsIFile).path, "OS.Constants.Path.localProfileDir is correct"); |
michael@0 | 195 | }); |
michael@0 | 196 | }); |
michael@0 | 197 | |
michael@0 | 198 | /** |
michael@0 | 199 | * Test OS.File.stat and OS.File.prototype.stat |
michael@0 | 200 | */ |
michael@0 | 201 | let test_stat = maketest("stat", function stat(test) { |
michael@0 | 202 | return Task.spawn(function() { |
michael@0 | 203 | // Open a file and stat it |
michael@0 | 204 | let file = yield OS.File.open(EXISTING_FILE); |
michael@0 | 205 | let stat1; |
michael@0 | 206 | |
michael@0 | 207 | try { |
michael@0 | 208 | test.info("Stating file"); |
michael@0 | 209 | stat1 = yield file.stat(); |
michael@0 | 210 | test.ok(true, "stat has worked " + stat1); |
michael@0 | 211 | test.ok(stat1, "stat is not empty"); |
michael@0 | 212 | } finally { |
michael@0 | 213 | yield file.close(); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | // Stat the same file without opening it |
michael@0 | 217 | test.info("Stating a file without opening it"); |
michael@0 | 218 | let stat2 = yield OS.File.stat(EXISTING_FILE); |
michael@0 | 219 | test.ok(true, "stat 2 has worked " + stat2); |
michael@0 | 220 | test.ok(stat2, "stat 2 is not empty"); |
michael@0 | 221 | for (let key in stat2) { |
michael@0 | 222 | test.is("" + stat1[key], "" + stat2[key], "Stat field " + key + "is the same"); |
michael@0 | 223 | } |
michael@0 | 224 | }); |
michael@0 | 225 | }); |
michael@0 | 226 | |
michael@0 | 227 | /** |
michael@0 | 228 | * Test feature detection using OS.File.Info.prototype on main thread |
michael@0 | 229 | */ |
michael@0 | 230 | let test_info_features_detect = maketest("features_detect", function features_detect(test) { |
michael@0 | 231 | return Task.spawn(function() { |
michael@0 | 232 | if (OS.Constants.Win) { |
michael@0 | 233 | // see if winBirthDate is defined |
michael@0 | 234 | if ("winBirthDate" in OS.File.Info.prototype) { |
michael@0 | 235 | test.ok(true, "winBirthDate is defined"); |
michael@0 | 236 | } else { |
michael@0 | 237 | test.fail("winBirthDate not defined though we are under Windows"); |
michael@0 | 238 | } |
michael@0 | 239 | } else if (OS.Constants.libc) { |
michael@0 | 240 | // see if unixGroup is defined |
michael@0 | 241 | if ("unixGroup" in OS.File.Info.prototype) { |
michael@0 | 242 | test.ok(true, "unixGroup is defined"); |
michael@0 | 243 | } else { |
michael@0 | 244 | test.fail("unixGroup is not defined though we are under Unix"); |
michael@0 | 245 | } |
michael@0 | 246 | } |
michael@0 | 247 | }); |
michael@0 | 248 | }); |
michael@0 | 249 | |
michael@0 | 250 | /** |
michael@0 | 251 | * Test OS.File.prototype.{read, readTo, write} |
michael@0 | 252 | */ |
michael@0 | 253 | let test_read_write = maketest("read_write", function read_write(test) { |
michael@0 | 254 | return Task.spawn(function() { |
michael@0 | 255 | // Test readTo/write |
michael@0 | 256 | let currentDir = yield OS.File.getCurrentDirectory(); |
michael@0 | 257 | let pathSource = OS.Path.join(currentDir, EXISTING_FILE); |
michael@0 | 258 | let pathDest = OS.Path.join(OS.Constants.Path.tmpDir, |
michael@0 | 259 | "osfile async test.tmp"); |
michael@0 | 260 | |
michael@0 | 261 | let fileSource = yield OS.File.open(pathSource); |
michael@0 | 262 | test.info("Input file opened"); |
michael@0 | 263 | let fileDest = yield OS.File.open(pathDest, |
michael@0 | 264 | { truncate: true, read: true, write: true}); |
michael@0 | 265 | test.info("Output file opened"); |
michael@0 | 266 | |
michael@0 | 267 | let stat = yield fileSource.stat(); |
michael@0 | 268 | test.info("Input stat worked"); |
michael@0 | 269 | let size = stat.size; |
michael@0 | 270 | let array = new Uint8Array(size); |
michael@0 | 271 | |
michael@0 | 272 | try { |
michael@0 | 273 | test.info("Now calling readTo"); |
michael@0 | 274 | let readLength = yield fileSource.readTo(array); |
michael@0 | 275 | test.info("ReadTo worked"); |
michael@0 | 276 | test.is(readLength, size, "ReadTo got all bytes"); |
michael@0 | 277 | let writeLength = yield fileDest.write(array); |
michael@0 | 278 | test.info("Write worked"); |
michael@0 | 279 | test.is(writeLength, size, "Write wrote all bytes"); |
michael@0 | 280 | |
michael@0 | 281 | // Test read |
michael@0 | 282 | yield fileSource.setPosition(0); |
michael@0 | 283 | let readAllResult = yield fileSource.read(); |
michael@0 | 284 | test.info("ReadAll worked"); |
michael@0 | 285 | test.is(readAllResult.length, size, "ReadAll read all bytes"); |
michael@0 | 286 | test.is(Array.prototype.join.call(readAllResult), |
michael@0 | 287 | Array.prototype.join.call(array), |
michael@0 | 288 | "ReadAll result is correct"); |
michael@0 | 289 | } finally { |
michael@0 | 290 | // Close stuff |
michael@0 | 291 | yield fileSource.close(); |
michael@0 | 292 | yield fileDest.close(); |
michael@0 | 293 | test.info("Files are closed"); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | stat = yield OS.File.stat(pathDest); |
michael@0 | 297 | test.is(stat.size, size, "Both files have the same size"); |
michael@0 | 298 | yield reference_compare_files(pathSource, pathDest, test); |
michael@0 | 299 | |
michael@0 | 300 | // Cleanup. |
michael@0 | 301 | OS.File.remove(pathDest); |
michael@0 | 302 | }); |
michael@0 | 303 | }); |
michael@0 | 304 | |
michael@0 | 305 | |
michael@0 | 306 | /** |
michael@0 | 307 | * Test file.{getPosition, setPosition} |
michael@0 | 308 | */ |
michael@0 | 309 | let test_position = maketest("position", function position(test) { |
michael@0 | 310 | return Task.spawn(function() { |
michael@0 | 311 | let file = yield OS.File.open(EXISTING_FILE); |
michael@0 | 312 | |
michael@0 | 313 | try { |
michael@0 | 314 | let stat = yield file.stat(); |
michael@0 | 315 | test.info("Obtained file length"); |
michael@0 | 316 | |
michael@0 | 317 | let view = new Uint8Array(stat.size); |
michael@0 | 318 | yield file.readTo(view); |
michael@0 | 319 | test.info("First batch of content read"); |
michael@0 | 320 | |
michael@0 | 321 | let CHUNK_SIZE = 178;// An arbitrary number of bytes to read from the file |
michael@0 | 322 | let pos = yield file.getPosition(); |
michael@0 | 323 | test.info("Obtained position"); |
michael@0 | 324 | test.is(pos, view.byteLength, "getPosition returned the end of the file"); |
michael@0 | 325 | pos = yield file.setPosition(-CHUNK_SIZE, OS.File.POS_END); |
michael@0 | 326 | test.info("Changed position"); |
michael@0 | 327 | test.is(pos, view.byteLength - CHUNK_SIZE, "setPosition returned the correct position"); |
michael@0 | 328 | |
michael@0 | 329 | let view2 = new Uint8Array(CHUNK_SIZE); |
michael@0 | 330 | yield file.readTo(view2); |
michael@0 | 331 | test.info("Read the end of the file"); |
michael@0 | 332 | for (let i = 0; i < CHUNK_SIZE; ++i) { |
michael@0 | 333 | if (view2[i] != view[i + view.byteLength - CHUNK_SIZE]) { |
michael@0 | 334 | test.is(view2[i], view[i], "setPosition put us in the right position"); |
michael@0 | 335 | } |
michael@0 | 336 | } |
michael@0 | 337 | } finally { |
michael@0 | 338 | yield file.close(); |
michael@0 | 339 | } |
michael@0 | 340 | }); |
michael@0 | 341 | }); |
michael@0 | 342 | |
michael@0 | 343 | /** |
michael@0 | 344 | * Test OS.File.prototype.{DirectoryIterator} |
michael@0 | 345 | */ |
michael@0 | 346 | let test_iter = maketest("iter", function iter(test) { |
michael@0 | 347 | return Task.spawn(function() { |
michael@0 | 348 | let currentDir = yield OS.File.getCurrentDirectory(); |
michael@0 | 349 | |
michael@0 | 350 | // Trivial walks through the directory |
michael@0 | 351 | test.info("Preparing iteration"); |
michael@0 | 352 | let iterator = new OS.File.DirectoryIterator(currentDir); |
michael@0 | 353 | let temporary_file_name = OS.Path.join(currentDir, "empty-temporary-file.tmp"); |
michael@0 | 354 | try { |
michael@0 | 355 | yield OS.File.remove(temporary_file_name); |
michael@0 | 356 | } catch (err) { |
michael@0 | 357 | // Ignore errors removing file |
michael@0 | 358 | } |
michael@0 | 359 | let allFiles1 = yield iterator.nextBatch(); |
michael@0 | 360 | test.info("Obtained all files through nextBatch"); |
michael@0 | 361 | test.isnot(allFiles1.length, 0, "There is at least one file"); |
michael@0 | 362 | test.isnot(allFiles1[0].path, null, "Files have a path"); |
michael@0 | 363 | |
michael@0 | 364 | // Ensure that we have the same entries with |reference_dir_contents| |
michael@0 | 365 | let referenceEntries = new Set(); |
michael@0 | 366 | for (let entry of reference_dir_contents(currentDir)) { |
michael@0 | 367 | referenceEntries.add(entry); |
michael@0 | 368 | } |
michael@0 | 369 | test.is(referenceEntries.size, allFiles1.length, "All the entries in the directory have been listed"); |
michael@0 | 370 | for (let entry of allFiles1) { |
michael@0 | 371 | test.ok(referenceEntries.has(entry.path), "File " + entry.path + " effectively exists"); |
michael@0 | 372 | // Ensure that we have correct isDir and isSymLink |
michael@0 | 373 | // Current directory is {objdir}/_tests/testing/mochitest/, assume it has some dirs and symlinks. |
michael@0 | 374 | var f = new FileUtils.File(entry.path); |
michael@0 | 375 | test.is(entry.isDir, f.isDirectory(), "Get file " + entry.path + " isDir correctly"); |
michael@0 | 376 | test.is(entry.isSymLink, f.isSymlink(), "Get file " + entry.path + " isSymLink correctly"); |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | yield iterator.close(); |
michael@0 | 380 | test.info("Closed iterator"); |
michael@0 | 381 | |
michael@0 | 382 | test.info("Double closing DirectoryIterator"); |
michael@0 | 383 | iterator = new OS.File.DirectoryIterator(currentDir); |
michael@0 | 384 | yield iterator.close(); |
michael@0 | 385 | yield iterator.close(); //double closing |DirectoryIterator| |
michael@0 | 386 | test.ok(true, "|DirectoryIterator| was closed twice successfully"); |
michael@0 | 387 | |
michael@0 | 388 | let allFiles2 = []; |
michael@0 | 389 | let i = 0; |
michael@0 | 390 | iterator = new OS.File.DirectoryIterator(currentDir); |
michael@0 | 391 | yield iterator.forEach(function(entry, index) { |
michael@0 | 392 | test.is(i++, index, "Getting the correct index"); |
michael@0 | 393 | allFiles2.push(entry); |
michael@0 | 394 | }); |
michael@0 | 395 | test.info("Obtained all files through forEach"); |
michael@0 | 396 | is(allFiles1.length, allFiles2.length, "Both runs returned the same number of files"); |
michael@0 | 397 | for (let i = 0; i < allFiles1.length; ++i) { |
michael@0 | 398 | if (allFiles1[i].path != allFiles2[i].path) { |
michael@0 | 399 | test.is(allFiles1[i].path, allFiles2[i].path, "Both runs return the same files"); |
michael@0 | 400 | break; |
michael@0 | 401 | } |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | // Testing batch iteration + whether an iteration can be stopped early |
michael@0 | 405 | let BATCH_LENGTH = 10; |
michael@0 | 406 | test.info("Getting some files through nextBatch"); |
michael@0 | 407 | yield iterator.close(); |
michael@0 | 408 | |
michael@0 | 409 | iterator = new OS.File.DirectoryIterator(currentDir); |
michael@0 | 410 | let someFiles1 = yield iterator.nextBatch(BATCH_LENGTH); |
michael@0 | 411 | let someFiles2 = yield iterator.nextBatch(BATCH_LENGTH); |
michael@0 | 412 | yield iterator.close(); |
michael@0 | 413 | |
michael@0 | 414 | iterator = new OS.File.DirectoryIterator(currentDir); |
michael@0 | 415 | yield iterator.forEach(function cb(entry, index, iterator) { |
michael@0 | 416 | if (index < BATCH_LENGTH) { |
michael@0 | 417 | test.is(entry.path, someFiles1[index].path, "Both runs return the same files (part 1)"); |
michael@0 | 418 | } else if (index < 2*BATCH_LENGTH) { |
michael@0 | 419 | test.is(entry.path, someFiles2[index - BATCH_LENGTH].path, "Both runs return the same files (part 2)"); |
michael@0 | 420 | } else if (index == 2 * BATCH_LENGTH) { |
michael@0 | 421 | test.info("Attempting to stop asynchronous forEach"); |
michael@0 | 422 | return iterator.close(); |
michael@0 | 423 | } else { |
michael@0 | 424 | test.fail("Can we stop an asynchronous forEach? " + index); |
michael@0 | 425 | } |
michael@0 | 426 | return null; |
michael@0 | 427 | }); |
michael@0 | 428 | yield iterator.close(); |
michael@0 | 429 | |
michael@0 | 430 | // Ensuring that we find new files if they appear |
michael@0 | 431 | let file = yield OS.File.open(temporary_file_name, { write: true } ); |
michael@0 | 432 | file.close(); |
michael@0 | 433 | iterator = new OS.File.DirectoryIterator(currentDir); |
michael@0 | 434 | try { |
michael@0 | 435 | let files = yield iterator.nextBatch(); |
michael@0 | 436 | is(files.length, allFiles1.length + 1, "The directory iterator has noticed the new file"); |
michael@0 | 437 | let exists = yield iterator.exists(); |
michael@0 | 438 | test.ok(exists, "After nextBatch, iterator detects that the directory exists"); |
michael@0 | 439 | } finally { |
michael@0 | 440 | yield iterator.close(); |
michael@0 | 441 | } |
michael@0 | 442 | |
michael@0 | 443 | // Ensuring that opening a non-existing directory fails consistently |
michael@0 | 444 | // once iteration starts. |
michael@0 | 445 | try { |
michael@0 | 446 | iterator = null; |
michael@0 | 447 | iterator = new OS.File.DirectoryIterator("/I do not exist"); |
michael@0 | 448 | let exists = yield iterator.exists(); |
michael@0 | 449 | test.ok(!exists, "Before any iteration, iterator detects that the directory doesn't exist"); |
michael@0 | 450 | let exn = null; |
michael@0 | 451 | try { |
michael@0 | 452 | yield iterator.next(); |
michael@0 | 453 | } catch (ex if ex instanceof OS.File.Error && ex.becauseNoSuchFile) { |
michael@0 | 454 | exn = ex; |
michael@0 | 455 | let exists = yield iterator.exists(); |
michael@0 | 456 | test.ok(!exists, "After one iteration, iterator detects that the directory doesn't exist"); |
michael@0 | 457 | } |
michael@0 | 458 | test.ok(exn, "Iterating through a directory that does not exist has failed with becauseNoSuchFile"); |
michael@0 | 459 | } finally { |
michael@0 | 460 | if (iterator) { |
michael@0 | 461 | iterator.close(); |
michael@0 | 462 | } |
michael@0 | 463 | } |
michael@0 | 464 | test.ok(!!iterator, "The directory iterator for a non-existing directory was correctly created"); |
michael@0 | 465 | }); |
michael@0 | 466 | }); |
michael@0 | 467 | |
michael@0 | 468 | /** |
michael@0 | 469 | * Test OS.File.prototype.{exists} |
michael@0 | 470 | */ |
michael@0 | 471 | let test_exists = maketest("exists", function exists(test) { |
michael@0 | 472 | return Task.spawn(function() { |
michael@0 | 473 | let fileExists = yield OS.File.exists(EXISTING_FILE); |
michael@0 | 474 | test.ok(fileExists, "file exists"); |
michael@0 | 475 | fileExists = yield OS.File.exists(EXISTING_FILE + ".tmp"); |
michael@0 | 476 | test.ok(!fileExists, "file does not exists"); |
michael@0 | 477 | }); |
michael@0 | 478 | }); |
michael@0 | 479 | |
michael@0 | 480 | /** |
michael@0 | 481 | * Test changes to OS.Shared.DEBUG flag. |
michael@0 | 482 | */ |
michael@0 | 483 | let test_debug = maketest("debug", function debug(test) { |
michael@0 | 484 | return Task.spawn(function() { |
michael@0 | 485 | function testSetDebugPref (pref) { |
michael@0 | 486 | try { |
michael@0 | 487 | Services.prefs.setBoolPref("toolkit.osfile.log", pref); |
michael@0 | 488 | } catch (x) { |
michael@0 | 489 | test.fail("Setting OS.Shared.DEBUG to " + pref + |
michael@0 | 490 | " should not cause error."); |
michael@0 | 491 | } finally { |
michael@0 | 492 | test.is(OS.Shared.DEBUG, pref, "OS.Shared.DEBUG is set correctly."); |
michael@0 | 493 | } |
michael@0 | 494 | } |
michael@0 | 495 | testSetDebugPref(true); |
michael@0 | 496 | let workerDEBUG = yield OS.File.GET_DEBUG(); |
michael@0 | 497 | test.is(workerDEBUG, true, "Worker's DEBUG is set."); |
michael@0 | 498 | testSetDebugPref(false); |
michael@0 | 499 | workerDEBUG = yield OS.File.GET_DEBUG(); |
michael@0 | 500 | test.is(workerDEBUG, false, "Worker's DEBUG is unset."); |
michael@0 | 501 | }); |
michael@0 | 502 | }); |
michael@0 | 503 | |
michael@0 | 504 | /** |
michael@0 | 505 | * Test logging in the main thread with set OS.Shared.DEBUG and |
michael@0 | 506 | * OS.Shared.TEST flags. |
michael@0 | 507 | */ |
michael@0 | 508 | let test_debug_test = maketest("debug_test", function debug_test(test) { |
michael@0 | 509 | return Task.spawn(function () { |
michael@0 | 510 | // Create a console listener. |
michael@0 | 511 | let consoleListener = { |
michael@0 | 512 | observe: function (aMessage) { |
michael@0 | 513 | // Ignore unexpected messages. |
michael@0 | 514 | if (!(aMessage instanceof Components.interfaces.nsIConsoleMessage)) { |
michael@0 | 515 | return; |
michael@0 | 516 | } |
michael@0 | 517 | if (aMessage.message.indexOf("TEST OS") < 0) { |
michael@0 | 518 | return; |
michael@0 | 519 | } |
michael@0 | 520 | test.ok(true, "DEBUG TEST messages are logged correctly."); |
michael@0 | 521 | } |
michael@0 | 522 | }; |
michael@0 | 523 | toggleDebugTest(true, consoleListener); |
michael@0 | 524 | // Execution of OS.File.exist method will trigger OS.File.LOG several times. |
michael@0 | 525 | let fileExists = yield OS.File.exists(EXISTING_FILE); |
michael@0 | 526 | toggleDebugTest(false, consoleListener); |
michael@0 | 527 | }); |
michael@0 | 528 | }); |
michael@0 | 529 | |
michael@0 | 530 |