瀏覽代碼

android: e2e refactor

Daniel Zlotin 8 年之前
父節點
當前提交
13b714ddc3

+ 20
- 92
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ApplicationLifecycleTest.java 查看文件

@@ -1,42 +1,13 @@
1 1
 package com.reactnativenavigation.e2e.androide2e;
2 2
 
3
-import android.support.test.runner.AndroidJUnit4;
4 3
 import android.support.test.uiautomator.By;
5
-import android.support.test.uiautomator.BySelector;
6
-import android.support.test.uiautomator.UiDevice;
7
-import android.support.test.uiautomator.UiObject;
8
-import android.support.test.uiautomator.UiObjectNotFoundException;
9
-import android.support.test.uiautomator.UiScrollable;
10
-import android.support.test.uiautomator.UiSelector;
11
-import android.support.test.uiautomator.Until;
12
-
13
-import org.junit.After;
14
-import org.junit.Before;
4
+
15 5
 import org.junit.FixMethodOrder;
16 6
 import org.junit.Test;
17
-import org.junit.runner.RunWith;
18 7
 import org.junit.runners.MethodSorters;
19 8
 
20
-import static android.support.test.InstrumentationRegistry.getInstrumentation;
21
-import static org.assertj.core.api.Java6Assertions.assertThat;
22
-
23
-@RunWith(AndroidJUnit4.class)
24 9
 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
