js/xpconnect/tests/unit/test_xpcomutils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/unit/test_xpcomutils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,218 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: sw=4 ts=4 sts=4 et
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/**
    1.11 + * This file tests the methods on XPCOMUtils.jsm.
    1.12 + */
    1.13 +
    1.14 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.15 +
    1.16 +const Cc = Components.classes;
    1.17 +const Ci = Components.interfaces;
    1.18 +
    1.19 +
    1.20 +////////////////////////////////////////////////////////////////////////////////
    1.21 +//// Tests
    1.22 +
    1.23 +add_test(function test_generateQI_string_names()
    1.24 +{
    1.25 +    var x = {
    1.26 +        QueryInterface: XPCOMUtils.generateQI([
    1.27 +            Components.interfaces.nsIClassInfo,
    1.28 +            "nsIDOMNode"
    1.29 +        ])
    1.30 +    };
    1.31 +
    1.32 +    try {
    1.33 +        x.QueryInterface(Components.interfaces.nsIClassInfo);
    1.34 +    } catch(e) {
    1.35 +        do_throw("Should QI to nsIClassInfo");
    1.36 +    }
    1.37 +    try {
    1.38 +        x.QueryInterface(Components.interfaces.nsIDOMNode);
    1.39 +    } catch(e) {
    1.40 +        do_throw("Should QI to nsIDOMNode");
    1.41 +    }
    1.42 +    try {
    1.43 +        x.QueryInterface(Components.interfaces.nsIDOMDocument);
    1.44 +        do_throw("QI should not have succeeded!");
    1.45 +    } catch(e) {}
    1.46 +    run_next_test();
    1.47 +});
    1.48 +
    1.49 +
    1.50 +add_test(function test_generateCI()
    1.51 +{
    1.52 +    const classID = Components.ID("562dae2e-7cff-432b-995b-3d4c03fa2b89");
    1.53 +    const classDescription = "generateCI test component";
    1.54 +    const flags = Components.interfaces.nsIClassInfo.DOM_OBJECT;
    1.55 +    var x = {
    1.56 +        QueryInterface: XPCOMUtils.generateQI([]),
    1.57 +        classInfo: XPCOMUtils.generateCI({classID: classID,
    1.58 +                                          interfaces: [],
    1.59 +                                          flags: flags,
    1.60 +                                          classDescription: classDescription})
    1.61 +    };
    1.62 +
    1.63 +    try {
    1.64 +        var ci = x.QueryInterface(Components.interfaces.nsIClassInfo);
    1.65 +        ci = ci.QueryInterface(Components.interfaces.nsISupports);
    1.66 +        ci = ci.QueryInterface(Components.interfaces.nsIClassInfo);
    1.67 +        do_check_eq(ci.classID, classID);
    1.68 +        do_check_eq(ci.flags, flags);
    1.69 +        do_check_eq(ci.classDescription, classDescription);
    1.70 +    } catch(e) {
    1.71 +        do_throw("Classinfo for x should not be missing or broken");
    1.72 +    }
    1.73 +    run_next_test();
    1.74 +});
    1.75 +
    1.76 +add_test(function test_defineLazyGetter()
    1.77 +{
    1.78 +    let accessCount = 0;
    1.79 +    let obj = {
    1.80 +      inScope: false
    1.81 +    };
    1.82 +    const TEST_VALUE = "test value";
    1.83 +    XPCOMUtils.defineLazyGetter(obj, "foo", function() {
    1.84 +        accessCount++;
    1.85 +        this.inScope = true;
    1.86 +        return TEST_VALUE;
    1.87 +    });
    1.88 +    do_check_eq(accessCount, 0);
    1.89 +
    1.90 +    // Get the property, making sure the access count has increased.
    1.91 +    do_check_eq(obj.foo, TEST_VALUE);
    1.92 +    do_check_eq(accessCount, 1);
    1.93 +    do_check_true(obj.inScope);
    1.94 +
    1.95 +    // Get the property once more, making sure the access count has not
    1.96 +    // increased.
    1.97 +    do_check_eq(obj.foo, TEST_VALUE);
    1.98 +    do_check_eq(accessCount, 1);
    1.99 +    run_next_test();
   1.100 +});
   1.101 +
   1.102 +
   1.103 +add_test(function test_defineLazyServiceGetter()
   1.104 +{
   1.105 +    let obj = { };
   1.106 +    XPCOMUtils.defineLazyServiceGetter(obj, "service",
   1.107 +                                       "@mozilla.org/consoleservice;1",
   1.108 +                                       "nsIConsoleService");
   1.109 +    let service = Cc["@mozilla.org/consoleservice;1"].
   1.110 +                  getService(Ci.nsIConsoleService);
   1.111 +
   1.112 +    // Check that the lazy service getter and the actual service have the same
   1.113 +    // properties.
   1.114 +    for (let prop in obj.service)
   1.115 +        do_check_true(prop in service);
   1.116 +    for (let prop in service)
   1.117 +        do_check_true(prop in obj.service);
   1.118 +    run_next_test();
   1.119 +});
   1.120 +
   1.121 +
   1.122 +add_test(function test_categoryRegistration()
   1.123 +{
   1.124 +  const CATEGORY_NAME = "test-cat";
   1.125 +  const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
   1.126 +  const XULAPPINFO_CID = Components.ID("{fc937916-656b-4fb3-a395-8c63569e27a8}");
   1.127 +
   1.128 +  // Create a fake app entry for our category registration apps filter.
   1.129 +  let XULAppInfo = {
   1.130 +    vendor: "Mozilla",
   1.131 +    name: "catRegTest",
   1.132 +    ID: "{adb42a9a-0d19-4849-bf4d-627614ca19be}",
   1.133 +    version: "1",
   1.134 +    appBuildID: "2007010101",
   1.135 +    platformVersion: "",
   1.136 +    platformBuildID: "2007010101",
   1.137 +    inSafeMode: false,
   1.138 +    logConsoleErrors: true,
   1.139 +    OS: "XPCShell",
   1.140 +    XPCOMABI: "noarch-spidermonkey",
   1.141 +    QueryInterface: XPCOMUtils.generateQI([
   1.142 +      Ci.nsIXULAppInfo,
   1.143 +      Ci.nsIXULRuntime,
   1.144 +    ])
   1.145 +  };
   1.146 +  let XULAppInfoFactory = {
   1.147 +    createInstance: function (outer, iid) {
   1.148 +      if (outer != null)
   1.149 +        throw Cr.NS_ERROR_NO_AGGREGATION;
   1.150 +      return XULAppInfo.QueryInterface(iid);
   1.151 +    }
   1.152 +  };
   1.153 +  let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
   1.154 +  registrar.registerFactory(
   1.155 +    XULAPPINFO_CID,
   1.156 +    "XULAppInfo",
   1.157 +    XULAPPINFO_CONTRACTID,
   1.158 +    XULAppInfoFactory
   1.159 +  );
   1.160 +
   1.161 +  // Load test components.
   1.162 +  do_load_manifest("CatRegistrationComponents.manifest");
   1.163 +
   1.164 +  const EXPECTED_ENTRIES = ["CatAppRegisteredComponent",
   1.165 +                            "CatRegisteredComponent"];
   1.166 +
   1.167 +  // Check who is registered in "test-cat" category.
   1.168 +  let foundEntriesCount = 0;
   1.169 +  let catMan = Cc["@mozilla.org/categorymanager;1"].
   1.170 +               getService(Ci.nsICategoryManager);
   1.171 +  let entries = catMan.enumerateCategory(CATEGORY_NAME);
   1.172 +  while (entries.hasMoreElements()) {
   1.173 +    foundEntriesCount++;
   1.174 +    let entry = entries.getNext().QueryInterface(Ci.nsISupportsCString).data;
   1.175 +    print("Check the found category entry (" + entry + ") is expected.");  
   1.176 +    do_check_true(EXPECTED_ENTRIES.indexOf(entry) != -1);
   1.177 +  }
   1.178 +  print("Check there are no more or less than expected entries.");
   1.179 +  do_check_eq(foundEntriesCount, EXPECTED_ENTRIES.length);
   1.180 +  run_next_test();
   1.181 +});
   1.182 +
   1.183 +add_test(function test_generateSingletonFactory()
   1.184 +{
   1.185 +  const XPCCOMPONENT_CONTRACTID = "@mozilla.org/singletonComponentTest;1";
   1.186 +  const XPCCOMPONENT_CID = Components.ID("{31031c36-5e29-4dd9-9045-333a5d719a3e}");
   1.187 +
   1.188 +  function XPCComponent() {}
   1.189 +  XPCComponent.prototype = {
   1.190 +    classID: XPCCOMPONENT_CID,
   1.191 +    _xpcom_factory: XPCOMUtils.generateSingletonFactory(XPCComponent),
   1.192 +    QueryInterface: XPCOMUtils.generateQI([])
   1.193 +  };
   1.194 +  let NSGetFactory = XPCOMUtils.generateNSGetFactory([XPCComponent]);
   1.195 +  let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
   1.196 +  registrar.registerFactory(
   1.197 +    XPCCOMPONENT_CID,
   1.198 +    "XPCComponent",
   1.199 +    XPCCOMPONENT_CONTRACTID,
   1.200 +    NSGetFactory(XPCCOMPONENT_CID)
   1.201 +  );
   1.202 +
   1.203 +  // First, try to instance the component.
   1.204 +  let instance = Cc[XPCCOMPONENT_CONTRACTID].createInstance(Ci.nsISupports);
   1.205 +  // Try again, check that it returns the same instance as before.
   1.206 +  do_check_eq(instance,
   1.207 +              Cc[XPCCOMPONENT_CONTRACTID].createInstance(Ci.nsISupports));
   1.208 +  // Now, for sanity, check that getService is also returning the same instance.
   1.209 +  do_check_eq(instance,
   1.210 +              Cc[XPCCOMPONENT_CONTRACTID].getService(Ci.nsISupports));
   1.211 +
   1.212 +  run_next_test();
   1.213 +});
   1.214 +
   1.215 +////////////////////////////////////////////////////////////////////////////////
   1.216 +//// Test Runner
   1.217 +
   1.218 +function run_test()
   1.219 +{
   1.220 +  run_next_test();
   1.221 +}

mercurial