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.view; 17 18 import android.app.Fragment; 19 import android.app.Instrumentation; 20 import android.content.ComponentName; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.content.pm.PackageManager; 24 import android.preference.PreferenceManager; 25 import android.test.ActivityInstrumentationTestCase2; 26 import android.test.suitebuilder.annotation.LargeTest; 27 import android.util.Pair; 28 import android.view.KeyEvent; 29 import android.view.View; 30 import android.widget.LinearLayout; 31 import android.widget.TextView; 32 import android.widget.ViewFlipper; 33 34 import com.android.emergency.ContactTestUtils; 35 import com.android.emergency.PreferenceKeys; 36 import com.android.emergency.R; 37 import com.android.emergency.edit.EditInfoActivity; 38 39 import java.util.ArrayList; 40 import java.util.Calendar; 41 42 /** 43 * Tests for {@link ViewInfoActivity}. 44 */ 45 @LargeTest 46 public class ViewInfoActivityTest extends ActivityInstrumentationTestCase2<ViewInfoActivity> { 47 private ArrayList<Pair<String, Fragment>> mFragments; 48 private LinearLayout mPersonalCard; 49 private TextView mPersonalCardLargeItem; 50 private ViewFlipper mViewFlipper; 51 private int mNoInfoIndex; 52 private int mTabsIndex; 53 ViewInfoActivityTest()54 public ViewInfoActivityTest() { 55 super(ViewInfoActivity.class); 56 } 57 58 @Override setUp()59 protected void setUp() throws Exception { 60 super.setUp(); 61 mPersonalCard = (LinearLayout) getActivity().findViewById(R.id.name_and_dob_linear_layout); 62 mPersonalCardLargeItem = (TextView) getActivity().findViewById(R.id.personal_card_large); 63 mViewFlipper = (ViewFlipper) getActivity().findViewById(R.id.view_flipper); 64 mNoInfoIndex = mViewFlipper 65 .indexOfChild(getActivity().findViewById(R.id.no_info)); 66 mTabsIndex = mViewFlipper.indexOfChild(getActivity().findViewById(R.id.tabs)); 67 68 PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().clear().commit(); 69 } 70 71 @Override tearDown()72 protected void tearDown() throws Exception { 73 PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().clear().commit(); 74 super.tearDown(); 75 } 76 testInitialState()77 public void testInitialState() throws Throwable { 78 onPause(); 79 onResume(); 80 81 mFragments = getActivity().getFragments(); 82 assertEquals(0, mFragments.size()); 83 assertEquals(View.GONE, mPersonalCard.getVisibility()); 84 assertEquals(View.GONE, getActivity().getTabLayout().getVisibility()); 85 assertEquals(mNoInfoIndex, mViewFlipper.getDisplayedChild()); 86 } 87 testNameSet()88 public void testNameSet() throws Throwable { 89 onPause(); 90 91 final String name = "John"; 92 PreferenceManager.getDefaultSharedPreferences(getActivity()) 93 .edit().putString(PreferenceKeys.KEY_NAME, name).commit(); 94 95 onResume(); 96 97 mFragments = getActivity().getFragments(); 98 assertEquals(0, mFragments.size()); 99 assertEquals(View.GONE, getActivity().getTabLayout().getVisibility()); 100 assertEquals(View.VISIBLE, 101 getActivity().findViewById(R.id.no_info).getVisibility()); 102 assertEquals(mNoInfoIndex, mViewFlipper.getDisplayedChild()); 103 assertEquals(View.VISIBLE, mPersonalCardLargeItem.getVisibility()); 104 assertEquals(name, mPersonalCardLargeItem.getText()); 105 } 106 testEmergencyInfoSet()107 public void testEmergencyInfoSet() throws Throwable { 108 onPause(); 109 110 final String allergies = "Peanuts"; 111 PreferenceManager.getDefaultSharedPreferences(getActivity()) 112 .edit().putString(PreferenceKeys.KEY_ALLERGIES, allergies).commit(); 113 114 onResume(); 115 116 mFragments = getActivity().getFragments(); 117 assertEquals(View.GONE, getActivity().getTabLayout().getVisibility()); 118 assertEquals(mTabsIndex, mViewFlipper.getDisplayedChild()); 119 assertEquals(1, mFragments.size()); 120 ViewEmergencyInfoFragment viewEmergencyInfoFragment = 121 (ViewEmergencyInfoFragment) mFragments.get(0).second; 122 assertNotNull(viewEmergencyInfoFragment); 123 } 124 testEmergencyContactSet()125 public void testEmergencyContactSet() throws Throwable { 126 onPause(); 127 128 final String emergencyContact = 129 ContactTestUtils.createContact(getActivity().getContentResolver(), 130 "John", "123").toString(); 131 PreferenceManager.getDefaultSharedPreferences(getActivity()) 132 .edit().putString(PreferenceKeys.KEY_EMERGENCY_CONTACTS, emergencyContact).commit(); 133 134 onResume(); 135 136 mFragments = getActivity().getFragments(); 137 assertEquals(mTabsIndex, mViewFlipper.getDisplayedChild()); 138 assertEquals(1, mFragments.size()); 139 assertEquals(View.GONE, getActivity().getTabLayout().getVisibility()); 140 ViewEmergencyContactsFragment viewEmergencyContactsFragment = 141 (ViewEmergencyContactsFragment) mFragments.get(0).second; 142 assertNotNull(viewEmergencyContactsFragment); 143 144 assertTrue(ContactTestUtils.deleteContact(getActivity().getContentResolver(), 145 "John", "123")); 146 } 147 testInfoAndEmergencyContactsSet()148 public void testInfoAndEmergencyContactsSet() throws Throwable { 149 onPause(); 150 151 final String emergencyContact = 152 ContactTestUtils.createContact(getActivity().getContentResolver(), 153 "John", "123").toString(); 154 PreferenceManager.getDefaultSharedPreferences(getActivity()).edit() 155 .putString(PreferenceKeys.KEY_EMERGENCY_CONTACTS, emergencyContact).commit(); 156 157 final String allergies = "Peanuts"; 158 PreferenceManager.getDefaultSharedPreferences(getActivity()) 159 .edit().putString(PreferenceKeys.KEY_ALLERGIES, allergies).commit(); 160 161 onResume(); 162 163 mFragments = getActivity().getFragments(); 164 assertEquals(mTabsIndex, mViewFlipper.getDisplayedChild()); 165 assertEquals(2, mFragments.size()); 166 assertEquals(View.VISIBLE, getActivity().getTabLayout().getVisibility()); 167 ViewEmergencyInfoFragment viewEmergencyInfoFragment = 168 (ViewEmergencyInfoFragment) mFragments.get(0).second; 169 assertNotNull(viewEmergencyInfoFragment); 170 ViewEmergencyContactsFragment viewEmergencyContactsFragment = 171 (ViewEmergencyContactsFragment) mFragments.get(1).second; 172 assertNotNull(viewEmergencyContactsFragment); 173 174 assertTrue(ContactTestUtils.deleteContact(getActivity().getContentResolver(), 175 "John", "123")); 176 } 177 testCanGoToEditInfoActivityFromMenu()178 public void testCanGoToEditInfoActivityFromMenu() { 179 final ViewInfoActivity activity = getActivity(); 180 181 Instrumentation.ActivityMonitor activityMonitor = 182 getInstrumentation().addMonitor(EditInfoActivity.class.getName(), 183 null /* result */, false /* block */); 184 185 activity.getMenu().performIdentifierAction(R.id.action_edit, 0 /* flags */); 186 187 EditInfoActivity editInfoActivity = (EditInfoActivity) 188 getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 1000 /* timeOut */); 189 assertNotNull(editInfoActivity); 190 assertEquals(true, getInstrumentation().checkMonitorHit(activityMonitor, 1 /* minHits */)); 191 editInfoActivity.finish(); 192 } 193 testCanGoToEditInfoActivityFromBroadcast()194 public void testCanGoToEditInfoActivityFromBroadcast() { 195 String action = "android.emergency.EDIT_EMERGENCY_CONTACTS"; 196 Instrumentation.ActivityMonitor activityMonitor = 197 getInstrumentation().addMonitor(new IntentFilter(action), 198 null /* result */, false /* block */); 199 getActivity().startActivity(new Intent(action)); 200 201 getInstrumentation().waitForIdleSync(); 202 assertEquals(true, getInstrumentation().checkMonitorHit(activityMonitor, 1 /* minHits */)); 203 204 EditInfoActivity editInfoActivity = (EditInfoActivity) 205 getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 1000 /* timeOut */); 206 assertNotNull(editInfoActivity); 207 // The contacts tab index is 1 208 assertEquals(1, editInfoActivity.getSelectedTabPosition()); 209 editInfoActivity.finish(); 210 } 211 onPause()212 private void onPause() throws Throwable { 213 runTestOnUiThread(new Runnable() { 214 @Override 215 public void run() { 216 getInstrumentation().callActivityOnPause(getActivity()); 217 } 218 }); 219 } 220 onResume()221 private void onResume() throws Throwable { 222 runTestOnUiThread(new Runnable() { 223 @Override 224 public void run() { 225 getInstrumentation().callActivityOnResume(getActivity()); 226 } 227 }); 228 getInstrumentation().waitForIdleSync(); 229 } 230 } 231 232