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