• 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.view;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.app.Instrumentation;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.support.test.uiautomator.UiDevice;
24 import android.util.Pair;
25 import android.view.Surface;
26 import android.view.View;
27 import android.widget.TextView;
28 import android.widget.ViewFlipper;
29 
30 import androidx.fragment.app.Fragment;
31 import androidx.preference.PreferenceManager;
32 import androidx.test.InstrumentationRegistry;
33 import androidx.test.runner.AndroidJUnit4;
34 
35 import com.android.emergency.ContactTestUtils;
36 import com.android.emergency.PreferenceKeys;
37 import com.android.emergency.R;
38 import com.android.emergency.edit.EditInfoActivity;
39 
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 
45 import java.util.ArrayList;
46 
47 /** Unit tests for {@link ViewInfoActivity}. */
48 @RunWith(AndroidJUnit4.class)
49 public class ViewInfoActivityTest {
50     private Instrumentation mInstrumentation;
51     private Context mTargetContext;
52     private UiDevice mDevice;
53     private int mInitialRotation;
54     private ViewInfoActivity mActivity;
55 
56     @Before
setUp()57     public void setUp() {
58         mInstrumentation = InstrumentationRegistry.getInstrumentation();
59         mTargetContext = mInstrumentation.getTargetContext();
60         mDevice = UiDevice.getInstance(mInstrumentation);
61         mInitialRotation = mDevice.getDisplayRotation();
62     }
63 
64     @After
tearDown()65     public void tearDown() {
66         // Restore orientation prior to starting test.
67         try {
68             switch (mInitialRotation) {
69                 case Surface.ROTATION_90:
70                     mDevice.setOrientationRight();
71                     break;
72                 case Surface.ROTATION_270:
73                     mDevice.setOrientationLeft();
74                     break;
75                 default:
76                     mDevice.setOrientationNatural();
77                     break;
78             }
79         } catch (Exception e) {
80             // Squelch and move along.
81         }
82         PreferenceManager.getDefaultSharedPreferences(mTargetContext).edit().clear().commit();
83     }
84 
85     @Test
testInitialState()86     public void testInitialState() {
87         ViewInfoActivity activity = startViewInfoActivity();
88 
89         assertThat(activity.getFragments()).isEmpty();
90         assertThat(activity.findViewById(R.id.name_and_dob_linear_layout).getVisibility())
91                 .isEqualTo(View.GONE);
92         assertThat(activity.getTabLayout().getVisibility()).isEqualTo(View.GONE);
93 
94         ViewFlipper viewFlipper = (ViewFlipper) activity.findViewById(R.id.view_flipper);
95         int noInfoIndex = viewFlipper.indexOfChild(activity.findViewById(R.id.no_info));
96         assertThat(viewFlipper.getDisplayedChild()).isEqualTo(noInfoIndex);
97     }
98 
99     @Test
testNameSet()100     public void testNameSet() {
101         final String name = "John";
102         PreferenceManager.getDefaultSharedPreferences(mTargetContext)
103                 .edit().putString(PreferenceKeys.KEY_NAME, name).commit();
104 
105         ViewInfoActivity activity = startViewInfoActivity();
106 
107         assertThat(activity.getFragments()).isEmpty();
108         assertThat(activity.getTabLayout().getVisibility()).isEqualTo(View.GONE);
109         assertThat(activity.findViewById(R.id.no_info).getVisibility())
110                 .isEqualTo(View.VISIBLE);
111 
112         ViewFlipper viewFlipper = (ViewFlipper) activity.findViewById(R.id.view_flipper);
113         int noInfoIndex = viewFlipper.indexOfChild(activity.findViewById(R.id.no_info));
114         assertThat(viewFlipper.getDisplayedChild()).isEqualTo(noInfoIndex);
115 
116         TextView personalCardLargeItem = (TextView) activity.findViewById(R.id.personal_card_large);
117         assertThat(personalCardLargeItem.getVisibility()).isEqualTo(View.VISIBLE);
118         assertThat(personalCardLargeItem.getText()).isEqualTo(name);
119     }
120 
121     @Test
testMedicalInfoSet()122     public void testMedicalInfoSet() {
123         final String allergies = "Peanuts";
124         PreferenceManager.getDefaultSharedPreferences(mTargetContext)
125                 .edit().putString(PreferenceKeys.KEY_ALLERGIES, allergies).commit();
126 
127         ViewInfoActivity activity = startViewInfoActivity();
128 
129         assertThat(activity.getTabLayout().getVisibility()).isEqualTo(View.GONE);
130         ViewFlipper viewFlipper = (ViewFlipper) activity.findViewById(R.id.view_flipper);
131         int tabsIndex = viewFlipper.indexOfChild(activity.findViewById(R.id.tabs));
132         assertThat(viewFlipper.getDisplayedChild()).isEqualTo(tabsIndex);
133 
134         ArrayList<Pair<String, Fragment>> fragments = activity.getFragments();
135         assertThat(fragments).hasSize(1);
136         ViewEmergencyInfoFragment viewEmergencyInfoFragment =
137                 (ViewEmergencyInfoFragment) fragments.get(0).second;
138         assertThat(viewEmergencyInfoFragment).isNotNull();
139     }
140 
141     @Test
testEmergencyContactsSet()142     public void testEmergencyContactsSet() {
143         final String emergencyContact =
144                 ContactTestUtils.createContact(mTargetContext.getContentResolver(),
145                         "John", "123").toString();
146         PreferenceManager.getDefaultSharedPreferences(mTargetContext)
147                 .edit().putString(PreferenceKeys.KEY_EMERGENCY_CONTACTS, emergencyContact).commit();
148 
149         ViewInfoActivity activity = startViewInfoActivity();
150 
151         ViewFlipper viewFlipper = (ViewFlipper) activity.findViewById(R.id.view_flipper);
152         int tabsIndex = viewFlipper.indexOfChild(activity.findViewById(R.id.tabs));
153         assertThat(viewFlipper.getDisplayedChild()).isEqualTo(tabsIndex);
154 
155         ArrayList<Pair<String, Fragment>> fragments = activity.getFragments();
156         assertThat(fragments).hasSize(1);
157         ViewEmergencyContactsFragment viewEmergencyContactsFragment =
158                 (ViewEmergencyContactsFragment) fragments.get(0).second;
159         assertThat(viewEmergencyContactsFragment).isNotNull();
160 
161         assertThat(
162                 ContactTestUtils.deleteContact(mTargetContext.getContentResolver(), "John", "123"))
163                 .isTrue();
164     }
165 
166     @Test
testMedicalInfoAndEmergencyContactsSet()167     public void testMedicalInfoAndEmergencyContactsSet() {
168         final String emergencyContact =
169                 ContactTestUtils.createContact(mTargetContext.getContentResolver(),
170                         "John", "123").toString();
171         PreferenceManager.getDefaultSharedPreferences(mTargetContext)
172                 .edit().putString(PreferenceKeys.KEY_EMERGENCY_CONTACTS, emergencyContact).commit();
173         final String allergies = "Peanuts";
174         PreferenceManager.getDefaultSharedPreferences(mTargetContext)
175                 .edit().putString(PreferenceKeys.KEY_ALLERGIES, allergies).commit();
176 
177         ViewInfoActivity activity = startViewInfoActivity();
178 
179         ViewFlipper viewFlipper = (ViewFlipper) activity.findViewById(R.id.view_flipper);
180         int tabsIndex = viewFlipper.indexOfChild(activity.findViewById(R.id.tabs));
181         assertThat(viewFlipper.getDisplayedChild()).isEqualTo(tabsIndex);
182 
183         ArrayList<Pair<String, Fragment>> fragments = activity.getFragments();
184         assertThat(fragments).hasSize(2);
185         ViewEmergencyInfoFragment viewEmergencyInfoFragment =
186                 (ViewEmergencyInfoFragment) fragments.get(0).second;
187         assertThat(viewEmergencyInfoFragment).isNotNull();
188         ViewEmergencyContactsFragment viewEmergencyContactsFragment =
189                 (ViewEmergencyContactsFragment) fragments.get(1).second;
190         assertThat(viewEmergencyContactsFragment).isNotNull();
191 
192         assertThat(
193                 ContactTestUtils.deleteContact(mTargetContext.getContentResolver(), "John", "123"))
194                 .isTrue();
195     }
196 
197     @Test
testCanGoToEditInfoActivityFromMenu()198     public void testCanGoToEditInfoActivityFromMenu() {
199         ViewInfoActivity activity = startViewInfoActivity();
200 
201         Instrumentation.ActivityMonitor activityMonitor =
202                 mInstrumentation.addMonitor(EditInfoActivity.class.getName(),
203                         null /* result */, false /* block */);
204 
205         activity.getMenu().performIdentifierAction(R.id.action_edit, 0 /* flags */);
206 
207         EditInfoActivity editInfoActivity = (EditInfoActivity)
208                 mInstrumentation.waitForMonitorWithTimeout(activityMonitor, 1000 /* timeOut */);
209         assertThat(editInfoActivity).isNotNull();
210         assertThat(mInstrumentation.checkMonitorHit(activityMonitor, 1 /* minHits */)).isTrue();
211         editInfoActivity.finish();
212     }
213 
214     @Test
testRotate_shouldNotCrash()215     public void testRotate_shouldNotCrash() throws Exception {
216         ViewInfoActivity activity = startViewInfoActivity();
217 
218         // Rotate the device.
219         mDevice.setOrientationLeft();
220         mDevice.setOrientationNatural();
221         mDevice.setOrientationRight();
222     }
223 
startViewInfoActivity()224     private ViewInfoActivity startViewInfoActivity() {
225         final Intent viewActivityIntent = new Intent(mTargetContext, ViewInfoActivity.class);
226         return (ViewInfoActivity) mInstrumentation.startActivitySync(viewActivityIntent);
227     }
228 }
229