Selaa lähdekoodia

Stop setting id for root views

Guy Carmeli 6 vuotta sitten
vanhempi
commit
c80453dc70

+ 16
- 0
lib/android/app/src/main/java/com/reactnativenavigation/utils/ViewUtils.java Näytä tiedosto

@@ -58,4 +58,20 @@ public class ViewUtils {
58 58
     public interface Matcher<T> {
59 59
         boolean match(T child);
60 60
     }
61
+
62
+    public static boolean isChildOf(ViewGroup parent, View child) {
63
+        if (parent == child) return true;
64
+
65
+        for (int i = 0; i < parent.getChildCount(); i++) {
66
+            View view = parent.getChildAt(i);
67
+            if (view == child) {
68
+                return true;
69
+            }
70
+
71
+            if (view instanceof ViewGroup) {
72
+                if (isChildOf((ViewGroup) view, child)) return true;
73
+            }
74
+        }
75
+        return false;
76
+    }
61 77
 }

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

@@ -12,7 +12,6 @@ import android.view.ViewTreeObserver;
12 12
 
13 13
 import com.reactnativenavigation.parse.Options;
14 14
 import com.reactnativenavigation.presentation.FabOptionsPresenter;
15
-import com.reactnativenavigation.utils.CompatUtils;
16 15
 import com.reactnativenavigation.utils.StringUtils;
17 16
 import com.reactnativenavigation.utils.Task;
18 17
 import com.reactnativenavigation.utils.UiUtils;
@@ -112,9 +111,6 @@ public abstract class ViewController<T extends ViewGroup> implements ViewTreeObs
112 111
                 throw new RuntimeException("Tried to create view after it has already been destroyed");
113 112
             }
114 113
             view = createView();
115
-            if (view.getId() < 0) {
116
-                view.setId(CompatUtils.generateViewId());
117
-            }
118 114
             view.getViewTreeObserver().addOnGlobalLayoutListener(this);
119 115
         }
120 116
         return view;

+ 4
- 5
lib/android/app/src/test/java/com/reactnativenavigation/BaseTest.java Näytä tiedosto

@@ -5,6 +5,7 @@ import android.support.v7.app.*;
5 5
 import android.view.*;
6 6
 
7 7
 import com.reactnativenavigation.parse.params.Bool;
8
+import com.reactnativenavigation.utils.ViewUtils;
8 9
 import com.reactnativenavigation.viewcontrollers.ViewController;
9 10
 
10 11
 import org.junit.*;
@@ -31,18 +32,16 @@ public abstract class BaseTest {
31 32
         return Robolectric.setupActivity(AppCompatActivity.class);
32 33
     }
33 34
 
