• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.edit;
17 
18 import android.app.AlertDialog;
19 import android.app.Fragment;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.net.Uri;
23 import android.os.PowerManager;
24 import android.preference.PreferenceManager;
25 import android.test.ActivityInstrumentationTestCase2;
26 import android.test.suitebuilder.annotation.LargeTest;
27 import android.util.Pair;
28 
29 import com.android.emergency.ContactTestUtils;
30 import com.android.emergency.PreferenceKeys;
31 import com.android.emergency.R;
32 import com.android.emergency.preferences.EmergencyContactsPreference;
33 import com.android.emergency.preferences.EmergencyEditTextPreference;
34 import com.android.emergency.preferences.EmergencyListPreference;
35 import com.android.emergency.preferences.NameAutoCompletePreference;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 /**
41  * Tests for {@link EditInfoActivity}.
42  */
43 @LargeTest
44 public class EditInfoActivityTest extends ActivityInstrumentationTestCase2<EditInfoActivity> {
45     private ArrayList<Pair<String, Fragment>> mFragments;
46     private EditEmergencyInfoFragment mEditEmergencyInfoFragment;
47     private EditEmergencyContactsFragment mEditEmergencyContactsFragment;
48     private PowerManager.WakeLock mKeepScreenOnWakeLock;
49 
EditInfoActivityTest()50     public EditInfoActivityTest() {
51         super(EditInfoActivity.class);
52     }
53 
54     @Override
setUp()55     protected void setUp() throws Exception {
56         super.setUp();
57         PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().clear().commit();
58         forceScreenOn();
59 
60         mFragments = getActivity().getFragments();
61         mEditEmergencyInfoFragment = (EditEmergencyInfoFragment) mFragments.get(0).second;
62         mEditEmergencyContactsFragment = (EditEmergencyContactsFragment) mFragments.get(1).second;
63     }
64 
65     @Override
tearDown()66     protected void tearDown() throws Exception {
67         PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().clear().commit();
68         releaseScreenOn();
69         super.tearDown();
70     }
71 
testTwoFragments()72     public void testTwoFragments() {
73         assertEquals(2, mFragments.size());
74     }
75 
testInitialState()76     public void testInitialState() {
77         for (String key : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
78             assertNotNull(mEditEmergencyInfoFragment.findPreference(key));
79         }
80         EmergencyContactsPreference emergencyContactsPreference =
81                 (EmergencyContactsPreference) mEditEmergencyContactsFragment
82                         .findPreference(PreferenceKeys.KEY_EMERGENCY_CONTACTS);
83         assertNotNull(emergencyContactsPreference);
84         assertEquals(0, emergencyContactsPreference.getPreferenceCount());
85     }
86 
testClearAllPreferences()87     public void testClearAllPreferences () throws Throwable {
88         EditInfoActivity editInfoActivity = getActivity();
89         final NameAutoCompletePreference namePreference =
90                 (NameAutoCompletePreference) mEditEmergencyInfoFragment
91                         .findPreference(PreferenceKeys.KEY_NAME);
92         final EmergencyEditTextPreference addressPreference =
93                 (EmergencyEditTextPreference) mEditEmergencyInfoFragment
94                         .findPreference(PreferenceKeys.KEY_ADDRESS);
95         final EmergencyListPreference bloodTypePreference =
96                 (EmergencyListPreference) mEditEmergencyInfoFragment
97                         .findPreference(PreferenceKeys.KEY_BLOOD_TYPE);
98         final EmergencyEditTextPreference allergiesPreference =
99                 (EmergencyEditTextPreference) mEditEmergencyInfoFragment
100                         .findPreference(PreferenceKeys.KEY_ALLERGIES);
101         final EmergencyEditTextPreference medicationsPreference =
102                 (EmergencyEditTextPreference) mEditEmergencyInfoFragment
103                         .findPreference(PreferenceKeys.KEY_MEDICATIONS);
104         final EmergencyEditTextPreference medicalConditionsPreference =
105                 (EmergencyEditTextPreference) mEditEmergencyInfoFragment
106                         .findPreference(PreferenceKeys.KEY_MEDICAL_CONDITIONS);
107         final EmergencyListPreference organDonorPreference =
108                 (EmergencyListPreference) mEditEmergencyInfoFragment
109                         .findPreference(PreferenceKeys.KEY_ORGAN_DONOR);
110 
111         final EmergencyContactsPreference emergencyContactsPreference =
112                 (EmergencyContactsPreference) mEditEmergencyContactsFragment
113                         .findPreference(PreferenceKeys.KEY_EMERGENCY_CONTACTS);
114         final Uri contactUri = ContactTestUtils
115                 .createContact(editInfoActivity.getContentResolver(), "Michael", "789");
116         final List<Uri> emergencyContacts = new ArrayList<>();
117         emergencyContacts.add(contactUri);
118 
119         runTestOnUiThread(new Runnable() {
120             @Override
121             public void run() {
122                 namePreference.setText("John");
123                 addressPreference.setText("Home");
124                 bloodTypePreference.setValue("A+");
125                 allergiesPreference.setText("Peanuts");
126                 medicationsPreference.setText("Aspirin");
127                 medicalConditionsPreference.setText("Asthma");
128                 organDonorPreference.setValue("Yes");
129                 emergencyContactsPreference.setEmergencyContacts(emergencyContacts);
130             }
131         });
132 
133         String unknownName = editInfoActivity.getResources().getString(R.string.unknown_name);
134         String unknownAddress = getActivity().getResources().getString(R.string.unknown_address);
135         String unknownBloodType =
136                 editInfoActivity.getResources().getString(R.string.unknown_blood_type);
137         String unknownAllergies =
138                 editInfoActivity.getResources().getString(R.string.unknown_allergies);
139         String unknownMedications =
140                 editInfoActivity.getResources().getString(R.string.unknown_medications);
141         String unknownMedicalConditions =
142                 editInfoActivity.getResources().getString(R.string.unknown_medical_conditions);
143         String unknownOrganDonor =
144                 editInfoActivity.getResources().getString(R.string.unknown_organ_donor);
145 
146         assertNotSame(unknownName, namePreference.getSummary());
147         assertNotSame(unknownAddress, addressPreference.getSummary());
148         assertNotSame(unknownBloodType, bloodTypePreference.getSummary());
149         assertNotSame(unknownAllergies, allergiesPreference.getSummary());
150         assertNotSame(unknownMedications, medicationsPreference.getSummary());
151         assertNotSame(unknownMedicalConditions, medicalConditionsPreference.getSummary());
152         assertNotSame(unknownOrganDonor, organDonorPreference.getSummary());
153         assertEquals(1, emergencyContactsPreference.getEmergencyContacts().size());
154         assertEquals(1, emergencyContactsPreference.getPreferenceCount());
155 
156 
157         EditInfoActivity.ClearAllDialogFragment clearAllDialogFragment =
158                 (EditInfoActivity.ClearAllDialogFragment) editInfoActivity.getFragmentManager()
159                         .findFragmentByTag(EditInfoActivity.TAG_CLEAR_ALL_DIALOG);
160         assertNull(clearAllDialogFragment);
161         getInstrumentation().invokeMenuActionSync(editInfoActivity, R.id.action_clear_all,
162                 0 /* flags */);
163         getInstrumentation().waitForIdleSync();
164         final EditInfoActivity.ClearAllDialogFragment clearAllDialogFragmentAfterwards =
165                 (EditInfoActivity.ClearAllDialogFragment) editInfoActivity.getFragmentManager()
166                         .findFragmentByTag(EditInfoActivity.TAG_CLEAR_ALL_DIALOG);
167 
168         assertTrue(clearAllDialogFragmentAfterwards.getDialog().isShowing());
169 
170         runTestOnUiThread(new Runnable() {
171             @Override
172             public void run() {
173                 ((AlertDialog) clearAllDialogFragmentAfterwards.getDialog())
174                         .getButton(DialogInterface.BUTTON_POSITIVE)
175                         .performClick();
176             }
177         });
178         getInstrumentation().waitForIdleSync();
179 
180         assertEquals(unknownName, namePreference.getSummary());
181         assertEquals(unknownAddress, addressPreference.getSummary());
182         assertEquals(unknownBloodType, bloodTypePreference.getSummary().toString());
183         assertEquals(unknownAllergies, allergiesPreference.getSummary());
184         assertEquals(unknownMedications, medicationsPreference.getSummary());
185         assertEquals(unknownMedicalConditions, medicalConditionsPreference.getSummary());
186         assertEquals(unknownOrganDonor, organDonorPreference.getSummary());
187         assertEquals(0, emergencyContactsPreference.getEmergencyContacts().size());
188         assertEquals(0, emergencyContactsPreference.getPreferenceCount());
189 
190         assertTrue(ContactTestUtils
191                 .deleteContact(getActivity().getContentResolver(), "Michael", "789"));
192     }
193 
testWarningDialog_onPauseAndResume()194     public void testWarningDialog_onPauseAndResume() throws Throwable {
195         final EditInfoActivity.WarningDialogFragment dialog =
196                 (EditInfoActivity.WarningDialogFragment) getActivity().getFragmentManager()
197                         .findFragmentByTag(EditInfoActivity.TAG_WARNING_DIALOG);
198         assertTrue(dialog.getDialog().isShowing());
199 
200         onPause();
201         onResume();
202 
203         final EditInfoActivity.WarningDialogFragment dialogAfterOnResume =
204                 (EditInfoActivity.WarningDialogFragment) getActivity().getFragmentManager()
205                         .findFragmentByTag(EditInfoActivity.TAG_WARNING_DIALOG);
206         assertTrue(dialogAfterOnResume.getDialog().isShowing());
207     }
208 
testWarningDialog_negativeButton()209     public void testWarningDialog_negativeButton() throws Throwable {
210         EditInfoActivity activity = getActivity();
211         final EditInfoActivity.WarningDialogFragment dialogFragment =
212                 (EditInfoActivity.WarningDialogFragment) activity.getFragmentManager()
213                         .findFragmentByTag(EditInfoActivity.TAG_WARNING_DIALOG);
214         assertNotNull(dialogFragment.getActivity());
215         assertTrue(dialogFragment.getDialog().isShowing());
216         runTestOnUiThread(new Runnable() {
217             @Override
218             public void run() {
219                 ((AlertDialog) dialogFragment.getDialog())
220                         .getButton(DialogInterface.BUTTON_NEGATIVE)
221                         .performClick();
222             }
223         });
224         getInstrumentation().waitForIdleSync();
225 
226         assertNull(dialogFragment.getDialog());
227     }
228 
testWarningDialog_positiveButton()229     public void testWarningDialog_positiveButton() throws Throwable {
230         EditInfoActivity activity = getActivity();
231         final EditInfoActivity.WarningDialogFragment dialogFragment =
232                 (EditInfoActivity.WarningDialogFragment) activity.getFragmentManager()
233                         .findFragmentByTag(EditInfoActivity.TAG_WARNING_DIALOG);
234         assertTrue(dialogFragment.getDialog().isShowing());
235 
236         runTestOnUiThread(new Runnable() {
237             @Override
238             public void run() {
239                 ((AlertDialog) dialogFragment.getDialog())
240                         .getButton(DialogInterface.BUTTON_POSITIVE)
241                         .performClick();
242             }
243         });
244         getInstrumentation().waitForIdleSync();
245         assertNull(dialogFragment.getDialog());
246 
247         onPause();
248         onResume();
249 
250         EditInfoActivity.WarningDialogFragment dialogAfterOnResume =
251                 (EditInfoActivity.WarningDialogFragment) getActivity().getFragmentManager()
252                         .findFragmentByTag(EditInfoActivity.TAG_WARNING_DIALOG);
253         assertNull(dialogAfterOnResume);
254     }
255 
testWarningDialogTimer_overOneDayAgo()256     public void testWarningDialogTimer_overOneDayAgo() throws Throwable {
257         EditInfoActivity activity = getActivity();
258         final EditInfoActivity.WarningDialogFragment dialogFragment =
259                 (EditInfoActivity.WarningDialogFragment) activity.getFragmentManager()
260                         .findFragmentByTag(EditInfoActivity.TAG_WARNING_DIALOG);
261         runTestOnUiThread(new Runnable() {
262             @Override
263             public void run() {
264                 ((AlertDialog) dialogFragment.getDialog())
265                         .getButton(DialogInterface.BUTTON_POSITIVE)
266                         .performClick();
267             }
268         });
269         getInstrumentation().waitForIdleSync();
270 
271         onPause();
272         // Manually make the last consent be a bit over a day ago
273         long overOneDayAgoMs = System.currentTimeMillis() - EditInfoActivity.ONE_DAY_MS - 60_000;
274         PreferenceManager.getDefaultSharedPreferences(activity).edit()
275                 .putLong(EditInfoActivity.KEY_LAST_CONSENT_TIME_MS,
276                         overOneDayAgoMs).commit();
277         onResume();
278 
279         EditInfoActivity.WarningDialogFragment dialogAfterOnResume =
280                 (EditInfoActivity.WarningDialogFragment) getActivity().getFragmentManager()
281                         .findFragmentByTag(EditInfoActivity.TAG_WARNING_DIALOG);
282         assertTrue(dialogAfterOnResume.getDialog().isShowing());
283     }
284 
onPause()285     private void onPause() throws Throwable {
286         runTestOnUiThread(new Runnable() {
287             @Override
288             public void run() {
289                 getInstrumentation().callActivityOnPause(getActivity());
290             }
291         });
292     }
293 
onResume()294     private void onResume() throws Throwable {
295         runTestOnUiThread(new Runnable() {
296             @Override
297             public void run() {
298                 getInstrumentation().callActivityOnResume(getActivity());
299             }
300         });
301         getInstrumentation().waitForIdleSync();
302     }
303 
forceScreenOn()304     private void forceScreenOn() {
305         int levelAndFlags = PowerManager.FULL_WAKE_LOCK
306                 | PowerManager.ON_AFTER_RELEASE
307                 | PowerManager.ACQUIRE_CAUSES_WAKEUP;
308         PowerManager powerManager =
309                 (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
310         mKeepScreenOnWakeLock = powerManager.newWakeLock(levelAndFlags, "EditEmergencyInfo");
311         mKeepScreenOnWakeLock.setReferenceCounted(false);
312         mKeepScreenOnWakeLock.acquire();
313     }
314 
releaseScreenOn()315     private void releaseScreenOn() {
316         mKeepScreenOnWakeLock.release();
317     }
318 }
319