ContactListItemViewTest.java revision 5c3603deaf07b00d842606479da8a77c747612d0
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.contacts.list;
18
19import android.database.Cursor;
20import android.database.MatrixCursor;
21import android.provider.ContactsContract;
22import android.test.ActivityInstrumentationTestCase2;
23import android.test.suitebuilder.annotation.LargeTest;
24import android.text.SpannableString;
25import android.text.Spanned;
26import android.widget.TextView;
27
28import com.android.contacts.activities.PeopleActivity;
29import com.android.contacts.common.test.IntegrationTestUtils;
30import com.android.contacts.format.SpannedTestUtils;
31
32/**
33 * Unit tests for {@link ContactListItemView}.
34 *
35 * It uses an {@link ActivityInstrumentationTestCase2} for {@link PeopleActivity} because we need
36 * to have the style properly setup.
37 */
38@LargeTest
39public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<PeopleActivity> {
40    /** The HTML code used to mark the start of the highlighted part. */
41    private static final String START = "<font color =\"#33b5e5\">";
42    /** The HTML code used to mark the end of the highlighted part. */
43    private static final String END = "</font>";
44
45    private IntegrationTestUtils mUtils;
46
47    public ContactListItemViewTest() {
48        super(PeopleActivity.class);
49    }
50
51    @Override
52    protected void setUp() throws Exception {
53        super.setUp();
54        // This test requires that the screen be turned on.
55        mUtils = new IntegrationTestUtils(getInstrumentation());
56        mUtils.acquireScreenWakeLock(getInstrumentation().getTargetContext());
57    }
58
59    @Override
60    protected void tearDown() throws Exception {
61        mUtils.releaseScreenWakeLock();
62        super.tearDown();
63    }
64
65    public void testShowDisplayName_Simple() {
66        Cursor cursor = createCursor("John Doe", "Doe John");
67        ContactListItemView view = createView();
68
69        view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY);
70
71        assertEquals(view.getNameTextView().getText().toString(), "John Doe");
72    }
73
74    public void testShowDisplayName_Unknown() {
75        Cursor cursor = createCursor("", "");
76        ContactListItemView view = createView();
77
78        view.setUnknownNameText("unknown");
79        view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY);
80
81        assertEquals(view.getNameTextView().getText().toString(), "unknown");
82    }
83
84    public void testShowDisplayName_WithPrefix() {
85        Cursor cursor = createCursor("John Doe", "Doe John");
86        ContactListItemView view = createView();
87
88        view.setHighlightedPrefix("DOE".toCharArray());
89        view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY);
90
91        CharSequence seq = view.getNameTextView().getText();
92        assertEquals("John Doe", seq.toString());
93        SpannedTestUtils.assertPrefixSpan(seq, 5, 7);
94    }
95
96    public void testShowDisplayName_WithPrefixReversed() {
97        Cursor cursor = createCursor("John Doe", "Doe John");
98        ContactListItemView view = createView();
99
100        view.setHighlightedPrefix("DOE".toCharArray());
101        view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_ALTERNATIVE);
102
103        CharSequence seq = view.getNameTextView().getText();
104        assertEquals("John Doe", seq.toString());
105        SpannedTestUtils.assertPrefixSpan(seq, 5, 7);
106    }
107
108    public void testSetSnippet_Prefix() {
109        ContactListItemView view = createView();
110        view.setHighlightedPrefix("TEST".toCharArray());
111        view.setSnippet("This is a test");
112
113        CharSequence seq = view.getSnippetView().getText();
114
115        assertEquals("This is a test", seq.toString());
116        SpannedTestUtils.assertPrefixSpan(seq, 10, 13);
117    }
118
119    /** Creates the view to be tested. */
120    private ContactListItemView createView() {
121        ContactListItemView view = new ContactListItemView(getActivity(), null);
122        // Set the name view to use a Spannable to represent its content.
123        view.getNameTextView().setText("", TextView.BufferType.SPANNABLE);
124        return view;
125    }
126
127    /**
128     * Creates a cursor containing a pair of values.
129     *
130     * @param name the name to insert in the first column of the cursor
131     * @param alternateName the alternate name to insert in the second column of the cursor
132     * @return the newly created cursor
133     */
134    private Cursor createCursor(String name, String alternateName) {
135        MatrixCursor cursor = new MatrixCursor(new String[]{"Name", "AlternateName"});
136        cursor.moveToFirst();
137        cursor.addRow(new Object[]{name, alternateName});
138        return cursor;
139    }
140}
141