Mozdev
mozilla/verbosio/test/unit/test_FileCommon.js
author Alex Vincent@SkyfireDEMO1.gateway.2wire.net
Tue Sep 02 22:03:06 2008 -0700 (17 months ago)
changeset 24 64a17b131ade
parent 236da8b043b6d3
child 41ebd08481eada
permissions -rw-r--r--
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;
     6 
     7 const ZipWriter = C_C("@mozilla.org/zipwriter;1",
     8                       "nsIZipWriter");
     9 const ZipReader = C_C("@mozilla.org/libjar/zip-reader;1",
    10                       "nsIZipReader",
    11                       "open");
    12 
    13 const InputStream = C_C("@mozilla.org/network/file-input-stream;1",
    14                         C_i.nsIFileInputStream,
    15                         "init");
    16 const SInputStream = C_C("@mozilla.org/scriptableinputstream;1",
    17                          C_i.nsIScriptableInputStream,
    18                          "init");
    19 const OutputStream = C_C("@mozilla.org/network/file-output-stream;1",
    20                          C_i.nsIFileOutputStream,
    21                          "init");
    22 
    23 function readFromStream(aInputStream) {
    24   var sInStream = new SInputStream(aInputStream);
    25 
    26   var contents = "";
    27   while (sInStream.available()) {
    28     contents += sInStream.read(sInStream.available());
    29   }
    30   sInStream.close();
    31   return contents;
    32 }
    33 
    34 const JAR_DIR = "verbosio/test/jar-data/";
    35 const XPI_DIR = "verbosio/test/xpi-data/";
    36 
    37 function addToZip(zipW, base, path) {
    38   zipW.addEntryFile(path, 0, do_get_file(base + path), false);
    39 }
    40 
    41 const DataBuffer = {
    42   spacePrefix: "",
    43   indent: function indent() {
    44     this.spacePrefix += "  ";
    45   },
    46   outdent: function outdent() {
    47     this.spacePrefix = this.spacePrefix.substr(2);
    48   },
    49 
    50   _data: "",
    51   addLine: function addLine(aData) {
    52     this._data += this.spacePrefix + aData + "\n";
    53   },
    54   flush: function flush() {
    55     do_check_eq(this.spacePrefix, "");
    56     var rv = this._data;
    57     this._data = "";
    58     return rv;
    59   }
    60 };
    61 
    62 function dumpJSStack() {
    63   dump((new Error).stack + "\n\n");
    64 }
    65 
    66 function FileIterator(aCommonFile) {
    67   try {
    68     var line = aCommonFile.leafName;
    69     if (aCommonFile.isDirectory)
    70       line += " (isDirectory)";
    71     if (aCommonFile.isZip)
    72       line += " (isZip)";
    73     DataBuffer.addLine(line);
    74     var contentFiles;
    75     if (aCommonFile.isDirectory || aCommonFile.isZip) {
    76       contentFiles = aCommonFile.getDirContents();
    77       DataBuffer.indent();
    78       for each (var file in contentFiles)
    79         FileIterator(file);
    80       DataBuffer.outdent();
    81     }
    82     else {
    83       try {
    84         contentFiles = aCommonFile.getDirContents();
    85         do_throw("Should've thrown NS_ERROR_NOT_AVAILABLE for " + line);
    86       }
    87       catch (e if ((e instanceof C_i.nsIException) &&
    88                    (e.result == C_r.NS_ERROR_NOT_AVAILABLE)))
    89       {
    90         // do nothing
    91       }
    92     }
    93   } catch (e)
    94   {
    95     dump(aCommonFile.toString() + "\n");
    96     throw e;
    97   }
    98 }
    99 
   100 var tempDir;
   101 var tempReadMe;
   102 var tempJAR;
   103 var tempXPI;
   104 
   105 function cleanup() {
   106   do_test_finished();
   107   dump("Clean up temporary files.\n\n");
   108 
   109   tempXPI.remove(true);
   110   tempJAR.remove(true);
   111   tempReadMe.remove(true);
   112   tempDir.remove(true);
   113 }
   114 
   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);
   122 
   123   tempDir.create(C_i.nsIFile.DIRECTORY_TYPE, 777);
   124 
   125   tempReadMe = tempDir.clone();
   126   tempReadMe.append("readme.txt");
   127   
   128   tempJAR = tempDir.clone()
   129   tempJAR.append("FileCommon.jar");
   130 
   131   tempXPI = tempDir.clone();
   132   tempXPI.append("FileCommon.xpi");
   133 
   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");
   139   zipW.close();
   140 
   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);
   146   zipW.close();
   147 
   148   zipW = null;
   149 
   150   tempJAR.remove(true);
   151 
   152   var readMeStream = new OutputStream(tempReadMe, -1, -1, 0);
   153   var readMeContents = "Hello World\n";
   154   readMeStream.write(readMeContents, readMeContents.length);
   155   readMeStream.close();
   156   readMeStream = null;
   157 
   158   DataBuffer.flush();
   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
   185 
   186   const expectedResults = DataBuffer.flush();
   187   dump(expectedResults + "\n\n");
   188 
   189   Components.utils.import("resource://gre/verbosio-app/modules/FileCommon.jsm");
   190   var rootFile = FileCommon.getFile(tempDir.path);
   191 
   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();
   196 
   197   dump(actualResults + "\n\n");
   198   do_check_eq("\n" + expectedResults, "\n" + actualResults);
   199 
   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);
   208 
   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));
   214 
   215   zipFile.write(readMeContents);
   216   do_check_eq(zipFile.read(), readMeContents);
   217 
   218   var zipR = new ZipReader(tempXPI);
   219   do_check_eq(readMeContents,
   220               readFromStream(zipR.getInputStream("chrome.manifest")));
   221   zipR.close();
   222 
   223   // Test reading and writing to a zipped file within another zipped file.
   224   var jarFile = FileCommon.getFile(tempXPI.path,
   225                                    "chrome/foo.jar",
   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));
   230   
   231   jarFile.write(readMeContents);
   232   var zipR = new ZipReader(tempXPI);
   233   zipR.extract("chrome/foo.jar", tempJAR);
   234   zipR.close();
   235   zipR = new ZipReader(tempJAR);
   236   do_check_eq(readMeContents,
   237               readFromStream(zipR.getInputStream("locale/en-US/foo.dtd")));
   238   zipR.close();
   239 
   240   zipR = null;
   241   zipW = null;
   242   flatFile = null;
   243   jarFile = null;
   244   zipFile = null;
   245 
   246   var obs = C_c["@mozilla.org/observer-service;1"]
   247                .getService(C_i.nsIObserverService);
   248   obs.notifyObservers(null, "verbosio-filecommon-shutdown", null);
   249 }
   250 
   251 function run_test() {
   252   try {
   253     run_real_test();
   254   } catch (e if e instanceof C_i.nsIException) {
   255     var msg = e.location;
   256     while (msg) {
   257       dump(msg.filename + " " + msg.lineNumber + "\n");
   258       msg = msg.caller;
   259     }
   260     throw e;
   261   }
   262   do_test_pending();
   263   do_timeout(1000, "cleanup()");
   264 }