Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 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.common.list; |
| 18 | |
| 19 | import android.database.Cursor; |
| 20 | import android.database.MatrixCursor; |
| 21 | import android.provider.ContactsContract; |
| 22 | import android.test.ActivityInstrumentationTestCase2; |
| 23 | import android.test.AndroidTestCase; |
| 24 | import android.test.suitebuilder.annotation.LargeTest; |
| 25 | import android.widget.TextView; |
| 26 | |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 27 | import com.android.contacts.common.format.SpannedTestUtils; |
Yorke Lee | 426e5d4 | 2014-03-18 13:07:00 -0700 | [diff] [blame] | 28 | import com.android.contacts.common.list.ContactListItemView; |
Yorke Lee | b3d841a | 2014-07-10 11:38:55 -0700 | [diff] [blame] | 29 | import com.android.contacts.common.preference.ContactsPreferences; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 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 |
| 38 | public 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 Lee | b3d841a | 2014-07-10 11:38:55 -0700 | [diff] [blame] | 60 | view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 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"); |
Yorke Lee | b3d841a | 2014-07-10 11:38:55 -0700 | [diff] [blame] | 70 | view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 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 | |
Chiao Cheng | a1554ef | 2012-12-21 15:45:54 -0800 | [diff] [blame] | 79 | view.setHighlightedPrefix("DOE"); |
Yorke Lee | b3d841a | 2014-07-10 11:38:55 -0700 | [diff] [blame] | 80 | view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 81 | |
| 82 | CharSequence seq = view.getNameTextView().getText(); |
| 83 | assertEquals("John Doe", seq.toString()); |
| 84 | SpannedTestUtils.assertPrefixSpan(seq, 5, 7); |
Qi Wang | 8a2f9f6 | 2016-03-03 14:05:51 -0800 | [diff] [blame] | 85 | // Talback should be without span tags. |
| 86 | assertEquals("John Doe", view.getNameTextView().getContentDescription()); |
| 87 | assertFalse("John Doe".equals(seq)); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | public void testShowDisplayName_WithPrefixReversed() { |
| 91 | Cursor cursor = createCursor("John Doe", "Doe John"); |
| 92 | ContactListItemView view = createView(); |
| 93 | |
Chiao Cheng | a1554ef | 2012-12-21 15:45:54 -0800 | [diff] [blame] | 94 | view.setHighlightedPrefix("DOE"); |
Yorke Lee | b3d841a | 2014-07-10 11:38:55 -0700 | [diff] [blame] | 95 | view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 96 | |
| 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 Cheng | a1554ef | 2012-12-21 15:45:54 -0800 | [diff] [blame] | 104 | view.setHighlightedPrefix("TEST"); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 105 | 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 | } |