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