Browse Source

android: fixing e2e

Daniel Zlotin 7 years ago
parent
commit
dbeda434de

+ 92
- 30
playground/android/app/src/androidTest/java/com/reactnativenavigation/playground/ApplicationLifecycleTest.java View File

7
 import android.support.test.rule.ActivityTestRule;
7
 import android.support.test.rule.ActivityTestRule;
8
 import android.support.test.runner.AndroidJUnit4;
8
 import android.support.test.runner.AndroidJUnit4;
9
 import android.support.test.uiautomator.UiDevice;
9
 import android.support.test.uiautomator.UiDevice;
10
+import android.support.test.uiautomator.UiObjectNotFoundException;
11
+import android.support.test.uiautomator.UiScrollable;
10
 import android.support.test.uiautomator.UiSelector;
12
 import android.support.test.uiautomator.UiSelector;
11
 
13
 
12
 import com.reactnativenavigation.views.NavigationSplashView;
14
 import com.reactnativenavigation.views.NavigationSplashView;
14
 import org.junit.After;
16
 import org.junit.After;
15
 import org.junit.Before;
17
 import org.junit.Before;
16
 import org.junit.FixMethodOrder;
18
 import org.junit.FixMethodOrder;
19
+import org.junit.Ignore;
17
 import org.junit.Rule;
20
 import org.junit.Rule;
18
 import org.junit.Test;
21
 import org.junit.Test;
19
 import org.junit.runner.RunWith;
22
 import org.junit.runner.RunWith;
27
 import static org.assertj.core.api.Java6Assertions.assertThat;
30
 import static org.assertj.core.api.Java6Assertions.assertThat;
28
 
31
 
29
 @RunWith(AndroidJUnit4.class)
32
 @RunWith(AndroidJUnit4.class)
33
+@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
30
 @SdkSuppress(minSdkVersion = 23)
34
 @SdkSuppress(minSdkVersion = 23)
31
 @TargetApi(23)
35
 @TargetApi(23)
32
-@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
33
 public class ApplicationLifecycleTest {
36
 public class ApplicationLifecycleTest {
34
 
37
 
35
     private ReactIdlingResource reactIdlingResource = new ReactIdlingResource();
38
     private ReactIdlingResource reactIdlingResource = new ReactIdlingResource();
38
     public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, false, false);
41
     public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, false, false);
39
 
42
 
40
     @Before
43
     @Before
41
-    public void beforeEach() {
44
+    public void beforeEach() throws Exception {
45
+        uiDevice().wakeUp();
46
+
42
         reactIdlingResource.start();
47
         reactIdlingResource.start();
43
         Espresso.registerIdlingResources(reactIdlingResource);
48
         Espresso.registerIdlingResources(reactIdlingResource);
49
+
50
+        rule.launchActivity(null);
44
     }
51
     }
45
 
52
 
46
     @After
53
     @After
47
     public void afterEach() {
54
     public void afterEach() {
48
-        uiDevice().waitForIdle();
49
         reactIdlingResource.stop();
55
         reactIdlingResource.stop();
50
         Espresso.unregisterIdlingResources(reactIdlingResource);
56
         Espresso.unregisterIdlingResources(reactIdlingResource);
57
+        uiDevice().waitForIdle();
51
     }
58
     }
52
 
59
 
53
-    private UiDevice uiDevice() {
54
-        return UiDevice.getInstance(getInstrumentation());
60
+    @Test
61
+    public void _1_showSplash_AcceptsOverlayPermissions_ShowsWelcomeScreen() throws Exception {
62
+        assertThat(rule.getActivity().getContentView()).isNotNull().isInstanceOf(NavigationSplashView.class);
63
+        acceptOverlayPermissionIfNeeded();
64
+        assertWelcomeShown();
55
     }
65
     }
56
 
66
 
57
-    private void acceptOverlayPermissionIfNeeded() throws Exception {
58
-        if (Settings.canDrawOverlays(getInstrumentation().getContext())) {
59
-            return;
60
-        }
67
+    @Test
68
+    public void _2_relaunchFromBackground() throws Exception {
69
+        assertWelcomeShown();
70
+
71
+        uiDevice().pressHome();
72
+        uiDevice().pressRecentApps();
61
         uiDevice().findObject(new UiSelector().text("Playground")).click();
73
         uiDevice().findObject(new UiSelector().text("Playground")).click();
62
-        uiDevice().findObject(new UiSelector().text("Permit drawing over other apps")).click();
63
-        uiDevice().pressBack();
64
-        uiDevice().pressBack();
74
+        uiDevice().waitForIdle();
75
+
76
+        assertWelcomeShown();
65
     }
77
     }
66
 
78
 
79
+
67
     @Test
80
     @Test
68
-    public void _1_ShowSplash_AcceptsOverlayPermissions_ShowsWelcomeScreen() throws Exception {
81
+    public void _3_relaunchAfterClose() throws Exception {
82
+        assertWelcomeShown();
83
+
84
+        uiDevice().pressBack();
85
+        uiDevice().waitForIdle();
86
+
69
         rule.launchActivity(null);
87
         rule.launchActivity(null);
70
-        assertThat(rule.getActivity().getContentView()).isNotNull().isInstanceOf(NavigationSplashView.class);
71
-        acceptOverlayPermissionIfNeeded();
88
+        uiDevice().waitForIdle();
89
+
72
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
90
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
73
     }
91
     }
