Browse Source

stack operations

Daniel Zlotin 7 years ago
parent
commit
72f4036e96

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

@@ -23,4 +23,16 @@ public class NavigationController extends ViewController {
23 23
 	public void pop() {
24 24
 		childControllers.pop();
25 25
 	}
26
+
27
+	public ViewController peek() {
28
+		return childControllers.peek();
29
+	}
30
+
31
+	public int size() {
32
+		return childControllers.size();
33
+	}
34
+
35
+	public boolean isEmpty() {
36
+		return childControllers.isEmpty();
37
+	}
26 38
 }

+ 10
- 0
lib/android/app/src/test/java/com/reactnativenavigation/BaseTest.java View File

@@ -1,5 +1,7 @@
1 1
 package com.reactnativenavigation;
2 2
 
3
+import org.junit.After;
4
+import org.junit.Before;
3 5
 import org.junit.runner.RunWith;
4 6
 import org.robolectric.RobolectricTestRunner;
5 7
 import org.robolectric.annotation.Config;
@@ -7,5 +9,13 @@ import org.robolectric.annotation.Config;
7 9
 @RunWith(RobolectricTestRunner.class)
8 10
 @Config(sdk = 25, constants = BuildConfig.class, manifest = "/../../../../../src/test/AndroidManifest.xml")
9 11
 public abstract class BaseTest {
12
+	@Before
13
+	public void beforeEach() {
14
+		//
15
+	}
10 16
 
17
+	@After
18
+	public void afterEach() {
19
+		//
20
+	}
11 21
 }

+ 36
- 17
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigationControllerTest.java View File

@@ -8,40 +8,59 @@ import static org.assertj.core.api.Java6Assertions.assertThat;
8 8
 
9 9
 public class NavigationControllerTest extends BaseTest {
10 10
 
11
+	private NavigationController uut;
12
+	private ViewController child1;
13
+	private ViewController child2;
14
+	private ViewController child3;
15
+
16
+	@Override
17
+	public void beforeEach() {
18
+		super.beforeEach();
19
+		uut = new NavigationController();
20
+		child1 = new ViewController();
21
+		child2 = new ViewController();
22
+		child3 = new ViewController();
23
+	}
24
+
11 25
 	@Test
12 26
 	public void isAViewController() throws Exception {
13
-		assertThat(new NavigationController()).isInstanceOf(ViewController.class);
27
+		assertThat(uut).isInstanceOf(ViewController.class);
14 28
 	}
15 29
 
16 30
 	@Test
17 31
 	public void holdsAStackOfViewControllers() throws Exception {
18
-		assertThat(new NavigationController().getChildControllers()).isEmpty();
19
-		ViewController c1 = new ViewController();
20
-		ViewController c2 = new ViewController();
21
-		ViewController c3 = new ViewController();
22
-		assertThat(new NavigationController(c1, c2, c3).getChildControllers()).containsExactly(c3, c2, c1);
32
+		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);
23 35
 	}
24 36
 
25 37
 	@Test
26 38
 	public void push() throws Exception {
27
-		NavigationController uut = new NavigationController();
28
-		ViewController c1 = new ViewController();
29 39
 		assertThat(uut.getChildControllers()).isEmpty();
30
-		uut.push(c1);
31
-		assertThat(uut.getChildControllers()).containsExactly(c1);
40
+		uut.push(child1);
41
+		assertThat(uut.getChildControllers()).containsExactly(child1);
32 42
 	}
33 43
 
34 44
 	@Test
35 45
 	public void pop() throws Exception {
36
-		NavigationController uut = new NavigationController();
37
-		ViewController c1 = new ViewController();
38
-		ViewController c2 = new ViewController();
39
-		uut.push(c1);
40
-		uut.push(c2);
41
-		assertThat(uut.getChildControllers()).containsExactly(c2, c1);
46
+		uut.push(child1);
47
+		uut.push(child2);
48
+		assertThat(uut.getChildControllers()).containsExactly(child2, child1);
42 49
 		uut.pop();
43
-		assertThat(uut.getChildControllers()).containsExactly(c1);
50
+		assertThat(uut.getChildControllers()).containsExactly(child1);
44 51
 		uut.pop();
45 52
 		assertThat(uut.getChildControllers()).isEmpty();
46 53
 	}
54
+
55
+	@Test
56
+	public void stackOperations() throws Exception {
57
+		assertThat(uut.peek()).isNull();
58
+		assertThat(uut.size()).isZero();
59
+		assertThat(uut.isEmpty()).isTrue();
60
+		uut.push(child1);
61
+		assertThat(uut.peek()).isEqualTo(child1);
62
+		assertThat(uut.size()).isEqualTo(1);
63
+		assertThat(uut.isEmpty()).isFalse();
64
+	}
65
+
47 66
 }