Browse Source

android: fixed e2e

Daniel Zlotin 8 years ago
parent
commit
c627f403cb

+ 1
- 1
playground/android/app/src/androidTest/AndroidManifest.xml View File

3
     package="com.reactnativenavigation.playground">
3
     package="com.reactnativenavigation.playground">
4
 
4
 
5
     <uses-sdk
5
     <uses-sdk
6
-        android:minSdkVersion="18"
6
+        android:minSdkVersion="23"
7
         tools:overrideLibrary="android.support.test.uiautomator.v18" />
7
         tools:overrideLibrary="android.support.test.uiautomator.v18" />
8
 
8
 
9
 </manifest>
9
 </manifest>

+ 32
- 12
playground/android/app/src/androidTest/java/com/reactnativenavigation/playground/ApplicationLifecycleTest.java View File

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