ContactListItemViewTest.java revision be15e0997ff7559d1c58abfcf45bcb1332dfc5eb
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.format.TestTextWithHighlightingFactory; 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 =\"#729a27\">"; 40 /** The HTML code used to mark the end of the highlighted part. */ 41 private static final String END = "</font>"; 42 43 public ContactListItemViewTest() { 44 super(PeopleActivity.class); 45 } 46 47 public void testShowDisplayName_Simple() { 48 Cursor cursor = createCursor("John Doe", "Doe John"); 49 ContactListItemView view = createView(); 50 51 view.showDisplayName(cursor, 0, 1, false, 52 ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY); 53 54 SpannedTestUtils.checkHtmlText("John Doe", view.getNameTextView()); 55 } 56 57 public void testShowDisplayName_Unknown() { 58 Cursor cursor = createCursor("", ""); 59 ContactListItemView view = createView(); 60 61 view.setUnknownNameText("unknown"); 62 view.showDisplayName(cursor, 0, 1, false, 63 ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY); 64 65 SpannedTestUtils.checkHtmlText("unknown", view.getNameTextView()); 66 } 67 68 public void testShowDisplayName_WithPrefix() { 69 Cursor cursor = createCursor("John Doe", "Doe John"); 70 ContactListItemView view = createView(); 71 72 view.setHighlightedPrefix("DOE".toCharArray()); 73 view.showDisplayName(cursor, 0, 1, false, 74 ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY); 75 76 SpannedTestUtils.checkHtmlText("John " + START + "Doe" + END, 77 view.getNameTextView()); 78 } 79 80 public void testShowDisplayName_WithPrefixReversed() { 81 Cursor cursor = createCursor("John Doe", "Doe John"); 82 ContactListItemView view = createView(); 83 84 view.setHighlightedPrefix("DOE".toCharArray()); 85 view.showDisplayName(cursor, 0, 1, false, 86 ContactsContract.Preferences.DISPLAY_ORDER_ALTERNATIVE); 87 88 SpannedTestUtils.checkHtmlText("John " + START + "Doe" + END, 89 view.getNameTextView()); 90 } 91 92 public void testShowDisplayName_WithHighlight() { 93 Cursor cursor = createCursor("John Doe", "Doe John"); 94 ContactListItemView view = createView(); 95 96 view.setTextWithHighlightingFactory(new TestTextWithHighlightingFactory()); 97 view.showDisplayName(cursor, 0, 1, true, 98 ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY); 99 100 SpannedTestUtils.checkHtmlText("<i>John Doe</i> <i>Doe John</i>", 101 view.getNameTextView()); 102 } 103 104 public void testSetSnippet_Prefix() { 105 ContactListItemView view = createView(); 106 view.setHighlightedPrefix("TEST".toCharArray()); 107 view.setSnippet("This is a test"); 108 SpannedTestUtils.checkHtmlText("This is a " + START + "test" + END, 109 view.getSnippetView()); 110 } 111 112 /** Creates the view to be tested. */ 113 private ContactListItemView createView() { 114 ContactListItemView view = new ContactListItemView(getActivity(), null); 115 // Set the name view to use a Spannable to represent its content. 116 view.getNameTextView().setText("", TextView.BufferType.SPANNABLE); 117 return view; 118 } 119 120 /** 121 * Creates a cursor containing a pair of values. 122 * 123 * @param name the name to insert in the first column of the cursor 124 * @param alternateName the alternate name to insert in the second column of the cursor 125 * @return the newly created cursor 126 */ 127 private Cursor createCursor(String name, String alternateName) { 128 MatrixCursor cursor = new MatrixCursor(new String[]{"Name", "AlternateName"}); 129 cursor.moveToFirst(); 130 cursor.addRow(new Object[]{name, alternateName}); 131 return cursor; 132 } 133} 134