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 17 package com.android.messaging.ui.contact; 18 19 import android.content.Context; 20 import android.database.Cursor; 21 import android.view.View; 22 import android.widget.ListView; 23 24 import androidx.test.filters.LargeTest; 25 import androidx.viewpager.widget.ViewPager; 26 27 import com.android.messaging.FakeFactory; 28 import com.android.messaging.R; 29 import com.android.messaging.datamodel.FakeDataModel; 30 import com.android.messaging.datamodel.action.ActionTestHelpers; 31 import com.android.messaging.datamodel.action.ActionTestHelpers.StubActionService; 32 import com.android.messaging.datamodel.action.GetOrCreateConversationAction; 33 import com.android.messaging.datamodel.data.ContactPickerData; 34 import com.android.messaging.datamodel.data.ParticipantData; 35 import com.android.messaging.datamodel.data.TestDataFactory; 36 import com.android.messaging.ui.CustomHeaderViewPagerAdapter; 37 import com.android.messaging.ui.FragmentTestCase; 38 import com.android.messaging.ui.UIIntents; 39 import com.android.messaging.ui.contact.ContactPickerFragment.ContactPickerFragmentHost; 40 41 import org.mockito.ArgumentMatchers; 42 import org.mockito.Mock; 43 import org.mockito.Mockito; 44 45 import java.util.List; 46 47 /** 48 * Unit tests for {@link ContactPickerFragment}. 49 */ 50 @LargeTest 51 public class ContactPickerFragmentTest 52 extends FragmentTestCase<ContactPickerFragment> { 53 54 @Mock protected ContactPickerData mMockContactPickerData; 55 @Mock protected UIIntents mMockUIIntents; 56 @Mock protected ContactPickerFragmentHost mockHost; 57 protected FakeDataModel mFakeDataModel; 58 private ActionTestHelpers.StubActionService mService; 59 ContactPickerFragmentTest()60 public ContactPickerFragmentTest() { 61 super(ContactPickerFragment.class); 62 } 63 64 @Override setUp()65 protected void setUp() throws Exception { 66 super.setUp(); 67 68 final Context context = getInstrumentation().getTargetContext(); 69 mService = new StubActionService(); 70 mFakeDataModel = new FakeDataModel(context) 71 .withContactPickerData(mMockContactPickerData) 72 .withActionService(mService); 73 FakeFactory.register(context) 74 .withDataModel(mFakeDataModel) 75 .withUIIntents(mMockUIIntents); 76 } 77 78 /** 79 * Helper method to initialize the ContactPickerFragment and its data. 80 */ initFragment(final int initialMode)81 private ContactPickerFragmentTest initFragment(final int initialMode) { 82 Mockito.when(mMockContactPickerData.isBound(ArgumentMatchers.anyString())) 83 .thenReturn(true); 84 85 getActivity().runOnUiThread(new Runnable() { 86 @Override 87 public void run() { 88 final ContactPickerFragment fragment = getFragment(); 89 fragment.setHost(mockHost); 90 fragment.setContactPickingMode(initialMode, false); 91 92 getActivity().setFragment(fragment); 93 Mockito.verify(mMockContactPickerData).init(fragment.getLoaderManager(), 94 fragment.mBinding); 95 } 96 }); 97 getInstrumentation().waitForIdleSync(); 98 return this; 99 } 100 101 /** 102 * Bind the datamodel with all contacts cursor to populate the all contacts list in the 103 * fragment. 104 */ loadWithAllContactsCursor(final Cursor cursor)105 private ContactPickerFragmentTest loadWithAllContactsCursor(final Cursor cursor) { 106 Mockito.when(mMockContactPickerData.isBound(ArgumentMatchers.anyString())) 107 .thenReturn(true); 108 109 getActivity().runOnUiThread(new Runnable() { 110 @Override 111 public void run() { 112 getFragment().onAllContactsCursorUpdated(cursor); 113 } 114 }); 115 getInstrumentation().waitForIdleSync(); 116 return this; 117 } 118 119 /** 120 * Bind the datamodel with frequent contacts cursor to populate the contacts list in the 121 * fragment. 122 */ loadWithFrequentContactsCursor(final Cursor cursor)123 private ContactPickerFragmentTest loadWithFrequentContactsCursor(final Cursor cursor) { 124 Mockito.when(mMockContactPickerData.isBound(ArgumentMatchers.anyString())) 125 .thenReturn(true); 126 getActivity().runOnUiThread(new Runnable() { 127 @Override 128 public void run() { 129 getFragment().onFrequentContactsCursorUpdated(cursor); 130 } 131 }); 132 getInstrumentation().waitForIdleSync(); 133 return this; 134 } 135 136 /** 137 * Test the initial state of the fragment before loading data. 138 */ testInitialState()139 public void testInitialState() { 140 initFragment(ContactPickerFragment.MODE_PICK_INITIAL_CONTACT); 141 142 // Make sure that the frequent contacts view is shown by default. 143 final ViewPager pager = (ViewPager) getFragment().getView().findViewById(R.id.pager); 144 final View currentPagedView = pager.getChildAt(pager.getCurrentItem()); 145 final View frequentContactsView = ((CustomHeaderViewPagerAdapter) pager.getAdapter()) 146 .getViewHolder(0).getView(null); 147 assertEquals(frequentContactsView, currentPagedView); 148 } 149 150 /** 151 * Verifies that list view gets correctly populated given a cursor. 152 */ testLoadAllContactsList()153 public void testLoadAllContactsList() { 154 final Cursor cursor = TestDataFactory.getAllContactListCursor(); 155 initFragment(ContactPickerFragment.MODE_PICK_INITIAL_CONTACT) 156 .loadWithAllContactsCursor(cursor); 157 final ListView listView = (ListView) getFragment().getView() 158 .findViewById(R.id.all_contacts_list); 159 assertEquals(cursor.getCount(), listView.getCount()); 160 } 161 162 /** 163 * Verifies that list view gets correctly populated given a cursor. 164 */ testLoadFrequentContactsList()165 public void testLoadFrequentContactsList() { 166 final Cursor cursor = TestDataFactory.getFrequentContactListCursor(); 167 initFragment(ContactPickerFragment.MODE_PICK_INITIAL_CONTACT) 168 .loadWithFrequentContactsCursor(cursor); 169 final ListView listView = (ListView) getFragment().getView() 170 .findViewById(R.id.frequent_contacts_list); 171 assertEquals(cursor.getCount(), listView.getCount()); 172 } 173 testPickInitialContact()174 public void testPickInitialContact() { 175 final Cursor cursor = TestDataFactory.getFrequentContactListCursor(); 176 initFragment(ContactPickerFragment.MODE_PICK_INITIAL_CONTACT) 177 .loadWithFrequentContactsCursor(cursor); 178 final ListView listView = (ListView) getFragment().getView() 179 .findViewById(R.id.frequent_contacts_list); 180 // Click on the first contact to add it. 181 final ContactListItemView cliv = (ContactListItemView) listView.getChildAt(0); 182 clickButton(cliv); 183 final ContactRecipientAutoCompleteView chipsView = (ContactRecipientAutoCompleteView) 184 getFragment().getView() 185 .findViewById(R.id.recipient_text_view); 186 // Verify the contact is added to the chips view. 187 final List<ParticipantData> participants = 188 chipsView.getRecipientParticipantDataForConversationCreation(); 189 assertEquals(1, participants.size()); 190 assertEquals(cliv.mData.getDestination(), participants.get(0).getSendDestination()); 191 assertTrue(mService.getCalls().get(0).action instanceof GetOrCreateConversationAction); 192 } 193 testLeaveChipsMode()194 public void testLeaveChipsMode() { 195 final Cursor cursor = TestDataFactory.getFrequentContactListCursor(); 196 initFragment(ContactPickerFragment.MODE_CHIPS_ONLY) 197 .loadWithFrequentContactsCursor(cursor); 198 // Click on the add more participants button 199 // TODO: Figure out a way to click on the add more participants button now that 200 // it's part of the menu. 201 // final ImageButton AddMoreParticipantsButton = (ImageButton) getFragment().getView() 202 // .findViewById(R.id.add_more_participants_button); 203 // clickButton(AddMoreParticipantsButton); 204 // Mockito.verify(mockHost).onInitiateAddMoreParticipants(); 205 } 206 testPickMoreContacts()207 public void testPickMoreContacts() { 208 final Cursor cursor = TestDataFactory.getFrequentContactListCursor(); 209 initFragment(ContactPickerFragment.MODE_PICK_MORE_CONTACTS) 210 .loadWithFrequentContactsCursor(cursor); 211 final ListView listView = (ListView) getFragment().getView() 212 .findViewById(R.id.frequent_contacts_list); 213 // Click on the first contact to add it. 214 final ContactListItemView cliv = (ContactListItemView) listView.getChildAt(0); 215 clickButton(cliv); 216 // Verify that we don't attempt to create a conversation right away. 217 assertEquals(0, mService.getCalls().size()); 218 } 219 } 220