• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.when;
21 
22 import android.app.Activity;
23 import android.app.Application;
24 import android.content.ContentResolver;
25 import android.content.Intent;
26 import android.net.Uri;
27 import com.android.emergency.ContactTestUtils;
28 import com.android.emergency.EmergencyContactManager;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.Robolectric;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 import org.robolectric.Shadows;
38 
39 /** Unit tests for {@link ContactPreference}. */
40 @RunWith(RobolectricTestRunner.class)
41 public class ContactPreferenceTest {
42 
43     private static final String NAME = "Jake";
44     private static final String PHONE_NUMBER = "123456";
45 
46     @Mock private ContactPreference.ContactFactory mContactFactory;
47     @Mock private EmergencyContactManager.Contact mContact;
48     private ContactPreference mPreference;
49     private Uri mPhoneUri;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54 
55         final ContentResolver contentResolver = RuntimeEnvironment.application.getContentResolver();
56         mPhoneUri = ContactTestUtils.createContact(contentResolver, NAME, PHONE_NUMBER);
57 
58         when(mContactFactory.getContact(any(), any())).thenReturn(mContact);
59         when(mContact.getName()).thenReturn(NAME);
60         when(mContact.getPhoneUri()).thenReturn(mPhoneUri);
61         when(mContact.getPhoneNumber()).thenReturn(PHONE_NUMBER);
62         when(mContact.getContactLookupUri()).thenReturn(mPhoneUri);
63 
64         final Activity activity = Robolectric.setupActivity(Activity.class);
65         mPreference = new ContactPreference(activity, mPhoneUri, mContactFactory);
66     }
67 
68     @Test
testContactPreference()69     public void testContactPreference() {
70         assertThat(mPreference.getPhoneUri()).isEqualTo(mPhoneUri);
71         assertThat(mPreference.getContact().getName()).isEqualTo(NAME);
72         assertThat(mPreference.getContact().getPhoneNumber()).isEqualTo(PHONE_NUMBER);
73 
74         assertThat(mPreference.getRemoveContactDialog()).isNull();
75         mPreference.setRemoveContactPreferenceListener(
76             preference -> {
77                 // Do nothing
78             });
79         assertThat(mPreference.getRemoveContactDialog()).isNotNull();
80     }
81 
82     @Test
testDisplayContact()83     public void testDisplayContact() {
84         mPreference.displayContact();
85 
86         final Intent expected = new Intent(Intent.ACTION_VIEW).setData(mPhoneUri);
87         final Application application = RuntimeEnvironment.application;
88         final Intent actual = Shadows.shadowOf(application).getNextStartedActivity();
89         assertThat(actual.filterEquals(expected)).isTrue();
90     }
91 }
92