summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMindy Pereira <mindyp@google.com>2011-09-26 16:11:41 -0700
committerMindy Pereira <mindyp@google.com>2011-09-27 09:01:34 -0700
commitae7e41106f289f2668c54fff6c457c6fc91ab4a9 (patch)
treed2d65362c9cfc97696bc456ce5076344438bbe34
parent53958d6d0ba2a0765fd3d272295b81c6d89a68d8 (diff)
downloadandroid_frameworks_ex-ae7e41106f289f2668c54fff6c457c6fc91ab4a9.tar.gz
android_frameworks_ex-ae7e41106f289f2668c54fff6c457c6fc91ab4a9.tar.bz2
android_frameworks_ex-ae7e41106f289f2668c54fff6c457c6fc91ab4a9.zip
Add more tests.
Tests verifying where a chip is position, removing a chip, and creating a chip. Change-Id: I2e9c6719a6efc365ab73c4d3c32bb29209b73e48
-rw-r--r--chips/src/com/android/ex/chips/RecipientEditTextView.java53
-rw-r--r--chips/tests/src/com/android/ex/chips/ChipsTest.java168
2 files changed, 197 insertions, 24 deletions
diff --git a/chips/src/com/android/ex/chips/RecipientEditTextView.java b/chips/src/com/android/ex/chips/RecipientEditTextView.java
index cedc60a..f4573c6 100644
--- a/chips/src/com/android/ex/chips/RecipientEditTextView.java
+++ b/chips/src/com/android/ex/chips/RecipientEditTextView.java
@@ -484,16 +484,18 @@ public class RecipientEditTextView extends MultiAutoCompleteTextView implements
photo = mDefaultContactPhoto;
}
// Draw the photo on the left side.
- Matrix matrix = new Matrix();
- RectF src = new RectF(0, 0, photo.getWidth(), photo.getHeight());
- Rect backgroundPadding = new Rect();
- mChipBackground.getPadding(backgroundPadding);
- RectF dst = new RectF(width - iconWidth + backgroundPadding.left,
- 0 + backgroundPadding.top,
- width - backgroundPadding.right,
- height - backgroundPadding.bottom);
- matrix.setRectToRect(src, dst, Matrix.ScaleToFit.FILL);
- canvas.drawBitmap(photo, matrix, paint);
+ if (photo != null) {
+ RectF src = new RectF(0, 0, photo.getWidth(), photo.getHeight());
+ Rect backgroundPadding = new Rect();
+ mChipBackground.getPadding(backgroundPadding);
+ RectF dst = new RectF(width - iconWidth + backgroundPadding.left,
+ 0 + backgroundPadding.top,
+ width - backgroundPadding.right,
+ height - backgroundPadding.bottom);
+ Matrix matrix = new Matrix();
+ matrix.setRectToRect(src, dst, Matrix.ScaleToFit.FILL);
+ canvas.drawBitmap(photo, matrix, paint);
+ }
} else {
// Don't leave any space for the icon. It isn't being drawn.
iconWidth = 0;
@@ -604,6 +606,17 @@ public class RecipientEditTextView extends MultiAutoCompleteTextView implements
mMoreItem = moreItem;
}
+
+ // Visible for testing.
+ /* package */ void setChipBackground(Drawable chipBackground) {
+ mChipBackground = chipBackground;
+ }
+
+ // Visible for testing.
+ /* package */ void setChipHeight(int height) {
+ mChipHeight = height;
+ }
+
/**
* Set whether to shrink the recipients field such that at most
* one line of recipients chips are shown when the field loses
@@ -1704,6 +1717,7 @@ public class RecipientEditTextView extends MultiAutoCompleteTextView implements
public boolean matchesChip(RecipientChip chip, int offset) {
int start = getChipStart(chip);
int end = getChipEnd(chip);
+
if (start == -1 || end == -1) {
return false;
}
@@ -1729,23 +1743,19 @@ public class RecipientEditTextView extends MultiAutoCompleteTextView implements
/**
* Remove the chip and any text associated with it from the RecipientEditTextView.
*/
- private void removeChip(RecipientChip chip) {
+ // Visible for testing.
+ /*pacakge*/ void removeChip(RecipientChip chip) {
Spannable spannable = getSpannable();
int spanStart = spannable.getSpanStart(chip);
int spanEnd = spannable.getSpanEnd(chip);
Editable text = getText();
- int toDelete = spanEnd;
boolean wasSelected = chip == mSelectedChip;
// Clear that there is a selected chip before updating any text.
if (wasSelected) {
mSelectedChip = null;
}
- // Always remove trailing spaces when removing a chip.
- while (toDelete >= 0 && toDelete < text.length() && text.charAt(toDelete) == ' ') {
- toDelete++;
- }
spannable.removeSpan(chip);
- text.delete(spanStart, toDelete);
+ text.delete(spanStart, spanEnd);
if (wasSelected) {
clearSelectedChip();
}
@@ -1769,14 +1779,9 @@ public class RecipientEditTextView extends MultiAutoCompleteTextView implements
Log.e(TAG, "The chip to replace does not exist but should.");
editable.insert(0, chipText);
} else {
- // There may be a space to replace with this chip's new associated
- // space. Check for it.
- int toReplace = end;
- while (toReplace >= 0 && toReplace < editable.length()
- && editable.charAt(toReplace) == ' ') {
- toReplace++;
+ if (!TextUtils.isEmpty(chipText)) {
+ editable.replace(start, end, chipText);
}
- editable.replace(start, toReplace, chipText);
}
setCursorVisible(true);
if (wasSelected) {
diff --git a/chips/tests/src/com/android/ex/chips/ChipsTest.java b/chips/tests/src/com/android/ex/chips/ChipsTest.java
index 768e500..cbf8fc5 100644
--- a/chips/tests/src/com/android/ex/chips/ChipsTest.java
+++ b/chips/tests/src/com/android/ex/chips/ChipsTest.java
@@ -17,6 +17,9 @@
package com.android.ex.chips;
import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
import android.test.AndroidTestCase;
import android.text.Editable;
import android.text.SpannableStringBuilder;
@@ -64,6 +67,10 @@ public class ChipsTest extends AndroidTestCase {
public int getLineHeight() {
return 48;
}
+
+ public Drawable getChipBackground(RecipientEntry contact) {
+ return createChipBackground();
+ }
}
private MockRecipientEditTextView createViewForTesting() {
@@ -355,6 +362,167 @@ public class ChipsTest extends AndroidTestCase {
assertEquals(mEditable.getSpanStart(moreChip), -1);
}
+ public void testMatchesChip() {
+ populateMocks(3);
+ MockRecipientEditTextView view = createViewForTesting();
+ view.setMoreItem(createTestMoreItem());
+ 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);
+
+ int firstStart = mEditable.toString().indexOf(first);
+ int firstEnd = firstStart + first.length();
+ int secondStart = mEditable.toString().indexOf(second);
+ int secondEnd = secondStart + second.length();
+ int thirdStart = mEditable.toString().indexOf(third);
+ int thirdEnd = thirdStart + third.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);
+ assertFalse(view.matchesChip(mMockRecips[mMockRecips.length - 3], -1));
+ assertFalse(view.matchesChip(mMockRecips[mMockRecips.length - 1], mEditable.length() + 1));
+ assertTrue(view.matchesChip(mMockRecips[mMockRecips.length - 3], firstStart));
+ assertTrue(view.matchesChip(mMockRecips[mMockRecips.length - 3], firstEnd));
+ assertTrue(view.matchesChip(mMockRecips[mMockRecips.length - 3], firstEnd - 1));
+ }
+
+ public void testRemoveChip() {
+ populateMocks(3);
+ MockRecipientEditTextView view = createViewForTesting();
+ view.setMoreItem(createTestMoreItem());
+ 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);
+
+ int firstStart = mEditable.toString().indexOf(first);
+ int firstEnd = firstStart + first.length();
+ int secondStart = mEditable.toString().indexOf(second);
+ int secondEnd = secondStart + second.length();
+ int thirdStart = mEditable.toString().indexOf(third);
+ int thirdEnd = thirdStart + third.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);
+ assertEquals(mEditable.toString(), first + second + third);
+ view.removeChip(mMockRecips[mMockRecips.length - 2]);
+ assertEquals(mEditable.toString(), first + third);
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 3]), firstStart);
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 3]), firstEnd);
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 2]), -1);
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 2]), -1);
+ int newThirdStart = mEditable.toString().indexOf(third);
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 1]), newThirdStart);
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 1]), newThirdStart
+ + third.length());
+
+ populateMocks(3);
+ view = createViewForTesting();
+ view.setMoreItem(createTestMoreItem());
+ mEditable = new SpannableStringBuilder();
+ mEditable.append(first + second + third);
+
+ firstStart = mEditable.toString().indexOf(first);
+ firstEnd = firstStart + first.length();
+ secondStart = mEditable.toString().indexOf(second);
+ secondEnd = secondStart + second.length();
+ thirdStart = mEditable.toString().indexOf(third);
+ thirdEnd = thirdStart + third.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);
+ assertEquals(mEditable.toString(), first + second + third);
+ view.removeChip(mMockRecips[mMockRecips.length - 3]);
+ assertEquals(mEditable.toString(), second + third);
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 3]), -1);
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 3]), -1);
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 2]), 0);
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 2]), second.length());
+ newThirdStart = mEditable.toString().indexOf(third);
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 1]), newThirdStart);
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 1]), newThirdStart
+ + third.length());
+
+ populateMocks(3);
+ view = createViewForTesting();
+ view.setMoreItem(createTestMoreItem());
+ mEditable = new SpannableStringBuilder();
+ mEditable.append(first + second + third);
+
+ firstStart = mEditable.toString().indexOf(first);
+ firstEnd = firstStart + first.length();
+ secondStart = mEditable.toString().indexOf(second);
+ secondEnd = secondStart + second.length();
+ thirdStart = mEditable.toString().indexOf(third);
+ thirdEnd = thirdStart + third.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);
+ assertEquals(mEditable.toString(), first + second + third);
+ view.removeChip(mMockRecips[mMockRecips.length - 1]);
+ assertEquals(mEditable.toString(), first + second);
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 3]), firstStart);
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 3]), firstEnd);
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 2]), secondStart);
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 2]), secondEnd);
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 1]), -1);
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 1]), -1);
+ }
+
+ public void testReplaceChip() {
+ populateMocks(3);
+ MockRecipientEditTextView view = createViewForTesting();
+ view.setMoreItem(createTestMoreItem());
+ view.setChipBackground(createChipBackground());
+ view.setChipHeight(48);
+ 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);
+
+ int firstStart = mEditable.toString().indexOf(first);
+ int firstEnd = firstStart + first.length();
+ int secondStart = mEditable.toString().indexOf(second);
+ int secondEnd = secondStart + second.length();
+ int thirdStart = mEditable.toString().indexOf(third);
+ int thirdEnd = thirdStart + third.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);
+ assertEquals(mEditable.toString(), first + second + third);
+ view.replaceChip(mMockRecips[mMockRecips.length - 3], RecipientEntry
+ .constructGeneratedEntry("replacement", "replacement@replacement.com"));
+ assertEquals(mEditable.toString(), mTokenizer
+ .terminateToken("replacement <replacement@replacement.com>")
+ + second + third);
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 3]), -1);
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 3]), -1);
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 2]), mEditable
+ .toString().indexOf(second));
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 2]), mEditable
+ .toString().indexOf(second)
+ + second.length());
+ assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 1]), mEditable
+ .toString().indexOf(third));
+ assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 1]), mEditable
+ .toString().indexOf(third)
+ + third.length());
+ RecipientChip[] spans = mEditable.getSpans(0, mEditable.length(), RecipientChip.class);
+ assertEquals(spans.length, 3);
+ spans = mEditable
+ .getSpans(0, mEditable.toString().indexOf(second) - 1, RecipientChip.class);
+ assertEquals((String) spans[0].getDisplay(), "replacement");
+ }
+
+ private Drawable createChipBackground() {
+ Bitmap drawable = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
+ return new BitmapDrawable(getContext().getResources(), drawable);
+ }
+
private TextView createTestMoreItem() {
TextView view = new TextView(getContext());
view.setText("<xliff:g id='count'>%1$s</xliff:g> more...");