Bug 1633607 - Add nsIAbManager.getDirectoryFromUID. r=mkmelin
authorGeoff Lankow <geoff@darktrojan.net>
Fri, 24 Apr 2020 21:02:44 +1200
changeset 38068 370823b8dff7d6ea7eb19587aa92c709443df5c1
parent 38067 ffa711650cb168714e3e6179267ab8656e8d47b5
child 38069 69e91e0bc92ce7e94f97a3069e2949bbca3c840f
push id2595
push userclokep@gmail.com
push dateMon, 04 May 2020 19:02:04 +0000
treeherdercomm-beta@f53913797371 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin
bugs1633607
Bug 1633607 - Add nsIAbManager.getDirectoryFromUID. r=mkmelin
mailnews/addrbook/jsaddrbook/AddrBookManager.jsm
mailnews/addrbook/public/nsIAbManager.idl
mailnews/addrbook/test/unit/head.js
mailnews/addrbook/test/unit/test_delete_book.js
mailnews/addrbook/test/unit/test_nsAbManager5.js
mailnews/addrbook/test/unit/xpcshell.ini
--- a/mailnews/addrbook/jsaddrbook/AddrBookManager.jsm
+++ b/mailnews/addrbook/jsaddrbook/AddrBookManager.jsm
@@ -205,16 +205,25 @@ AddrBookManager.prototype = {
     ensureInitialized();
     for (let dir of store.values()) {
       if (dir.dirPrefId == dirPrefId) {
         return dir;
       }
     }
     return null;
   },
