diff options
author | Mindy Pereira <mindyp@google.com> | 2011-10-12 13:18:04 -0700 |
---|---|---|
committer | Mindy Pereira <mindyp@google.com> | 2011-10-13 15:31:06 -0700 |
commit | 49ce9866ae314b3513e17008f107ceded23f9cf0 (patch) | |
tree | 21a32b6ceb874ed7dd760debbae738991ebd1b88 /chips/tests | |
parent | 04d53a95a237538cb7c9a2b4c5091d90e5c09fb7 (diff) | |
download | android_frameworks_ex-49ce9866ae314b3513e17008f107ceded23f9cf0.tar.gz android_frameworks_ex-49ce9866ae314b3513e17008f107ceded23f9cf0.tar.bz2 android_frameworks_ex-49ce9866ae314b3513e17008f107ceded23f9cf0.zip |
Update pasting to handle more cases.
Fixes bug:5026774 pasting in email addresses changes
Change-Id: Id16e7470eb10a998fdb5efde06cb4449bb9d49e7
Diffstat (limited to 'chips/tests')
-rw-r--r-- | chips/tests/src/com/android/ex/chips/ChipsTest.java | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/chips/tests/src/com/android/ex/chips/ChipsTest.java b/chips/tests/src/com/android/ex/chips/ChipsTest.java index eeeed0b..41b9eab 100644 --- a/chips/tests/src/com/android/ex/chips/ChipsTest.java +++ b/chips/tests/src/com/android/ex/chips/ChipsTest.java @@ -69,9 +69,20 @@ public class ChipsTest extends AndroidTestCase { return 48; } + @Override public Drawable getChipBackground(RecipientEntry contact) { return createChipBackground(); } + + @Override + public int length() { + return mEditable != null ? mEditable.length() : 0; + } + + @Override + public String toString() { + return mEditable != null ? mEditable.toString() : ""; + } } private MockRecipientEditTextView createViewForTesting() { @@ -599,6 +610,180 @@ public class ChipsTest extends AndroidTestCase { assertEquals((String) spans[0].getDisplay(), "replacement"); } + public void testHandlePaste() { + // Start with an empty edit field. + // Add an address; the text should be left as is. + MockRecipientEditTextView view = createViewForTesting(); + view.setMoreItem(createTestMoreItem()); + view.setChipBackground(createChipBackground()); + view.setChipHeight(48); + mEditable = new SpannableStringBuilder(); + mEditable.append("user@user.com"); + view.setSelection(mEditable.length()); + view.handlePaste(); + assertEquals(mEditable.getSpans(0, mEditable.length(), RecipientChip.class).length, 0); + assertEquals(mEditable.toString(), "user@user.com"); + + // Test adding a single address to an empty chips field with a space at + // the end of it. The address should stay as text. + mEditable = new SpannableStringBuilder(); + String tokenizedUser = "user@user.com" + " "; + mEditable.append(tokenizedUser); + view.setSelection(mEditable.length()); + view.handlePaste(); + assertEquals(mEditable.getSpans(0, mEditable.length(), RecipientChip.class).length, 0); + assertEquals(mEditable.toString(), tokenizedUser); + + // Test adding a single address to an empty chips field with a semicolon at + // the end of it. The address should become a chip + mEditable = new SpannableStringBuilder(); + tokenizedUser = "user@user.com;"; + mEditable.append(tokenizedUser); + view.setSelection(mEditable.length()); + view.handlePaste(); + assertEquals(mEditable.getSpans(0, mEditable.length(), RecipientChip.class).length, 1); + + // Test adding 2 address to an empty chips field. The second to last + // address should become a chip and the last address should stay as + // text. + mEditable = new SpannableStringBuilder(); + mEditable.append("user1,user2@user.com"); + view.setSelection(mEditable.length()); + view.handlePaste(); + assertEquals(mEditable.getSpans(0, mEditable.length(), RecipientChip.class).length, 1); + assertEquals(mEditable.getSpans(0, mEditable.toString().indexOf("user2@user.com"), + RecipientChip.class).length, 1); + assertEquals(mEditable.toString(), "<user1>, user2@user.com"); + + // Test adding a single address to the end of existing chips. The existing + // chips should remain, and the last address should stay as text. + populateMocks(3); + String first = (String) mTokenizer.terminateToken("FIRST"); + String second = (String) mTokenizer.terminateToken("SECOND"); + String third = (String) mTokenizer.terminateToken("THIRD"); + mEditable = new SpannableStringBuilder(); + mEditable.append(first + second + third); + view.setSelection(mEditable.length()); + int firstStart = mEditable.toString().indexOf(first); + int firstEnd = firstStart + first.trim().length(); + int secondStart = mEditable.toString().indexOf(second); + int secondEnd = secondStart + second.trim().length(); + int thirdStart = mEditable.toString().indexOf(third); + int thirdEnd = thirdStart + third.trim().length(); + mEditable.setSpan(mMockRecips[mMockRecips.length - 3], firstStart, firstEnd, 0); + mEditable.setSpan(mMockRecips[mMockRecips.length - 2], secondStart, secondEnd, 0); + mEditable.setSpan(mMockRecips[mMockRecips.length - 1], thirdStart, thirdEnd, 0); + + mEditable.append("user@user.com"); + view.setSelection(mEditable.length()); + view.handlePaste(); + assertEquals(mEditable.getSpans(0, mEditable.length(), RecipientChip.class).length, + mMockRecips.length); + assertEquals(mEditable.toString(), first + second + third + "user@user.com"); + + // Paste 2 addresses after existing chips. We expect the first address to be turned into + // a chip and the second to be left as text. + populateMocks(3); + mEditable = new SpannableStringBuilder(); + mEditable.append(first + second + third); + + mEditable.setSpan(mMockRecips[mMockRecips.length - 3], firstStart, firstEnd, 0); + mEditable.setSpan(mMockRecips[mMockRecips.length - 2], secondStart, secondEnd, 0); + mEditable.setSpan(mMockRecips[mMockRecips.length - 1], thirdStart, thirdEnd, 0); + + mEditable.append("user1, user2@user.com"); + view.setSelection(mEditable.length()); + view.handlePaste(); + assertEquals(mEditable.getSpans(0, mEditable.length(), RecipientChip.class).length, + mMockRecips.length + 1); + assertEquals(mEditable.getSpans(mEditable.toString().indexOf("<user1>"), mEditable + .toString().indexOf("user2@user.com") - 1, RecipientChip.class).length, 1); + assertEquals(mEditable.getSpans(mEditable.toString().indexOf("user2@user.com"), mEditable + .length(), RecipientChip.class).length, 0); + assertEquals(mEditable.toString(), first + second + third + "<user1>, user2@user.com"); + + // Paste 2 addresses after existing chips. We expect the first address to be turned into + // a chip and the second to be left as text. This removes the space seperator char between + // addresses. + populateMocks(3); + mEditable = new SpannableStringBuilder(); + mEditable.append(first + second + third); + + mEditable.setSpan(mMockRecips[mMockRecips.length - 3], firstStart, firstEnd, 0); + mEditable.setSpan(mMockRecips[mMockRecips.length - 2], secondStart, secondEnd, 0); + mEditable.setSpan(mMockRecips[mMockRecips.length - 1], thirdStart, thirdEnd, 0); + + mEditable.append("user1,user2@user.com"); + view.setSelection(mEditable.length()); + view.handlePaste(); + assertEquals(mEditable.getSpans(0, mEditable.length(), RecipientChip.class).length, + mMockRecips.length + 1); + assertEquals(mEditable.getSpans(mEditable.toString().indexOf("<user1>"), mEditable + .toString().indexOf("user2@user.com") - 1, RecipientChip.class).length, 1); + assertEquals(mEditable.getSpans(mEditable.toString().indexOf("user2@user.com"), mEditable + .length(), RecipientChip.class).length, 0); + assertEquals(mEditable.toString(), first + second + third + "<user1>, user2@user.com"); + + // Test a complete token pasted in at the end. It should be turned into a chip. + mEditable = new SpannableStringBuilder(); + mEditable.append("user1, user2@user.com,"); + view.setSelection(mEditable.length()); + view.handlePaste(); + assertEquals(mEditable.getSpans(0, mEditable.length(), RecipientChip.class).length, 2); + assertEquals(mEditable.getSpans(mEditable.toString().indexOf("<user1>"), mEditable + .toString().indexOf("user2@user.com") - 1, RecipientChip.class).length, 1); + assertEquals(mEditable.getSpans(mEditable.toString().indexOf("user2@user.com"), mEditable + .length(), RecipientChip.class).length, 1); + assertEquals(mEditable.toString(), "<user1>, <user2@user.com>, "); + } + + public void testGetPastTerminators() { + MockRecipientEditTextView view = createViewForTesting(); + view.setMoreItem(createTestMoreItem()); + view.setChipBackground(createChipBackground()); + view.setChipHeight(48); + String test = "test"; + mEditable = new SpannableStringBuilder(); + mEditable.append(test); + assertEquals(view.movePastTerminators(mTokenizer.findTokenEnd(mEditable.toString(), 0)), + test.length()); + + test = "test,"; + mEditable = new SpannableStringBuilder(); + mEditable.append(test); + assertEquals(view.movePastTerminators(mTokenizer.findTokenEnd(mEditable.toString(), 0)), + test.length()); + + test = "test, "; + mEditable = new SpannableStringBuilder(); + mEditable.append(test); + assertEquals(view.movePastTerminators(mTokenizer.findTokenEnd(mEditable.toString(), 0)), + test.length()); + + test = "test;"; + mEditable = new SpannableStringBuilder(); + mEditable.append(test); + assertEquals(view.movePastTerminators(mTokenizer.findTokenEnd(mEditable.toString(), 0)), + test.length()); + + test = "test; "; + mEditable = new SpannableStringBuilder(); + mEditable.append(test); + assertEquals(view.movePastTerminators(mTokenizer.findTokenEnd(mEditable.toString(), 0)), + test.length()); + } + + public void testIsCompletedToken() { + MockRecipientEditTextView view = createViewForTesting(); + view.setMoreItem(createTestMoreItem()); + view.setChipBackground(createChipBackground()); + view.setChipHeight(48); + assertTrue(view.isCompletedToken("test;")); + assertTrue(view.isCompletedToken("test,")); + assertFalse(view.isCompletedToken("test")); + assertFalse(view.isCompletedToken("test ")); + } + private Drawable createChipBackground() { Bitmap drawable = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); return new BitmapDrawable(getContext().getResources(), drawable); |