Home | History | Annotate | Download | only in preferences
      1 /*
      2  * Copyright (C) 2016 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 android.app.AlertDialog;
     19 import android.content.ContentResolver;
     20 import android.content.DialogInterface;
     21 import android.net.Uri;
     22 import android.preference.PreferenceManager;
     23 import android.test.ActivityInstrumentationTestCase2;
     24 import android.test.suitebuilder.annotation.LargeTest;
     25 import android.view.View;
     26 
     27 import com.android.emergency.ContactTestUtils;
     28 import com.android.emergency.PreferenceKeys;
     29 import com.android.emergency.R;
     30 import com.android.emergency.edit.EditEmergencyContactsFragment;
     31 import com.android.emergency.edit.EditInfoActivity;
     32 
     33 import java.util.ArrayList;
     34 import java.util.List;
     35 
     36 /**
     37  * Tests for {@link EmergencyContactsPreference}.
     38  */
     39 @LargeTest
     40 public class EmergencyContactsPreferenceTest
     41         extends ActivityInstrumentationTestCase2<EditInfoActivity> {
     42     private EmergencyContactsPreference mEmergencyContactsPreference;
     43     private EditEmergencyContactsFragment mEditEmergencyContactsFragment;
     44     private ContentResolver mContentResolver;
     45 
     46     public EmergencyContactsPreferenceTest() {
     47         super(EditInfoActivity.class);
     48     }
     49 
     50     @Override
     51     protected void setUp() throws Exception {
     52         super.setUp();
     53         mEditEmergencyContactsFragment =
     54                 (EditEmergencyContactsFragment) getActivity().getFragments().get(1).second;
     55         mEmergencyContactsPreference =
     56                 (EmergencyContactsPreference) mEditEmergencyContactsFragment
     57                         .findPreference(PreferenceKeys.KEY_EMERGENCY_CONTACTS);
     58         try {
     59             runTestOnUiThread(new Runnable() {
     60                 @Override
     61                 public void run() {
     62                     mEmergencyContactsPreference.setEmergencyContacts(new ArrayList<Uri>());
     63                 }
     64             });
     65         } catch (Throwable throwable) {
     66             fail("Should not fail" + throwable);
     67         }
     68 
     69         mContentResolver = getActivity().getContentResolver();
     70     }
     71 
     72     @Override
     73     protected void tearDown() throws Exception {
     74         PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().clear().commit();
     75         super.tearDown();
     76     }
     77 
     78     public void testEmptyState() {
     79         assertNotNull(mEmergencyContactsPreference);
     80         assertTrue(mEmergencyContactsPreference.isPersistent());
     81         assertTrue(mEmergencyContactsPreference.isNotSet());
     82         assertTrue(mEmergencyContactsPreference.getEmergencyContacts().isEmpty());
     83         assertEquals(0, mEmergencyContactsPreference.getPreferenceCount());
     84     }
     85 
     86     public void testAddAndRemoveEmergencyContact() throws Throwable {
     87         final String name = "Jane";
     88         final String phoneNumber = "456";
     89 
     90         final Uri emergencyContactUri =
     91                 ContactTestUtils.createContact(mContentResolver, name, phoneNumber);
     92         runTestOnUiThread(new Runnable() {
     93             @Override
     94             public void run() {
     95                 mEmergencyContactsPreference.addNewEmergencyContact(emergencyContactUri);
     96             }
     97         });
     98 
     99         assertEquals(1, mEmergencyContactsPreference.getEmergencyContacts().size());
    100         assertEquals(1, mEmergencyContactsPreference.getPreferenceCount());
    101         ContactPreference contactPreference = (ContactPreference)
    102                 mEmergencyContactsPreference.getPreference(0);
    103 
    104         assertEquals(emergencyContactUri, contactPreference.getContactUri());
    105         assertEquals(name, contactPreference.getTitle());
    106         assertTrue(((String) contactPreference.getSummary()).contains(phoneNumber));
    107 
    108         runTestOnUiThread(new Runnable() {
    109             @Override
    110             public void run() {
    111                 mEmergencyContactsPreference.onRemoveContactPreference(
    112                         (ContactPreference) mEmergencyContactsPreference.getPreference(0));
    113             }
    114         });
    115 
    116         assertEquals(0, mEmergencyContactsPreference.getEmergencyContacts().size());
    117         assertEquals(0, mEmergencyContactsPreference.getPreferenceCount());
    118 
    119         // Clean up the inserted contact
    120         assertTrue(ContactTestUtils.deleteContact(mContentResolver, name, phoneNumber));
    121     }
    122 
    123     public void testReloadFromPreference() throws Throwable {
    124         final String nameJane = "Jane";
    125         final String phoneNumberJane = "456";
    126         final Uri emergencyContactJane = ContactTestUtils
    127                 .createContact(mContentResolver, nameJane, phoneNumberJane);
    128 
    129         final String nameJohn = "John";
    130         final String phoneNumberJohn = "123";
    131         final Uri emergencyContactJohn = ContactTestUtils
    132                 .createContact(mContentResolver, nameJohn, phoneNumberJohn);
    133 
    134         final List<Uri> emergencyContacts = new ArrayList<>();
    135         emergencyContacts.add(emergencyContactJane);
    136         emergencyContacts.add(emergencyContactJohn);
    137         runTestOnUiThread(new Runnable() {
    138             @Override
    139             public void run() {
    140                 mEmergencyContactsPreference.setEmergencyContacts(emergencyContacts);
    141             }
    142         });
    143 
    144         assertEquals(2, mEmergencyContactsPreference.getEmergencyContacts().size());
    145         assertEquals(2, mEmergencyContactsPreference.getPreferenceCount());
    146 
    147         // Delete Jane from other app (e.g. contacts)
    148         assertTrue(ContactTestUtils
    149                 .deleteContact(mContentResolver, nameJane, phoneNumberJane));
    150         getInstrumentation().waitForIdleSync();
    151 
    152         runTestOnUiThread(new Runnable() {
    153             @Override
    154             public void run() {
    155                 mEmergencyContactsPreference.reloadFromPreference();
    156             }
    157         });
    158 
    159         getInstrumentation().waitForIdleSync();
    160 
    161         // Assert the only remaining contact is John
    162         assertEquals(1, mEmergencyContactsPreference.getEmergencyContacts().size());
    163         assertEquals(1, mEmergencyContactsPreference.getPreferenceCount());
    164         ContactPreference contactPreference = (ContactPreference)
    165                 mEmergencyContactsPreference.getPreference(0);
    166         assertEquals(emergencyContactJohn, contactPreference.getContactUri());
    167 
    168         // Clean up the inserted contact
    169         assertTrue(ContactTestUtils
    170                 .deleteContact(mContentResolver, nameJohn, phoneNumberJohn));
    171     }
    172 
    173     public void testWidgetClick_positiveButton() throws Throwable {
    174         final String name = "Jane";
    175         final String phoneNumber = "456";
    176 
    177         final Uri emergencyContactUri =
    178                 ContactTestUtils.createContact(mContentResolver, name, phoneNumber);
    179         runTestOnUiThread(new Runnable() {
    180             @Override
    181             public void run() {
    182                 mEmergencyContactsPreference.addNewEmergencyContact(emergencyContactUri);
    183             }
    184         });
    185 
    186         assertEquals(1, mEmergencyContactsPreference.getEmergencyContacts().size());
    187         assertEquals(1, mEmergencyContactsPreference.getPreferenceCount());
    188         ContactPreference contactPreference = (ContactPreference)
    189                 mEmergencyContactsPreference.getPreference(0);
    190 
    191         View contactPreferenceView = contactPreference.getView(null, null);
    192         assertNotNull(contactPreferenceView);
    193         final View deleteContactWidget = contactPreferenceView.findViewById(R.id.delete_contact);
    194         assertEquals(View.VISIBLE, deleteContactWidget.getVisibility());
    195 
    196         runTestOnUiThread(new Runnable() {
    197             @Override
    198             public void run() {
    199                 deleteContactWidget.performClick();
    200             }
    201         });
    202 
    203         getInstrumentation().waitForIdleSync();
    204         final AlertDialog removeContactDialog = contactPreference.getRemoveContactDialog();
    205         assertTrue(removeContactDialog.isShowing());
    206 
    207         runTestOnUiThread(new Runnable() {
    208             @Override
    209             public void run() {
    210                 removeContactDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
    211             }
    212         });
    213         getInstrumentation().waitForIdleSync();
    214 
    215         assertEquals(0, mEmergencyContactsPreference.getEmergencyContacts().size());
    216         assertEquals(0, mEmergencyContactsPreference.getPreferenceCount());
    217 
    218         // Clean up the inserted contact
    219         assertTrue(ContactTestUtils.deleteContact(mContentResolver, name, phoneNumber));
    220     }
    221 
    222     public void testWidgetClick_negativeButton() throws Throwable {
    223         final String name = "Jane";
    224         final String phoneNumber = "456";
    225 
    226         final Uri emergencyContactUri =
    227                 ContactTestUtils.createContact(mContentResolver, name, phoneNumber);
    228         runTestOnUiThread(new Runnable() {
    229             @Override
    230             public void run() {
    231                 mEmergencyContactsPreference.addNewEmergencyContact(emergencyContactUri);
    232             }
    233         });
    234 
    235         assertEquals(1, mEmergencyContactsPreference.getEmergencyContacts().size());
    236         assertEquals(1, mEmergencyContactsPreference.getPreferenceCount());
    237         ContactPreference contactPreference = (ContactPreference)
    238                 mEmergencyContactsPreference.getPreference(0);
    239 
    240         View contactPreferenceView = contactPreference.getView(null, null);
    241         assertNotNull(contactPreferenceView);
    242         final View deleteContactWidget = contactPreferenceView.findViewById(R.id.delete_contact);
    243         assertEquals(View.VISIBLE, deleteContactWidget.getVisibility());
    244 
    245         runTestOnUiThread(new Runnable() {
    246             @Override
    247             public void run() {
    248                 deleteContactWidget.performClick();
    249             }
    250         });
    251         getInstrumentation().waitForIdleSync();
    252 
    253         getInstrumentation().waitForIdleSync();
    254         final AlertDialog removeContactDialog = contactPreference.getRemoveContactDialog();
    255         assertTrue(removeContactDialog.isShowing());
    256 
    257         runTestOnUiThread(new Runnable() {
    258             @Override
    259             public void run() {
    260                 removeContactDialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick();
    261             }
    262         });
    263 
    264         assertEquals(1, mEmergencyContactsPreference.getEmergencyContacts().size());
    265         assertEquals(1, mEmergencyContactsPreference.getPreferenceCount());
    266 
    267         // Clean up the inserted contact
    268         assertTrue(ContactTestUtils.deleteContact(mContentResolver, name, phoneNumber));
    269     }
    270 }