+  getDirectoryFromUID(uid) {
+    ensureInitialized();
+    for (let dir of store.values()) {
+      if (dir.UID == uid) {
+        return dir;
+      }
+    }
+    return null;
+  },
   newAddressBook(dirName, uri, type, prefName) {
     function ensureUniquePrefName() {
       let leafName = dirName.replace(/\W/g, "");
       if (!leafName) {
         leafName = "_nonascii";
       }
 
       let existingNames = Array.from(store.values(), dir => dir.dirPrefId);
--- a/mailnews/addrbook/public/nsIAbManager.idl
+++ b/mailnews/addrbook/public/nsIAbManager.idl
@@ -41,16 +41,24 @@ interface nsIAbManager : nsISupports
    * Returns the directory that has the supplied dirPrefId.
    *
    * @param aDirPrefId  The dirPrefId of the directory.
    * @return            The found AB directory.
    */
   nsIAbDirectory getDirectoryFromId(in ACString aDirPrefId);
 
   /**
+   * Returns the directory that has the supplied UID.
+   *
+   * @param aUID        The UID of the directory.
+   * @return            The found AB directory.
+   */
+  nsIAbDirectory getDirectoryFromUID(in ACString aUID);
+
+  /**
    * Creates a new address book.
    *
    * @param  aDirName   The description of the address book.
    * @param  aURI       The URI for the address book. This is specific to each
    *                    type of address book.
    * @param  aType      The type of the address book (see nsDirPrefs.h)
    * @param  aPrefName  Overrides the default of ldap_2.servers.<aDirName>
    *                    (note that the caller must ensure its uniqueness).
--- a/mailnews/addrbook/test/unit/head.js
+++ b/mailnews/addrbook/test/unit/head.js
@@ -11,8 +11,23 @@ do_get_profile();
 
 // Import the required setup scripts.
 /* import-globals-from ../../../test/resources/abSetup.js */
 load("../../../resources/abSetup.js");
 
 registerCleanupFunction(function() {
   load("../../../resources/mailShutdown.js");
 });
+
+function promiseDirectoryRemoved() {
+  return new Promise(resolve => {
+    let observer = {
+      onItemRemoved() {
+        MailServices.ab.removeAddressBookListener(this);
+        resolve();
+      },
+    };
+    MailServices.ab.addAddressBookListener(
+      observer,
+      Ci.nsIAbListener.directoryRemoved
+    );
+  });
+}
--- a/mailnews/addrbook/test/unit/test_delete_book.js
+++ b/mailnews/addrbook/test/unit/test_delete_book.js
@@ -8,31 +8,16 @@ var { MailServices } = ChromeUtils.impor
   "resource:///modules/MailServices.jsm"
 );
 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
 
 function getExistingDirectories() {
   return Array.from(MailServices.ab.directories, d => d.dirPrefId);
 }
 
-function promiseDirectoryRemoved() {
-  return new Promise(resolve => {
-    let observer = {
-      onItemRemoved() {
-        MailServices.ab.removeAddressBookListener(this);
-        resolve();
-      },
-    };
-    MailServices.ab.addAddressBookListener(
-      observer,
-      Ci.nsIAbListener.directoryRemoved
-    );
-  });
-}
-
 add_task(async function clearPref() {
   Assert.deepEqual(getExistingDirectories(), [
     "ldap_2.servers.pab",
     "ldap_2.servers.history",
   ]);
   equal(
     Services.prefs.getStringPref("mail.collect_addressbook"),
     "jsaddrbook://history.sqlite"
new file mode 100644
--- /dev/null
+++ b/mailnews/addrbook/test/unit/test_nsAbManager5.js
@@ -0,0 +1,45 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+add_task(async function createAddressBook() {
+  Assert.ok(!MailServices.ab.getDirectoryFromUID("nonsense"));
+
+  let pabFromURI = MailServices.ab.getDirectory(kPABData.URI);
+  let pabFromId = MailServices.ab.getDirectoryFromId(kPABData.dirPrefID);
+  let pabFromUID = MailServices.ab.getDirectoryFromUID(pabFromURI.UID);
+
+  Assert.equal(pabFromId, pabFromURI);
+  Assert.equal(pabFromUID, pabFromURI);
+
+  let historyFromURI = MailServices.ab.getDirectory(kCABData.URI);
+  let historyFromId = MailServices.ab.getDirectoryFromId(kCABData.dirPrefID);
+  let historyFromUID = MailServices.ab.getDirectoryFromUID(historyFromURI.UID);
+
+  Assert.equal(historyFromId, historyFromURI);
+  Assert.equal(historyFromUID, historyFromURI);
+  Assert.notEqual(historyFromUID, pabFromUID);
+
+  let newPrefId = MailServices.ab.newAddressBook(
+    "new book",
+    "",
+    kPABData.dirType
+  );
+  let newFromId = MailServices.ab.getDirectoryFromId(newPrefId);
+
+  let newFromURI = MailServices.ab.getDirectory(newFromId.URI);
+  let newFromUID = MailServices.ab.getDirectoryFromUID(newFromId.UID);
+
+  Assert.equal(newFromId, newFromURI);
+  Assert.equal(newFromUID, newFromURI);
+  Assert.notEqual(newFromUID, pabFromUID);
+  Assert.notEqual(newFromUID, historyFromUID);
+
+  let deletePromise = promiseDirectoryRemoved();
+  MailServices.ab.deleteAddressBook(newFromId.URI);
+  await deletePromise;
+
+  Assert.ok(!MailServices.ab.getDirectoryFromUID(newFromId.UID));
+});
--- a/mailnews/addrbook/test/unit/xpcshell.ini
+++ b/mailnews/addrbook/test/unit/xpcshell.ini
@@ -31,9 +31,10 @@ skip-if = debug # Fails for unknown reas
 [test_nsAbAutoCompleteSearch4.js]
 [test_nsAbAutoCompleteSearch5.js]
 [test_nsAbAutoCompleteSearch6.js]
 [test_nsAbAutoCompleteSearch7.js]
 [test_nsAbManager1.js]
 [test_nsAbManager2.js]
 [test_nsAbManager3.js]
 [test_nsAbManager4.js]
+[test_nsAbManager5.js]
 [test_nsIAbCard.js]