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.edit;
17 
18 import static androidx.test.espresso.Espresso.onView;
19 import static androidx.test.espresso.action.ViewActions.click;
20 import static androidx.test.espresso.matcher.ViewMatchers.withText;
21 
22 import android.app.Instrumentation;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.support.test.uiautomator.UiDevice;
26 import android.view.Surface;
27 
28 import androidx.test.InstrumentationRegistry;
29 import androidx.test.runner.AndroidJUnit4;
30 
31 import com.android.emergency.R;
32 
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 
38 /** Unit tests for {@link EditMedicalInfoActivity}. */
39 @RunWith(AndroidJUnit4.class)
40 public final class EditMedicalInfoActivityTest {
41     private Instrumentation mInstrumentation;
42     private Context mTargetContext;
43     private UiDevice mDevice;
44     private int mInitialRotation;
45 
46     @Before
setUp()47     public void setUp() {
48         mInstrumentation = InstrumentationRegistry.getInstrumentation();
49         mTargetContext = mInstrumentation.getTargetContext();
50         mDevice = UiDevice.getInstance(mInstrumentation);
51         mInitialRotation = mDevice.getDisplayRotation();
52     }
53 
54     @After
tearDown()55     public void tearDown() {
56         // Restore orientation prior to starting test.
57         try {
58             switch (mInitialRotation) {
59                 case Surface.ROTATION_90:
60                     mDevice.setOrientationRight();
61                     break;
62                 case Surface.ROTATION_270:
63                     mDevice.setOrientationLeft();
64                     break;
65                 default:
66                     mDevice.setOrientationNatural();
67                     break;
68             }
69         } catch (Exception e) {
70             // Squelch and move along.
71         }
72     }
73 
74     @Test
testRotate_editNameDialogOpen_shouldNotCrash()75     public void testRotate_editNameDialogOpen_shouldNotCrash() throws Exception {
76         final Intent editActivityIntent = new Intent(mTargetContext, EditMedicalInfoActivity.class);
77         EditMedicalInfoActivity activity =
78                 (EditMedicalInfoActivity) mInstrumentation.startActivitySync(editActivityIntent);
79         EditMedicalInfoFragment fragment = activity.getFragment();
80 
81         // Click on the Name field to pop up the name edit dialog.
82         onView(withText(R.string.name)).perform(click());
83 
84         // Rotate the device.
85         mDevice.setOrientationLeft();
86         mDevice.setOrientationNatural();
87         mDevice.setOrientationRight();
88     }
89 }
90