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.

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

mercurial