toolkit/components/osfile/tests/xpcshell/head.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 "use strict";
michael@0 5
michael@0 6 let {utils: Cu, interfaces: Ci} = Components;
michael@0 7
michael@0 8 let {OS} = Cu.import("resource://gre/modules/osfile.jsm", {});
michael@0 9 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
michael@0 10 let {Promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
michael@0 11 let {Task} = Cu.import("resource://gre/modules/Task.jsm", {});
michael@0 12 let {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", {});
michael@0 13 let {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", {});
michael@0 14
michael@0 15 Services.prefs.setBoolPref("toolkit.osfile.log", true);
michael@0 16
michael@0 17 /**
michael@0 18 * As add_task, but execute the test both with native operations and
michael@0 19 * without.
michael@0 20 */
michael@0 21 function add_test_pair(generator) {
michael@0 22 add_task(function*() {
michael@0 23 do_print("Executing test " + generator.name + " with native operations");
michael@0 24 Services.prefs.setBoolPref("toolkit.osfile.native", true);
michael@0 25 return Task.spawn(generator);
michael@0 26 });
michael@0 27 add_task(function*() {
michael@0 28 do_print("Executing test " + generator.name + " without native operations");
michael@0 29 Services.prefs.setBoolPref("toolkit.osfile.native", false);
michael@0 30 return Task.spawn(generator);
michael@0 31 });
michael@0 32 }
michael@0 33
michael@0 34 /**
michael@0 35 * Fetch asynchronously the contents of a file using xpcom.
michael@0 36 *
michael@0 37 * Used for comparing xpcom-based results to os.file-based results.
michael@0 38 *
michael@0 39 * @param {string} path The _absolute_ path to the file.
michael@0 40 * @return {promise}
michael@0 41 * @resolves {string} The contents of the file.
michael@0 42 */
michael@0 43 function reference_fetch_file(path, test) {
michael@0 44 do_print("Fetching file " + path);
michael@0 45 let deferred = Promise.defer();
michael@0 46 let file = new FileUtils.File(path);
michael@0 47 NetUtil.asyncFetch(file,
michael@0 48 function(stream, status) {
michael@0 49 if (!Components.isSuccessCode(status)) {
michael@0 50 deferred.reject(status);
michael@0 51 return;
michael@0 52 }
michael@0 53 let result, reject;
michael@0 54 try {
michael@0 55 result = NetUtil.readInputStreamToString(stream, stream.available());
michael@0 56 } catch (x) {
michael@0 57 reject = x;
michael@0 58 }
michael@0 59 stream.close();
michael@0 60 if (reject) {
michael@0 61 deferred.reject(reject);
michael@0 62 } else {
michael@0 63 deferred.resolve(result);
michael@0 64 }
michael@0 65 });
michael@0 66 return deferred.promise;
michael@0 67 };
michael@0 68
michael@0 69 /**
michael@0 70 * Compare asynchronously the contents two files using xpcom.
michael@0 71 *
michael@0 72 * Used for comparing xpcom-based results to os.file-based results.
michael@0 73 *
michael@0 74 * @param {string} a The _absolute_ path to the first file.
michael@0 75 * @param {string} b The _absolute_ path to the second file.
michael@0 76 *
michael@0 77 * @resolves {null}
michael@0 78 */
michael@0 79 function reference_compare_files(a, b, test) {
michael@0 80 return Task.spawn(function*() {
michael@0 81 do_print("Comparing files " + a + " and " + b);
michael@0 82 let a_contents = yield reference_fetch_file(a, test);
michael@0 83 let b_contents = yield reference_fetch_file(b, test);
michael@0 84 do_check_eq(a_contents, b_contents);
michael@0 85 });
michael@0 86 };

mercurial