js/xpconnect/tests/unit/component-file.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/unit/component-file.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
     1.9 +
    1.10 +const Ci = Components.interfaces;
    1.11 +
    1.12 +function do_check_true(cond, text) {
    1.13 +  // we don't have the test harness' utilities in this scope, so we need this
    1.14 +  // little helper. In the failure case, the exception is propagated to the
    1.15 +  // caller in the main run_test() function, and the test fails.
    1.16 +  if (!cond)
    1.17 +    throw "Failed check: " + text;
    1.18 +}
    1.19 +
    1.20 +function FileComponent() {
    1.21 +  this.wrappedJSObject = this;
    1.22 +}
    1.23 +FileComponent.prototype =
    1.24 +{
    1.25 +  doTest: function() {
    1.26 +    // throw if anything goes wrong
    1.27 +
    1.28 +    // find the current directory path
    1.29 +    var file = Components.classes["@mozilla.org/file/directory_service;1"]
    1.30 +               .getService(Ci.nsIProperties)
    1.31 +               .get("CurWorkD", Ci.nsIFile);
    1.32 +    file.append("xpcshell.ini");
    1.33 +
    1.34 +    // should be able to construct a file
    1.35 +    var f1 = File(file.path);
    1.36 +    // with either constructor syntax
    1.37 +    var f2 = new File(file.path);
    1.38 +    // and with nsIFiles
    1.39 +    var f3 = File(file);
    1.40 +    var f4 = new File(file);
    1.41 +
    1.42 +    // do some tests
    1.43 +    do_check_true(f1 instanceof Ci.nsIDOMFile, "Should be a DOM File");
    1.44 +    do_check_true(f2 instanceof Ci.nsIDOMFile, "Should be a DOM File");
    1.45 +    do_check_true(f3 instanceof Ci.nsIDOMFile, "Should be a DOM File");
    1.46 +    do_check_true(f4 instanceof Ci.nsIDOMFile, "Should be a DOM File");
    1.47 +
    1.48 +    do_check_true(f1.name == "xpcshell.ini", "Should be the right file");
    1.49 +    do_check_true(f2.name == "xpcshell.ini", "Should be the right file");
    1.50 +    do_check_true(f3.name == "xpcshell.ini", "Should be the right file");
    1.51 +    do_check_true(f4.name == "xpcshell.ini", "Should be the right file");
    1.52 +
    1.53 +    do_check_true(f1.type == "", "Should be the right type");
    1.54 +    do_check_true(f2.type == "", "Should be the right type");
    1.55 +    do_check_true(f3.type == "", "Should be the right type");
    1.56 +    do_check_true(f4.type == "", "Should be the right type");
    1.57 +
    1.58 +    var threw = false;
    1.59 +    try {
    1.60 +      // Needs a ctor argument
    1.61 +      var f7 = File();
    1.62 +    } catch (e) {
    1.63 +      threw = true;
    1.64 +    }
    1.65 +    do_check_true(threw, "No ctor arguments should throw");
    1.66 +
    1.67 +    var threw = false;
    1.68 +    try {
    1.69 +      // Needs a valid ctor argument
    1.70 +      var f7 = File(Date(132131532));
    1.71 +    } catch (e) {
    1.72 +      threw = true;
    1.73 +    }
    1.74 +    do_check_true(threw, "Passing a random object should fail");
    1.75 +
    1.76 +    var threw = false
    1.77 +    try {
    1.78 +      // Directories fail
    1.79 +      var dir = Components.classes["@mozilla.org/file/directory_service;1"]
    1.80 +                          .getService(Ci.nsIProperties)
    1.81 +                          .get("CurWorkD", Ci.nsIFile);
    1.82 +      var f7 = File(dir)
    1.83 +    } catch (e) {
    1.84 +      threw = true;
    1.85 +    }
    1.86 +    do_check_true(threw, "Can't create a File object for a directory");
    1.87 +
    1.88 +    return true;
    1.89 +  },
    1.90 +
    1.91 +  // nsIClassInfo + information for XPCOM registration code in XPCOMUtils.jsm
    1.92 +  classDescription: "File in components scope code",
    1.93 +  classID: Components.ID("{da332370-91d4-464f-a730-018e14769cab}"),
    1.94 +  contractID: "@mozilla.org/tests/component-file;1",
    1.95 +
    1.96 +  // nsIClassInfo
    1.97 +  implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
    1.98 +  flags: 0,
    1.99 +
   1.100 +  getInterfaces: function getInterfaces(aCount) {
   1.101 +    var interfaces = [Components.interfaces.nsIClassInfo];
   1.102 +    aCount.value = interfaces.length;
   1.103 +    return interfaces;
   1.104 +  },
   1.105 +
   1.106 +  getHelperForLanguage: function getHelperForLanguage(aLanguage) {
   1.107 +    return null;
   1.108 +  },
   1.109 +
   1.110 +  // nsISupports
   1.111 +  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIClassInfo])
   1.112 +};
   1.113 +
   1.114 +var gComponentsArray = [FileComponent];
   1.115 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory(gComponentsArray);

mercurial