Bug 543088 Autocomplete - primary email address no longer appears before additional email address (now sorted alphabetically). r+sr-bienvenu
authorMark Banner <bugzilla@standard8.plus.com>
Wed, 17 Mar 2010 07:26:22 +0000
changeset 5192 a65be94303220eeca31da68a11c8af3dbf989fde
parent 5191 79666fdca182c3bb90b8627b83fcd83746b90030
child 5193 024c73a5eefd43c52c40591679859f73bb96ec84
push idunknown
push userunknown
push dateunknown
bugs543088
Bug 543088 Autocomplete - primary email address no longer appears before additional email address (now sorted alphabetically). r+sr-bienvenu
mailnews/addrbook/src/nsAbAutoCompleteSearch.js
mailnews/addrbook/test/unit/test_nsAbAutoCompleteSearch4.js
--- a/mailnews/addrbook/src/nsAbAutoCompleteSearch.js
+++ b/mailnews/addrbook/src/nsAbAutoCompleteSearch.js
@@ -168,25 +168,25 @@ nsAbAutoCompleteSearch.prototype = {
     var commentColumn = this._commentColumn == 1 ? directory.dirName : "";
 
     // Now iterate through all the cards.
     while (childCards.hasMoreElements()) {
       var card = childCards.getNext();
 
       if (card instanceof Components.interfaces.nsIAbCard) {
         if (card.isMailList)
-          this._addToResult(commentColumn, directory, card, "", result);
+          this._addToResult(commentColumn, directory, card, "", true, result);
         else {
           let email = card.primaryEmail;
           if (email)
-            this._addToResult(commentColumn, directory, card, email, result);
+            this._addToResult(commentColumn, directory, card, email, true, result);
 
           email = card.getProperty("SecondEmail", "");
           if (email)
-            this._addToResult(commentColumn, directory, card, email, result);
+            this._addToResult(commentColumn, directory, card, email, false, result);
         }
       }
     }
   },
 
   /**
    * Searches for cards in a directory matching against email addresses only.
    * When matches are found it will add them to the results.
@@ -207,27 +207,27 @@ nsAbAutoCompleteSearch.prototype = {
     let commentColumn = this._commentColumn == 1 ? directory.dirName : "";
 
     // Now iterate through all the cards.
     while (childCards.hasMoreElements()) {
       let card = childCards.getNext();
 
       if (card instanceof Components.interfaces.nsIAbCard) {
         if (card.isMailList)
-          this._addToResult(commentColumn, directory, card, "", result);
+          this._addToResult(commentColumn, directory, card, "", false, result);
         else {
           let email = card.primaryEmail;
           if (email && email.toLocaleLowerCase()
                             .lastIndexOf(fullString, 0) == 0)
-            this._addToResult(commentColumn, directory, card, email, result);
+            this._addToResult(commentColumn, directory, card, email, true, result);
 
           email = card.getProperty("SecondEmail", "");
           if (email && email.toLocaleLowerCase()
                             .lastIndexOf(fullString, 0) == 0)
-            this._addToResult(commentColumn, directory, card, email, result);
+            this._addToResult(commentColumn, directory, card, email, false, result);
         }
       }
     }
   },
 
   /**
    * Checks a card against the search parameters to see if it should be
    * included in the result.
@@ -311,20 +311,22 @@ nsAbAutoCompleteSearch.prototype = {
    * order the results by popularity.
    *
    * @param commentColumn  The text to be displayed in the comment column
    *                       (if any).
    * @param directory      The directory that the card is in.
    * @param card           The card being added to the results.
    * @param emailToUse     The email address from the card that should be used
    *                       for this result.
+   * @param isPrimaryEmail Is the emailToUse the primary email? Set to true if
+   *                       it is the case. For mailing lists set it to true.
    * @param result         The result to add the new entry to.
    */
   _addToResult: function _addToResult(commentColumn, directory, card,
-                                      emailToUse, result) {
+                                      emailToUse, isPrimaryEmail, result) {
     var emailAddress =
       this._parser.makeFullAddress(card.displayName,
                                    card.isMailList ?
                                    card.getProperty("Notes", "") || card.displayName :
                                    emailToUse);
 
     // The old code used to try it manually. I think if the parser can't work
     // out the address from what we've supplied, then its busted and we're not
@@ -345,17 +347,19 @@ nsAbAutoCompleteSearch.prototype = {
            cardPopularityIndex <
            result._searchResults[insertPosition].popularity)
       ++insertPosition;
 
     // Next sort on full address
     while (insertPosition < result._searchResults.length &&
            cardPopularityIndex ==
            result._searchResults[insertPosition].popularity &&
-           emailAddress > result._searchResults[insertPosition].value)
+           ((card == result._searchResults[insertPosition].card &&
+             !isPrimaryEmail) ||
+            emailAddress > result._searchResults[insertPosition].value))
       ++insertPosition;
 
     result._searchResults.splice(insertPosition, 0, {
       value: emailAddress,
       comment: commentColumn,
       card: card,
       emailToUse: emailToUse,
       popularity: cardPopularityIndex
--- a/mailnews/addrbook/test/unit/test_nsAbAutoCompleteSearch4.js
+++ b/mailnews/addrbook/test/unit/test_nsAbAutoCompleteSearch4.js
@@ -1,49 +1,80 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /*
  * Fourth Test suite for nsAbAutoCompleteSearch - test for second email address.
  */
 
 const ACR = Components.interfaces.nsIAutoCompleteResult;
 
 const cards = [
-  { email: "primary@invalid.com", secondEmail: "second@invalid.com",
+  // Basic tests for primary and secondary emails.
+  { email: "primary@test.invalid", secondEmail: "second@test.invalid",
     firstName: "" },
-  { email: "test1@invalid.com", secondEmail: "test2@invalid.com",
+  { email: "test1@test.invalid", secondEmail: "test2@test.invalid",
     firstName: "firstName" },
-  { email: "bar1@invalid.com", secondEmail: "bar2@invalid.com",
+  { email: "bar1@test.invalid", secondEmail: "bar2@test.invalid",
     firstName: "sweet" },
-  { email: "boo1@invalid.com", secondEmail: "boo2@invalid.com",
+  { email: "boo1@test.invalid", secondEmail: "boo2@test.invalid",
     firstName: "sample" },
-  { email: "name@invalid.com", secondEmail: "thename@invalid.com",
-    firstName: "thename" }
+  { email: "name@test.invalid", secondEmail: "thename@test.invalid",
+    firstName: "thename" },
+  // Test to check correct sorting of primary and secondary emails.
+  { email: "foo_b@test.invalid", secondEmail: "foo_a@test.invalid",
+    displayName: "sortbasic" },
+  { email: "d@test.invalid", secondEmail: "e@test.invalid",
+    displayName: "testsort" },
+  { email: "c@test.invalid", secondEmail: "a@test.invalid",
+    displayName: "testsort" },
+  // "2testsort" does the same as "testsort" but turns the cards around to
+  // ensure the order is always consistent.
+  { email: "c@test.invalid", secondEmail: "a@test.invalid",
+    displayName: "2testsort" },
+  { email: "d@test.invalid", secondEmail: "e@test.invalid",
+    displayName: "2testsort" },
+  { email: "g@test.invalid", secondEmail: "f@test.invalid",
+    displayName: "3testsort", popularityIndex: 3 },
+  { email: "j@test.invalid", secondEmail: "h@test.invalid",
+    displayName: "3testsort", popularityIndex: 5 }
 ];
 
 // These are for the initial search
-const searches = [ "primary", "second", "firstName", "thename" ];
+const searches = [ "primary", "second", "firstName", "thename", "sortbasic",
+                   "testsort", "2testsort", "3testsort" ];
 
-const expectedResults = [ [ "primary@invalid.com" ],
-                          [ "second@invalid.com" ],
-                          [ "test1@invalid.com",
-                            "test2@invalid.com" ],
-                          [ "name@invalid.com",
-                            "thename@invalid.com" ],
-                          ];
+const expectedResults = [ [ "primary@test.invalid" ],
+                          [ "second@test.invalid" ],
+                          [ "test1@test.invalid",
+                            "test2@test.invalid" ],
+                          [ "name@test.invalid",
+                            "thename@test.invalid" ],
+                          [ "sortbasic <foo_b@test.invalid>",
+                            "sortbasic <foo_a@test.invalid>" ],
+                          [ "testsort <c@test.invalid>",
+                            "testsort <a@test.invalid>",
+                            "testsort <d@test.invalid>",
+                            "testsort <e@test.invalid>" ],
+                          [ "2testsort <c@test.invalid>",
+                            "2testsort <a@test.invalid>",
+                            "2testsort <d@test.invalid>",
+                            "2testsort <e@test.invalid>" ],
+                          [ "3testsort <j@test.invalid>",
+                            "3testsort <h@test.invalid>",
+                            "3testsort <g@test.invalid>",
+                            "3testsort <f@test.invalid>" ] ];
 
 // These are for subsequent searches - reducing the number of results.
 const reductionSearches = [ "b", "bo", "boo2" ];
 
-const reductionExpectedResults = [ [ "bar1@invalid.com",
-                                     "bar2@invalid.com",
-                                     "boo1@invalid.com",
-                                     "boo2@invalid.com" ],
-                                   [ "boo1@invalid.com",
-                                     "boo2@invalid.com" ],
-                                   [ "boo2@invalid.com" ] ];
+const reductionExpectedResults = [ [ "bar1@test.invalid",
+                                     "bar2@test.invalid",
+                                     "boo1@test.invalid",
+                                     "boo2@test.invalid" ],
+                                   [ "boo1@test.invalid",
+                                     "boo2@test.invalid" ],
+                                   [ "boo2@test.invalid" ] ];
 
 function acObserver() {}
 
 acObserver.prototype = {
   _search: null,
   _result: null,
 
   onSearchResult: function (aSearch, aResult) {