1 /*
2  * Copyright (C) 2015 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 package com.android.messaging.ui.contact;
17 
18 import android.content.Context;
19 import android.provider.ContactsContract.CommonDataKinds.Phone;
20 import android.provider.ContactsContract.Contacts;
21 import android.view.View;
22 import android.widget.TextView;
23 
24 import com.android.messaging.FakeFactory;
25 import com.android.messaging.R;
26 import com.android.messaging.datamodel.FakeCursor;
27 import com.android.messaging.datamodel.FakeDataModel;
28 import com.android.messaging.datamodel.data.ContactListItemData;
29 import com.android.messaging.datamodel.data.TestDataFactory;
30 import com.android.messaging.ui.ContactIconView;
31 import com.android.messaging.ui.ViewTest;
32 
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 
36 public class ContactListItemViewTest extends ViewTest<ContactListItemView> {
37 
38     @Mock ContactListItemView.HostInterface mockHost;
39 
40     @Override
setUp()41     protected void setUp() throws Exception {
42         super.setUp();
43         final Context context = getInstrumentation().getTargetContext();
44         FakeFactory.register(context)
45             .withDataModel(new FakeDataModel(context));
46     }
47 
verifyAddedContactForData(final ContactListItemData data, final ContactListItemView view)48     protected void verifyAddedContactForData(final ContactListItemData data,
49             final ContactListItemView view) {
50         Mockito.verify(mockHost).onContactListItemClicked(data, view);
51     }
52 
verifyContent( final ContactListItemView view, final String contactName, final String contactDetail, final String avatarUrl, final boolean showAvatar)53     protected void verifyContent(
54             final ContactListItemView view,
55             final String contactName,
56             final String contactDetail,
57             final String avatarUrl,
58             final boolean showAvatar) {
59         final TextView contactNameView = (TextView) view.findViewById(R.id.contact_name);
60         final TextView contactDetailView = (TextView) view.findViewById(R.id.contact_details);
61         final ContactIconView avatarView = (ContactIconView) view.findViewById(R.id.contact_icon);
62 
63         assertNotNull(contactNameView);
64         assertEquals(contactName, contactNameView.getText());
65         assertNotNull(contactDetail);
66         assertEquals(contactDetail, contactDetailView.getText());
67         assertNotNull(avatarView);
68         if (showAvatar) {
69             assertTrue(avatarView.mImageRequestBinding.isBound());
70             assertEquals(View.VISIBLE, avatarView.getVisibility());
71         } else {
72             assertFalse(avatarView.mImageRequestBinding.isBound());
73             assertEquals(View.INVISIBLE, avatarView.getVisibility());
74         }
75     }
76 
testBindFirstLevel()77     public void testBindFirstLevel() {
78         final ContactListItemView view = getView();
79         final FakeCursor cursor = TestDataFactory.getAllContactListCursor();
80         final int row = TestDataFactory.CONTACT_LIST_CURSOR_FIRST_LEVEL_CONTACT_INDEX;
81         cursor.moveToPosition(row);
82         view.bind(cursor, mockHost, false, null);
83         verifyContent(view, (String) cursor.getAt(Contacts.DISPLAY_NAME, row),
84                 (String) cursor.getAt(Phone.NUMBER, row),
85                 (String) cursor.getAt(Contacts.PHOTO_THUMBNAIL_URI, row), true);
86     }
87 
testBindSecondLevel()88     public void testBindSecondLevel() {
89         final ContactListItemView view = getView();
90         final FakeCursor cursor = TestDataFactory.getAllContactListCursor();
91         final int row = TestDataFactory.CONTACT_LIST_CURSOR_SECOND_LEVEL_CONTACT_INDEX;
92         cursor.moveToPosition(row);
93         view.bind(cursor, mockHost, false, null);
94         verifyContent(view, (String) cursor.getAt(Contacts.DISPLAY_NAME, row),
95                 (String) cursor.getAt(Phone.NUMBER, row),
96                 (String) cursor.getAt(Contacts.PHOTO_THUMBNAIL_URI, row), false);
97     }
98 
testClickAddedContact()99     public void testClickAddedContact() {
100         final ContactListItemView view = getView();
101         final FakeCursor cursor = TestDataFactory.getAllContactListCursor();
102         cursor.moveToFirst();
103 
104         view.bind(cursor, mockHost, false, null);
105         view.performClick();
106         verifyAddedContactForData(view.mData, view);
107     }
108 
testBindTwice()109     public void testBindTwice() {
110         final ContactListItemView view = getView();
111         final FakeCursor cursor = TestDataFactory.getAllContactListCursor();
112 
113         cursor.moveToFirst();
114         view.bind(cursor, mockHost, false, null);
115 
116         cursor.moveToNext();
117         view.bind(cursor, mockHost, false, null);
118         verifyContent(view, (String) cursor.getAt(Contacts.DISPLAY_NAME, 1),
119                 (String) cursor.getAt(Phone.NUMBER, 1),
120                 (String) cursor.getAt(Contacts.PHOTO_THUMBNAIL_URI, 1), true);
121     }
122 
123     @Override
getLayoutIdForView()124     protected int getLayoutIdForView() {
125         return R.layout.contact_list_item_view;
126     }
127 }
128