Fix failing ContactPreferenceTest cases
Test: make -j56 RunEmergencyInfoRoboTests
Change-Id: Iad16c5cca3acb0d84bf53ff2e3680ec5aaac4749
diff --git a/tests/robolectric/src/com/android/emergency/preferences/ContactPreferenceTest.java b/tests/robolectric/src/com/android/emergency/preferences/ContactPreferenceTest.java
new file mode 100644
index 0000000..e53d9a2
--- /dev/null
+++ b/tests/robolectric/src/com/android/emergency/preferences/ContactPreferenceTest.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.emergency.preferences;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.when;
+
+import android.app.Activity;
+import android.app.Application;
+import android.content.ContentResolver;
+import android.content.Intent;
+import android.net.Uri;
+import com.android.emergency.ContactTestUtils;
+import com.android.emergency.EmergencyContactManager;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.Shadows;
+
+/** Unit tests for {@link ContactPreference}. */
+@RunWith(RobolectricTestRunner.class)
+public class ContactPreferenceTest {
+
+ private static final String NAME = "Jake";
+ private static final String PHONE_NUMBER = "123456";
+
+ @Mock private ContactPreference.ContactFactory mContactFactory;
+ @Mock private EmergencyContactManager.Contact mContact;
+ private ContactPreference mPreference;
+ private Uri mPhoneUri;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+
+ final ContentResolver contentResolver = RuntimeEnvironment.application.getContentResolver();
+ mPhoneUri = ContactTestUtils.createContact(contentResolver, NAME, PHONE_NUMBER);
+
+ when(mContactFactory.getContact(any(), any())).thenReturn(mContact);
+ when(mContact.getName()).thenReturn(NAME);
+ when(mContact.getPhoneUri()).thenReturn(mPhoneUri);
+ when(mContact.getPhoneNumber()).thenReturn(PHONE_NUMBER);
+ when(mContact.getContactLookupUri()).thenReturn(mPhoneUri);
+
+ final Activity activity = Robolectric.setupActivity(Activity.class);
+ mPreference = new ContactPreference(activity, mPhoneUri, mContactFactory);
+ }
+
+ @Test
+ public void testContactPreference() {
+ assertThat(mPreference.getPhoneUri()).isEqualTo(mPhoneUri);
+ assertThat(mPreference.getContact().getName()).isEqualTo(NAME);
+ assertThat(mPreference.getContact().getPhoneNumber()).isEqualTo(PHONE_NUMBER);
+
+ assertThat(mPreference.getRemoveContactDialog()).isNull();
+ mPreference.setRemoveContactPreferenceListener(
+ preference -> {
+ // Do nothing
+ });
+ assertThat(mPreference.getRemoveContactDialog()).isNotNull();
+ }
+
+ @Test
+ public void testDisplayContact() {
+ mPreference.displayContact();
+
+ final Intent expected = new Intent(Intent.ACTION_VIEW).setData(mPhoneUri);
+ final Application application = RuntimeEnvironment.application;
+ final Intent actual = Shadows.shadowOf(application).getNextStartedActivity();
+ assertThat(actual.filterEquals(expected)).isTrue();
+ }
+}
diff --git a/tests/robolectric/src/com/android/emergency/preferences/ContactPreferencesTest.java b/tests/robolectric/src/com/android/emergency/preferences/ContactPreferencesTest.java
deleted file mode 100644
index 7d3c7d7..0000000
--- a/tests/robolectric/src/com/android/emergency/preferences/ContactPreferencesTest.java
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.emergency.preferences;
-
-import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.any;
-import static org.mockito.Mockito.anyInt;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.when;
-
-import android.content.ComponentName;
-import android.content.ContextWrapper;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.content.pm.ActivityInfo;
-import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
-import android.net.Uri;
-import android.os.Looper;
-import android.provider.ContactsContract;
-
-import com.android.emergency.ContactTestUtils;
-import com.android.emergency.EmergencyContactManager;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
-import org.robolectric.Shadows;
-
-/** Unit tests for {@link ContactPreferences}. */
-@RunWith(RobolectricTestRunner.class)
-public class ContactPreferencesTest {
- private static final String NAME = "Jake";
- private static final String PHONE_NUMBER = "123456";
- @Mock private PackageManager mPackageManager;
- @Mock private ContactPreference.ContactFactory mContactFactory;
- @Mock private EmergencyContactManager.Contact mContact;
- private ContextWrapper mContext;
- private ContactPreference mPreference;
- private Uri mPhoneUri;
-
- @Before
- public void setUp() {
- MockitoAnnotations.initMocks(this);
-
- mContext = spy(RuntimeEnvironment.application);
- doReturn(mPackageManager).when(mContext).getPackageManager();
-
- mPhoneUri = ContactTestUtils.createContact(
- mContext.getContentResolver(), NAME, PHONE_NUMBER);
-
- when(mContactFactory.getContact(any(), any())).thenReturn(mContact);
- when(mContact.getName()).thenReturn(NAME);
- when(mContact.getPhoneUri()).thenReturn(mPhoneUri);
- when(mContact.getPhoneNumber()).thenReturn(PHONE_NUMBER);
- when(mContact.getContactLookupUri()).thenReturn(mPhoneUri);
-
- mPreference = new ContactPreference(mContext, mPhoneUri, mContactFactory);
- }
-
- @Test
- public void testContactPreference() {
- assertThat(mPreference.getPhoneUri()).isEqualTo(mPhoneUri);
- assertThat(mPreference.getContact().getName()).isEqualTo(NAME);
- assertThat(mPreference.getContact().getPhoneNumber()).isEqualTo(PHONE_NUMBER);
-
- assertThat(mPreference.getRemoveContactDialog()).isNull();
- mPreference.setRemoveContactPreferenceListener(
- new ContactPreference.RemoveContactPreferenceListener() {
- @Override
- public void onRemoveContactPreference(ContactPreference preference) {
- // Do nothing
- }
- });
- assertThat(mPreference.getRemoveContactDialog()).isNotNull();
- }
-
- @Test
- public void testDisplayContact() throws Throwable {
- mPreference.displayContact();
-
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setData(mPhoneUri);
- assertThat(Shadows.shadowOf(mContext).getNextStartedActivity().filterEquals(intent))
- .isTrue();
- }
-
- @Test
- public void testCallContact() throws Throwable {
- final String name = "a name";
- final String packageName = "a package name";
-
- List<ResolveInfo> resolveInfos = new ArrayList<>();
- ResolveInfo resolveInfo = new ResolveInfo();
- resolveInfo.activityInfo = new ActivityInfo();
- resolveInfo.activityInfo.name = name;
- resolveInfo.activityInfo.packageName = packageName;
- resolveInfos.add(resolveInfo);
- when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt()))
- .thenReturn(resolveInfos);
-
- mPreference.callContact();
-
- Intent intent = new Intent(Intent.ACTION_CALL);
- intent.setData(Uri.parse("tel:" + PHONE_NUMBER));
- intent.setComponent(new ComponentName(packageName, name));
- assertThat(Shadows.shadowOf(mContext).getNextStartedActivity().filterEquals(intent))
- .isTrue();
- }
-}