Bug 745664 - Test. r=mkmelin, a=rkent
authorHiroyuki Ikezoe <hiikezoe@mozilla-japan.org>
Mon, 27 Apr 2015 11:48:00 +0200
changeset 25916 781dcceb04f76d0b37ba748ac5f618eca9ce766e
parent 25915 f70c422c7904fddaac456c2787b342bbca964401
child 25917 1bf43580b6f27bb960aa0e8455ac7d92394c92a2
push id1850
push userclokep@gmail.com
push dateWed, 08 Mar 2017 19:29:12 +0000
treeherdercomm-esr52@028df196b2d9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin, rkent
bugs745664
Bug 745664 - Test. r=mkmelin, a=rkent
mailnews/addrbook/test/unit/test_nsAbManager3.js
mailnews/addrbook/test/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/mailnews/addrbook/test/unit/test_nsAbManager3.js
@@ -0,0 +1,100 @@
+/* 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/. */
+
+/**
+ * Tests that an address book, once renamed, is not deleted when a sibling address book is deleted.
+ */
+
+let gAbListener;
+
+function abListener() {
+}
+
+abListener.prototype = {
+  mReceived: 0,
+  mDirectory: null,
+
+  onItemAdded: function(aParentItem, aItem) {
+    this.mReceived |= Ci.nsIAbListener.itemAdded;
+    this.mDirectory = aItem;
+  },
+
+  onItemRemoved: function(aParentItem, aItem) {
+    this.mReceived |= Ci.nsIAbListener.directoryRemoved;
+    this.mDirectory = aItem;
+  },
+
+  onItemPropertyChanged: function(aItem, aProperty, aOldValue, aNewValue) {
+    this.mReceived |= Ci.nsIAbListener.itemChanged;
+    this.mDirectory = aItem;
+  },
+
+  reset: function() {
+    this.mReceived = 0;
+    this.mDirectory = null;
+  }
+}
+
+function addDirectory(dirName) {
+  MailServices.ab.newAddressBook(dirName, "", kPABData.dirType);
+
+  do_check_eq(gAbListener.mReceived, Ci.nsIAbListener.itemAdded);
+
+  let newDirectory = gAbListener.mDirectory.QueryInterface(Ci.nsIAbDirectory);
+  do_check_eq(newDirectory.dirName, dirName);
+
+  gAbListener.reset();
+
+  return newDirectory;
+}
+
+function renameDirectory(directory, newName) {
+  directory.dirName = newName;
+
+  do_check_eq(gAbListener.mReceived, Ci.nsIAbListener.itemChanged);
+  do_check_eq(gAbListener.mDirectory, directory);
+
+  gAbListener.reset();
+}
+
+function removeDirectory(directory) {
+  MailServices.ab.deleteAddressBook(directory.URI);
+
+  do_check_eq(gAbListener.mReceived, Ci.nsIAbListener.directoryRemoved);
+  do_check_eq(gAbListener.mDirectory, directory.QueryInterface(Ci.nsIAbDirectory));
+
+  gAbListener.reset();
+}
+
+/**
+ * Create 4 addressbooks (directories). Rename the second one and delete
+ * the third one. Check if their names are still correct. (bug 745664)
+ */
+function run_test() {
+  gAbListener = new abListener();
+  MailServices.ab.addAddressBookListener(gAbListener, Ci.nsIAbListener.all);
+  do_register_cleanup(function() {
+    MailServices.ab.removeAddressBookListener(gAbListener);
+  });
+
+  let dirNames = ["testAb0", "testAb1", "testAb2", "testAb3"];
+  let directories = [];
+
+  for (let dirName of dirNames) {
+    directories.push(addDirectory(dirName));
+  }
+
+  dirNames[1] = "newTestAb1";
+  renameDirectory(directories[1], dirNames[1]);
+  for (let dir in dirNames) {
+    do_check_eq(dirNames[dir], directories[dir].dirName);
+  }
+  removeDirectory(directories[2]);
+  dirNames.splice(2, 1);
+  directories.splice(2, 1);
+
+  for (let dir in dirNames) {
+    do_check_eq(dirNames[dir], directories[dir].dirName);
+  }
+}
--- a/mailnews/addrbook/test/unit/xpcshell.ini
+++ b/mailnews/addrbook/test/unit/xpcshell.ini
@@ -22,10 +22,11 @@ support-files = data/*
 [test_nsAbAutoCompleteSearch2.js]
 [test_nsAbAutoCompleteSearch3.js]
 [test_nsAbAutoCompleteSearch4.js]
 [test_nsAbAutoCompleteSearch5.js]
 [test_nsAbAutoCompleteSearch6.js]
 [test_nsAbAutoCompleteSearch7.js]
 [test_nsAbManager1.js]
 [test_nsAbManager2.js]
+[test_nsAbManager3.js]
 [test_nsIAbCard.js]
 [test_uuid.js]