ChipsTest.java revision 01162ce6739af1c9d9870f8e7e489f805c7e6794
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.ex.chips; 18 19import android.content.Context; 20import android.test.AndroidTestCase; 21import android.text.Editable; 22import android.text.SpannableStringBuilder; 23import android.text.util.Rfc822Tokenizer; 24 25import com.android.ex.chips.RecipientEditTextView; 26import com.android.ex.chips.RecipientEntry; 27 28 29public class ChipsTest extends AndroidTestCase { 30 private RecipientChip[] mMockRecips = new RecipientChip[5]; 31 32 private RecipientEntry[] mMockEntries = new RecipientEntry[5]; 33 34 private Rfc822Tokenizer mTokenizer; 35 36 private Editable mEditable; 37 38 class MockRecipientEditTextView extends RecipientEditTextView { 39 40 public MockRecipientEditTextView(Context context) { 41 super(context, null); 42 mTokenizer = new Rfc822Tokenizer(); 43 setTokenizer(mTokenizer); 44 for (int i = 0; i < mMockRecips.length; i++) { 45 mMockEntries[i] = RecipientEntry.constructGeneratedEntry("user", 46 "user@username.com"); 47 } 48 for (int i = 0; i < mMockRecips.length; i++) { 49 mMockRecips[i] = new RecipientChip(null, mMockEntries[i], i); 50 } 51 } 52 53 @Override 54 public RecipientChip[] getSortedRecipients() { 55 return mMockRecips; 56 } 57 58 @Override 59 public Editable getText() { 60 return mEditable; 61 } 62 63 @Override 64 public Editable getSpannable() { 65 return mEditable; 66 } 67 } 68 69 private MockRecipientEditTextView createViewForTesting() { 70 mEditable = new SpannableStringBuilder(); 71 MockRecipientEditTextView view = new MockRecipientEditTextView(getContext()); 72 return view; 73 } 74 75 public void testCreateDisplayText() { 76 RecipientEditTextView view = createViewForTesting(); 77 RecipientEntry entry = RecipientEntry.constructGeneratedEntry("User Name, Jr", 78 "user@username.com"); 79 String test = view.createDisplayText(entry); 80 assertEquals("Expected a properly formatted RFC email address", 81 "\"User Name, Jr\" <user@username.com>, ", test); 82 83 RecipientEntry alreadyFormatted = RecipientEntry.constructFakeEntry("user@username.com, "); 84 test = view.createDisplayText(alreadyFormatted); 85 assertEquals("Expected a properly formatted RFC email address", "<user@username.com>, ", 86 test); 87 88 RecipientEntry alreadyFormattedNoSpace = RecipientEntry 89 .constructFakeEntry("user@username.com,"); 90 test = view.createDisplayText(alreadyFormattedNoSpace); 91 assertEquals("Expected a properly formatted RFC email address", "<user@username.com>, ", 92 test); 93 94 RecipientEntry alreadyNamed = RecipientEntry.constructGeneratedEntry("User Name", 95 "\"User Name, Jr\" <user@username.com>"); 96 test = view.createDisplayText(alreadyNamed); 97 assertEquals( 98 "Expected address that used the name not the excess address name", 99 "User Name <user@username.com>, ", test); 100 } 101 102 public void testSanitizeBetween() { 103 MockRecipientEditTextView view = createViewForTesting(); 104 String first = (String) mTokenizer.terminateToken("FIRST"); 105 String second = (String) mTokenizer.terminateToken("SECOND"); 106 String extra = "EXTRA"; 107 mEditable.append(first + extra + second); 108 int firstStart = mEditable.toString().indexOf(first); 109 int firstEnd = firstStart + first.length(); 110 int secondStart = mEditable.toString().indexOf(second); 111 int secondEnd = secondStart + second.length(); 112 mEditable.setSpan(mMockRecips[mMockRecips.length - 2], firstStart, firstEnd, 0); 113 mEditable.setSpan(mMockRecips[mMockRecips.length - 1], secondStart, secondEnd, 0); 114 view.sanitizeBetween(); 115 String editableString = mEditable.toString(); 116 assertEquals(editableString.indexOf(extra), -1); 117 assertEquals(editableString.indexOf(first), firstStart); 118 assertEquals(editableString.indexOf(second), secondStart - extra.length()); 119 assertEquals(editableString, (first + second)); 120 121 mEditable = new SpannableStringBuilder(); 122 mEditable.append(first); 123 firstStart = mEditable.toString().indexOf(first); 124 firstEnd = firstStart + first.length(); 125 mEditable.setSpan(mMockRecips[mMockRecips.length - 1], firstStart, firstEnd, 0); 126 view.sanitizeBetween(); 127 assertEquals(mEditable.toString(), first); 128 } 129} 130