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 androidx.test.espresso.Espresso.onView; 19 import static androidx.test.espresso.action.ViewActions.click; 20 import static androidx.test.espresso.matcher.ViewMatchers.isEnabled; 21 import static androidx.test.espresso.matcher.ViewMatchers.withId; 22 import static androidx.test.espresso.matcher.ViewMatchers.withText; 23 24 import static com.google.common.truth.Truth.assertThat; 25 26 import android.app.Instrumentation; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.net.Uri; 30 import android.os.Looper; 31 import android.view.View; 32 33 import androidx.preference.PreferenceManager; 34 import androidx.test.InstrumentationRegistry; 35 import androidx.test.espresso.UiController; 36 import androidx.test.espresso.ViewAction; 37 import androidx.test.runner.AndroidJUnit4; 38 39 import com.android.emergency.ContactTestUtils; 40 import com.android.emergency.PreferenceKeys; 41 import com.android.emergency.R; 42 import com.android.emergency.edit.EditInfoActivity; 43 import com.android.emergency.edit.EditInfoFragment; 44 45 import org.hamcrest.Matcher; 46 import org.junit.After; 47 import org.junit.Before; 48 import org.junit.BeforeClass; 49 import org.junit.Test; 50 import org.junit.runner.RunWith; 51 52 /** Unit tests for {@link EmergencyContactsPreference}. */ 53 @RunWith(AndroidJUnit4.class) 54 public final class EmergencyContactsPreferenceTest { 55 private static final String NAME = "Jane"; 56 private static final String PHONE_NUMBER = "456"; 57 58 private Instrumentation mInstrumentation; 59 private Context mTargetContext; 60 private EmergencyContactsPreference mPreference; 61 62 @BeforeClass oneTimeSetup()63 public static void oneTimeSetup() { 64 if (Looper.myLooper() == null) { 65 Looper.prepare(); 66 } 67 } 68 69 @Before setUp()70 public void setUp() { 71 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 72 mTargetContext = mInstrumentation.getTargetContext(); 73 74 // In case a previous test crashed or failed, clear any previous shared preference value. 75 PreferenceManager.getDefaultSharedPreferences(mTargetContext).edit().clear().commit(); 76 77 // Create a contact that'll be used in each unit test. 78 final Uri contactUri = ContactTestUtils.createContact( 79 mTargetContext.getContentResolver(), NAME, PHONE_NUMBER); 80 PreferenceManager.getDefaultSharedPreferences(mTargetContext) 81 .edit().putString(PreferenceKeys.KEY_EMERGENCY_CONTACTS, contactUri.toString()) 82 .commit(); 83 84 mPreference = startActivityAndGetEmergencyContactsPreference(); 85 mPreference.addNewEmergencyContact(contactUri); 86 } 87 88 @After tearDown()89 public void tearDown() { 90 // Clean up the inserted contact 91 assertThat(ContactTestUtils.deleteContact( 92 mTargetContext.getContentResolver(), NAME, PHONE_NUMBER)).isTrue(); 93 PreferenceManager.getDefaultSharedPreferences(mTargetContext).edit().clear().commit(); 94 } 95 96 @Test testWidgetClick_positiveButton()97 public void testWidgetClick_positiveButton() { 98 assertThat(mPreference.getEmergencyContacts()).hasSize(1); 99 assertThat(mPreference.getPreferenceCount()).isEqualTo(1); 100 101 onView(withId(R.id.delete_contact)).perform(new RelaxedClick()); 102 onView(withText(R.string.remove)).perform(click()); 103 104 assertThat(mPreference.getEmergencyContacts()).isEmpty(); 105 assertThat(mPreference.getPreferenceCount()).isEqualTo(0); 106 } 107 108 @Test testWidgetClick_negativeButton()109 public void testWidgetClick_negativeButton() { 110 assertThat(mPreference.getEmergencyContacts()).hasSize(1); 111 assertThat(mPreference.getPreferenceCount()).isEqualTo(1); 112 113 onView(withId(R.id.delete_contact)).perform(new RelaxedClick()); 114 onView(withText(android.R.string.cancel)).perform(click()); 115 116 assertThat(mPreference.getEmergencyContacts()).hasSize(1); 117 assertThat(mPreference.getPreferenceCount()).isEqualTo(1); 118 } 119 startActivityAndGetEmergencyContactsPreference()120 private EmergencyContactsPreference startActivityAndGetEmergencyContactsPreference() { 121 final Intent editActivityIntent = new Intent(mTargetContext, EditInfoActivity.class); 122 EditInfoActivity activity = 123 (EditInfoActivity) mInstrumentation.startActivitySync(editActivityIntent); 124 EditInfoFragment fragment = (EditInfoFragment) activity.getFragment(); 125 126 return (EmergencyContactsPreference) fragment.findPreference( 127 PreferenceKeys.KEY_EMERGENCY_CONTACTS); 128 } 129 130 /** ViewAction that allows a click even when the UI is partially obscured. */ 131 private static class RelaxedClick implements ViewAction { 132 @Override getConstraints()133 public Matcher<View> getConstraints() { 134 // No constraints, the caller ensures them. 135 return isEnabled(); 136 } 137 138 @Override getDescription()139 public String getDescription() { 140 return "single click, no constraints!"; 141 } 142 143 @Override perform(UiController uiController, View view)144 public void perform(UiController uiController, View view) { 145 view.performClick(); 146 } 147 } 148 } 149