ContactPreferencesTest.java revision 951138888055139e6ba35724472d1785f9319837
1/* 2 * Copyright (C) 2017 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 */ 16package com.android.emergency.preferences; 17 18import static com.google.common.truth.Truth.assertThat; 19import static org.mockito.Mockito.any; 20import static org.mockito.Mockito.anyInt; 21import static org.mockito.Mockito.doReturn; 22import static org.mockito.Mockito.spy; 23import static org.mockito.Mockito.when; 24 25import android.app.Instrumentation; 26import android.content.ComponentName; 27import android.content.Context; 28import android.content.ContextWrapper; 29import android.content.Intent; 30import android.content.IntentFilter; 31import android.content.pm.ActivityInfo; 32import android.content.pm.PackageManager; 33import android.content.pm.ResolveInfo; 34import android.net.Uri; 35import android.os.Looper; 36import android.provider.ContactsContract; 37import android.test.suitebuilder.annotation.SmallTest; 38 39import com.android.emergency.ContactTestUtils; 40import com.android.emergency.EmergencyContactManager; 41import com.android.emergency.TestConfig; 42 43import java.util.ArrayList; 44import java.util.List; 45 46import org.junit.Before; 47import org.junit.Rule; 48import org.junit.Test; 49import org.junit.runner.RunWith; 50import org.mockito.Mock; 51import org.mockito.MockitoAnnotations; 52import org.robolectric.RobolectricTestRunner; 53import org.robolectric.RuntimeEnvironment; 54import org.robolectric.Shadows; 55import org.robolectric.annotation.Config; 56 57/** Unit tests for {@link ContactPreferences}. */ 58@SmallTest 59@RunWith(RobolectricTestRunner.class) 60@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 61public class ContactPreferencesTest { 62 private static final String NAME = "Jake"; 63 private static final String PHONE_NUMBER = "123456"; 64 @Mock private PackageManager mPackageManager; 65 @Mock private ContactPreference.ContactFactory mContactFactory; 66 @Mock private EmergencyContactManager.Contact mContact; 67 private ContextWrapper mContext; 68 private ContactPreference mPreference; 69 private Uri mPhoneUri; 70 71 @Before 72 public void setUp() { 73 MockitoAnnotations.initMocks(this); 74 75 mContext = spy(RuntimeEnvironment.application); 76 doReturn(mPackageManager).when(mContext).getPackageManager(); 77 78 mPhoneUri = ContactTestUtils.createContact( 79 mContext.getContentResolver(), NAME, PHONE_NUMBER); 80 81 when(mContactFactory.getContact(any(), any())).thenReturn(mContact); 82 when(mContact.getName()).thenReturn(NAME); 83 when(mContact.getPhoneUri()).thenReturn(mPhoneUri); 84 when(mContact.getPhoneNumber()).thenReturn(PHONE_NUMBER); 85 when(mContact.getContactLookupUri()).thenReturn(mPhoneUri); 86 87 mPreference = new ContactPreference(mContext, mPhoneUri, mContactFactory); 88 } 89 90 @Test 91 public void testContactPreference() { 92 assertThat(mPreference.getPhoneUri()).isEqualTo(mPhoneUri); 93 assertThat(mPreference.getContact().getName()).isEqualTo(NAME); 94 assertThat(mPreference.getContact().getPhoneNumber()).isEqualTo(PHONE_NUMBER); 95 96 assertThat(mPreference.getRemoveContactDialog()).isNull(); 97 mPreference.setRemoveContactPreferenceListener( 98 new ContactPreference.RemoveContactPreferenceListener() { 99 @Override 100 public void onRemoveContactPreference(ContactPreference preference) { 101 // Do nothing 102 } 103 }); 104 assertThat(mPreference.getRemoveContactDialog()).isNotNull(); 105 } 106 107 @Test 108 public void testDisplayContact() throws Throwable { 109 mPreference.displayContact(); 110 111 Intent intent = new Intent(Intent.ACTION_VIEW); 112 intent.setData(mPhoneUri); 113 assertThat(Shadows.shadowOf(mContext).getNextStartedActivity().filterEquals(intent)) 114 .isTrue(); 115 } 116 117 @Test 118 public void testCallContact() throws Throwable { 119 final String name = "a name"; 120 final String packageName = "a package name"; 121 122 List<ResolveInfo> resolveInfos = new ArrayList<>(); 123 ResolveInfo resolveInfo = new ResolveInfo(); 124 resolveInfo.activityInfo = new ActivityInfo(); 125 resolveInfo.activityInfo.name = name; 126 resolveInfo.activityInfo.packageName = packageName; 127 resolveInfos.add(resolveInfo); 128 when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())) 129 .thenReturn(resolveInfos); 130 131 mPreference.callContact(); 132 133 Intent intent = new Intent(Intent.ACTION_CALL); 134 intent.setData(Uri.parse("tel:" + PHONE_NUMBER)); 135 intent.setComponent(new ComponentName(packageName, name)); 136 assertThat(Shadows.shadowOf(mContext).getNextStartedActivity().filterEquals(intent)) 137 .isTrue(); 138 } 139} 140