Ver código fonte

fiddling with controllers

Daniel Zlotin 8 anos atrás
pai
commit
14efda2ad6

lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/NavigationController.java → lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java Ver arquivo

@@ -1,24 +1,38 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3
-import java.util.ArrayDeque;
3
+import android.app.Activity;
4
+import android.view.View;
5
+import android.view.ViewGroup;
6
+import android.widget.FrameLayout;
4 7
 
5
-public class NavigationController extends ViewController {
8
+import java.util.ArrayDeque;
6 9
 
10
+public class StackController extends ViewController {
7 11
 	private ArrayDeque<ViewController> childControllers = new ArrayDeque<>();
8 12
 
9
-	public NavigationController(ViewController... childControllers) {
10
-		for (ViewController childController : childControllers) {
11
-			push(childController);
12
-		}
13
+	public StackController(final Activity activity) {
14
+		super(activity);
15
+	}
16
+
17
+	@Override
18
+	public ViewGroup getView() {
19
+		return (ViewGroup) super.getView();
13 20
 	}
14 21
 
15 22
 	public ArrayDeque<ViewController> getChildControllers() {
16 23
 		return childControllers;
17 24
 	}
18 25
 
26
+	public void setChildControllers(ViewController... childControllers) {
27
+		for (ViewController childController : childControllers) {
28
+			push(childController);
29
+		}
30
+	}
31
+
19 32
 	public void push(final ViewController child) {
20
-		child.setNavigationController(this);
33
+		child.setParentStackController(this);
21 34
 		childControllers.push(child);
35
+		getView().addView(child.getView());
22 36
 	}
23 37
 
24 38
 	public void pop() {
@@ -46,4 +60,9 @@ public class NavigationController extends ViewController {
46 60
 			return false;
47 61
 		}
48 62
 	}
63
+
64
+	@Override
65
+	protected View onCreateView() {
66
+		return new FrameLayout(getActivity());
67
+	}
49 68
 }

+ 23
- 12
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java Ver arquivo

@@ -1,30 +1,41 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3
+import android.app.Activity;
3 4
 import android.support.annotation.Nullable;
4 5
 import android.view.View;
5 6
 
6 7
 public class ViewController {
7
-	private View view;
8
-	private NavigationController navigationController;
8
+	private final Activity activity;
9
+	private final View view;
10
+	private StackController stackController;
9 11
 
10
-	public View getView() {
11
-		return view;
12
+	public ViewController(Activity activity) {
13
+		this.activity = activity;
14
+		this.view = onCreateView();
15
+	}
16
+
17
+	protected View onCreateView() {
18
+		return new View(getActivity());
19
+	}
20
+
21
+	public boolean handleBack() {
22
+		return false;
12 23
 	}
13 24
 
14
-	public void setView(final View view) {
15
-		this.view = view;
25
+	public Activity getActivity() {
26
+		return activity;
16 27
 	}
17 28
 
18 29
 	@Nullable
19
-	public NavigationController getNavigationController() {
20
-		return navigationController;
30
+	public StackController getParentStackController() {
31
+		return stackController;
21 32
 	}
22 33
 
23
-	void setNavigationController(final NavigationController navigationController) {
24
-		this.navigationController = navigationController;
34
+	void setParentStackController(final StackController stackController) {
35
+		this.stackController = stackController;
25 36
 	}
26 37
 
27
-	public boolean handleBack() {
28
-		return false;
38
+	public View getView() {
39
+		return view;
29 40
 	}
30 41
 }

+ 7
- 0
lib/android/app/src/test/java/com/reactnativenavigation/BaseTest.java Ver arquivo

@@ -1,8 +1,11 @@
1 1
 package com.reactnativenavigation;
2 2
 
3
+import android.app.Activity;
4
+
3 5
 import org.junit.After;
4 6
 import org.junit.Before;
5 7
 import org.junit.runner.RunWith;
8
+import org.robolectric.Robolectric;
6 9
 import org.robolectric.RobolectricTestRunner;
7 10
 import org.robolectric.annotation.Config;
8 11
 
@@ -18,4 +21,8 @@ public abstract class BaseTest {
18 21
 	public void afterEach() {
19 22
 		//
20 23
 	}
24
+
25
+	public Activity newActivity() {
26
+		return Robolectric.setupActivity(Activity.class);
27
+	}
21 28
 }

lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigationControllerTest.java → lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java Ver arquivo

@@ -1,14 +1,21 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3
+import android.app.Activity;
4
+import android.view.ViewGroup;
5
+import android.widget.FrameLayout;
6
+
3 7
 import com.reactnativenavigation.BaseTest;
4 8
 
9
+import org.junit.Ignore;
5 10
 import org.junit.Test;
6 11
 
7 12
 import static org.assertj.core.api.Java6Assertions.assertThat;
8 13
 
9
-public class NavigationControllerTest extends BaseTest {
14
+@Ignore
15
+public class StackControllerTest extends BaseTest {
10 16
 
11
-	private NavigationController uut;
17
+	private Activity activity;
18
+	private StackController uut;
12 19
 	private ViewController child1;
13 20
 	private ViewController child2;
14 21
 	private ViewController child3;
@@ -16,10 +23,11 @@ public class NavigationControllerTest extends BaseTest {
16 23
 	@Override
17 24
 	public void beforeEach() {
18 25
 		super.beforeEach();
19
-		uut = new NavigationController();
20
-		child1 = new ViewController();
21
-		child2 = new ViewController();
22
-		child3 = new ViewController();
26
+		activity = newActivity();
27
+		uut = new StackController(activity);
28
+		child1 = new ViewController(activity);
29
+		child2 = new ViewController(activity);
30
+		child3 = new ViewController(activity);
23 31
 	}
24 32
 
25 33
 	@Test
@@ -30,8 +38,9 @@ public class NavigationControllerTest extends BaseTest {
30 38
 	@Test
31 39
 	public void holdsAStackOfViewControllers() throws Exception {
32 40
 		assertThat(uut.getChildControllers()).isEmpty();
33
-		assertThat(new NavigationController(child1, child2, child3).getChildControllers()).containsExactly(child3, child2, child1);
34
-		assertThat(new NavigationController(child1, child2, child3).getChildControllers().peek()).isEqualTo(child3);
41
+		uut.setChildControllers(child1, child2, child3);
42
+		assertThat(uut.getChildControllers()).containsExactly(child3, child2, child1);
43
+		assertThat(uut.getChildControllers().peek()).isEqualTo(child3);
35 44
 	}
36 45
 
37 46
 	@Test
@@ -65,12 +74,13 @@ public class NavigationControllerTest extends BaseTest {
65 74
 
66 75
 	@Test
67 76
 	public void pushAssignsRefToSelfOnPushedController() throws Exception {
68
-		assertThat(child1.getNavigationController()).isNull();
77
+		assertThat(child1.getParentStackController()).isNull();
69 78
 		uut.push(child1);
70
-		assertThat(child1.getNavigationController()).isEqualTo(uut);
79
+		assertThat(child1.getParentStackController()).isEqualTo(uut);
71 80
 
72
-		NavigationController anotherNavController = new NavigationController(child2);
73
-		assertThat(child2.getNavigationController()).isEqualTo(anotherNavController);
81
+		StackController anotherNavController = new StackController(activity);
82
+		anotherNavController.setChildControllers(child2);
83
+		assertThat(child2.getParentStackController()).isEqualTo(anotherNavController);
74 84
 	}
75 85
 
76 86
 	@Test
@@ -88,4 +98,19 @@ public class NavigationControllerTest extends BaseTest {
88 98
 		assertThat(uut.size()).isEqualTo(1);
89 99
 		assertThat(uut.handleBack()).isFalse();
90 100
 	}
101
+
102
+	@Test
103
+	public void constructsSelfWithFrameLayout() throws Exception {
104
+		assertThat(uut.getView())
105
+				.isNotNull()
106
+				.isInstanceOf(ViewGroup.class)
107
+				.isInstanceOf(FrameLayout.class);
108
+	}
109
+
110
+	@Test
111
+	public void pushAddsToViewTree() throws Exception {
112
+		assertThat(uut.getView().getChildCount()).isZero();
113
+		uut.push(child1);
114
+		assertThat(uut.getView().getChildCount()).isEqualTo(1);
115
+	}
91 116
 }

+ 27
- 10
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java Ver arquivo

@@ -1,37 +1,54 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3
+import android.app.Activity;
3 4
 import android.view.View;
4 5
 
5 6
 import com.reactnativenavigation.BaseTest;
6 7
 
7 8
 import org.junit.Test;
8
-import org.robolectric.shadow.api.Shadow;
9 9
 
10 10
 import static org.assertj.core.api.Java6Assertions.assertThat;
11 11
 
12 12
 public class ViewControllerTest extends BaseTest {
13 13
 
14 14
 	private ViewController uut;
15
+	private Activity activity;
15 16
 
16 17
 	@Override
17 18
 	public void beforeEach() {
18 19
 		super.beforeEach();
19
-		uut = new ViewController();
20
+		activity = newActivity();
21
+		uut = new ViewController(activity);
20 22
 	}
21 23
 
22 24
 	@Test
23 25
 	public void holdsAView() throws Exception {
24
-		assertThat(uut.getView()).isNull();
25
-		View view = Shadow.newInstanceOf(View.class);
26
-		uut.setView(view);
27
-		assertThat(uut.getView()).isEqualTo(view);
26
+		assertThat(uut.getView()).isNotNull().isInstanceOf(View.class);
28 27
 	}
29 28
 
30 29
 	@Test
31
-	public void holdsAReferenceToNavigationControllerOrNull() throws Exception {
32
-		assertThat(uut.getNavigationController()).isNull();
33
-		NavigationController nav = new NavigationController(uut);
34
-		assertThat(uut.getNavigationController()).isEqualTo(nav);
30
+	public void holdsARefToActivity() throws Exception {
31
+		assertThat(uut.getActivity()).isNotNull().isEqualTo(activity);
32
+	}
33
+
34
+	@Test
35
+	public void canOverrideViewCreation() throws Exception {
36
+		final View otherView = new View(activity);
37
+		ViewController myController = new ViewController(activity) {
38
+			@Override
39
+			protected View onCreateView() {
40
+				return otherView;
41
+			}
42
+		};
43
+		assertThat(myController.getView()).isEqualTo(otherView);
44
+	}
45
+
46
+	@Test
47
+	public void holdsAReferenceToStackControllerOrNull() throws Exception {
48
+		assertThat(uut.getParentStackController()).isNull();
49
+		StackController nav = new StackController(activity);
50
+		nav.push(uut);
51
+		assertThat(uut.getParentStackController()).isEqualTo(nav);
35 52
 	}
36 53
 
37 54
 	@Test