25
-public class ApplicationLifecycleTest {
26
-
27
-	private static final String PACKAGE_NAME = "com.reactnativenavigation.playground";
28
-	private static final long TIMEOUT = 3000;
29
-
30
-	@Before
31
-	public void beforeEach() throws Exception {
32
-		uiDevice().wakeUp();
33
-		uiDevice().setOrientationNatural();
34
-	}
35
-
36
-	@After
37
-	public void afterEach() throws Exception {
38
-		uiDevice().executeShellCommand("am force-stop " + PACKAGE_NAME);
39
-	}
10
+public class ApplicationLifecycleTest extends BaseTest {
40 11
 
41 12
 	@Test
42 13
 	public void _1_acceptsOverlayPermissions_ShowsWelcomeScreen() throws Exception {
@@ -48,23 +19,23 @@ public class ApplicationLifecycleTest {
48 19
 	public void _2_relaunchFromBackground() throws Exception {
49 20
 		launchTheApp();
50 21
 		assertMainShown();
51
-		push();
52
-		assertPushedScreenShown();
22
+		elementByText("PUSH").click();
23
+		assertExists(By.text("Pushed Screen"));
53 24
 
54
-		uiDevice().pressHome();
55
-		uiDevice().pressRecentApps();
25
+		device().pressHome();
26
+		device().pressRecentApps();
56 27
 		elementByText("Playground").click();
57 28
 
58
-		assertPushedScreenShown();
29
+		assertExists(By.text("Pushed Screen"));
59 30
 	}
60 31
 
61 32
 	@Test
62 33
 	public void _3_relaunchAfterClose() throws Exception {
63 34
 		launchTheApp();
64
-		push();
65
-		assertPushedScreenShown();
35
+		elementByText("PUSH").click();
36
+		assertExists(By.text("Pushed Screen"));
66 37
 
67
-		uiDevice().pressBack();
38
+		device().pressBack();
68 39
 
69 40
 		launchTheApp();
70 41
 		assertMainShown();
@@ -73,74 +44,31 @@ public class ApplicationLifecycleTest {
73 44
 	@Test
74 45
 	public void _4_deviceOrientationDoesNotDestroyActivity() throws Exception {
75 46
 		launchTheApp();
76
-		push();
77
-		assertPushedScreenShown();
47
+		elementByText("PUSH").click();
48
+		assertExists(By.text("Pushed Screen"));
78 49
 
79
-		uiDevice().setOrientationLeft();
50
+		device().setOrientationLeft();
80 51
 		Thread.sleep(100);
81 52
 
82
-		assertPushedScreenShown();
53
+		assertExists(By.text("Pushed Screen"));
83 54
 	}
84 55
 
85 56
 	@Test
86 57
 	public void _5_relaunchAfterActivityKilledBySystem() throws Exception {
87 58
 		launchTheApp();
88
-		push();
89
-		assertPushedScreenShown();
59
+		elementByText("PUSH").click();
60
+		assertExists(By.text("Pushed Screen"));
90 61
 
91
-		uiDevice().pressHome();
92
-		uiDevice().waitForIdle();
93
-		uiDevice().executeShellCommand("am kill " + PACKAGE_NAME);
62
+		device().pressHome();
63
+		device().waitForIdle();
64
+		device().executeShellCommand("am kill " + PACKAGE_NAME);
94 65
 
95
-		uiDevice().pressRecentApps();
66
+		device().pressRecentApps();
96 67
 		elementByText("Playground").click();
97 68
 		assertMainShown();
98 69
 	}
99 70
 
100
-	private void push() throws UiObjectNotFoundException {
101
-		elementByText("PUSH").click();
102
-	}
103
-
104 71
 	private void assertMainShown() {
105 72
 		assertExists(By.text("React Native Navigation!"));
106 73
 	}
107
-
108
-	private void launchTheApp() throws Exception {
109
-		uiDevice().executeShellCommand("am start -n " + PACKAGE_NAME + "/.MainActivity");
110
-		uiDevice().waitForIdle();
111
-		acceptOverlayPermissionIfNeeded();
112
-		uiDevice().wait(Until.gone(By.textContains("Please wait")), 1000 * 60 * 3);
113
-	}
114
-
115
-	private void acceptOverlayPermissionIfNeeded() throws Exception {
116
-		if (elementByText("Draw over other apps").exists()) {
117
-			if (!elementByText("Playground").exists()) {
118
-				scrollToText("Playground");
119
-			}
120
-			elementByText("Playground").click();
121
-			elementByText("Permit drawing over other apps").click();
122
-			uiDevice().pressBack();
123
-			uiDevice().pressBack();
124
-		}
125
-	}
126
-
127
-	private UiDevice uiDevice() {
128
-		return UiDevice.getInstance(getInstrumentation());
129
-	}
130
-
131
-	private UiObject elementByText(String text) {
132
-		return uiDevice().findObject(new UiSelector().text(text));
133
-	}
134
-
135
-	private void scrollToText(String txt) throws Exception {
136
-		new UiScrollable(new UiSelector().scrollable(true)).scrollTextIntoView(txt);
137
-	}
138
-
139
-	private void assertExists(BySelector selector) {
140
-		assertThat(uiDevice().wait(Until.hasObject(selector), TIMEOUT)).isTrue();
141
-	}
142
-
143
-	private void assertPushedScreenShown() {
144
-		assertExists(By.text("Pushed Screen"));
145
-	}
146 74
 }

+ 69
- 0
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/BaseTest.java 查看文件

@@ -0,0 +1,69 @@
1
+package com.reactnativenavigation.e2e.androide2e;
2
+
3
+import android.support.test.runner.AndroidJUnit4;
4
+import android.support.test.uiautomator.By;
5
+import android.support.test.uiautomator.BySelector;
6
+import android.support.test.uiautomator.UiDevice;
7
+import android.support.test.uiautomator.UiObject;
8
+import android.support.test.uiautomator.UiScrollable;
9
+import android.support.test.uiautomator.UiSelector;
10
+import android.support.test.uiautomator.Until;
11
+
12
+import org.junit.After;
13
+import org.junit.Before;
14
+import org.junit.runner.RunWith;
15
+
16
+import static android.support.test.InstrumentationRegistry.getInstrumentation;
17
+import static org.assertj.core.api.Java6Assertions.assertThat;
18
+
19
+@RunWith(AndroidJUnit4.class)
20
+public abstract class BaseTest {
21
+	public static final String PACKAGE_NAME = "com.reactnativenavigation.playground";
22
+	public static final long TIMEOUT = 3000;
23
+
24
+	@Before
25
+	public void beforeEach() throws Exception {
26
+		device().wakeUp();
27
+		device().setOrientationNatural();
28
+	}
29
+
30
+	@After
31
+	public void afterEach() throws Exception {
32
+		device().executeShellCommand("am force-stop " + PACKAGE_NAME);
33
+	}
34
+
35
+	public UiDevice device() {
36
+		return UiDevice.getInstance(getInstrumentation());
37
+	}
38
+
39
+	public void launchTheApp() throws Exception {
40
+		device().executeShellCommand("am start -n " + PACKAGE_NAME + "/.MainActivity");
41
+		device().waitForIdle();
42
+		acceptOverlayPermissionIfNeeded();
43
+		device().wait(Until.gone(By.textContains("Please wait")), 1000 * 60 * 3);
44
+	}
45
+
46
+	public void acceptOverlayPermissionIfNeeded() throws Exception {
47
+		if (elementByText("Draw over other apps").exists()) {
48
+			if (!elementByText("Playground").exists()) {
49
+				scrollToText("Playground");
50
+			}
51
+			elementByText("Playground").click();
52
+			elementByText("Permit drawing over other apps").click();
53
+			device().pressBack();
54
+			device().pressBack();
55
+		}
56
+	}
57
+
58
+	public UiObject elementByText(String text) {
59
+		return device().findObject(new UiSelector().text(text));
60
+	}
61
+
62
+	public void scrollToText(String txt) throws Exception {
63
+		new UiScrollable(new UiSelector().scrollable(true)).scrollTextIntoView(txt);
64
+	}
65
+
66
+	public void assertExists(BySelector selector) {
67
+		assertThat(device().wait(Until.hasObject(selector), TIMEOUT)).isTrue();
68
+	}
69
+}

+ 13
- 0
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/TopLevelApiTest.java 查看文件

@@ -0,0 +1,13 @@
1
+package com.reactnativenavigation.e2e.androide2e;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.assertj.core.api.Java6Assertions.assertThat;
6
+
7
+public class TopLevelApiTest extends BaseTest {
8
+
9
+	@Test
10
+	public void switchToTabBasedApp() throws Exception {
11
+		assertThat(1 + 1).isPositive();
12
+	}
13
+}

+ 4
- 1
android/app/src/main/java/com/reactnativenavigation/react/NavigationEventEmitter.java 查看文件

@@ -7,6 +7,9 @@ import com.facebook.react.bridge.WritableMap;
7 7
 import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
8 8
 
9 9
 public class NavigationEventEmitter {
10
+	private static String onAppLaunched = "RNN.appLaunched";
11
+	private static String containerStart = "RNN.containerStart";
12
+	private static String containerStop = "RNN.containerStop";
10 13
 
11 14
 	public static NavigationEventEmitter emit(ReactContext context) {
12 15
 		return new NavigationEventEmitter(context);
@@ -19,7 +22,7 @@ public class NavigationEventEmitter {
19 22
 	}
20 23
 
21 24
 	public void appLaunched() {
22
-		emit("RNN.appLaunched");
25
+		emit(onAppLaunched);
23 26
 	}
24 27
 
25 28
 	private void emit(String eventName) {