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