Browse Source

no need for espresso, uiAutomator works fast enough

Daniel Zlotin 8 years ago
parent
commit
9dd7d326f1

+ 40
- 16
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ApplicationLifecycleTest.java View File

1
 package com.reactnativenavigation.e2e.androide2e;
1
 package com.reactnativenavigation.e2e.androide2e;
2
 
2
 
3
-import android.content.Intent;
4
 import android.support.test.runner.AndroidJUnit4;
3
 import android.support.test.runner.AndroidJUnit4;
5
-import android.support.test.uiautomator.By;
6
 import android.support.test.uiautomator.UiDevice;
4
 import android.support.test.uiautomator.UiDevice;
7
-import android.support.test.uiautomator.Until;
5
+import android.support.test.uiautomator.UiScrollable;
6
+import android.support.test.uiautomator.UiSelector;
8
 
7
 
9
 import org.junit.After;
8
 import org.junit.After;
10
 import org.junit.Before;
9
 import org.junit.Before;
10
+import org.junit.FixMethodOrder;
11
 import org.junit.Test;
11
 import org.junit.Test;
12
 import org.junit.runner.RunWith;
12
 import org.junit.runner.RunWith;
13
+import org.junit.runners.MethodSorters;
14
+
15
+import java.io.IOException;
13
 
16
 
14
 import static android.support.test.InstrumentationRegistry.getInstrumentation;
17
 import static android.support.test.InstrumentationRegistry.getInstrumentation;
15
 import static org.assertj.core.api.Java6Assertions.assertThat;
18
 import static org.assertj.core.api.Java6Assertions.assertThat;
16
 
19
 
17
 @RunWith(AndroidJUnit4.class)
20
 @RunWith(AndroidJUnit4.class)
21
+@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
18
 public class ApplicationLifecycleTest {
22
 public class ApplicationLifecycleTest {
19
 
23
 
20
     private static final String PACKAGE_NAME = "com.reactnativenavigation.playground";
24
     private static final String PACKAGE_NAME = "com.reactnativenavigation.playground";
21
-    private static final int TIMEOUT = 10000;
25
+    private static final int TIMEOUT = 2000;
22
 
26
 
23
     @Before
27
     @Before
24
     public void beforeEach() {
28
     public void beforeEach() {
26
     }
30
     }
27
 
31
 
28
     @After
32
     @After
29
-    public void afterEach() {
30
-        //
33
+    public void afterEach() throws IOException {
34
+        uiDevice().executeShellCommand("am force-stop " + PACKAGE_NAME);
31
     }
35
     }
32
 
36
 
33
     @Test
37
     @Test
34
-    public void showSplash_AcceptsOverlayPermissions_ShowsWelcomeScreen() throws Exception {
38
+    public void _1_showSplash_AcceptsOverlayPermissions_ShowsWelcomeScreen() throws Exception {
35
         launchTheApp();
39
         launchTheApp();
36
 //        assertThat(uiDevice().wait(Until.hasObject(By.desc("NavigationSplashView")), TIMEOUT)).isTrue();
40
 //        assertThat(uiDevice().wait(Until.hasObject(By.desc("NavigationSplashView")), TIMEOUT)).isTrue();
37
         acceptOverlayPermissionIfNeeded();
41
         acceptOverlayPermissionIfNeeded();
38
         assertMainShown();
42
         assertMainShown();
39
     }
43
     }
40
 
44
 
45
+    @Test
46
+    public void _2_relaunchFromBackground() throws Exception {
47
+        launchTheApp();
48
+        assertMainShown();
49
+
50
+        uiDevice().pressHome();
51
+        uiDevice().pressRecentApps();
52
+        uiDevice().findObject(new UiSelector().text("Playground")).click();
53
+        assertMainShown();
54
+    }
55
+
56
+    @Test
57
+    public void _3_relaunchAfterClose() throws Exception {
58
+        launchTheApp();
59
+        assertMainShown();
60
+
61
+        uiDevice().pressBack();
62
+
63
+        launchTheApp();
64
+        assertMainShown();
65
+    }
66
+
41
     private void assertMainShown() {
67
     private void assertMainShown() {
42
-        assertThat(uiDevice().findObject(By.text("React Native Navigation!"))).isNotNull();
68
+        assertThat(uiDevice().findObject(new UiSelector().text("React Native Navigation!")).exists()).isTrue();
43
     }
69
     }
44
 
70
 
45
     private void acceptOverlayPermissionIfNeeded() throws Exception {
71
     private void acceptOverlayPermissionIfNeeded() throws Exception {
46
-        if (uiDevice().findObject(By.text("Draw over other apps")) != null) {
47
-            uiDevice().findObject(By.text("Playground")).click();
48
-            uiDevice().findObject(By.text("Permit drawing over other apps")).click();
72
+        if (uiDevice().findObject(new UiSelector().text("Draw over other apps")).exists()) {
73
+            uiDevice().findObject(new UiSelector().text("Playground")).click();
74
+            uiDevice().findObject(new UiSelector().text("Permit drawing over other apps")).click();
49
             uiDevice().pressBack();
75
             uiDevice().pressBack();
50
             uiDevice().pressBack();
76
             uiDevice().pressBack();
51
         }
77
         }
54
     private void launchTheApp() throws Exception {
80
     private void launchTheApp() throws Exception {
55
         uiDevice().wakeUp();
81
         uiDevice().wakeUp();
56
         uiDevice().pressHome();
82
         uiDevice().pressHome();
57
-        uiDevice().waitForIdle();
58
-        Intent intent = getInstrumentation().getContext().getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME);
59
-        assertThat(intent).isNotNull();
60
-        getInstrumentation().getContext().startActivity(intent);
61
-        uiDevice().wait(Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)), TIMEOUT);
83
+        uiDevice().findObject(new UiSelector().description("Apps")).clickAndWaitForNewWindow();
84
+        new UiScrollable(new UiSelector().scrollable(true)).scrollTextIntoView("Playground");
85
+        uiDevice().findObject(new UiSelector().text("Playground")).clickAndWaitForNewWindow();
62
     }
86
     }
63
 
87
 
64
     private UiDevice uiDevice() {
88
     private UiDevice uiDevice() {