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