74
 
92
 
75
     @Test
93
     @Test
76
-    public void _2_RelaunchFromBackground() throws Exception {
77
-        rule.launchActivity(null);
78
-        uiDevice().waitForIdle();
94
+    @Ignore
95
+    public void relaunchAfterActivityKilledBySystem() throws Exception {
96
+        //TODO this test should be compiled in its own APK and run separately.
97
+        assertWelcomeShown();
79
 
98
 
80
-        onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
81
-        uiDevice().waitForIdle();
99
+        setSettingsNoBackgroundProcesses();
100
+        openCalculator();
82
 
101
 
83
-        uiDevice().pressHome();
84
         uiDevice().pressRecentApps();
102
         uiDevice().pressRecentApps();
85
         uiDevice().findObject(new UiSelector().text("Playground")).click();
103
         uiDevice().findObject(new UiSelector().text("Playground")).click();
86
         uiDevice().waitForIdle();
104
         uiDevice().waitForIdle();
87
 
105
 
88
-        onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
106
+        assertWelcomeShown();
107
+
108
+        setSettingsNormalProcesses();
89
     }
109
     }
90
 
110
 
91
-    @Test
92
-    public void _3_RelaunchAfterClose() throws Exception {
93
-        rule.launchActivity(null);
111
+    private void setSettingsNoBackgroundProcesses() throws Exception {
112
+        uiDevice().pressHome();
94
         uiDevice().waitForIdle();
113
         uiDevice().waitForIdle();
114
+        uiDevice().findObject(new UiSelector().description("Apps")).clickAndWaitForNewWindow();
115
+        new UiScrollable(new UiSelector().scrollable(true)).scrollTextIntoView("Settings");
116
+        uiDevice().findObject(new UiSelector().text("Settings")).click();
117
+        new UiScrollable(new UiSelector().scrollable(true)).flingToEnd(100);
118
+        uiDevice().findObject(new UiSelector().text("Developer options")).click();
119
+        new UiScrollable(new UiSelector().scrollable(true)).flingToEnd(100);
120
+        uiDevice().findObject(new UiSelector().text("Background process limit")).click();
121
+        uiDevice().findObject(new UiSelector().text("No background processes")).click();
122
+        uiDevice().pressHome();
123
+        uiDevice().waitForIdle();
124
+    }
95
 
125
 
96
-        uiDevice().pressBack();
126
+    private void openCalculator() throws Exception {
127
+        uiDevice().pressHome();
97
         uiDevice().waitForIdle();
128
         uiDevice().waitForIdle();
129
+        uiDevice().findObject(new UiSelector().description("Apps")).clickAndWaitForNewWindow();
130
+        new UiScrollable(new UiSelector().scrollable(true)).scrollTextIntoView("Calculator");
131
+        uiDevice().findObject(new UiSelector().text("Calculator")).click();
132
+    }
98
 
133
 
99
-        rule.launchActivity(null);
134
+    private void setSettingsNormalProcesses() throws UiObjectNotFoundException {
135
+        uiDevice().pressHome();
100
         uiDevice().waitForIdle();
136
         uiDevice().waitForIdle();
137
+        uiDevice().findObject(new UiSelector().description("Apps")).clickAndWaitForNewWindow();
138
+        new UiScrollable(new UiSelector().scrollable(true)).scrollTextIntoView("Settings");
139
+        uiDevice().findObject(new UiSelector().text("Settings")).click();
140
+        new UiScrollable(new UiSelector().scrollable(true)).flingToEnd(100);
141
+        uiDevice().findObject(new UiSelector().text("Developer options")).click();
142
+        new UiScrollable(new UiSelector().scrollable(true)).flingToEnd(100);
143
+        uiDevice().findObject(new UiSelector().text("Background process limit")).click();
144
+        uiDevice().findObject(new UiSelector().text("Standard limit")).click();
145
+        uiDevice().pressHome();
146
+        uiDevice().waitForIdle();
147
+    }
101
 
148
 
149
+    private void assertWelcomeShown() {
102
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
150
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
151
+        uiDevice().waitForIdle();
103
     }
152
     }
153
+
154
+    private UiDevice uiDevice() {
155
+        return UiDevice.getInstance(getInstrumentation());
156
+    }
157
+
158
+    private void acceptOverlayPermissionIfNeeded() throws Exception {
159
+        if (Settings.canDrawOverlays(getInstrumentation().getContext())) {
160
+            return;
161
+        }
162
+        uiDevice().waitForIdle();
163
+        uiDevice().findObject(new UiSelector().text("Playground")).click();
164
+        uiDevice().findObject(new UiSelector().text("Permit drawing over other apps")).click();
165
+        uiDevice().pressBack();
166
+        uiDevice().pressBack();
167
+    }
168
+
104
 }
169
 }
105
-//        it('launch after activity killed by system', () => {
106
-//        //
107
-//        });
108
 //
170
 //
109
 //        it('launch after reactContext killed by system', () => {
171
 //        it('launch after reactContext killed by system', () => {
110
 //        //
172
 //        //