diff options
Diffstat (limited to 'tests/src/com')
-rw-r--r-- | tests/src/com/android/dialer/list/PhoneFavoritesTileAdapterTest.java | 248 |
1 files changed, 220 insertions, 28 deletions
diff --git a/tests/src/com/android/dialer/list/PhoneFavoritesTileAdapterTest.java b/tests/src/com/android/dialer/list/PhoneFavoritesTileAdapterTest.java index d0547bda8..881938400 100644 --- a/tests/src/com/android/dialer/list/PhoneFavoritesTileAdapterTest.java +++ b/tests/src/com/android/dialer/list/PhoneFavoritesTileAdapterTest.java @@ -1,5 +1,8 @@ package com.android.dialer.list; +import com.google.common.collect.Lists; + +import android.content.Context; import android.database.Cursor; import android.database.MatrixCursor; import android.provider.ContactsContract.PinnedPositions; @@ -8,15 +11,18 @@ import android.test.suitebuilder.annotation.SmallTest; import com.android.contacts.common.ContactTileLoaderFactory; import com.android.contacts.common.list.ContactEntry; +import com.android.contacts.common.preference.ContactsPreferences; import com.android.dialer.list.PhoneFavoritesTileAdapter.OnDataSetChangedForAnimationListener; +import junit.framework.Assert; + import java.util.ArrayList; @SmallTest public class PhoneFavoritesTileAdapterTest extends AndroidTestCase { - private PhoneFavoritesTileAdapter mAdapter; - private static final OnDataSetChangedForAnimationListener - sOnDataSetChangedForAnimationListener = new OnDataSetChangedForAnimationListener() { + + private static final OnDataSetChangedForAnimationListener NOOP_ANIMATION_LISTENER = + new OnDataSetChangedForAnimationListener() { @Override public void onDataSetChangedForAnimation(long... idsInPlace) {} @@ -24,18 +30,161 @@ public class PhoneFavoritesTileAdapterTest extends AndroidTestCase { public void cacheOffsetsForDatasetChange() {} }; + private PhoneFavoritesTileAdapter mAdapter; + + @Override + public void setUp() { + this.mAdapter = new PhoneFavoritesTileAdapter(getContext(), null, NOOP_ANIMATION_LISTENER); + } + /** - * TODO: Add tests - * - * Test cases (various combinations of): - * No pinned contacts - * One pinned contact - * Multiple pinned contacts with differing pinned positions - * Multiple pinned contacts with conflicting pinned positions - * Pinned contacts with pinned positions at the start, middle, end, and outside the list + * For all arrangeContactsByPinnedPosition tests, the id for a particular ContactEntry + * represents the index at which it should be located after calling + * arrangeContactsByPinnedPosition */ - public void testArrangeContactsByPinnedPosition() { + public void testArrangeContactsByPinnedPosition_NoPinned() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(0), + getTestContactEntry(1), getTestContactEntry(2)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_NoPinned_RemoveDemoted() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(0), + getTestContactEntry(-1, PinnedPositions.DEMOTED), getTestContactEntry(1)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 2); + } + + public void testArrangeContactsByPinnedPosition_OnePinned_Beginning() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(1), + getTestContactEntry(0, 1), getTestContactEntry(2)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_OnePinned_Middle() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(0), + getTestContactEntry(1, 2), getTestContactEntry(2)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_OnePinned_End() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(0), + getTestContactEntry(2, 3), getTestContactEntry(1)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_OnePinned_Outside() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(0), + getTestContactEntry(2, 5), getTestContactEntry(1)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_OnePinned_RemoveDemoted() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(1, 2), + getTestContactEntry(-1, PinnedPositions.DEMOTED), getTestContactEntry(0)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 2); + } + + public void testArrangeContactsByPinnedPosition_TwoPinned_Split() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(0, 1), + getTestContactEntry(1), getTestContactEntry(2, 3)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_TwoPinned_Adjacent() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(1, 2), + getTestContactEntry(0), getTestContactEntry(2, 3)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_TwoPinned_Conflict_UnpinnedBefore() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(1, 2), + getTestContactEntry(0), getTestContactEntry(2, 2)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_TwoPinned_Conflict_UnpinnedAfter() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(0, 1), + getTestContactEntry(2), getTestContactEntry(1, 1)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_TwoPinned_Conflict_RemoveDemoted() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(1, 2), + getTestContactEntry(-1, PinnedPositions.DEMOTED), getTestContactEntry(0, 2)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 2); + } + + public void testArrangeContactsByPinnedPosition_AllPinned() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(1, 2), + getTestContactEntry(0, 1), getTestContactEntry(2, 3)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_AllPinned_TwoConflicts_ConflictsFirst() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(1, 2), + getTestContactEntry(0, 2), getTestContactEntry(2, 3)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_AllPinned_TwoConflicts_ConflictsLast() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(0, 2), + getTestContactEntry(1, 3), getTestContactEntry(2, 3)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_AllPinned_AllConflicts() { + ArrayList<ContactEntry> toArrange = Lists.newArrayList(getTestContactEntry(2, 3), + getTestContactEntry(1, 3), getTestContactEntry(0, 3)); + mAdapter.arrangeContactsByPinnedPosition(toArrange); + + assertContactEntryListPositionsMatchId(toArrange, 3); + } + + public void testArrangeContactsByPinnedPosition_All_Pinned_AllConflicts_SortNameAlternative() { + Context context = getContext(); + context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE).edit() + .putInt(ContactsPreferences.SORT_ORDER_KEY, + ContactsPreferences.SORT_ORDER_ALTERNATIVE) + .commit(); + ArrayList<ContactEntry> actual = Lists.newArrayList( + getTestContactEntry(1, 3, "2", "1"), + getTestContactEntry(2, 3, "0", "2"), + getTestContactEntry(0, 3, "1", "0") + ); + mAdapter.arrangeContactsByPinnedPosition(actual); + + assertContactEntryListPositionsMatchId(actual, 3); } /** @@ -55,6 +204,45 @@ public class PhoneFavoritesTileAdapterTest extends AndroidTestCase { } + public void testSetContactCursor_DisplayNameOrder_Primary() { + setNameDisplayOrder(getContext(), ContactsPreferences.DISPLAY_ORDER_PRIMARY); + Cursor testCursor = getCursorForTest(1, 0); + mAdapter.setContactCursor(testCursor); + Assert.assertEquals(1, mAdapter.mContactEntries.size()); + Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_PRIMARY, + mAdapter.mContactEntries.get(0).nameDisplayOrder); + } + + public void testSetContactCursor_DisplayNameOrder_Alternative() { + setNameDisplayOrder(getContext(), ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE); + Cursor testCursor = getCursorForTest(1, 0); + mAdapter.setContactCursor(testCursor); + Assert.assertEquals(1, mAdapter.mContactEntries.size()); + Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE, + mAdapter.mContactEntries.get(0).nameDisplayOrder); + } + + public void testSetContactCursor_DisplayNameOrder_Changed() { + setNameDisplayOrder(getContext(), ContactsPreferences.DISPLAY_ORDER_PRIMARY); + Cursor testCursor = getCursorForTest(1, 0); + mAdapter.setContactCursor(testCursor); + Assert.assertEquals(1, mAdapter.mContactEntries.size()); + Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_PRIMARY, + mAdapter.mContactEntries.get(0).nameDisplayOrder); + + setNameDisplayOrder(getContext(), ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE); + mAdapter.refreshContactsPreferences(); + mAdapter.setContactCursor(testCursor); + Assert.assertEquals(1, mAdapter.mContactEntries.size()); + Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE, + mAdapter.mContactEntries.get(0).nameDisplayOrder); + } + + private void setNameDisplayOrder(Context context, int displayOrder) { + context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE).edit().putInt( + ContactsPreferences.DISPLAY_ORDER_KEY, displayOrder).commit(); + } + /** * Returns a cursor containing starred and frequent contacts for test purposes. * @@ -72,38 +260,42 @@ public class PhoneFavoritesTileAdapterTest extends AndroidTestCase { // The only field that really matters for testing is the contact id. for (int i = 0; i < numStarred; i++) { c.addRow(new Object[] {countId, null, 1, null, null, 0, 0, null, 0, - PinnedPositions.UNPINNED, countId}); + PinnedPositions.UNPINNED, countId, null}); countId++; } // Add frequent contact entries. These entries have the starred field set to 0 (false). for (int i = 0; i < numFrequents; i++) { c.addRow(new Object[] {countId, null, 0, null, null, 0, 0, null, 0, - PinnedPositions.UNPINNED, countId}); + PinnedPositions.UNPINNED, countId, null}); countId++; } return c; } - /** - * Returns a ContactEntry with test data corresponding to the provided contact Id - * - * @param id Non-negative id - * @return ContactEntry item used for testing - */ - private ContactEntry getTestContactEntry(int id, boolean isFavorite) { + private ContactEntry getTestContactEntry(int id) { + return getTestContactEntry(id, PinnedPositions.UNPINNED); + } + + private ContactEntry getTestContactEntry(int id, int pinned) { + return getTestContactEntry(id, pinned, String.valueOf(id), String.valueOf(id)); + } + + private ContactEntry getTestContactEntry(int id, int pinned, String namePrimaryAppend, + String nameAlternativeAppend) { ContactEntry contactEntry = new ContactEntry(); contactEntry.id = id; - contactEntry.isFavorite = isFavorite; + contactEntry.pinned = pinned; + contactEntry.namePrimary = namePrimaryAppend; + contactEntry.nameAlternative = nameAlternativeAppend; return contactEntry; } - private void assertContactEntryRowsEqual(ArrayList<ContactEntry> expected, - ArrayList<ContactEntry> actual) { - assertEquals(expected.size(), actual.size()); - for (int i = 0; i < actual.size(); i++) { - assertEquals(expected.get(i).id, actual.get(i).id); - assertEquals(expected.get(i).isFavorite, actual.get(i).isFavorite); + private void assertContactEntryListPositionsMatchId(ArrayList<ContactEntry> contactEntries, + int expectedSize) { + Assert.assertEquals(expectedSize, contactEntries.size()); + for (int i = 0; i < expectedSize; ++i) { + Assert.assertEquals(i, contactEntries.get(i).id); } } } |