blob: c9b2e6d492dde5e9ef782bdc516722de050747be [file] [log] [blame]
Chiao Cheng89437e82012-11-01 13:41:51 -07001/*
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
Chiao Cheng89437e82012-11-01 13:41:51 -070027import com.android.contacts.common.format.SpannedTestUtils;
Yorke Lee426e5d42014-03-18 13:07:00 -070028import com.android.contacts.common.list.ContactListItemView;
Yorke Leeb3d841a2014-07-10 11:38:55 -070029import com.android.contacts.common.preference.ContactsPreferences;
Chiao Cheng89437e82012-11-01 13:41:51 -070030
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
Yorke Leeb3d841a2014-07-10 11:38:55 -070060 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY);
Chiao Cheng89437e82012-11-01 13:41:51 -070061
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");
Yorke Leeb3d841a2014-07-10 11:38:55 -070070 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY);
Chiao Cheng89437e82012-11-01 13:41:51 -070071
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
Chiao Chenga1554ef2012-12-21 15:45:54 -080079 view.setHighlightedPrefix("DOE");
Yorke Leeb3d841a2014-07-10 11:38:55 -070080 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY);
Chiao Cheng89437e82012-11-01 13:41:51 -070081
82 CharSequence seq = view.getNameTextView().getText();
83 assertEquals("John Doe", seq.toString());
84 SpannedTestUtils.assertPrefixSpan(seq, 5, 7);
Qi Wang8a2f9f62016-03-03 14:05:51 -080085 // Talback should be without span tags.
86 assertEquals("John Doe", view.getNameTextView().getContentDescription());
87 assertFalse("John Doe".equals(seq));
Chiao Cheng89437e82012-11-01 13:41:51 -070088 }
89
90 public void testShowDisplayName_WithPrefixReversed() {
91 Cursor cursor = createCursor("John Doe", "Doe John");
92 ContactListItemView view = createView();
93
Chiao Chenga1554ef2012-12-21 15:45:54 -080094 view.setHighlightedPrefix("DOE");
Yorke Leeb3d841a2014-07-10 11:38:55 -070095 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE);
Chiao Cheng89437e82012-11-01 13:41:51 -070096
97 CharSequence seq = view.getNameTextView().getText();
98 assertEquals("John Doe", seq.toString());
99 SpannedTestUtils.assertPrefixSpan(seq, 5, 7);
100 }
101
102 public void testSetSnippet_Prefix() {
103 ContactListItemView view = createView();
Chiao Chenga1554ef2012-12-21 15:45:54 -0800104 view.setHighlightedPrefix("TEST");
Chiao Cheng89437e82012-11-01 13:41:51 -0700105 view.setSnippet("This is a test");
106
107 CharSequence seq = view.getSnippetView().getText();
108
109 assertEquals("This is a test", seq.toString());
110 SpannedTestUtils.assertPrefixSpan(seq, 10, 13);
111 }
112
113 /** Creates the view to be tested. */
114 private ContactListItemView createView() {
115 ContactListItemView view = new ContactListItemView(getContext());
116 // Set the name view to use a Spannable to represent its content.
117 view.getNameTextView().setText("", TextView.BufferType.SPANNABLE);
118 return view;
119 }
120
121 /**
122 * Creates a cursor containing a pair of values.
123 *
124 * @param name the name to insert in the first column of the cursor
125 * @param alternateName the alternate name to insert in the second column of the cursor
126 * @return the newly created cursor
127 */
128 private Cursor createCursor(String name, String alternateName) {
129 MatrixCursor cursor = new MatrixCursor(new String[]{"Name", "AlternateName"});
130 cursor.moveToFirst();
131 cursor.addRow(new Object[]{name, alternateName});
132 return cursor;
133 }
134}