Browse Source

android onAppear

Daniel Zlotin 7 years ago
parent
commit
30c2211cff

+ 1
- 0
lib/android/app/build.gradle View File

57
     testCompile 'junit:junit:4.12'
57
     testCompile 'junit:junit:4.12'
58
     testCompile 'org.robolectric:robolectric:3.3'
58
     testCompile 'org.robolectric:robolectric:3.3'
59
     testCompile 'org.assertj:assertj-core:2.5.0'
59
     testCompile 'org.assertj:assertj-core:2.5.0'
60
+    testCompile 'com.squareup.assertj:assertj-android:1.1.1'
60
     testCompile 'org.mockito:mockito-core:2.7.5'
61
     testCompile 'org.mockito:mockito-core:2.7.5'
61
 }
62
 }

+ 12
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java View File

4
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
5
 import android.support.annotation.Nullable;
5
 import android.support.annotation.Nullable;
6
 import android.view.View;
6
 import android.view.View;
7
+import android.view.ViewTreeObserver;
7
 
8
 
8
 import com.reactnativenavigation.utils.StringUtils;
9
 import com.reactnativenavigation.utils.StringUtils;
9
 
10
 
10
-public abstract class ViewController {
11
+public abstract class ViewController implements ViewTreeObserver.OnGlobalLayoutListener {
11
 
12
 
12
 	private final Activity activity;
13
 	private final Activity activity;
13
 	private final String id;
14
 	private final String id;
43
 	public View getView() {
44
 	public View getView() {
44
 		if (view == null) {
45
 		if (view == null) {
45
 			view = createView();
46
 			view = createView();
47
+			view.getViewTreeObserver().addOnGlobalLayoutListener(this);
46
 		}
48
 		}
47
 		return view;
49
 		return view;
48
 	}
50
 	}
59
 	public ViewController findControllerById(String id) {
61
 	public ViewController findControllerById(String id) {
60
 		return isSameId(id) ? this : null;
62
 		return isSameId(id) ? this : null;
61
 	}
63
 	}
64
+
65
+	public void onAppear() {
66
+	}
67
+
68
+	@Override
69
+	public void onGlobalLayout() {
70
+		if (getView().isShown())
71
+			onAppear();
72
+	}
62
 }
73
 }

+ 24
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java View File

2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.view.View;
4
 import android.view.View;
5
+import android.view.ViewParent;
5
 
6
 
6
 import com.reactnativenavigation.BaseTest;
7
 import com.reactnativenavigation.BaseTest;
7
 import com.reactnativenavigation.mocks.SimpleViewController;
8
 import com.reactnativenavigation.mocks.SimpleViewController;
8
 
9
 
10
+import org.assertj.android.api.Assertions;
9
 import org.junit.Test;
11
 import org.junit.Test;
12
+import org.robolectric.Shadows;
10
 
13
 
11
 import static org.assertj.core.api.Java6Assertions.assertThat;
14
 import static org.assertj.core.api.Java6Assertions.assertThat;
15
+import static org.mockito.Mockito.mock;
16
+import static org.mockito.Mockito.spy;
17
+import static org.mockito.Mockito.times;
18
+import static org.mockito.Mockito.verify;
12
 
19
 
13
 public class ViewControllerTest extends BaseTest {
20
 public class ViewControllerTest extends BaseTest {
14
 
21
 
74
 		assertThat(uut.findControllerById("456")).isNull();
81
 		assertThat(uut.findControllerById("456")).isNull();
75
 		assertThat(uut.findControllerById("uut")).isEqualTo(uut);
82
 		assertThat(uut.findControllerById("uut")).isEqualTo(uut);
76
 	}
83
 	}
84
+
85
+	@Test
86
+	public void onAppear_WhenVisibleOnScreen() throws Exception {
87
+		ViewController spy = spy(uut);
88
+		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
89
+		Assertions.assertThat(spy.getView()).isNotShown();
90
+		verify(spy, times(0)).onAppear();
91
+
92
+		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
93
+		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
94
+		Assertions.assertThat(spy.getView()).isShown();
95
+
96
+		verify(spy, times(1)).onAppear();
97
+	}
98
+
99
+
77
 }
100
 }
101
+