toolkit/modules/tests/xpcshell/test_FileUtils.js

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:4cd1ec2b468e
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 Components.utils.import("resource://gre/modules/FileUtils.jsm");
5
6 function do_check_throws(f, result, stack) {
7 if (!stack)
8 stack = Components.stack.caller;
9
10 try {
11 f();
12 } catch (exc) {
13 if (exc.result == result)
14 return;
15 do_throw("expected result " + result + ", caught " + exc, stack);
16 }
17 do_throw("expected result " + result + ", none thrown", stack);
18 }
19
20 const gProfD = do_get_profile();
21
22 add_test(function test_getFile() {
23 let file = FileUtils.getFile("ProfD", ["foobar"]);
24 do_check_true(file instanceof Components.interfaces.nsIFile);
25 do_check_false(file.exists());
26
27 let other = gProfD.clone();
28 other.append("foobar");
29 do_check_true(file.equals(other));
30
31 run_next_test();
32 });
33
34 add_test(function test_getFile_nonexistentDir() {
35 do_check_throws(function () {
36 let file = FileUtils.getFile("NonexistentD", ["foobar"]);
37 }, Components.results.NS_ERROR_FAILURE);
38
39 run_next_test();
40 });
41
42 add_test(function test_getFile_createDirs() {
43 let file = FileUtils.getFile("ProfD", ["a", "b", "foobar"]);
44 do_check_true(file instanceof Components.interfaces.nsIFile);
45 do_check_false(file.exists());
46
47 let other = gProfD.clone();
48 other.append("a");
49 do_check_true(other.isDirectory());
50 other.append("b");
51 do_check_true(other.isDirectory());
52 other.append("foobar");
53 do_check_true(file.equals(other));
54
55 run_next_test();
56 });
57
58 add_test(function test_getDir() {
59 let dir = FileUtils.getDir("ProfD", ["foodir"]);
60 do_check_true(dir instanceof Components.interfaces.nsIFile);
61 do_check_false(dir.exists());
62
63 let other = gProfD.clone();
64 other.append("foodir");
65 do_check_true(dir.equals(other));
66
67 run_next_test();
68 });
69
70 add_test(function test_getDir_nonexistentDir() {
71 do_check_throws(function () {
72 let file = FileUtils.getDir("NonexistentD", ["foodir"]);
73 }, Components.results.NS_ERROR_FAILURE);
74
75 run_next_test();
76 });
77
78 add_test(function test_getDir_shouldCreate() {
79 let dir = FileUtils.getDir("ProfD", ["c", "d", "foodir"], true);
80 do_check_true(dir instanceof Components.interfaces.nsIFile);
81 do_check_true(dir.exists());
82
83 let other = gProfD.clone();
84 other.append("c");
85 do_check_true(other.isDirectory());
86 other.append("d");
87 do_check_true(other.isDirectory());
88 other.append("foodir");
89 do_check_true(dir.equals(other));
90
91 run_next_test();
92 });
93
94 let openFileOutputStream_defaultFlags = function (aKind, aFileName) {
95 let file = FileUtils.getFile("ProfD", [aFileName]);
96 let fos;
97 do_check_true(aKind == "atomic" || aKind == "safe" || aKind == "");
98 if (aKind == "atomic") {
99 fos = FileUtils.openAtomicFileOutputStream(file);
100 } else if (aKind == "safe") {
101 fos = FileUtils.openSafeFileOutputStream(file);
102 } else {
103 fos = FileUtils.openFileOutputStream(file);
104 }
105 do_check_true(fos instanceof Components.interfaces.nsIFileOutputStream);
106 if (aKind == "atomic" || aKind == "safe") {
107 do_check_true(fos instanceof Components.interfaces.nsISafeOutputStream);
108 }
109
110 // FileUtils.openFileOutputStream or FileUtils.openAtomicFileOutputStream()
111 // or FileUtils.openSafeFileOutputStream() opens the stream with DEFER_OPEN
112 // which means the file will not be open until we write to it.
113 do_check_false(file.exists());
114
115 let data = "test_default_flags";
116 fos.write(data, data.length);
117 do_check_true(file.exists());
118
119 // No nsIXULRuntime in xpcshell, so use this trick to determine whether we're
120 // on Windows.
121 if ("@mozilla.org/windows-registry-key;1" in Components.classes) {
122 do_check_eq(file.permissions, 0666);
123 } else {
124 do_check_eq(file.permissions, FileUtils.PERMS_FILE);
125 }
126
127 run_next_test();
128 };
129
130 let openFileOutputStream_modeFlags = function(aKind, aFileName) {
131 let file = FileUtils.getFile("ProfD", [aFileName]);
132 let fos;
133 do_check_true(aKind == "atomic" || aKind == "safe" || aKind == "");
134 if (aKind == "atomic") {
135 fos = FileUtils.openAtomicFileOutputStream(file, FileUtils.MODE_WRONLY);
136 } else if (aKind == "safe") {
137 fos = FileUtils.openSafeFileOutputStream(file, FileUtils.MODE_WRONLY);
138 } else {
139 fos = FileUtils.openFileOutputStream(file, FileUtils.MODE_WRONLY);
140 }
141 let data = "test_modeFlags";
142 do_check_throws(function () {
143 fos.write(data, data.length);
144 }, Components.results.NS_ERROR_FILE_NOT_FOUND);
145 do_check_false(file.exists());
146
147 run_next_test();
148 };
149
150 let closeFileOutputStream = function(aKind, aFileName) {
151 let file = FileUtils.getFile("ProfD", [aFileName]);
152 let fos;
153 do_check_true(aKind == "atomic" || aKind == "safe");
154 if (aKind == "atomic") {
155 fos = FileUtils.openAtomicFileOutputStream(file);
156 } else if (aKind == "safe") {
157 fos = FileUtils.openSafeFileOutputStream(file);
158 }
159
160 // We can write data to the stream just fine while it's open.
161 let data = "testClose";
162 fos.write(data, data.length);
163
164 // But once we close it, we can't anymore.
165 if (aKind == "atomic") {
166 FileUtils.closeAtomicFileOutputStream(fos);
167 } else if (aKind == "safe"){
168 FileUtils.closeSafeFileOutputStream(fos);
169 }
170 do_check_throws(function () {
171 fos.write(data, data.length);
172 }, Components.results.NS_BASE_STREAM_CLOSED);
173 run_next_test();
174 };
175
176 add_test(function test_openFileOutputStream_defaultFlags() {
177 openFileOutputStream_defaultFlags("", "george");
178 });
179
180 // openFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE
181 // as the default mode flags, but we can pass in our own if we want to.
182 add_test(function test_openFileOutputStream_modeFlags() {
183 openFileOutputStream_modeFlags("", "ringo");
184 });
185
186 add_test(function test_openAtomicFileOutputStream_defaultFlags() {
187 openFileOutputStream_defaultFlags("atomic", "peiyong");
188 });
189
190 // openAtomicFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE
191 // as the default mode flags, but we can pass in our own if we want to.
192 add_test(function test_openAtomicFileOutputStream_modeFlags() {
193 openFileOutputStream_modeFlags("atomic", "lin");
194 });
195
196 add_test(function test_closeAtomicFileOutputStream() {
197 closeFileOutputStream("atomic", "peiyonglin");
198 });
199
200 add_test(function test_openSafeFileOutputStream_defaultFlags() {
201 openFileOutputStream_defaultFlags("safe", "john");
202 });
203
204 // openSafeFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE
205 // as the default mode flags, but we can pass in our own if we want to.
206 add_test(function test_openSafeFileOutputStream_modeFlags() {
207 openFileOutputStream_modeFlags("safe", "paul");
208 });
209
210 add_test(function test_closeSafeFileOutputStream() {
211 closeFileOutputStream("safe", "georgee");
212 });
213
214 add_test(function test_newFile() {
215 let testfile = FileUtils.getFile("ProfD", ["test"]);
216 let testpath = testfile.path;
217 let file = new FileUtils.File(testpath);
218 do_check_true(file instanceof Components.interfaces.nsILocalFile);
219 do_check_true(file.equals(testfile));
220 do_check_eq(file.path, testpath);
221 run_next_test();
222 });
223
224 function run_test() {
225 run_next_test();
226 }

mercurial