js/xpconnect/tests/unit/test_xpcomutils.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  * vim: sw=4 ts=4 sts=4 et
     3  * This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 /**
     8  * This file tests the methods on XPCOMUtils.jsm.
     9  */
    11 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    13 const Cc = Components.classes;
    14 const Ci = Components.interfaces;
    17 ////////////////////////////////////////////////////////////////////////////////
    18 //// Tests
    20 add_test(function test_generateQI_string_names()
    21 {
    22     var x = {
    23         QueryInterface: XPCOMUtils.generateQI([
    24             Components.interfaces.nsIClassInfo,
    25             "nsIDOMNode"
    26         ])
    27     };
    29     try {
    30         x.QueryInterface(Components.interfaces.nsIClassInfo);
    31     } catch(e) {
    32         do_throw("Should QI to nsIClassInfo");
    33     }
    34     try {
    35         x.QueryInterface(Components.interfaces.nsIDOMNode);
    36     } catch(e) {
    37         do_throw("Should QI to nsIDOMNode");
    38     }
    39     try {
    40         x.QueryInterface(Components.interfaces.nsIDOMDocument);
    41         do_throw("QI should not have succeeded!");
    42     } catch(e) {}
    43     run_next_test();
    44 });
    47 add_test(function test_generateCI()
    48 {
    49     const classID = Components.ID("562dae2e-7cff-432b-995b-3d4c03fa2b89");
    50     const classDescription = "generateCI test component";
    51     const flags = Components.interfaces.nsIClassInfo.DOM_OBJECT;
    52     var x = {
    53         QueryInterface: XPCOMUtils.generateQI([]),
    54         classInfo: XPCOMUtils.generateCI({classID: classID,
    55                                           interfaces: [],
    56                                           flags: flags,
    57                                           classDescription: classDescription})
    58     };
    60     try {
    61         var ci = x.QueryInterface(Components.interfaces.nsIClassInfo);
    62         ci = ci.QueryInterface(Components.interfaces.nsISupports);
    63         ci = ci.QueryInterface(Components.interfaces.nsIClassInfo);
    64         do_check_eq(ci.classID, classID);
    65         do_check_eq(ci.flags, flags);
    66         do_check_eq(ci.classDescription, classDescription);
    67     } catch(e) {
    68         do_throw("Classinfo for x should not be missing or broken");
    69     }
    70     run_next_test();
    71 });
    73 add_test(function test_defineLazyGetter()
    74 {
    75     let accessCount = 0;
    76     let obj = {
    77       inScope: false
    78     };
    79     const TEST_VALUE = "test value";
    80     XPCOMUtils.defineLazyGetter(obj, "foo", function() {
    81         accessCount++;
    82         this.inScope = true;
    83         return TEST_VALUE;
    84     });
    85     do_check_eq(accessCount, 0);
    87     // Get the property, making sure the access count has increased.
    88     do_check_eq(obj.foo, TEST_VALUE);
    89     do_check_eq(accessCount, 1);
    90     do_check_true(obj.inScope);
    92     // Get the property once more, making sure the access count has not
    93     // increased.
    94     do_check_eq(obj.foo, TEST_VALUE);
    95     do_check_eq(accessCount, 1);
    96     run_next_test();
    97 });
   100 add_test(function test_defineLazyServiceGetter()
   101 {
   102     let obj = { };
   103     XPCOMUtils.defineLazyServiceGetter(obj, "service",
   104                                        "@mozilla.org/consoleservice;1",
   105                                        "nsIConsoleService");
   106     let service = Cc["@mozilla.org/consoleservice;1"].
   107                   getService(Ci.nsIConsoleService);
   109     // Check that the lazy service getter and the actual service have the same
   110     // properties.
   111     for (let prop in obj.service)
   112         do_check_true(prop in service);
   113     for (let prop in service)
   114         do_check_true(prop in obj.service);
   115     run_next_test();
   116 });
   119 add_test(function test_categoryRegistration()
   120 {
   121   const CATEGORY_NAME = "test-cat";
   122   const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
   123   const XULAPPINFO_CID = Components.ID("{fc937916-656b-4fb3-a395-8c63569e27a8}");
   125   // Create a fake app entry for our category registration apps filter.
   126   let XULAppInfo = {
   127     vendor: "Mozilla",
   128     name: "catRegTest",
   129     ID: "{adb42a9a-0d19-4849-bf4d-627614ca19be}",
   130     version: "1",
   131     appBuildID: "2007010101",
   132     platformVersion: "",
   133     platformBuildID: "2007010101",
   134     inSafeMode: false,
   135     logConsoleErrors: true,
   136     OS: "XPCShell",
   137     XPCOMABI: "noarch-spidermonkey",
   138     QueryInterface: XPCOMUtils.generateQI([
   139       Ci.nsIXULAppInfo,
   140       Ci.nsIXULRuntime,
   141     ])
   142   };
   143   let XULAppInfoFactory = {
   144     createInstance: function (outer, iid) {
   145       if (outer != null)
   146         throw Cr.NS_ERROR_NO_AGGREGATION;
   147       return XULAppInfo.QueryInterface(iid);
   148     }
   149   };
   150   let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
   151   registrar.registerFactory(
   152     XULAPPINFO_CID,
   153     "XULAppInfo",
   154     XULAPPINFO_CONTRACTID,
   155     XULAppInfoFactory
   156   );
   158   // Load test components.
   159   do_load_manifest("CatRegistrationComponents.manifest");
   161   const EXPECTED_ENTRIES = ["CatAppRegisteredComponent",
   162                             "CatRegisteredComponent"];
   164   // Check who is registered in "test-cat" category.
   165   let foundEntriesCount = 0;
   166   let catMan = Cc["@mozilla.org/categorymanager;1"].
   167                getService(Ci.nsICategoryManager);
   168   let entries = catMan.enumerateCategory(CATEGORY_NAME);
   169   while (entries.hasMoreElements()) {
   170     foundEntriesCount++;
   171     let entry = entries.getNext().QueryInterface(Ci.nsISupportsCString).data;
   172     print("Check the found category entry (" + entry + ") is expected.");  
   173     do_check_true(EXPECTED_ENTRIES.indexOf(entry) != -1);
   174   }
   175   print("Check there are no more or less than expected entries.");
   176   do_check_eq(foundEntriesCount, EXPECTED_ENTRIES.length);
   177   run_next_test();
   178 });
   180 add_test(function test_generateSingletonFactory()
   181 {
   182   const XPCCOMPONENT_CONTRACTID = "@mozilla.org/singletonComponentTest;1";
   183   const XPCCOMPONENT_CID = Components.ID("{31031c36-5e29-4dd9-9045-333a5d719a3e}");
   185   function XPCComponent() {}
   186   XPCComponent.prototype = {
   187     classID: XPCCOMPONENT_CID,
   188     _xpcom_factory: XPCOMUtils.generateSingletonFactory(XPCComponent),
   189     QueryInterface: XPCOMUtils.generateQI([])
   190   };
   191   let NSGetFactory = XPCOMUtils.generateNSGetFactory([XPCComponent]);
   192   let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
   193   registrar.registerFactory(
   194     XPCCOMPONENT_CID,
   195     "XPCComponent",
   196     XPCCOMPONENT_CONTRACTID,
   197     NSGetFactory(XPCCOMPONENT_CID)
   198   );
   200   // First, try to instance the component.
   201   let instance = Cc[XPCCOMPONENT_CONTRACTID].createInstance(Ci.nsISupports);
   202   // Try again, check that it returns the same instance as before.
   203   do_check_eq(instance,
   204               Cc[XPCCOMPONENT_CONTRACTID].createInstance(Ci.nsISupports));
   205   // Now, for sanity, check that getService is also returning the same instance.
   206   do_check_eq(instance,
   207               Cc[XPCCOMPONENT_CONTRACTID].getService(Ci.nsISupports));
   209   run_next_test();
   210 });
   212 ////////////////////////////////////////////////////////////////////////////////
   213 //// Test Runner
   215 function run_test()
   216 {
   217   run_next_test();
   218 }

mercurial