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