Browse Source

android:fixed android first e2e using espresso

Daniel Zlotin 8 years ago
parent
commit
7fd8580674

+ 5
- 6
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

@@ -41,23 +41,22 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
41 41
             }
42 42
         };
43 43
 
44
-        if (ReactDevPermission.shouldAskPermission()) {
45
-            ReactDevPermission.askPermission(this);
46
-            return;
47
-        }
48
-
49 44
         host.getReactInstanceManager().addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
50 45
             @Override
51 46
             public void onReactContextInitialized(ReactContext context) {
52 47
                 new NavigationEventEmitter(context).emitAppLaunched();
53 48
             }
54 49
         });
55
-        host.getReactInstanceManager().createReactContextInBackground();
56 50
     }
57 51
 
58 52
     @Override
59 53
     protected void onResume() {
60 54
         super.onResume();
55
+        if (ReactDevPermission.shouldAskPermission()) {
56
+            ReactDevPermission.askPermission(this);
57
+        } else {
58
+            host.getReactInstanceManager().createReactContextInBackground();
59
+        }
61 60
         host.getReactInstanceManager().onHostResume(this, this);
62 61
     }
63 62
 

+ 0
- 9
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationApplication.java View File

@@ -2,23 +2,14 @@ package com.reactnativenavigation.controllers;
2 2
 
3 3
 import android.app.Application;
4 4
 
5
-import com.facebook.react.bridge.ReactContext;
6
-import com.reactnativenavigation.react.NavigationEventEmitter;
7
-
8 5
 public abstract class NavigationApplication extends Application {
9 6
     public static NavigationApplication instance;
10
-    public NavigationEventEmitter eventEmitter;
11 7
 
12 8
     @Override
13 9
     public void onCreate() {
14 10
         super.onCreate();
15 11
         instance = this;
16
-
17 12
     }
18 13
 
19 14
     public abstract boolean isDebug();
20
-
21
-    public void onReactContextInitialized(ReactContext context) {
22
-        eventEmitter = new NavigationEventEmitter(context);
23
-    }
24 15
 }

+ 83
- 0
playground/android/app/src/androidTest/java/com/reactnativenavigation/playground/ApplicationLifecycleTest.java View File

@@ -0,0 +1,83 @@
1
+package com.reactnativenavigation.playground;
2
+
3
+import android.support.test.espresso.Espresso;
4
+import android.support.test.espresso.IdlingResource;
5
+import android.support.test.rule.ActivityTestRule;
6
+import android.support.test.runner.AndroidJUnit4;
7
+import android.support.test.uiautomator.UiDevice;
8
+import android.support.test.uiautomator.UiObjectNotFoundException;
9
+import android.support.test.uiautomator.UiSelector;
10
+
11
+import com.facebook.react.ReactNativeHost;
12
+import com.reactnativenavigation.controllers.NavigationActivity;
13
+
14
+import org.junit.FixMethodOrder;
15
+import org.junit.Rule;
16
+import org.junit.Test;
17
+import org.junit.runner.RunWith;
18
+import org.junit.runners.MethodSorters;
19
+
20
+import java.io.IOException;
21
+
22
+import static android.support.test.InstrumentationRegistry.getInstrumentation;
23
+import static android.support.test.espresso.Espresso.onView;
24
+import static android.support.test.espresso.assertion.ViewAssertions.matches;
25
+import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
26
+import static android.support.test.espresso.matcher.ViewMatchers.withText;
27
+
28
+@RunWith(AndroidJUnit4.class)
29
+@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
30
+public class ApplicationLifecycleTest {
31
+
32
+    @Rule
33
+    public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, false, false);
34
+
35
+    private void registerIdlingResource() {
36
+        Espresso.registerIdlingResources(new IdlingResource() {
37
+            @Override
38
+            public String getName() {
39
+                return "React Bridge";
40
+            }
41
+
42
+            @Override
43
+            public boolean isIdleNow() {
44
+                ReactNativeHost host = NavigationActivity.instance.getHost();
45
+                return host != null
46
+                        && host.hasInstance()
47
+                        && host.getReactInstanceManager().hasStartedCreatingInitialContext()
48
+                        && host.getReactInstanceManager().getCurrentReactContext() != null;
49
+            }
50
+
51
+            @Override
52
+            public void registerIdleTransitionCallback(final ResourceCallback callback) {
53
+                new Thread(new Runnable() {
54
+                    @Override
55
+                    public void run() {
56
+                        while (!isIdleNow()) {
57
+                            try {
58
+                                Thread.sleep(1000);
59
+                            } catch (InterruptedException e) {
60
+                                e.printStackTrace();
61
+                            }
62
+                        }
63
+                        callback.onTransitionToIdle();
64
+                    }
65
+                }).start();
66
+            }
67
+        });
68
+    }
69
+
70
+    @Test
71
+    public void startApp_LoadsBridge_ThenShowsWelcomeScreen() throws UiObjectNotFoundException, IOException {
72
+        rule.launchActivity(null);
73
+
74
+        UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().text("Playground")).click();
75
+        UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().text("Permit drawing over other apps")).click();
76
+        UiDevice.getInstance(getInstrumentation()).pressBack();
77
+        UiDevice.getInstance(getInstrumentation()).pressBack();
78
+
79
+        registerIdlingResource();
80
+
81
+        onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
82
+    }
83
+}

