Selaa lähdekoodia

destroy on pop

Daniel Zlotin 7 vuotta sitten
vanhempi
commit
e5fcf65125

+ 0
- 2
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ScreenLifecycleTest.java Näytä tiedosto

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

+ 17
- 26
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java Näytä tiedosto

@@ -2,7 +2,6 @@ package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4 4
 import android.support.annotation.NonNull;
5
-import android.support.annotation.Nullable;
6 5
 import android.view.ViewGroup;
7 6
 import android.widget.FrameLayout;
8 7
 
@@ -57,6 +56,7 @@ public class StackController extends ParentController {
57 56
 			@Override
58 57
 			public void run() {
59 58
 				getView().removeView(poppedTop.getView());
59
+				poppedTop.destroy();
60 60
 			}
61 61
 		});
62 62
 	}
@@ -66,6 +66,22 @@ public class StackController extends ParentController {
66 66
 			pop();
67 67
 		} else {
68 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,31 +113,6 @@ public class StackController extends ParentController {
97 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 116
 	@NonNull
126 117
 	@Override
127 118
 	public Collection<ViewController> getChildControllers() {

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java Näytä tiedosto

@@ -71,6 +71,11 @@ public abstract class ViewController implements ViewTreeObserver.OnGlobalLayoutL
71 71
 		//
72 72
 	}
73 73
 
74
+	public void destroy() {
75
+		getView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
76
+		view = null;
77
+	}
78
+
74 79
 	@Override
75 80
 	public void onGlobalLayout() {
76 81
 		if (!isShown && isViewShown()) {

+ 54
- 8
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java Näytä tiedosto

@@ -8,9 +8,13 @@ import com.reactnativenavigation.BaseTest;
8 8
 import com.reactnativenavigation.mocks.SimpleViewController;
9 9
 import com.reactnativenavigation.mocks.TestStackAnimator;
10 10
 
11
+import org.assertj.core.api.iterable.Extractor;
11 12
 import org.junit.Test;
12 13
 
13 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 19
 public class StackControllerTest extends BaseTest {
16 20
 
@@ -175,11 +179,6 @@ public class StackControllerTest extends BaseTest {
175 179
 		assertHasSingleChildViewOfController(child2);
176 180
 	}
177 181
 
178
-	@Test
179
-	public void getStackControllerReturnsSelf() throws Exception {
180
-		assertThat(uut.getParentStackController()).isEqualTo(uut);
181
-	}
182
-
183 182
 	@Test
184 183
 	public void popTo_PopsTopUntilControllerIsNewTop() throws Exception {
185 184
 		uut.push(child1);
@@ -242,6 +241,50 @@ public class StackControllerTest extends BaseTest {
242 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 288
 	private void assertHasSingleChildViewOfController(ViewController childController) {
246 289
 		assertThat(uut.getView().getChildCount()).isEqualTo(1);
247 290
 		assertThat(uut.getView().getChildAt(0)).isEqualTo(childController.getView());
@@ -249,8 +292,11 @@ public class StackControllerTest extends BaseTest {
249 292
 
250 293
 	private void assertContainsOnlyId(String... ids) {
251 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
 }