1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/osfile/tests/xpcshell/head.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,86 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +"use strict"; 1.8 + 1.9 +let {utils: Cu, interfaces: Ci} = Components; 1.10 + 1.11 +let {OS} = Cu.import("resource://gre/modules/osfile.jsm", {}); 1.12 +let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); 1.13 +let {Promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); 1.14 +let {Task} = Cu.import("resource://gre/modules/Task.jsm", {}); 1.15 +let {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", {}); 1.16 +let {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", {}); 1.17 + 1.18 +Services.prefs.setBoolPref("toolkit.osfile.log", true); 1.19 + 1.20 +/** 1.21 + * As add_task, but execute the test both with native operations and 1.22 + * without. 1.23 + */ 1.24 +function add_test_pair(generator) { 1.25 + add_task(function*() { 1.26 + do_print("Executing test " + generator.name + " with native operations"); 1.27 + Services.prefs.setBoolPref("toolkit.osfile.native", true); 1.28 + return Task.spawn(generator); 1.29 + }); 1.30 + add_task(function*() { 1.31 + do_print("Executing test " + generator.name + " without native operations"); 1.32 + Services.prefs.setBoolPref("toolkit.osfile.native", false); 1.33 + return Task.spawn(generator); 1.34 + }); 1.35 +} 1.36 + 1.37 +/** 1.38 + * Fetch asynchronously the contents of a file using xpcom. 1.39 + * 1.40 + * Used for comparing xpcom-based results to os.file-based results. 1.41 + * 1.42 + * @param {string} path The _absolute_ path to the file. 1.43 + * @return {promise} 1.44 + * @resolves {string} The contents of the file. 1.45 + */ 1.46 +function reference_fetch_file(path, test) { 1.47 + do_print("Fetching file " + path); 1.48 + let deferred = Promise.defer(); 1.49 + let file = new FileUtils.File(path); 1.50 + NetUtil.asyncFetch(file, 1.51 + function(stream, status) { 1.52 + if (!Components.isSuccessCode(status)) { 1.53 + deferred.reject(status); 1.54 + return; 1.55 + } 1.56 + let result, reject; 1.57 + try { 1.58 + result = NetUtil.readInputStreamToString(stream, stream.available()); 1.59 + } catch (x) { 1.60 + reject = x; 1.61 + } 1.62 + stream.close(); 1.63 + if (reject) { 1.64 + deferred.reject(reject); 1.65 + } else { 1.66 + deferred.resolve(result); 1.67 + } 1.68 + }); 1.69 + return deferred.promise; 1.70 +}; 1.71 + 1.72 +/** 1.73 + * Compare asynchronously the contents two files using xpcom. 1.74 + * 1.75 + * Used for comparing xpcom-based results to os.file-based results. 1.76 + * 1.77 + * @param {string} a The _absolute_ path to the first file. 1.78 + * @param {string} b The _absolute_ path to the second file. 1.79 + * 1.80 + * @resolves {null} 1.81 + */ 1.82 +function reference_compare_files(a, b, test) { 1.83 + return Task.spawn(function*() { 1.84 + do_print("Comparing files " + a + " and " + b); 1.85 + let a_contents = yield reference_fetch_file(a, test); 1.86 + let b_contents = yield reference_fetch_file(b, test); 1.87 + do_check_eq(a_contents, b_contents); 1.88 + }); 1.89 +};