+ 0
- 52
playground/android/app/src/androidTest/java/com/reactnativenavigation/playground/ApplicationTest.java View File

@@ -1,52 +0,0 @@
1
-package com.reactnativenavigation.playground;
2
-
3
-import android.app.Activity;
4
-import android.app.Instrumentation;
5
-import android.provider.Settings;
6
-import android.support.test.espresso.intent.Intents;
7
-import android.support.test.rule.ActivityTestRule;
8
-import android.support.test.runner.AndroidJUnit4;
9
-import android.support.test.uiautomator.UiDevice;
10
-import android.support.test.uiautomator.UiObjectNotFoundException;
11
-import android.support.test.uiautomator.UiSelector;
12
-
13
-import org.junit.Rule;
14
-import org.junit.Test;
15
-import org.junit.runner.RunWith;
16
-
17
-import static android.support.test.InstrumentationRegistry.getInstrumentation;
18
-import static android.support.test.espresso.Espresso.onView;
19
-import static android.support.test.espresso.assertion.ViewAssertions.matches;
20
-import static android.support.test.espresso.intent.matcher.IntentMatchers.hasAction;
21
-import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
22
-import static android.support.test.espresso.matcher.ViewMatchers.withText;
23
-
24
-@RunWith(AndroidJUnit4.class)
25
-public class ApplicationTest {
26
-
27
-    @Rule
28
-    public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, false, false);
29
-
30
-    @Test
31
-    public void startAppShowsTheSplash() throws InterruptedException {
32
-        Intents.init();
33
-        Intents.intending(hasAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
34
-
35
-        rule.launchActivity(null);
36
-
37
-        onView(withText("Splash :)")).check(matches(isDisplayed()));
38
-        Intents.release();
39
-    }
40
-
41
-    @Test
42
-    public void startAppLoadsBridgeShowsWelcomeScreen() throws UiObjectNotFoundException {
43
-        rule.launchActivity(null);
44
-
45
-        UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().text("Playground")).click();
46
-        UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().text("Permit drawing over other apps")).click();
47
-        UiDevice.getInstance(getInstrumentation()).pressBack();
48
-        UiDevice.getInstance(getInstrumentation()).pressBack();
49
-
50
-        onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
51
-    }
52
-}

+ 1
- 1
playground/scripts/e2e.android.js View File

@@ -7,7 +7,7 @@ const release = _.includes(process.argv, 'release');
7 7
 function e2e() { //eslint-disable-line
8 8
   try {
9 9
     shellUtils.exec.execSync(`echo 'travis_fold:start:android-espresso'`);
10
-    shellUtils.exec.execSync(`cd android && ./gradlew --stop`);
10
+    shellUtils.exec.execSync(`cd android && ./gradlew uninstallAll`);
11 11
     shellUtils.exec.execSync(`cd android && ./gradlew connectedDebugAndroidTest`);
12 12
   } finally {
13 13
     shellUtils.exec.execSync(`echo 'travis_fold:end:android-espresso'`);