34
-    public void assertIsChildById(ViewGroup parent, View child) {
35
+    public void assertIsChild(ViewGroup parent, View child) {
35 36
         assertThat(parent).isNotNull();
36 37
         assertThat(child).isNotNull();
37
-        assertThat(child.getId()).isNotZero().isPositive();
38
-        assertThat(parent.findViewById(child.getId())).isNotNull().isEqualTo(child);
38
+        assertThat(ViewUtils.isChildOf(parent, child)).isTrue();
39 39
     }
40 40
 
41 41
     public void assertNotChildOf(ViewGroup parent, View child) {
42 42
         assertThat(parent).isNotNull();
43 43
         assertThat(child).isNotNull();
44
-        assertThat(child.getId()).isNotZero().isPositive();
45
-        assertThat(parent.findViewById(child.getId())).isNull();
44
+        assertThat(ViewUtils.isChildOf(parent, child)).isFalse();
46 45
     }
47 46
 
48 47
     protected void disablePushAnimation(ViewController... controllers) {

+ 14
- 14
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java Näytä tiedosto

@@ -65,14 +65,14 @@ public class NavigatorTest extends BaseTest {
65 65
     public void setRoot_AddsChildControllerView() {
66 66
         assertThat(uut.getView().getChildCount()).isZero();
67 67
         uut.setRoot(child1, new MockPromise());
68
-        assertIsChildById(uut.getView(), child1.getView());
68
+        assertIsChild(uut.getView(), child1.getView());
69 69
     }
70 70
 
71 71
     @Test
72 72
     public void setRoot_ReplacesExistingChildControllerViews() {
73 73
         uut.setRoot(child1, new MockPromise());
74 74
         uut.setRoot(child2, new MockPromise());
75
-        assertIsChildById(uut.getView(), child2.getView());
75
+        assertIsChild(uut.getView(), child2.getView());
76 76
     }
77 77
 
78 78
     @Test
@@ -87,20 +87,20 @@ public class NavigatorTest extends BaseTest {
87 87
         stackController.push(child1, new CommandListenerAdapter());
88 88
         uut.setRoot(stackController, new MockPromise());
89 89
 
90
-        assertIsChildById(uut.getView(), stackController.getView());
91
-        assertIsChildById(stackController.getView(), child1.getView());
90
+        assertIsChild(uut.getView(), stackController.getView());
91
+        assertIsChild(stackController.getView(), child1.getView());
92 92
 
93 93
         uut.push(child1.getId(), child2, new CommandListenerAdapter());
94 94
 
95
-        assertIsChildById(uut.getView(), stackController.getView());
96
-        assertIsChildById(stackController.getView(), child2.getView());
95
+        assertIsChild(uut.getView(), stackController.getView());
96
+        assertIsChild(stackController.getView(), child2.getView());
97 97
     }
98 98
 
99 99
     @Test
100 100
     public void push_InvalidPushWithoutAStack_DoesNothing() {
101 101
         uut.setRoot(child1, new MockPromise());
102 102
         uut.push(child1.getId(), child2, new CommandListenerAdapter());
103
-        assertIsChildById(uut.getView(), child1.getView());
103
+        assertIsChild(uut.getView(), child1.getView());
104 104
     }
105 105
 
106 106
     @Test
@@ -273,19 +273,19 @@ public class NavigatorTest extends BaseTest {
273 273
     }
274 274
 
275 275
     @Test
276
-    public void push_Promise() {
276
+    public void push_promise() {
277 277
         final StackController stackController = newStack();
278 278
         stackController.push(child1, new CommandListenerAdapter());
279 279
         uut.setRoot(stackController, new MockPromise());
280 280
 
281
-        assertIsChildById(uut.getView(), stackController.getView());
282
-        assertIsChildById(stackController.getView(), child1.getView());
281
+        assertIsChild(uut.getView(), stackController.getView());
282
+        assertIsChild(stackController.getView(), child1.getView());
283 283
 
284 284
         uut.push(child1.getId(), child2, new CommandListenerAdapter() {
285 285
             @Override
286 286
             public void onSuccess(String childId) {
287
-                assertIsChildById(uut.getView(), stackController.getView());
288
-                assertIsChildById(stackController.getView(), child2.getView());
287
+                assertIsChild(uut.getView(), stackController.getView());
288
+                assertIsChild(stackController.getView(), child2.getView());
289 289
             }
290 290
         });
291 291
     }
@@ -296,7 +296,7 @@ public class NavigatorTest extends BaseTest {
296 296
         uut.push(child1.getId(), child2, new CommandListenerAdapter() {
297 297
             @Override
298 298
             public void onError(String message) {
299
-                assertIsChildById(uut.getView(), child1.getView());
299
+                assertIsChild(uut.getView(), child1.getView());
300 300
             }
301 301
         });
302 302
 
@@ -341,7 +341,7 @@ public class NavigatorTest extends BaseTest {
341 341
         stackController.push(child1, new CommandListenerAdapter());
342 342
         uut.showModal(stackController, new MockPromise());
343 343
         uut.push(stackController.getId(), child2, new CommandListenerAdapter());
344
-        assertIsChildById(stackController.getView(), child2.getView());
344
+        assertIsChild(stackController.getView(), child2.getView());
345 345
     }
346 346
 
347 347
     @Test

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

@@ -219,16 +219,16 @@ public class StackControllerTest extends BaseTest {
219 219
 
220 220
     @Test
221 221
     public void push_addsToViewTree() {
222
-        assertThat(uut.getView().findViewById(child1.getView().getId())).isNull();
222
+        assertNotChildOf(uut.getView(), child1.getView());
223 223
         uut.push(child1, new CommandListenerAdapter());
224
-        assertThat(uut.getView().findViewById(child1.getView().getId())).isNotNull();
224
+        assertIsChild(uut.getView(), child1.getView());
225 225
     }
226 226
 
227 227
     @Test
228 228
     public void push_removesPreviousFromTree() {
229
-        assertThat(uut.getView().findViewById(child1.getView().getId())).isNull();
229
+        assertNotChildOf(uut.getView(), child1.getView());
230 230
         uut.push(child1, new CommandListenerAdapter());
231
-        assertThat(uut.getView().findViewById(child1.getView().getId())).isNotNull();
231
+        assertIsChild(uut.getView(), child1.getView());
232 232
         uut.push(child2, new CommandListenerAdapter() {
233 233
             @Override
234 234
             public void onSuccess(String childId) {
@@ -299,11 +299,11 @@ public class StackControllerTest extends BaseTest {
299 299
         uut.push(child2, new CommandListenerAdapter() {
300 300
             @Override
301 301
             public void onSuccess(String childId) {
302
-                assertIsChildById(uut.getView(), child2View);
302
+                assertIsChild(uut.getView(), child2View);
303 303
                 assertNotChildOf(uut.getView(), child1View);
304 304
                 uut.pop(new CommandListenerAdapter());
305 305
                 assertNotChildOf(uut.getView(), child2View);
306
-                assertIsChildById(uut.getView(), child1View);
306
+                assertIsChild(uut.getView(), child1View);
307 307
             }
308 308
         });
309 309
     }
@@ -318,7 +318,7 @@ public class StackControllerTest extends BaseTest {
318 318
                     @Override
319 319
                     public void onSuccess(String childId) {
320 320
                         assertContainsOnlyId(child1.getId());
321
-                        assertIsChildById(uut.getView(), child1.getView());
321
+                        assertIsChild(uut.getView(), child1.getView());
322 322
                     }
323 323
                 });
324 324
             }
@@ -329,10 +329,10 @@ public class StackControllerTest extends BaseTest {
329 329
     public void popSpecific_deepInStack() {
330 330
         uut.push(child1, new CommandListenerAdapter());
331 331
         uut.push(child2, new CommandListenerAdapter());
332
-        assertIsChildById(uut.getView(), child2.getView());
332
+        assertIsChild(uut.getView(), child2.getView());
333 333
         uut.popSpecific(child1, new CommandListenerAdapter());
334 334
         assertContainsOnlyId(child2.getId());
335
-        assertIsChildById(uut.getView(), child2.getView());
335
+        assertIsChild(uut.getView(), child2.getView());
336 336
     }
337 337
 
338 338
     @Test

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

@@ -185,11 +185,6 @@ public class ViewControllerTest extends BaseTest {
185 185
         verify(spy, times(1)).onViewDisappear();
186 186
     }
187 187
 
188
-    @Test
189
-    public void assignsIdToCreatedView() {
190
-        assertThat(uut.getView().getId()).isPositive();
191
-    }
192
-
193 188
     @Test
194 189
     public void onDestroy_RemovesSelfFromParentIfExists() {
195 190
         LinearLayout parent = new LinearLayout(activity);