Add a test case for b/8321211

Emails like:
"Ex Ample" <example@example.com>
were displaying improperly.

Change-Id: Idd43814352e0c4455733adc13b89eb6fc1aca1bb
diff --git a/chips/src/com/android/ex/chips/RecipientEditTextView.java b/chips/src/com/android/ex/chips/RecipientEditTextView.java
index f53ae25..d5ee73a 100644
--- a/chips/src/com/android/ex/chips/RecipientEditTextView.java
+++ b/chips/src/com/android/ex/chips/RecipientEditTextView.java
@@ -157,7 +157,8 @@
 
     private TextView mMoreItem;
 
-    private final ArrayList<String> mPendingChips = new ArrayList<String>();
+    // VisibleForTesting
+    final ArrayList<String> mPendingChips = new ArrayList<String>();
 
     private Handler mHandler;
 
@@ -169,7 +170,8 @@
 
     private ListPopupWindow mAddressPopup;
 
-    private ArrayList<RecipientChip> mTemporaryRecipients;
+    // VisibleForTesting
+    ArrayList<RecipientChip> mTemporaryRecipients;
 
     private ArrayList<RecipientChip> mRemovedSpans;
 
@@ -903,7 +905,8 @@
      * Create a chip that represents just the email address of a recipient. At some later
      * point, this chip will be attached to a real contact entry, if one exists.
      */
-    private void createReplacementChip(int tokenStart, int tokenEnd, Editable editable,
+    // VisibleForTesting
+    void createReplacementChip(int tokenStart, int tokenEnd, Editable editable,
             boolean visible) {
         if (alreadyHasChip(tokenStart, tokenEnd)) {
             // There is already a chip present at this location.
@@ -911,9 +914,10 @@
             return;
         }
         String token = editable.toString().substring(tokenStart, tokenEnd);
-        int commitCharIndex = token.trim().lastIndexOf(COMMIT_CHAR_COMMA);
-        if (commitCharIndex == token.length() - 1) {
-            token = token.substring(0, token.length() - 1);
+        final String trimmedToken = token.trim();
+        int commitCharIndex = trimmedToken.lastIndexOf(COMMIT_CHAR_COMMA);
+        if (commitCharIndex == trimmedToken.length() - 1) {
+            token = trimmedToken.substring(0, trimmedToken.length() - 1);
         }
         RecipientEntry entry = createTokenizedEntry(token);
         if (entry != null) {
diff --git a/chips/tests/src/com/android/ex/chips/ChipsTest.java b/chips/tests/src/com/android/ex/chips/ChipsTest.java
index 3d020a3..be74627 100644
--- a/chips/tests/src/com/android/ex/chips/ChipsTest.java
+++ b/chips/tests/src/com/android/ex/chips/ChipsTest.java
@@ -929,4 +929,62 @@
             mMockRecips[i] = new VisibleRecipientChip(null, mMockEntries[i]);
         }
     }
+
+    /**
+     * <p>
+     * Ensure the original text is always accurate, regardless of the type of email. The original
+     * text is used to determine where to display the chip span. If this test fails, it means some
+     * text that should be turned into one whole chip may behave unexpectedly.
+     * </p>
+     * <p>
+     * For example, a bug was seen where
+     *
+     * <pre>
+     * "Android User" <android@example.com>
+     * </pre>
+     *
+     * was converted to
+     *
+     * <pre>
+     * Android User [android@example.com]
+     * </pre>
+     *
+     * where text inside [] is a chip.
+     * </p>
+     */
+    public void testCreateReplacementChipOriginalText() {
+        // Name in quotes + email address
+        testCreateReplacementChipOriginalText("\"Android User\" <android@example.com>,");
+        // Name in quotes + email address without brackets
+        testCreateReplacementChipOriginalText("\"Android User\" android@example.com,");
+        // Name in quotes
+        testCreateReplacementChipOriginalText("\"Android User\",");
+        // Name without quotes + email address
+        testCreateReplacementChipOriginalText("Android User <android@example.com>,");
+        // Name without quotes
+        testCreateReplacementChipOriginalText("Android User,");
+        // Email address
+        testCreateReplacementChipOriginalText("<android@example.com>,");
+        // Email address without brackets
+        testCreateReplacementChipOriginalText("android@example.com,");
+    }
+
+    private void testCreateReplacementChipOriginalText(final String email) {
+        // No trailing space
+        attemptCreateReplacementChipOriginalText(email.trim());
+        // Trailing space
+        attemptCreateReplacementChipOriginalText(email.trim() + " ");
+    }
+
+    private void attemptCreateReplacementChipOriginalText(final String email) {
+        final RecipientEditTextView view = new RecipientEditTextView(getContext(), null);
+
+        view.setText(email);
+        view.mPendingChips.add(email);
+
+        view.createReplacementChip(0, email.length(), view.getText(), true);
+        // The "original text" should be the email without the comma or space(s)
+        assertEquals(email.replaceAll(",\\s*$", ""),
+                view.mTemporaryRecipients.get(0).getOriginalText().toString().trim());
+    }
 }