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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/unit/component-blob.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,85 @@
     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 Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cu = Components.utils;
    1.13 +
    1.14 +function do_check_true(cond, text) {
    1.15 +  // we don't have the test harness' utilities in this scope, so we need this
    1.16 +  // little helper. In the failure case, the exception is propagated to the
    1.17 +  // caller in the main run_test() function, and the test fails.
    1.18 +  if (!cond)
    1.19 +    throw "Failed check: " + text;
    1.20 +}
    1.21 +
    1.22 +function BlobComponent() {
    1.23 +  this.wrappedJSObject = this;
    1.24 +}
    1.25 +BlobComponent.prototype =
    1.26 +{
    1.27 +  doTest: function() {
    1.28 +    // throw if anything goes wrong
    1.29 +    let testContent = "<a id=\"a\"><b id=\"b\">hey!<\/b><\/a>";
    1.30 +    // should be able to construct a file
    1.31 +    var f1 = Blob([testContent], {"type" : "text/xml"});
    1.32 +    // with either constructor syntax
    1.33 +    var f2 = new Blob([testContent], {"type" : "text/xml"});
    1.34 +
    1.35 +    // do some tests
    1.36 +    do_check_true(f1 instanceof Ci.nsIDOMBlob, "Should be a DOM Blob");
    1.37 +    do_check_true(f2 instanceof Ci.nsIDOMBlob, "Should be a DOM Blob");
    1.38 +
    1.39 +    do_check_true(!(f1 instanceof Ci.nsIDOMFile), "Should not be a DOM File");
    1.40 +    do_check_true(!(f2 instanceof Ci.nsIDOMFile), "Should not be a DOM File");
    1.41 +
    1.42 +    do_check_true(f1.type == "text/xml", "Wrong type");
    1.43 +    do_check_true(f2.type == "text/xml", "Wrong type");
    1.44 +
    1.45 +    do_check_true(f1.size == testContent.length, "Wrong content size");
    1.46 +    do_check_true(f2.size == testContent.length, "Wrong content size");
    1.47 +
    1.48 +    var f3 = new Blob();
    1.49 +    do_check_true(f3.size == 0, "Wrong size");
    1.50 +    do_check_true(f3.type == "", "Wrong type");
    1.51 +
    1.52 +    var threw = false;
    1.53 +    try {
    1.54 +      // Needs a valid ctor argument
    1.55 +      var f3 = Blob(Date(132131532));
    1.56 +    } catch (e) {
    1.57 +      threw = true;
    1.58 +    }
    1.59 +    do_check_true(threw, "Passing a random object should fail");
    1.60 +
    1.61 +    return true;
    1.62 +  },
    1.63 +
    1.64 +  // nsIClassInfo + information for XPCOM registration code in XPCOMUtils.jsm
    1.65 +  classDescription: "Blob in components scope code",
    1.66 +  classID: Components.ID("{06215993-a3c2-41e3-bdfd-0a3a2cc0b65c}"),
    1.67 +  contractID: "@mozilla.org/tests/component-blob;1",
    1.68 +
    1.69 +  // nsIClassInfo
    1.70 +  implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
    1.71 +  flags: 0,
    1.72 +
    1.73 +  getInterfaces: function getInterfaces(aCount) {
    1.74 +    var interfaces = [Components.interfaces.nsIClassInfo];
    1.75 +    aCount.value = interfaces.length;
    1.76 +    return interfaces;
    1.77 +  },
    1.78 +
    1.79 +  getHelperForLanguage: function getHelperForLanguage(aLanguage) {
    1.80 +    return null;
    1.81 +  },
    1.82 +
    1.83 +  // nsISupports
    1.84 +  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIClassInfo])
    1.85 +};
    1.86 +
    1.87 +var gComponentsArray = [BlobComponent];
    1.88 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory(gComponentsArray);

mercurial