blob: 09c2f36a7317b1e960559edb191bf910c5335590 [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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.list;
Chiao Cheng89437e82012-11-01 13:41:51 -070018
19import android.database.Cursor;
20import android.database.MatrixCursor;
Chiao Cheng89437e82012-11-01 13:41:51 -070021import android.test.ActivityInstrumentationTestCase2;
22import android.test.AndroidTestCase;
23import android.test.suitebuilder.annotation.LargeTest;
24import android.widget.TextView;
25
Gary Mai69c182a2016-12-05 13:07:03 -080026import com.android.contacts.format.SpannedTestUtils;
27import com.android.contacts.preference.ContactsPreferences;
Chiao Cheng89437e82012-11-01 13:41:51 -070028
29/**
Gary Mai69c182a2016-12-05 13:07:03 -080030 * Unit tests for {@link com.android.contacts.list.ContactListItemView}.
Chiao Cheng89437e82012-11-01 13:41:51 -070031 *
32 * It uses an {@link ActivityInstrumentationTestCase2} for {@link PeopleActivity} because we need
33 * to have the style properly setup.
34 */
35@LargeTest
36public class ContactListItemViewTest extends AndroidTestCase {
37
38 //private IntegrationTestUtils mUtils;
39
40 @Override
41 protected void setUp() throws Exception {
42 super.setUp();
43 // This test requires that the screen be turned on.
44 //mUtils = new IntegrationTestUtils(getInstrumentation());
45 //mUtils.acquireScreenWakeLock(getInstrumentation().getTargetContext());
46 }
47
48 @Override
49 protected void tearDown() throws Exception {
50 //mUtils.releaseScreenWakeLock();
51 super.tearDown();
52 }
53
54 public void testShowDisplayName_Simple() {
55 Cursor cursor = createCursor("John Doe", "Doe John");
56 ContactListItemView view = createView();
57
Yorke Leeb3d841a2014-07-10 11:38:55 -070058 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY);
Chiao Cheng89437e82012-11-01 13:41:51 -070059
60 assertEquals(view.getNameTextView().getText().toString(), "John Doe");
61 }
62
63 public void testShowDisplayName_Unknown() {
64 Cursor cursor = createCursor("", "");
65 ContactListItemView view = createView();
66
67 view.setUnknownNameText("unknown");
Yorke Leeb3d841a2014-07-10 11:38:55 -070068 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY);
Chiao Cheng89437e82012-11-01 13:41:51 -070069
70 assertEquals(view.getNameTextView().getText().toString(), "unknown");
71 }
72
73 public void testShowDisplayName_WithPrefix() {
74 Cursor cursor = createCursor("John Doe", "Doe John");
75 ContactListItemView view = createView();
76
Chiao Chenga1554ef2012-12-21 15:45:54 -080077 view.setHighlightedPrefix("DOE");
Yorke Leeb3d841a2014-07-10 11:38:55 -070078 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY);
Chiao Cheng89437e82012-11-01 13:41:51 -070079
80 CharSequence seq = view.getNameTextView().getText();
81 assertEquals("John Doe", seq.toString());
82 SpannedTestUtils.assertPrefixSpan(seq, 5, 7);
Qi Wang8a2f9f62016-03-03 14:05:51 -080083 // Talback should be without span tags.
84 assertEquals("John Doe", view.getNameTextView().getContentDescription());
85 assertFalse("John Doe".equals(seq));
Chiao Cheng89437e82012-11-01 13:41:51 -070086 }
87
88 public void testShowDisplayName_WithPrefixReversed() {
89 Cursor cursor = createCursor("John Doe", "Doe John");
90 ContactListItemView view = createView();
91
Chiao Chenga1554ef2012-12-21 15:45:54 -080092 view.setHighlightedPrefix("DOE");
Yorke Leeb3d841a2014-07-10 11:38:55 -070093 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE);
Chiao Cheng89437e82012-11-01 13:41:51 -070094
95 CharSequence seq = view.getNameTextView().getText();
96 assertEquals("John Doe", seq.toString());
97 SpannedTestUtils.assertPrefixSpan(seq, 5, 7);
98 }
99
100 public void testSetSnippet_Prefix() {
101 ContactListItemView view = createView();
Chiao Chenga1554ef2012-12-21 15:45:54 -0800102 view.setHighlightedPrefix("TEST");
Chiao Cheng89437e82012-11-01 13:41:51 -0700103 view.setSnippet("This is a test");
104
105 CharSequence seq = view.getSnippetView().getText();
106
107 assertEquals("This is a test", seq.toString());
108 SpannedTestUtils.assertPrefixSpan(seq, 10, 13);
109 }
110
111 /** Creates the view to be tested. */
112 private ContactListItemView createView() {
113 ContactListItemView view = new ContactListItemView(getContext());
114 // Set the name view to use a Spannable to represent its content.
115 view.getNameTextView().setText("", TextView.BufferType.SPANNABLE);
116 return view;
117 }
118
119 /**
120 * Creates a cursor containing a pair of values.
121 *
122 * @param name the name to insert in the first column of the cursor
123 * @param alternateName the alternate name to insert in the second column of the cursor
124 * @return the newly created cursor
125 */
126 private Cursor createCursor(String name, String alternateName) {
127 MatrixCursor cursor = new MatrixCursor(new String[]{"Name", "AlternateName"});
128 cursor.moveToFirst();
129 cursor.addRow(new Object[]{name, alternateName});
130 return cursor;
131 }
132}