Browse Source

destroy on pop

Daniel Zlotin 7 years ago
parent
commit
e5fcf65125

+ 0
- 2
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ScreenLifecycleTest.java View File

2
 
2
 
3
 import android.support.test.uiautomator.By;
3
 import android.support.test.uiautomator.By;
4
 
4
 
5
-import org.junit.Ignore;
6
 import org.junit.Test;
5
 import org.junit.Test;
7
 
6
 
8
 public class ScreenLifecycleTest extends BaseTest {
7
 public class ScreenLifecycleTest extends BaseTest {
18
 	}
17
 	}
19
 
18
 
20
 	@Test
19
 	@Test
21
-	@Ignore
22
 	public void unmountIsCalledWhenPopped() throws Exception {
20
 	public void unmountIsCalledWhenPopped() throws Exception {
23
 		launchTheApp();
21
 		launchTheApp();
24
 		assertMainShown();
22
 		assertMainShown();

+ 17
- 26
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java View File

2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
5
-import android.support.annotation.Nullable;
6
 import android.view.ViewGroup;
5
 import android.view.ViewGroup;
7
 import android.widget.FrameLayout;
6
 import android.widget.FrameLayout;
8
 
7
 
57
 			@Override
56
 			@Override
58
 			public void run() {
57
 			public void run() {
59
 				getView().removeView(poppedTop.getView());
58
 				getView().removeView(poppedTop.getView());
59
+				poppedTop.destroy();
60
 			}
60
 			}
61
 		});
61
 		});
62
 	}
62
 	}
66
 			pop();
66
 			pop();
67
 		} else {
67
 		} else {
68
 			stack.remove(childController.getId());
68
 			stack.remove(childController.getId());
69
+			childController.destroy();
70
+		}
71
+	}
72
+
73
+	public void popTo(final ViewController viewController) {
74
+		if (!stack.containsId(viewController.getId())) {
75
+			return;
76
+		}
77
+		while (!stack.isTop(viewController.getId())) {
78
+			pop();
79
+		}
80
+	}
81
+
82
+	public void popToRoot() {
83
+		while (canPop()) {
84
+			pop();
69
 		}
85
 		}
70
 	}
86
 	}
71
 
87
 
97
 		return new FrameLayout(getActivity());
113
 		return new FrameLayout(getActivity());
98
 	}
114
 	}
99
 
115
 
100
-	@Nullable
101
-	@Override
102
-	public StackController getParentStackController() {
103
-		return this;
104
-	}
105
-
106
-	public void popTo(final ViewController viewController) {
107
-		if (!stack.containsId(viewController.getId())) {
108
-			return;
109
-		}
110
-		while (!stack.isTop(viewController.getId())) {
111
-			pop();
112
-		}
113
-	}
114
-
115
-	public void popToRoot() {
116
-		while (canPop()) {
117
-			pop();
118
-		}
119
-	}
120
-
121
-	boolean containsId(String id) {
122
-		return stack.containsId(id);
123
-	}
124
-
125
 	@NonNull
116
 	@NonNull
126
 	@Override
117
 	@Override
127
 	public Collection<ViewController> getChildControllers() {
118
 	public Collection<ViewController> getChildControllers() {

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

71
 		//
71
 		//
72
 	}
72
 	}
73
 
73
 
74
+	public void destroy() {
75
+		getView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
76
+		view = null;
77
+	}
78
+
74
 	@Override
79
 	@Override
