Home | History | Annotate | Download | only in list
      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 
     27 import com.android.contacts.common.format.SpannedTestUtils;
     28 import com.android.contacts.common.list.ContactListItemView;
     29 import com.android.contacts.common.preference.ContactsPreferences;
     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 
     60         view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY);
     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");
     70         view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY);
     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 
     79         view.setHighlightedPrefix("DOE");
     80         view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_PRIMARY);
     81 
     82         CharSequence seq = view.getNameTextView().getText();
     83         assertEquals("John Doe", seq.toString());
     84         SpannedTestUtils.assertPrefixSpan(seq, 5, 7);
     85         // Talback should be without span tags.
     86         assertEquals("John Doe", view.getNameTextView().getContentDescription());
     87         assertFalse("John Doe".equals(seq));
     88     }
     89 
     90     public void testShowDisplayName_WithPrefixReversed() {
     91         Cursor cursor = createCursor("John Doe", "Doe John");
     92         ContactListItemView view = createView();
     93 
     94         view.setHighlightedPrefix("DOE");
     95         view.showDisplayName(cursor, 0, ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE);
     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();
    104         view.setHighlightedPrefix("TEST");
    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 }
    135