|
@@ -1,38 +1,45 @@
|
1
|
1
|
package com.reactnativenavigation.playground;
|
2
|
2
|
|
|
3
|
+import android.provider.Settings;
|
|
4
|
+import android.support.annotation.RequiresApi;
|
3
|
5
|
import android.support.test.espresso.Espresso;
|
4
|
6
|
import android.support.test.espresso.IdlingResource;
|
|
7
|
+import android.support.test.filters.SdkSuppress;
|
5
|
8
|
import android.support.test.rule.ActivityTestRule;
|
6
|
9
|
import android.support.test.runner.AndroidJUnit4;
|
7
|
10
|
import android.support.test.uiautomator.UiDevice;
|
8
|
|
-import android.support.test.uiautomator.UiObjectNotFoundException;
|
9
|
11
|
import android.support.test.uiautomator.UiSelector;
|
10
|
12
|
|
11
|
13
|
import com.facebook.react.ReactNativeHost;
|
12
|
14
|
import com.reactnativenavigation.controllers.NavigationActivity;
|
13
|
15
|
|
14
|
|
-import org.junit.FixMethodOrder;
|
15
|
16
|
import org.junit.Rule;
|
16
|
17
|
import org.junit.Test;
|
17
|
18
|
import org.junit.runner.RunWith;
|
18
|
|
-import org.junit.runners.MethodSorters;
|
19
|
|
-
|
20
|
|
-import java.io.IOException;
|
21
|
19
|
|
22
|
20
|
import static android.support.test.InstrumentationRegistry.getInstrumentation;
|
23
|
21
|
import static android.support.test.espresso.Espresso.onView;
|
24
|
22
|
import static android.support.test.espresso.assertion.ViewAssertions.matches;
|
25
|
23
|
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
|
26
|
24
|
import static android.support.test.espresso.matcher.ViewMatchers.withText;
|
|
25
|
+import static org.junit.Assume.assumeFalse;
|
|
26
|
+import static org.junit.Assume.assumeTrue;
|
27
|
27
|
|
28
|
28
|
@RunWith(AndroidJUnit4.class)
|
29
|
|
-@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
|
|
29
|
+@SdkSuppress(minSdkVersion = 23)
|
|
30
|
+@RequiresApi(api = 23)
|
30
|
31
|
public class ApplicationLifecycleTest {
|
31
|
32
|
|
32
|
33
|
@Rule
|
33
|
|
- public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, false, false);
|
|
34
|
+ public ActivityTestRule<MainActivity> rule = new ActivityTestRule<MainActivity>(MainActivity.class, false, false) {
|
|
35
|
+ @Override
|
|
36
|
+ protected void afterActivityLaunched() {
|
|
37
|
+ super.afterActivityLaunched();
|
|
38
|
+ registerIdlingResource(rule.getActivity());
|
|
39
|
+ }
|
|
40
|
+ };
|
34
|
41
|
|
35
|
|
- private void registerIdlingResource() {
|
|
42
|
+ private void registerIdlingResource(final NavigationActivity activity) {
|
36
|
43
|
Espresso.registerIdlingResources(new IdlingResource() {
|
37
|
44
|
@Override
|
38
|
45
|
public String getName() {
|
|
@@ -41,7 +48,7 @@ public class ApplicationLifecycleTest {
|
41
|
48
|
|
42
|
49
|
@Override
|
43
|
50
|
public boolean isIdleNow() {
|
44
|
|
- ReactNativeHost host = NavigationActivity.instance.getHost();
|
|
51
|
+ ReactNativeHost host = activity.getHost();
|
45
|
52
|
return host != null
|
46
|
53
|
&& host.hasInstance()
|
47
|
54
|
&& host.getReactInstanceManager().hasStartedCreatingInitialContext()
|
|
@@ -67,17 +74,30 @@ public class ApplicationLifecycleTest {
|
67
|
74
|
});
|
68
|
75
|
}
|
69
|
76
|
|
70
|
|
- @Test
|
71
|
|
- public void startApp_LoadsBridge_ThenShowsWelcomeScreen() throws UiObjectNotFoundException, IOException {
|
|
77
|
+ private void launchActivity() {
|
72
|
78
|
rule.launchActivity(null);
|
|
79
|
+ }
|
73
|
80
|
|
|
81
|
+ private void acceptPermission() throws Exception {
|
74
|
82
|
UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().text("Playground")).click();
|
75
|
83
|
UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().text("Permit drawing over other apps")).click();
|
76
|
84
|
UiDevice.getInstance(getInstrumentation()).pressBack();
|
77
|
85
|
UiDevice.getInstance(getInstrumentation()).pressBack();
|
|
86
|
+ }
|
78
|
87
|
|
79
|
|
- registerIdlingResource();
|
80
|
88
|
|
|
89
|
+ @Test
|
|
90
|
+ public void startApp_HandleOverlayPermissions_LoadsBridge_ThenShowsWelcomeScreen() throws Exception {
|
|
91
|
+ assumeFalse(Settings.canDrawOverlays(getInstrumentation().getContext()));
|
|
92
|
+ launchActivity();
|
|
93
|
+ acceptPermission();
|
|
94
|
+ onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ @Test
|
|
98
|
+ public void startApp_NoPermissionRequired_ShowsWelcomeScreen() {
|
|
99
|
+ assumeTrue(Settings.canDrawOverlays(getInstrumentation().getContext()));
|
|
100
|
+ launchActivity();
|
81
|
101
|
onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
|
82
|
102
|
}
|
83
|
103
|
}
|