Przeglądaj źródła

android popToRoot

Daniel Zlotin 8 lat temu
rodzic
commit
b76a1c7290

+ 11
- 0
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ScreenStackTest.java Wyświetl plik

42
 		elementByText("POP TO STACK POSITION 1").click();
42
 		elementByText("POP TO STACK POSITION 1").click();
43
 		assertExists(By.text("Stack Position: 1"));
43
 		assertExists(By.text("Stack Position: 1"));
44
 	}
44
 	}
45
+
46
+	@Test
47
+	public void popToRoot() throws Exception {
48
+		launchTheApp();
49
+		assertMainShown();
50
+		elementByText("PUSH").click();
51
+		elementByText("PUSH").click();
52
+		elementByText("PUSH").click();
53
+		elementByText("POP TO ROOT").click();
54
+		assertMainShown();
55
+	}
45
 }
56
 }

+ 10
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java Wyświetl plik

76
 		});
76
 		});
77
 	}
77
 	}
78
 
78
 
79
+	@ReactMethod
80
+	public void popToRoot(final String onContainerId) {
81
+		handle(new Runnable() {
82
+			@Override
83
+			public void run() {
84
+				store.getViewController(onContainerId).getStackController().popToRoot();
85
+			}
86
+		});
87
+	}
88
+
79
 	private NavigationActivity activity() {
89
 	private NavigationActivity activity() {
80
 		return (NavigationActivity) getCurrentActivity();
90
 		return (NavigationActivity) getCurrentActivity();
81
 	}
91
 	}

+ 6
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java Wyświetl plik

99
 			pop();
99
 			pop();
100
 		}
100
 		}
101
 	}
101
 	}
102
+
103
+	public void popToRoot() {
104
+		while (canPop()) {
105
+			pop();
106
+		}
107
+	}
102
 }
108
 }

+ 22
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java Wyświetl plik

203
 		assertThat(uut.size()).isEqualTo(2);
203
 		assertThat(uut.size()).isEqualTo(2);
204
 	}
204
 	}
205
 
205
 
206
+	@Test
207
+	public void popToRoot_PopsEverythingAboveFirstController() throws Exception {
208
+		uut.push(child1);
209
+		uut.push(child2);
210
+		uut.push(child3);
211
+
212
+		assertThat(uut.size()).isEqualTo(3);
213
+		assertThat(uut.peek()).isEqualTo(child3);
214
+
215
+		uut.popToRoot();
216
+
217
+		assertThat(uut.size()).isEqualTo(1);
218
+		assertThat(uut.peek()).isEqualTo(child1);
219
+	}
220
+
221
+	@Test
222
+	public void popToRoot_EmptyStackDoesNothing() throws Exception {
223
+		assertThat(uut.isEmpty()).isTrue();
224
+		uut.popToRoot();
225
+		assertThat(uut.isEmpty()).isTrue();
226
+	}
227
+
206
 	private void assertHasSingleChildViewOfController(ViewController childController) {
228
 	private void assertHasSingleChildViewOfController(ViewController childController) {
207
 		assertThat(uut.getView().getChildCount()).isEqualTo(1);
229
 		assertThat(uut.getView().getChildCount()).isEqualTo(1);
208
 		assertThat(uut.getView().getChildAt(0)).isEqualTo(childController.getView());
230
 		assertThat(uut.getView().getChildAt(0)).isEqualTo(childController.getView());