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 17 package com.android.contacts.list; 18 19 import android.database.Cursor; 20 import android.database.MatrixCursor; 21 import android.test.ActivityInstrumentationTestCase2; 22 import android.test.AndroidTestCase; 23 import android.test.suitebuilder.annotation.LargeTest; 24 import android.widget.TextView; 25 26 import com.android.contacts.format.SpannedTestUtils; 27 import com.android.contacts.preference.ContactsPreferences; 28 29 /** 30 * Unit tests for {@link com.android.contacts.list.ContactListItemView}. 31 * 32 * It uses an {@link ActivityInstrumentationTestCase2} for {@link PeopleActivity} because we need 33 * to have the style properly setup. 34 */ 35 @LargeTest 36 public 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 58 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY); 59 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"); 68 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY); 69 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 77 view.setHighlightedPrefix("DOE"); 78 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY); 79 80 CharSequence seq = view.getNameTextView().getText(); 81 assertEquals("John Doe", seq.toString()); 82 SpannedTestUtils.assertPrefixSpan(seq, 5, 7); 83 // Talback should be without span tags. 84 assertEquals("John Doe", view.getNameTextView().getContentDescription()); 85 assertFalse("John Doe".equals(seq)); 86 } 87 88 public void testShowDisplayName_WithPrefixReversed() { 89 Cursor cursor = createCursor("John Doe", "Doe John"); 90 ContactListItemView view = createView(); 91 92 view.setHighlightedPrefix("DOE"); 93 view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE); 94 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(); 102 view.setHighlightedPrefix("TEST"); 103 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 } 133