Bug 1285063 - Part 1: Add a helper to XPCOMUtils to iterate over entries in a category. r=kmag
authorMatthew Wein <mwein@mozilla.com>
Mon, 11 Jul 2016 17:33:06 -0700
changeset 346940 e3a7713a67355531701df9c0baab805c539aa26c
parent 346939 0d58d0626631c57f3312331bd8ef2a1469e0a590
child 346941 e9ca8dc4b42e341a9ecc451f9033ce2947418b41
push id6389
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:38:22 +0000
treeherdermozilla-beta@01d67bfe6c81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1285063
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1285063 - Part 1: Add a helper to XPCOMUtils to iterate over entries in a category. r=kmag MozReview-Commit-ID: 3mjrPrRuLej
js/xpconnect/loader/XPCOMUtils.jsm
js/xpconnect/tests/unit/test_xpcomutils.js
xpcom/ds/nsINIProcessor.js
--- a/js/xpconnect/loader/XPCOMUtils.jsm
+++ b/js/xpconnect/loader/XPCOMUtils.jsm
@@ -336,46 +336,50 @@ this.XPCOMUtils = {
       Services.prefs.addObserver(aPreference, observer, true);
 
       defineGetter(lazyGetter);
       return lazyGetter();
     });
   },
 
   /**
-   * Convenience access to category manager
-   */
-  get categoryManager() {
-    return Components.classes["@mozilla.org/categorymanager;1"]
-           .getService(Ci.nsICategoryManager);
-  },
-
-  /**
    * Helper which iterates over a nsISimpleEnumerator.
    * @param e The nsISimpleEnumerator to iterate over.
    * @param i The expected interface for each element.
    */
-  IterSimpleEnumerator: function XPCU_IterSimpleEnumerator(e, i)
+  IterSimpleEnumerator: function* XPCU_IterSimpleEnumerator(e, i)
   {
     while (e.hasMoreElements())
       yield e.getNext().QueryInterface(i);
   },
 
   /**
    * Helper which iterates over a string enumerator.
    * @param e The string enumerator (nsIUTF8StringEnumerator or
    *          nsIStringEnumerator) over which to iterate.
    */
-  IterStringEnumerator: function XPCU_IterStringEnumerator(e)
+  IterStringEnumerator: function* XPCU_IterStringEnumerator(e)
   {
     while (e.hasMore())
       yield e.getNext();
   },
 
   /**
+   * Helper which iterates over the entries in a category.
+   * @param aCategory The name of the category over which to iterate.
+   */
+  enumerateCategoryEntries: function* XPCOMUtils_enumerateCategoryEntries(aCategory)
+  {
+    let category = this.categoryManager.enumerateCategory(aCategory);
+    for (let entry of this.IterSimpleEnumerator(category, Ci.nsISupportsCString)) {
+      yield [entry.data, this.categoryManager.getCategoryEntry(aCategory, entry.data)];
+    }
+  },
+
+  /**
    * Returns an nsIFactory for |component|.
    */
   _getFactory: function XPCOMUtils__getFactory(component) {
     var factory = component.prototype._xpcom_factory;
     if (!factory) {
       factory = {
         createInstance: function(outer, iid) {
           if (outer)
@@ -439,16 +443,20 @@ this.XPCOMUtils = {
   },
 };
 
 XPCOMUtils.defineLazyModuleGetter(this, "Preferences",
                                   "resource://gre/modules/Preferences.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "Services",
                                   "resource://gre/modules/Services.jsm");
 
+XPCOMUtils.defineLazyServiceGetter(XPCOMUtils, "categoryManager",
+                                   "@mozilla.org/categorymanager;1",
+                                   "nsICategoryManager");
+
 /**
  * Helper for XPCOMUtils.generateQI to avoid leaks - see bug 381651#c1
  */
 function makeQI(interfaceNames) {
   return function XPCOMUtils_QueryInterface(iid) {
     if (iid.equals(Ci.nsISupports))
       return this;
     if (iid.equals(Ci.nsIClassInfo) && "classInfo" in this)
--- a/js/xpconnect/tests/unit/test_xpcomutils.js
+++ b/js/xpconnect/tests/unit/test_xpcomutils.js
@@ -183,32 +183,30 @@ add_test(function test_categoryRegistrat
     "XULAppInfo",
     XULAPPINFO_CONTRACTID,
     XULAppInfoFactory
   );
 
   // Load test components.
   do_load_manifest("CatRegistrationComponents.manifest");
 
-  const EXPECTED_ENTRIES = ["CatAppRegisteredComponent",
-                            "CatRegisteredComponent"];
+  const EXPECTED_ENTRIES = new Map([
+    ["CatRegisteredComponent", "@unit.test.com/cat-registered-component;1"],
+    ["CatAppRegisteredComponent", "@unit.test.com/cat-app-registered-component;1"],
+  ]);
 
-  // Check who is registered in "test-cat" category.
-  let foundEntriesCount = 0;
-  let catMan = Cc["@mozilla.org/categorymanager;1"].
-               getService(Ci.nsICategoryManager);
-  let entries = catMan.enumerateCategory(CATEGORY_NAME);
-  while (entries.hasMoreElements()) {
-    foundEntriesCount++;
-    let entry = entries.getNext().QueryInterface(Ci.nsISupportsCString).data;
-    print("Check the found category entry (" + entry + ") is expected.");  
-    do_check_true(EXPECTED_ENTRIES.indexOf(entry) != -1);
+  // Verify the correct entries are registered in the "test-cat" category.
+  for (let [name, value] of XPCOMUtils.enumerateCategoryEntries(CATEGORY_NAME)) {
+    print("Verify that the name/value pair exists in the expected entries.");
+    ok(EXPECTED_ENTRIES.has(name));
+    do_check_eq(EXPECTED_ENTRIES.get(name), value);
+    EXPECTED_ENTRIES.delete(name);
   }
-  print("Check there are no more or less than expected entries.");
-  do_check_eq(foundEntriesCount, EXPECTED_ENTRIES.length);
+  print("Check that all of the expected entries have been deleted.");
+  do_check_eq(EXPECTED_ENTRIES.size, 0);
   run_next_test();
 });
 
 add_test(function test_generateSingletonFactory()
 {
   const XPCCOMPONENT_CONTRACTID = "@mozilla.org/singletonComponentTest;1";
   const XPCCOMPONENT_CID = Components.ID("{31031c36-5e29-4dd9-9045-333a5d719a3e}");
 
--- a/xpcom/ds/nsINIProcessor.js
+++ b/xpcom/ds/nsINIProcessor.js
@@ -70,19 +70,19 @@ INIProcessor.prototype = {
      */
     _readFile : function() {
         // If file doesn't exist, there's nothing to do.
         if (!this._iniFile.exists() || 0 == this._iniFile.fileSize)
             return;
 
         let iniParser = Cc["@mozilla.org/xpcom/ini-parser-factory;1"]
             .getService(Ci.nsIINIParserFactory).createINIParser(this._iniFile);
-        for (let section in XPCOMUtils.IterStringEnumerator(iniParser.getSections())) {
+        for (let section of XPCOMUtils.IterStringEnumerator(iniParser.getSections())) {
             this._iniData[section] = {};
-            for (let key in XPCOMUtils.IterStringEnumerator(iniParser.getKeys(section))) {
+            for (let key of XPCOMUtils.IterStringEnumerator(iniParser.getKeys(section))) {
                 this._iniData[section][key] = iniParser.getString(section, key);
             }
         }
     },
 
     // nsIINIParser
 
     getSections : function() {