Fix FileCommon.jsm to pass its xpcshell test... and fix the test, too.
1 const C_i = Components.interfaces;
2 const C_r = Components.results;
3 const C_c = Components.classes;
4 const C_C = Components.Constructor;
5 const C_E = Components.Exception;
7 const ZipWriter = C_C("@mozilla.org/zipwriter;1",
9 const ZipReader = C_C("@mozilla.org/libjar/zip-reader;1",
13 const InputStream = C_C("@mozilla.org/network/file-input-stream;1",
14 C_i.nsIFileInputStream,
16 const SInputStream = C_C("@mozilla.org/scriptableinputstream;1",
17 C_i.nsIScriptableInputStream,
19 const OutputStream = C_C("@mozilla.org/network/file-output-stream;1",
20 C_i.nsIFileOutputStream,
23 function readFromStream(aInputStream) {
24 var sInStream = new SInputStream(aInputStream);
27 while (sInStream.available()) {
28 contents += sInStream.read(sInStream.available());
34 const JAR_DIR = "verbosio/test/jar-data/";
35 const XPI_DIR = "verbosio/test/xpi-data/";
37 function addToZip(zipW, base, path) {
38 zipW.addEntryFile(path, 0, do_get_file(base + path), false);
43 indent: function indent() {
44 this.spacePrefix += " ";
46 outdent: function outdent() {
47 this.spacePrefix = this.spacePrefix.substr(2);
51 addLine: function addLine(aData) {
52 this._data += this.spacePrefix + aData + "\n";
54 flush: function flush() {
55 do_check_eq(this.spacePrefix, "");
62 function dumpJSStack() {
63 dump((new Error).stack + "\n\n");
66 function FileIterator(aCommonFile) {
68 var line = aCommonFile.leafName;
69 if (aCommonFile.isDirectory)
70 line += " (isDirectory)";
71 if (aCommonFile.isZip)
73 DataBuffer.addLine(line);
75 if (aCommonFile.isDirectory || aCommonFile.isZip) {
76 contentFiles = aCommonFile.getDirContents();
78 for each (var file in contentFiles)
84 contentFiles = aCommonFile.getDirContents();
85 do_throw("Should've thrown NS_ERROR_NOT_AVAILABLE for " + line);
87 catch (e if ((e instanceof C_i.nsIException) &&
88 (e.result == C_r.NS_ERROR_NOT_AVAILABLE)))
95 dump(aCommonFile.toString() + "\n");
107 dump("Clean up temporary files.\n\n");
109 tempXPI.remove(true);
110 tempJAR.remove(true);
111 tempReadMe.remove(true);
112 tempDir.remove(true);
115 function run_real_test() {
116 tempDir = C_c["@mozilla.org/file/directory_service;1"]
117 .getService(C_i.nsIProperties)
118 .get("TmpD", C_i.nsIFile);
119 tempDir.append("FileCommonTest");
120 if (tempDir.exists())
121 tempDir.remove(true);
123 tempDir.create(C_i.nsIFile.DIRECTORY_TYPE, 777);
125 tempReadMe = tempDir.clone();
126 tempReadMe.append("readme.txt");
128 tempJAR = tempDir.clone()
129 tempJAR.append("FileCommon.jar");
131 tempXPI = tempDir.clone();
132 tempXPI.append("FileCommon.xpi");
134 var zipW = new ZipWriter();
135 zipW.open(tempJAR, 0x02 | 0x08 | 0x10);
136 addToZip(zipW, JAR_DIR, "content/foo.xul");
137 addToZip(zipW, JAR_DIR, "locale/en-US/foo.dtd");
138 addToZip(zipW, JAR_DIR, "locale/en-US/foo.properties");
141 zipW = new ZipWriter();
142 zipW.open(tempXPI, 0x02 | 0x08 | 0x10);
143 addToZip(zipW, XPI_DIR, "chrome.manifest");
144 // Add the JAR file to the XPI.
145 zipW.addEntryFile("chrome/foo.jar", 0, tempJAR, false);
150 tempJAR.remove(true);
152 var readMeStream = new OutputStream(tempReadMe, -1, -1, 0);
153 var readMeContents = "Hello World\n";
154 readMeStream.write(readMeContents, readMeContents.length);
155 readMeStream.close();
159 DataBuffer.addLine("FileCommonTest (isDirectory)")
160 DataBuffer.indent(); // FileCommonTest directory
161 DataBuffer.addLine("FileCommon.xpi (isZip)");
162 DataBuffer.indent(); // FileCommon.xpi
163 DataBuffer.addLine("chrome (isDirectory)");
164 DataBuffer.indent(); // chrome directory
165 DataBuffer.addLine("foo.jar (isZip)");
166 DataBuffer.indent(); // foo.jar
167 DataBuffer.addLine("content (isDirectory)");
168 DataBuffer.indent(); // content directory
169 DataBuffer.addLine("foo.xul");
170 DataBuffer.outdent(); // content directory
171 DataBuffer.addLine("locale (isDirectory)");
172 DataBuffer.indent(); // locale directory
173 DataBuffer.addLine("en-US (isDirectory)");
174 DataBuffer.indent(); // en-US directory
175 DataBuffer.addLine("foo.dtd");
176 DataBuffer.addLine("foo.properties");
177 DataBuffer.outdent(); // en-US directory
178 DataBuffer.outdent(); // locale directory
179 DataBuffer.outdent(); // foo.jar
180 DataBuffer.outdent(); // chrome directory
181 DataBuffer.addLine("chrome.manifest");
182 DataBuffer.outdent(); // FileCommon.xpi
183 DataBuffer.addLine("readme.txt");
184 DataBuffer.outdent(); // FileCommonTest directory
186 const expectedResults = DataBuffer.flush();
187 dump(expectedResults + "\n\n");
189 Components.utils.import("resource://gre/verbosio-app/modules/FileCommon.jsm");
190 var rootFile = FileCommon.getFile(tempDir.path);
192 // Now we start the real tests.
193 // Compare the contents of our XPI to the projected contents, above.
194 FileIterator(rootFile);
195 const actualResults = DataBuffer.flush();
197 dump(actualResults + "\n\n");
198 do_check_eq("\n" + expectedResults, "\n" + actualResults);
200 // Test reading and writing to a flat file.
201 var flatFile = FileCommon.getFile(tempReadMe.path);
202 do_check_eq(flatFile.read(), readMeContents);
203 readMeContents = "Goodbye\n";
204 flatFile.write(readMeContents);
205 do_check_eq(flatFile.read(), readMeContents);
206 readMeStream = new InputStream(tempReadMe, -1, 0, 0);
207 do_check_eq(readFromStream(readMeStream), readMeContents);
209 // Test reading and writing to a zipped file.
210 var zipFile = FileCommon.getFile(tempXPI.path, "chrome.manifest");
211 var chromeManifest = do_get_file(XPI_DIR + "chrome.manifest");
212 var inStream = new InputStream(chromeManifest, -1, 0, 0);
213 do_check_eq(zipFile.read(), readFromStream(inStream));
215 zipFile.write(readMeContents);
216 do_check_eq(zipFile.read(), readMeContents);
218 var zipR = new ZipReader(tempXPI);
219 do_check_eq(readMeContents,
220 readFromStream(zipR.getInputStream("chrome.manifest")));
223 // Test reading and writing to a zipped file within another zipped file.
224 var jarFile = FileCommon.getFile(tempXPI.path,
226 "locale/en-US/foo.dtd");
227 var fooDTD = do_get_file(JAR_DIR + "locale/en-US/foo.dtd");
228 inStream = new InputStream(fooDTD, -1, 0, 0);
229 do_check_eq(jarFile.read(), readFromStream(inStream));
231 jarFile.write(readMeContents);
232 var zipR = new ZipReader(tempXPI);
233 zipR.extract("chrome/foo.jar", tempJAR);
235 zipR = new ZipReader(tempJAR);
236 do_check_eq(readMeContents,
237 readFromStream(zipR.getInputStream("locale/en-US/foo.dtd")));
246 var obs = C_c["@mozilla.org/observer-service;1"]
247 .getService(C_i.nsIObserverService);
248 obs.notifyObservers(null, "verbosio-filecommon-shutdown", null);
251 function run_test() {
254 } catch (e if e instanceof C_i.nsIException) {
255 var msg = e.location;
257 dump(msg.filename + " " + msg.lineNumber + "\n");
263 do_timeout(1000, "cleanup()");