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
 	public void pop() {
23
 	public void pop() {
24
 		childControllers.pop();
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
 package com.reactnativenavigation;
1
 package com.reactnativenavigation;
2
 
2
 
3
+import org.junit.After;
4
+import org.junit.Before;
3
 import org.junit.runner.RunWith;
5
 import org.junit.runner.RunWith;
4
 import org.robolectric.RobolectricTestRunner;
6
 import org.robolectric.RobolectricTestRunner;
5
 import org.robolectric.annotation.Config;
7
 import org.robolectric.annotation.Config;
7
 @RunWith(RobolectricTestRunner.class)
9
 @RunWith(RobolectricTestRunner.class)
8
 @Config(sdk = 25, constants = BuildConfig.class, manifest = "/../../../../../src/test/AndroidManifest.xml")
10
 @Config(sdk = 25, constants = BuildConfig.class, manifest = "/../../../../../src/test/AndroidManifest.xml")
9
 public abstract class BaseTest {
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
 
8
 
9
 public class NavigationControllerTest extends BaseTest {
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
 	@Test
25
 	@Test
12
 	public void isAViewController() throws Exception {
26
 	public void isAViewController() throws Exception {
13
-		assertThat(new NavigationController()).isInstanceOf(ViewController.class);
27
+		assertThat(uut).isInstanceOf(ViewController.class);
14
 	}
28
 	}
15
 
29
 
16
 	@Test
30
 	@Test
17
 	public void holdsAStackOfViewControllers() throws Exception {
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
 	@Test
37
 	@Test
26
 	public void push() throws Exception {
38
 	public void push() throws Exception {
27
-		NavigationController uut = new NavigationController();
28
-		ViewController c1 = new ViewController();
29
 		assertThat(uut.getChildControllers()).isEmpty();
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
 	@Test
44
 	@Test
35
 	public void pop() throws Exception {
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
 		uut.pop();
49
 		uut.pop();
43
-		assertThat(uut.getChildControllers()).containsExactly(c1);
50
+		assertThat(uut.getChildControllers()).containsExactly(child1);
44
 		uut.pop();
51
 		uut.pop();
45
 		assertThat(uut.getChildControllers()).isEmpty();
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
 }