75
 	public void onGlobalLayout() {
80
 	public void onGlobalLayout() {
76
 		if (!isShown && isViewShown()) {
81
 		if (!isShown && isViewShown()) {

+ 54
- 8
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java View File

8
 import com.reactnativenavigation.mocks.SimpleViewController;
8
 import com.reactnativenavigation.mocks.SimpleViewController;
9
 import com.reactnativenavigation.mocks.TestStackAnimator;
9
 import com.reactnativenavigation.mocks.TestStackAnimator;
10
 
10
 
11
+import org.assertj.core.api.iterable.Extractor;
11
 import org.junit.Test;
12
 import org.junit.Test;
12
 
13
 
13
 import static org.assertj.core.api.Java6Assertions.assertThat;
14
 import static org.assertj.core.api.Java6Assertions.assertThat;
15
+import static org.mockito.Mockito.spy;
16
+import static org.mockito.Mockito.times;
17
+import static org.mockito.Mockito.verify;
14
 
18
 
15
 public class StackControllerTest extends BaseTest {
19
 public class StackControllerTest extends BaseTest {
16
 
20
 
175
 		assertHasSingleChildViewOfController(child2);
179
 		assertHasSingleChildViewOfController(child2);
176
 	}
180
 	}
177
 
181
 
178
-	@Test
179
-	public void getStackControllerReturnsSelf() throws Exception {
180
-		assertThat(uut.getParentStackController()).isEqualTo(uut);
181
-	}
182
-
183
 	@Test
182
 	@Test
184
 	public void popTo_PopsTopUntilControllerIsNewTop() throws Exception {
183
 	public void popTo_PopsTopUntilControllerIsNewTop() throws Exception {
185
 		uut.push(child1);
184
 		uut.push(child1);
242
 		assertThat(uut.findControllerById(child2.getId())).isEqualTo(child2);
241
 		assertThat(uut.findControllerById(child2.getId())).isEqualTo(child2);
243
 	}
242
 	}
244
 
243
 
244
+	@Test
245
+	public void pop_CallsDestroyOnPoppedChild() throws Exception {
246
+		child1 = spy(child1);
247
+		child2 = spy(child2);
248
+		child3 = spy(child3);
249
+		uut.push(child1);
250
+		uut.push(child2);
251
+		uut.push(child3);
252
+
253
+		verify(child3, times(0)).destroy();
254
+		uut.pop();
255
+		verify(child3, times(1)).destroy();
256
+	}
257
+
258
+	@Test
259
+	public void popSpecific_CallsDestroyOnPoppedChild() throws Exception {
260
+		child1 = spy(child1);
261
+		child2 = spy(child2);
262
+		child3 = spy(child3);
263
+		uut.push(child1);
264
+		uut.push(child2);
265
+		uut.push(child3);
266
+
267
+		verify(child2, times(0)).destroy();
268
+		uut.popSpecific(child2);
269
+		verify(child2, times(1)).destroy();
270
+	}
271
+
272
+	@Test
273
+	public void popTo_CallsDestroyOnPoppedChild() throws Exception {
274
+		child1 = spy(child1);
275
+		child2 = spy(child2);
276
+		child3 = spy(child3);
277
+		uut.push(child1);
278
+		uut.push(child2);
279
+		uut.push(child3);
280
+
281
+		verify(child2, times(0)).destroy();
282
+		verify(child3, times(0)).destroy();
283
+		uut.popTo(child1);
284
+		verify(child2, times(1)).destroy();
285
+		verify(child3, times(1)).destroy();
286
+	}
287
+
245
 	private void assertHasSingleChildViewOfController(ViewController childController) {
288
 	private void assertHasSingleChildViewOfController(ViewController childController) {
246
 		assertThat(uut.getView().getChildCount()).isEqualTo(1);
289
 		assertThat(uut.getView().getChildCount()).isEqualTo(1);
247
 		assertThat(uut.getView().getChildAt(0)).isEqualTo(childController.getView());
290
 		assertThat(uut.getView().getChildAt(0)).isEqualTo(childController.getView());
249
 
292
 
250
 	private void assertContainsOnlyId(String... ids) {
293
 	private void assertContainsOnlyId(String... ids) {
251
 		assertThat(uut.size()).isEqualTo(ids.length);
294
 		assertThat(uut.size()).isEqualTo(ids.length);
252
-		for (String id : ids) {
253
-			assertThat(uut.containsId(id));
254
-		}
295
+		assertThat(uut.getChildControllers()).extracting(new Extractor<ViewController, String>() {
296
+			@Override
297
+			public String extract(final ViewController input) {
298
+				return input.getId();
299
+			}
300
+		}).containsOnly(ids);
255
 	}
301
 	}
256
 }
302
 }