|
@@ -1,21 +1,21 @@
|
1
|
1
|
package com.reactnativenavigation.playground;
|
2
|
2
|
|
|
3
|
+import android.annotation.TargetApi;
|
3
|
4
|
import android.provider.Settings;
|
4
|
5
|
import android.support.test.espresso.Espresso;
|
5
|
|
-import android.support.test.espresso.IdlingResource;
|
6
|
6
|
import android.support.test.filters.SdkSuppress;
|
7
|
7
|
import android.support.test.rule.ActivityTestRule;
|
8
|
8
|
import android.support.test.runner.AndroidJUnit4;
|
9
|
9
|
import android.support.test.uiautomator.UiDevice;
|
10
|
10
|
import android.support.test.uiautomator.UiSelector;
|
11
|
11
|
|
12
|
|
-import com.facebook.react.ReactNativeHost;
|
13
|
|
-import com.reactnativenavigation.controllers.NavigationActivity;
|
14
|
12
|
import com.reactnativenavigation.views.NavigationSplashView;
|
15
|
13
|
|
|
14
|
+import org.junit.FixMethodOrder;
|
16
|
15
|
import org.junit.Rule;
|
17
|
16
|
import org.junit.Test;
|
18
|
17
|
import org.junit.runner.RunWith;
|
|
18
|
+import org.junit.runners.MethodSorters;
|
19
|
19
|
|
20
|
20
|
import static android.support.test.InstrumentationRegistry.getInstrumentation;
|
21
|
21
|
import static android.support.test.espresso.Espresso.onView;
|
|
@@ -26,77 +26,98 @@ import static org.assertj.core.api.Java6Assertions.assertThat;
|
26
|
26
|
|
27
|
27
|
@RunWith(AndroidJUnit4.class)
|
28
|
28
|
@SdkSuppress(minSdkVersion = 23)
|
|
29
|
+@TargetApi(23)
|
|
30
|
+@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
|
29
|
31
|
public class ApplicationLifecycleTest {
|
30
|
32
|
|
|
33
|
+ private ReactIdlingResource reactIdlingResource;
|
|
34
|
+
|
31
|
35
|
@Rule
|
32
|
36
|
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<MainActivity>(MainActivity.class, false, false) {
|
33
|
37
|
@Override
|
34
|
38
|
protected void afterActivityLaunched() {
|
35
|
39
|
super.afterActivityLaunched();
|
36
|
|
- registerReactIdlingResource(rule.getActivity());
|
|
40
|
+ reactIdlingResource = new ReactIdlingResource(getActivity());
|
|
41
|
+ Espresso.registerIdlingResources(reactIdlingResource);
|
37
|
42
|
}
|
38
|
|
- };
|
39
|
43
|
|
40
|
|
- private void registerReactIdlingResource(final NavigationActivity activity) {
|
41
|
|
- Espresso.registerIdlingResources(new IdlingResource() {
|
42
|
|
- @Override
|
43
|
|
- public String getName() {
|
44
|
|
- return "React Bridge";
|
45
|
|
- }
|
46
|
|
-
|
47
|
|
- @Override
|
48
|
|
- public boolean isIdleNow() {
|
49
|
|
- ReactNativeHost host = activity.getHost();
|
50
|
|
- return host != null
|
51
|
|
- && host.hasInstance()
|
52
|
|
- && host.getReactInstanceManager().hasStartedCreatingInitialContext()
|
53
|
|
- && host.getReactInstanceManager().getCurrentReactContext() != null;
|
54
|
|
- }
|
55
|
|
-
|
56
|
|
- @Override
|
57
|
|
- public void registerIdleTransitionCallback(final ResourceCallback callback) {
|
58
|
|
- new Thread(new Runnable() {
|
59
|
|
- @Override
|
60
|
|
- public void run() {
|
61
|
|
- while (!isIdleNow()) {
|
62
|
|
- try {
|
63
|
|
- Thread.sleep(1000);
|
64
|
|
- } catch (InterruptedException e) {
|
65
|
|
- e.printStackTrace();
|
66
|
|
- }
|
67
|
|
- }
|
68
|
|
- callback.onTransitionToIdle();
|
69
|
|
- }
|
70
|
|
- }).start();
|
71
|
|
- }
|
72
|
|
- });
|
73
|
|
- }
|
|
44
|
+ @Override
|
|
45
|
+ protected void afterActivityFinished() {
|
|
46
|
+ super.afterActivityFinished();
|
|
47
|
+ Espresso.unregisterIdlingResources(reactIdlingResource);
|
|
48
|
+ }
|
|
49
|
+ };
|
74
|
50
|
|
75
|
51
|
private void launchActivity() {
|
76
|
52
|
rule.launchActivity(null);
|
77
|
53
|
}
|
78
|
54
|
|
|
55
|
+ private UiDevice uiDevice() {
|
|
56
|
+ return UiDevice.getInstance(getInstrumentation());
|
|
57
|
+ }
|
|
58
|
+
|
79
|
59
|
private void acceptOverlayPermissionIfNeeded() throws Exception {
|
80
|
60
|
if (Settings.canDrawOverlays(getInstrumentation().getContext())) {
|
81
|
61
|
return;
|
82
|
62
|
}
|
83
|
|
- UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().text("Playground")).click();
|
84
|
|
- UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().text("Permit drawing over other apps")).click();
|
85
|
|
- UiDevice.getInstance(getInstrumentation()).pressBack();
|
86
|
|
- UiDevice.getInstance(getInstrumentation()).pressBack();
|
|
63
|
+ uiDevice().findObject(new UiSelector().text("Playground")).click();
|
|
64
|
+ uiDevice().findObject(new UiSelector().text("Permit drawing over other apps")).click();
|
|
65
|
+ uiDevice().pressBack();
|
|
66
|
+ uiDevice().pressBack();
|
87
|
67
|
}
|
88
|
68
|
|
89
|
69
|
@Test
|
90
|
|
- public void acceptsOverlayPermissions_ShowsWelcomeScreen() throws Exception {
|
|
70
|
+ public void _1_acceptsOverlayPermissions_ShowsWelcomeScreen() throws Exception {
|
91
|
71
|
launchActivity();
|
92
|
72
|
acceptOverlayPermissionIfNeeded();
|
93
|
73
|
onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
|
94
|
74
|
}
|
95
|
75
|
|
96
|
76
|
@Test
|
97
|
|
- public void showsSplashOnStartup() throws Exception {
|
|
77
|
+ public void _2_showsSplashOnStartup() throws Exception {
|
98
|
78
|
launchActivity();
|
99
|
79
|
assertThat(rule.getActivity().getContentView()).isNotNull().isInstanceOf(NavigationSplashView.class);
|
100
|
80
|
acceptOverlayPermissionIfNeeded();
|
101
|
81
|
}
|
|
82
|
+
|
|
83
|
+ @Test
|
|
84
|
+ public void _3_relaunchFromBackground() throws Exception {
|
|
85
|
+ launchActivity();
|
|
86
|
+ acceptOverlayPermissionIfNeeded();
|
|
87
|
+ onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
|
|
88
|
+
|
|
89
|
+ uiDevice().pressHome();
|
|
90
|
+ uiDevice().pressRecentApps();
|
|
91
|
+ uiDevice().findObject(new UiSelector().text("Playground")).click();
|
|
92
|
+
|
|
93
|
+ onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
|
|
94
|
+ }
|
102
|
95
|
}
|
|
96
|
+// xdescribe('android application lifecycle', () => {
|
|
97
|
+////launch, pause, and resume
|
|
98
|
+//
|
|
99
|
+// it('launch already running in background', () => {
|
|
100
|
+// //
|
|
101
|
+// });
|
|
102
|
+//
|
|
103
|
+// it('launch after activity killed by system', () => {
|
|
104
|
+// //
|
|
105
|
+// });
|
|
106
|
+//
|
|
107
|
+// it('launch and ask react overlay permissions', () => {
|
|
108
|
+// //
|
|
109
|
+// });
|
|
110
|
+//
|
|
111
|
+// it('launch after reactContext killed by system', () => {
|
|
112
|
+// //
|
|
113
|
+// });
|
|
114
|
+//
|
|
115
|
+// it('launch from push notification', () => {
|
|
116
|
+// //
|
|
117
|
+// });
|
|
118
|
+//
|
|
119
|
+// it('launch from intent filter', () => {
|
|
120
|
+// //
|
|
121
|
+// });
|
|
122
|
+// });
|
|
123
|
+
|