196be3c5abb72e14fa1c291c8f6154b16d20e383
[gecko.git] / toolkitFunctions.js
blob196be3c5abb72e14fa1c291c8f6154b16d20e383
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
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/
8  *
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
12  * License.
13  *
14  * The Original Code is Mozilla XUL Toolkit Testing Code.
15  *
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.
20  *
21  * Contributor(s):
22  *
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.
34  *
35  * ***** END LICENSE BLOCK ***** */
37 /**
38  * Provides a temporary save directory.
39  *
40  * @return
41  *        nsIFile pointing to the new or existing directory.
42  */
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);
49   return saveDir;
52 /**
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.
57  *
58  * @param aSaveFunction
59  *        The function to call. This is usually the subject of the entire test
60  *        being run.
61  */
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();
68   try {
69     mockTransferForContinuingRegisterer.register();
70     try {
71       aSaveFunction();
72     }
73     finally {
74       mockTransferForContinuingRegisterer.unregister();
75     }
76   }
77   finally {
78     mockFilePickerRegisterer.unregister();
79   }
82 /**
83  * Reads the contents of the provided short file (up to 1 MiB).
84  *
85  * @param aFile
86  *        nsIFile object pointing to the file to be read.
87  *
88  * @return
89  *        String containing the raw octets read from the file.
90  */
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);
95   try {
96     var scrInputStream = Cc["@mozilla.org/scriptableinputstream;1"].
97                          createInstance(Ci.nsIScriptableInputStream);
98     scrInputStream.init(inputStream);
99     try {
100       // Assume that the file is much shorter than 1 MiB.
101       return scrInputStream.read(1048576);
102     }
103     finally {
104       // Close the scriptable stream after reading, even if the operation
105       // failed.
106       scrInputStream.close();
107     }
108   }
109   finally {
110     // Close the stream after reading, if it is still open, even if the read
111     // operation failed.
112     inputStream.close();
113   }