1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Mozilla XUL Toolkit Testing Code.
16 * The Initial Developer of the Original Code is
17 * Paolo Amadini <http://www.amadzone.org/>.
18 * Portions created by the Initial Developer are Copyright (C) 2009
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 * Provides a temporary save directory.
41 * nsIFile pointing to the new or existing directory.
43 function createTemporarySaveDirectory() {
44 var saveDir = Cc["@mozilla.org/file/directory_service;1"].
45 getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
46 saveDir.append("testsavedir");
47 if (!saveDir.exists())
48 saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
53 * Calls the provided save function while replacing the file picker component
54 * with a mock implementation that returns a temporary file path and custom
55 * filters, and the download component with an implementation that does not
56 * depend on the download manager.
58 * @param aSaveFunction
59 * The function to call. This is usually the subject of the entire test
62 function callSaveWithMockObjects(aSaveFunction) {
63 // Call the provided function while the mock object factories are in place and
64 // ensure that, even in case of exceptions during the function's execution,
65 // the mock object factories are unregistered before proceeding with the other
66 // tests in the suite.
67 mockFilePickerRegisterer.register();
69 mockTransferForContinuingRegisterer.register();
74 mockTransferForContinuingRegisterer.unregister();
78 mockFilePickerRegisterer.unregister();
83 * Reads the contents of the provided short file (up to 1 MiB).
86 * nsIFile object pointing to the file to be read.
89 * String containing the raw octets read from the file.
91 function readShortFile(aFile) {
92 var inputStream = Cc["@mozilla.org/network/file-input-stream;1"].
93 createInstance(Ci.nsIFileInputStream);
94 inputStream.init(aFile, -1, 0, 0);
96 var scrInputStream = Cc["@mozilla.org/scriptableinputstream;1"].
97 createInstance(Ci.nsIScriptableInputStream);
98 scrInputStream.init(inputStream);
100 // Assume that the file is much shorter than 1 MiB.
101 return scrInputStream.read(1048576);
104 // Close the scriptable stream after reading, even if the operation
106 scrInputStream.close();
110 // Close the stream after reading, if it is still open, even if the read