155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leepackage com.android.dialer.list;
255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
33ce60186f646b461635b16d7d9a806ddc0b61bacYorke Leeimport android.database.Cursor;
43ce60186f646b461635b16d7d9a806ddc0b61bacYorke Leeimport android.database.MatrixCursor;
53ce60186f646b461635b16d7d9a806ddc0b61bacYorke Leeimport android.provider.ContactsContract.PinnedPositions;
655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leeimport android.test.AndroidTestCase;
755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
83ce60186f646b461635b16d7d9a806ddc0b61bacYorke Leeimport com.android.contacts.common.ContactTileLoaderFactory;
93ce60186f646b461635b16d7d9a806ddc0b61bacYorke Leeimport com.android.contacts.common.list.ContactEntry;
103ce60186f646b461635b16d7d9a806ddc0b61bacYorke Leeimport com.android.dialer.list.PhoneFavoritesTileAdapter.OnDataSetChangedForAnimationListener;
113ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
123ce60186f646b461635b16d7d9a806ddc0b61bacYorke Leeimport java.util.ArrayList;
133ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
1455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leepublic class PhoneFavoritesTileAdapterTest extends AndroidTestCase {
1555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    private PhoneFavoritesTileAdapter mAdapter;
163ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    private static final OnDataSetChangedForAnimationListener
173ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee            sOnDataSetChangedForAnimationListener = new OnDataSetChangedForAnimationListener() {
183ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee                @Override
193ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee                public void onDataSetChangedForAnimation(long... idsInPlace) {}
2055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
213ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee                @Override
223ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee                public void cacheOffsetsForDatasetChange() {}
233ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee            };
2455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
2555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    /**
2618e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee     * TODO: Add tests
2755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     *
2855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Test cases (various combinations of):
2955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * No pinned contacts
3055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * One pinned contact
3155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Multiple pinned contacts with differing pinned positions
3255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Multiple pinned contacts with conflicting pinned positions
3355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Pinned contacts with pinned positions at the start, middle, end, and outside the list
3455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     */
3555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    public void testArrangeContactsByPinnedPosition() {
3655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
3755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    }
3855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
3955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    /**
4018e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee     * TODO: Add tests
4155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     *
4255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * This method assumes that contacts have already been reordered by
4355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * arrangeContactsByPinnedPosition, so we can test it with a less expansive set of test data.
4455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     *
4555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Test cases:
4655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Pin a single contact at the start, middle and end of a completely unpinned list
4755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Pin a single contact at the start, middle and end of a list with various numbers of
4855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * pinned contacts
4955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Pin a single contact at the start, middle and end of a list where all contacts are pinned
5055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * such that contacts are forced to the left as necessary.
5155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     */
5255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    public void testGetReflowedPinnedPositions() {
5355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
5455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    }
5555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
563ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    public void testGetRowIndex_NoRowLimit() {
573ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        mAdapter = getAdapterForTest(2, PhoneFavoritesTileAdapter.NO_ROW_LIMIT);
583ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(0, mAdapter.getRowCount(0));
593ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(1, mAdapter.getRowCount(1));
603ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(1, mAdapter.getRowCount(2));
613ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(2, mAdapter.getRowCount(4));
623ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(4, mAdapter.getRowCount(7));
633ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(100, mAdapter.getRowCount(199));
643ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
653ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        mAdapter = getAdapterForTest(5, PhoneFavoritesTileAdapter.NO_ROW_LIMIT);
663ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(0, mAdapter.getRowCount(0));
673ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(1, mAdapter.getRowCount(1));
683ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(1, mAdapter.getRowCount(3));
693ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(1, mAdapter.getRowCount(5));
703ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(2, mAdapter.getRowCount(7));
713ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(2, mAdapter.getRowCount(10));
723ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(40, mAdapter.getRowCount(199));
733ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    }
743ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
753ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    public void testGetItemId_NoRowLimit() {
763ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        mAdapter = getAdapterForTest(2, PhoneFavoritesTileAdapter.NO_ROW_LIMIT);
773ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(0, mAdapter.getItemId(0));
783ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(1, mAdapter.getItemId(1));
793ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(5, mAdapter.getItemId(5));
803ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(10, mAdapter.getItemId(10));
813ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    }
823ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
833ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    public void testGetAdjustedItemId_NoRowLimit() {
843ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        mAdapter = getAdapterForTest(2, PhoneFavoritesTileAdapter.NO_ROW_LIMIT);
853ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(0, mAdapter.getAdjustedItemId(0));
863ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(1, mAdapter.getAdjustedItemId(1));
873ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(5, mAdapter.getAdjustedItemId(5));
883ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(10, mAdapter.getAdjustedItemId(10));
893ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    }
9055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
913ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    public void testGetItem_NoRowLimit() {
923ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        mAdapter = getAdapterForTest(2, PhoneFavoritesTileAdapter.NO_ROW_LIMIT);
933ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        mAdapter.setContactCursor(getCursorForTest(5, 5));
943ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
953ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        final ArrayList<ContactEntry> row1 = new ArrayList<ContactEntry> ();
963ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        row1.add(getTestContactEntry(0, true));
973ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        row1.add(getTestContactEntry(1, true));
983ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertContactEntryRowsEqual(row1, mAdapter.getItem(0));
993ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
1003ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        final ArrayList<ContactEntry> row3 = new ArrayList<ContactEntry> ();
1013ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        row3.add(getTestContactEntry(4, true));
1023ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        row3.add(getTestContactEntry(5, false));
1033ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertContactEntryRowsEqual(row3, mAdapter.getItem(2));
1043ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
1053ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        final ArrayList<ContactEntry> row5 = new ArrayList<ContactEntry> ();
1063ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        row5.add(getTestContactEntry(8, false));
1073ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        row5.add(getTestContactEntry(9, false));
1083ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertContactEntryRowsEqual(row5, mAdapter.getItem(4));
1093ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    }
1103ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
1113ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    /**
1123ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     * Ensures that PhoneFavoritesTileAdapter returns true for hasStableIds. This is needed for
1133ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     * animation purposes.
1143ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     */
1153ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    public void testHasStableIds() {
1163ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        mAdapter = new PhoneFavoritesTileAdapter(getContext(), null, null, 2, 2);
1173ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertTrue(mAdapter.hasStableIds());
1183ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    }
1193ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
1203ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    private PhoneFavoritesTileAdapter getAdapterForTest(int numCols, int numRows) {
1213ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        return new PhoneFavoritesTileAdapter(getContext(), null,
1223ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee                sOnDataSetChangedForAnimationListener, numCols, numRows);
1233ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    }
1243ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
1253ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    /**
1263ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     * Returns a cursor containing starred and frequent contacts for test purposes.
1273ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     *
1283ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     * @param numStarred Number of starred contacts in the cursor. Cannot be a negative number.
1293ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     * @param numFrequents Number of frequent contacts in the cursor. Cannot be a negative number.
1303ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     * @return Cursor containing the required number of rows, each representing one ContactEntry
1313ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     */
1323ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    private Cursor getCursorForTest(int numStarred, int numFrequents) {
1333ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertTrue(numStarred >= 0);
1343ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertTrue(numFrequents >= 0);
1353ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        final MatrixCursor c = new MatrixCursor(ContactTileLoaderFactory.COLUMNS_PHONE_ONLY);
1363ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        int countId = 0;
1373ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
1383ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        // Add starred contact entries. These entries have the starred field set to 1 (true).
1393ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        // The only field that really matters for testing is the contact id.
1403ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        for (int i = 0; i < numStarred; i++) {
1413ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee            c.addRow(new Object[] {countId, null, 1, null, null, 0, 0, null, 0,
1423ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee                    PinnedPositions.UNPINNED, countId});
1433ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee            countId++;
1443ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        }
1453ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
1463ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        // Add frequent contact entries. These entries have the starred field set to 0 (false).
1473ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        for (int i = 0; i < numFrequents; i++) {
1483ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee            c.addRow(new Object[] {countId, null, 0, null, null, 0, 0, null, 0,
1493ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee                    PinnedPositions.UNPINNED, countId});
1503ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee            countId++;
1513ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        }
1523ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        return c;
1533ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    }
1543ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
1553ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    /**
1563ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     * Returns a ContactEntry with test data corresponding to the provided contact Id
1573ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     *
1583ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     * @param id Non-negative id
1593ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     * @return ContactEntry item used for testing
1603ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee     */
1613ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    private ContactEntry getTestContactEntry(int id, boolean isFavorite) {
1623ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        ContactEntry contactEntry = new ContactEntry();
1633ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        contactEntry.id = id;
1643ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        contactEntry.isFavorite = isFavorite;
1653ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        return contactEntry;
1663ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    }
1673ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee
1683ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    private void assertContactEntryRowsEqual(ArrayList<ContactEntry> expected,
1693ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee            ArrayList<ContactEntry> actual) {
1703ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        assertEquals(expected.size(), actual.size());
1713ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        for (int i = 0; i < actual.size(); i++) {
1723ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee            assertEquals(expected.get(i).id, actual.get(i).id);
1733ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee            assertEquals(expected.get(i).isFavorite, actual.get(i).isFavorite);
1743ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee        }
1753ce60186f646b461635b16d7d9a806ddc0b61bacYorke Lee    }
17655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee}
177