Bug 673480 - Cannot delete Mailing Lists; r+a=Standard8
authorMike Conley <mconley@mozilla.com>
Fri, 22 Jul 2011 16:13:49 -0400
changeset 8370 f0dd4c316c5a24b47f87286cacac212c3d421542
parent 8369 9c7f4200754d15645795987dc45345d616be9643
child 8371 a166416526d2dfeab16b40918e409f2f26f2946d
push idunknown
push userunknown
push dateunknown
bugs673480
Bug 673480 - Cannot delete Mailing Lists; r+a=Standard8
mail/test/mozmill/addrbook/test-address-book.js
mail/test/mozmill/shared-modules/test-address-book-helpers.js
mailnews/addrbook/src/nsAbManager.cpp
--- a/mail/test/mozmill/addrbook/test-address-book.js
+++ b/mail/test/mozmill/addrbook/test-address-book.js
@@ -43,16 +43,17 @@ var MODULE_NAME = 'test-address-book';
 
 var RELATIVE_ROOT = '../shared-modules';
 var MODULE_REQUIRES = ['address-book-helpers', 'folder-display-helpers'];
 
 const kPromptServiceUUID = "{6cc9c9fe-bc0b-432b-a410-253ef8bcc699}";
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource:///modules/Services.jsm");
+Cu.import("resource:///modules/mailServices.js");
 
 let abController = null;
 var addrBook1, addrBook2, addrBook3, addrBook4;
 var mListA, mListB, mListC, mListD, mListE;
 
 var gMockPromptService = {
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptService]),
   _will_return: null,
@@ -321,8 +322,57 @@ function test_deleting_contacts_causes_c
   // The contact should have been deleted.
   assert_equals(abController.window.gAbView.rowCount,
                 totalEntries - toDelete.length);
 
   Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
             .unregisterFactory(Components.ID(kPromptServiceUUID),
                                gMockPromptServiceFactory);
 }
+
+/* Tests that attempting to delete a mailing list causes a
+ * confirmation dialog to be brought up, and that deletion
+ * actually works if the user clicks "OK".
+ */
+function test_deleting_mailing_lists() {
+
+  Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
+            .registerFactory(Components.ID(kPromptServiceUUID),
+                             "Mock Prompt Service",
+                             "@mozilla.org/embedcomp/prompt-service;1",
+                             gMockPromptServiceFactory);
+
+  // Create a new mailing list, and add it to one of our
+  // address books
+  let newList = create_mailing_list("Delete Me!");
+  let addedList = addrBook1.addMailList(newList);
+  let mlURI = addedList.URI;
+
+  // Make sure it got added.
+  assert_true(addrBook1.hasDirectory(addedList));
+
+  // Let's click "cancel" on the confirm dialog box
+  // first.
+  gMockPromptService._return(false);
+
+  abController.window.AbDeleteDirectory(addedList.URI);
+
+  // Test that the confirmation dialog was brought up.
+  assert_true(gMockPromptService._did_confirm);
+
+  // Ensure that the mailing list was not removed.
+  assert_true(addrBook1.hasDirectory(addedList));
+
+  // This time, let's click "OK" on the confirm dialog box
+  gMockPromptService._return(true);
+
+  abController.window.AbDeleteDirectory(addedList.URI);
+
+  // Test that the confirmation dialog was brought up.
+  assert_true(gMockPromptService._did_confirm);
+
+  // Ensure that the mailing list was removed.
+  assert_false(addrBook1.hasDirectory(addedList));
+
+  Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
+            .unregisterFactory(Components.ID(kPromptServiceUUID),
+                               gMockPromptServiceFactory);
+}
--- a/mail/test/mozmill/shared-modules/test-address-book-helpers.js
+++ b/mail/test/mozmill/shared-modules/test-address-book-helpers.js
@@ -276,31 +276,31 @@ function load_contacts_into_mailing_list
 {
   for each (contact_info in aContacts) {
     let contact = create_contact(contact_info.email,
                                  contact_info.displayName, true);
     aMailingList.addressLists.appendElement(contact, false);
   }
 }
 
-/* given some address book, return the row index for that address book
- * in the tree view.  throws an error if it cannot find the address book.
- * @param aaddrbook an address book to search for
+/* Given some address book, return the row index for that address book
+ * in the tree view.  Throws an error if it cannot find the address book.
+ * @param aAddrBook an address book to search for
  * @return the row index for that address book
  */
-function get_address_book_tree_view_index(aaddrbook)
+function get_address_book_tree_view_index(aAddrBook)
 {
   let addrbooks = abController.window.gDirectoryTreeView._rowMap;
   for (let i = 0; i < addrbooks.length; i++) {
-    if (addrbooks[i]._directory == aaddrbook) {
+    if (addrbooks[i]._directory == aAddrBook) {
       return i;
     }
   }
-  throw error("could not find the index for the address book named "
-              + aaddrbook.dirname);
+  throw Error("Could not find the index for the address book named "
+              + aAddrBook.dirName);
 }
 
 /* Given some contact, return the row index for that contact in the
  * address book view.  Assumes that the address book that the contact
  * belongs to is currently selected.  Throws an error if it cannot
  * find the contact.
  * @param aContact a contact to search for
  * @return the row index for that contact
@@ -393,17 +393,20 @@ function set_address_books_collapsed(aAd
  * @param aIndex the row index of the target address book
  * @return the displayed name of the address book
  */
 function get_name_of_address_book_element_at(aIndex)
 {
   return abController.window.gDirectoryTreeView.getCellText(aIndex, 0);
 }
 
-/* Selects a given address book in the tree view.
+/* Selects a given address book in the tree view.  Assumes that
+ * the parent of aAddrBook in the treeView is not collapsed.
+ * Since mailing lists are technically address books, this will
+ * work for mailing lists too.
  * @param aAddrBook an address book to select
  */
 function select_address_book(aAddrBook)
 {
   let aIndex = get_address_book_tree_view_index(aAddrBook);
   abController.window.gDirectoryTreeView.selection.select(aIndex);
 }
 
--- a/mailnews/addrbook/src/nsAbManager.cpp
+++ b/mailnews/addrbook/src/nsAbManager.cpp
@@ -368,17 +368,39 @@ NS_IMETHODIMP nsAbManager::DeleteAddress
       NS_ENSURE_SUCCESS(rv, rv);
 
       mAbStore.Remove(childURI);
     }
   }
 
   mAbStore.Remove(aURI);
 
-  return rootDirectory->DeleteDirectory(directory);
+  PRBool isMailList;
+  rv = directory->GetIsMailList(&isMailList);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  if (!isMailList)
+    // If we're not a mailing list, then our parent
+    // must be the root address book directory.
+    return rootDirectory->DeleteDirectory(directory);
+
+  nsCString parentUri;
+  parentUri.Append(aURI);
+  PRInt32 pos = parentUri.RFindChar('/');
+
+  // If we didn't find a /, we're in trouble.
+  if (pos == -1)
+    return NS_ERROR_FAILURE;
+
+  parentUri = StringHead(parentUri, pos);
+  nsCOMPtr<nsIAbDirectory> parentDirectory;
+  rv = GetDirectory(parentUri, getter_AddRefs(parentDirectory));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  return parentDirectory->DeleteDirectory(directory);
 }
 
 NS_IMETHODIMP nsAbManager::AddAddressBookListener(nsIAbListener *aListener,
                                                   abListenerNotifyFlagValue aNotifyFlags)
 {
   NS_ENSURE_ARG_POINTER(aListener);
 
   abListener newListener(aListener, aNotifyFlags);