Browse Source

Stop setting id for root views

Guy Carmeli 6 years ago
parent
commit
c80453dc70

+ 16
- 0
lib/android/app/src/main/java/com/reactnativenavigation/utils/ViewUtils.java View File

58
     public interface Matcher<T> {
58
     public interface Matcher<T> {
59
         boolean match(T child);
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 View File

12
 
12
 
13
 import com.reactnativenavigation.parse.Options;
13
 import com.reactnativenavigation.parse.Options;
14
 import com.reactnativenavigation.presentation.FabOptionsPresenter;
14
 import com.reactnativenavigation.presentation.FabOptionsPresenter;
15
-import com.reactnativenavigation.utils.CompatUtils;
16
 import com.reactnativenavigation.utils.StringUtils;
15
 import com.reactnativenavigation.utils.StringUtils;
17
 import com.reactnativenavigation.utils.Task;
16
 import com.reactnativenavigation.utils.Task;
18
 import com.reactnativenavigation.utils.UiUtils;
17
 import com.reactnativenavigation.utils.UiUtils;
112
                 throw new RuntimeException("Tried to create view after it has already been destroyed");
111
                 throw new RuntimeException("Tried to create view after it has already been destroyed");
113
             }
112
             }
114
             view = createView();
113
             view = createView();
115
-            if (view.getId() < 0) {
116
-                view.setId(CompatUtils.generateViewId());
117
-            }
118
             view.getViewTreeObserver().addOnGlobalLayoutListener(this);
114
             view.getViewTreeObserver().addOnGlobalLayoutListener(this);
119
         }
115
         }
120
         return view;
116
         return view;

+ 4
- 5
lib/android/app/src/test/java/com/reactnativenavigation/BaseTest.java View File

5
 import android.view.*;
5
 import android.view.*;
6
 
6
 
7
 import com.reactnativenavigation.parse.params.Bool;
7
 import com.reactnativenavigation.parse.params.Bool;
8
+import com.reactnativenavigation.utils.ViewUtils;
8
 import com.reactnativenavigation.viewcontrollers.ViewController;
9
 import com.reactnativenavigation.viewcontrollers.ViewController;
9
 
10
 
10
 import org.junit.*;
11
 import org.junit.*;
31
         return Robolectric.setupActivity(AppCompatActivity.class);
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
         assertThat(parent).isNotNull();
36
         assertThat(parent).isNotNull();
36
         assertThat(child).isNotNull();
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
     public void assertNotChildOf(ViewGroup parent, View child) {
41
     public void assertNotChildOf(ViewGroup parent, View child) {
42
         assertThat(parent).isNotNull();
42
         assertThat(parent).isNotNull();
43
         assertThat(child).isNotNull();
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
     protected void disablePushAnimation(ViewController... controllers) {
47
     protected void disablePushAnimation(ViewController... controllers) {

+ 14
- 14
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java View File

65
     public void setRoot_AddsChildControllerView() {
65
     public void setRoot_AddsChildControllerView() {
66
         assertThat(uut.getView().getChildCount()).isZero();
66
         assertThat(uut.getView().getChildCount()).isZero();
67
         uut.setRoot(child1, new MockPromise());
67
         uut.setRoot(child1, new MockPromise());
68
-        assertIsChildById(uut.getView(), child1.getView());
68
+        assertIsChild(uut.getView(), child1.getView());
69
     }
69
     }
70
 
70
 
71
     @Test
71
     @Test
72
     public void setRoot_ReplacesExistingChildControllerViews() {
72
     public void setRoot_ReplacesExistingChildControllerViews() {
73
         uut.setRoot(child1, new MockPromise());
73
         uut.setRoot(child1, new MockPromise());
74
         uut.setRoot(child2, new MockPromise());
74
         uut.setRoot(child2, new MockPromise());
75
-        assertIsChildById(uut.getView(), child2.getView());
75
+        assertIsChild(uut.getView(), child2.getView());
76
     }
76
     }
77
 
77
 
78
     @Test
78
     @Test
87
         stackController.push(child1, new CommandListenerAdapter());
87
         stackController.push(child1, new CommandListenerAdapter());
88
         uut.setRoot(stackController, new MockPromise());
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
         uut.push(child1.getId(), child2, new CommandListenerAdapter());
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
     @Test
99
     @Test
100
     public void push_InvalidPushWithoutAStack_DoesNothing() {
100
     public void push_InvalidPushWithoutAStack_DoesNothing() {
101
         uut.setRoot(child1, new MockPromise());
101
         uut.setRoot(child1, new MockPromise());
102
         uut.push(child1.getId(), child2, new CommandListenerAdapter());
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
     @Test
106
     @Test
273
     }
273
     }
274
 
274
 
275
     @Test
275
     @Test
276
-    public void push_Promise() {
276
+    public void push_promise() {
277
         final StackController stackController = newStack();
277
         final StackController stackController = newStack();
278
         stackController.push(child1, new CommandListenerAdapter());
278
         stackController.push(child1, new CommandListenerAdapter());
279
         uut.setRoot(stackController, new MockPromise());
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
         uut.push(child1.getId(), child2, new CommandListenerAdapter() {
284
         uut.push(child1.getId(), child2, new CommandListenerAdapter() {
285
             @Override
285
             @Override
286
             public void onSuccess(String childId) {
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
         uut.push(child1.getId(), child2, new CommandListenerAdapter() {
296
         uut.push(child1.getId(), child2, new CommandListenerAdapter() {
297
             @Override
297
             @Override
298
             public void onError(String message) {
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
         stackController.push(child1, new CommandListenerAdapter());
341
         stackController.push(child1, new CommandListenerAdapter());
342
         uut.showModal(stackController, new MockPromise());
342
         uut.showModal(stackController, new MockPromise());
343
         uut.push(stackController.getId(), child2, new CommandListenerAdapter());
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
     @Test
347
     @Test

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

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

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

185
         verify(spy, times(1)).onViewDisappear();
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
     @Test
188
     @Test
194
     public void onDestroy_RemovesSelfFromParentIfExists() {
189
     public void onDestroy_RemovesSelfFromParentIfExists() {
195
         LinearLayout parent = new LinearLayout(activity);
190
         LinearLayout parent = new LinearLayout(activity);