Browse Source

android: fixed application lifecycle

Daniel Zlotin 7 years ago
parent
commit
6a38813386

+ 14
- 13
playground/android/app/src/androidTest/java/com/reactnativenavigation/playground/ApplicationLifecycleTest.java View File

39
 
39
 
40
     @Before
40
     @Before
41
     public void beforeEach() {
41
     public void beforeEach() {
42
+        reactIdlingResource.start();
42
         Espresso.registerIdlingResources(reactIdlingResource);
43
         Espresso.registerIdlingResources(reactIdlingResource);
43
     }
44
     }
44
 
45
 
45
     @After
46
     @After
46
     public void afterEach() {
47
     public void afterEach() {
48
+        uiDevice().waitForIdle();
49
+        reactIdlingResource.stop();
47
         Espresso.unregisterIdlingResources(reactIdlingResource);
50
         Espresso.unregisterIdlingResources(reactIdlingResource);
48
     }
51
     }
49
 
52
 
70
     }
73
     }
71
 
74
 
72
     @Test
75
     @Test
73
-    public void _2_relaunchFromBackground() throws Exception {
76
+    public void _2_RelaunchFromBackground() throws Exception {
74
         rule.launchActivity(null);
77
         rule.launchActivity(null);
78
+
75
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
79
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
80
+        uiDevice().waitForIdle();
76
 
81
 
77
         uiDevice().pressHome();
82
         uiDevice().pressHome();
78
         uiDevice().pressRecentApps();
83
         uiDevice().pressRecentApps();
79
         uiDevice().findObject(new UiSelector().text("Playground")).click();
84
         uiDevice().findObject(new UiSelector().text("Playground")).click();
85
+        uiDevice().waitForIdle();
80
 
86
 
81
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
87
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
82
     }
88
     }
83
 
89
 
84
     @Test
90
     @Test
85
-    public void _3_relaunchAfterClose() throws Exception {
91
+    public void _3_RelaunchAfterClose() throws Exception {
86
         rule.launchActivity(null);
92
         rule.launchActivity(null);
93
+        uiDevice().waitForIdle();
94
+
87
         uiDevice().pressBack();
95
         uiDevice().pressBack();
96
+        uiDevice().waitForIdle();
97
+
88
         rule.launchActivity(null);
98
         rule.launchActivity(null);
99
+        uiDevice().waitForIdle();
100
+
89
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
101
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
90
     }
102
     }
91
 }
103
 }
92
-//    xdescribe('android application lifecycle', () => {
93
-////launch, pause, and resume
94
-//
95
-//        it('launch already running in background', () => {
96
-//        //
97
-//        });
98
-//
99
 //        it('launch after activity killed by system', () => {
104
 //        it('launch after activity killed by system', () => {
100
 //        //
105
 //        //
101
 //        });
106
 //        });
102
 //
107
 //
103
-//        it('launch and ask react overlay permissions', () => {
104
-//        //
105
-//        });
106
-//
107
 //        it('launch after reactContext killed by system', () => {
108
 //        it('launch after reactContext killed by system', () => {
108
 //        //
109
 //        //
109
 //        });
110
 //        });

+ 16
- 3
playground/android/app/src/androidTest/java/com/reactnativenavigation/playground/ReactIdlingResource.java View File

13
     private ResourceCallback callback;
13
     private ResourceCallback callback;
14
     private AtomicBoolean registered = new AtomicBoolean(false);
14
     private AtomicBoolean registered = new AtomicBoolean(false);
15
     private AtomicBoolean bridgeIdle = new AtomicBoolean(false);
15
     private AtomicBoolean bridgeIdle = new AtomicBoolean(false);
16
+    private AtomicBoolean shouldRun = new AtomicBoolean(false);
16
 
17
 
17
     ReactIdlingResource() {
18
     ReactIdlingResource() {
19
+    }
20
+
21
+    public void start() {
22
+        shouldRun.set(true);
18
         UiThread.postDelayed(new Runnable() {
23
         UiThread.postDelayed(new Runnable() {
19
             @Override
24
             @Override
20
             public void run() {
25
             public void run() {
21
-                if (!isIdleNow()) {
22
-                    UiThread.postDelayed(this, 10);
26
+                if (shouldRun.get()) {
27
+                    isIdleNow();
28
+                    UiThread.postDelayed(this, 100);
23
                 }
29
                 }
24
             }
30
             }
25
-        }, 10);
31
+        }, 100);
32
+    }
33
+
34
+    public void stop() {
35
+        shouldRun.set(false);
36
+        ReactNativeHost host = NavigationApplication.instance.getReactNativeHost();
37
+        if (host != null && host.getReactInstanceManager().getCurrentReactContext() != null)
38
+            host.getReactInstanceManager().getCurrentReactContext().getCatalystInstance().removeBridgeIdleDebugListener(this);
26
     }
39
     }
27
 
40
 
28
 
41
 

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

11
         android:theme="@style/AppTheme">
11
         android:theme="@style/AppTheme">
12
         <activity
12
         <activity
13
             android:name=".MainActivity"
13
             android:name=".MainActivity"
14
-            android:label="@string/app_name">
14
+            android:label="@string/app_name"
15
+            android:launchMode="singleTask">
15
             <intent-filter>
16
             <intent-filter>
16
                 <action android:name="android.intent.action.MAIN" />
17
                 <action android:name="android.intent.action.MAIN" />
17
                 <category android:name="android.intent.category.LAUNCHER" />
18
                 <category android:name="android.intent.category.LAUNCHER" />

+ 0
- 1
playground/android/app/src/main/java/com/reactnativenavigation/playground/MainActivity.java View File

3
 import com.reactnativenavigation.NavigationActivity;
3
 import com.reactnativenavigation.NavigationActivity;
4
 
4
 
5
 public class MainActivity extends NavigationActivity {
5
 public class MainActivity extends NavigationActivity {
6
-
7
 }
6
 }