Browse Source

Push pop to BottomTabsLayout

Guy Carmeli 8 years ago
parent
commit
868fe5c4b4

+ 6
- 6
android/app/src/main/java/com/reactnativenavigation/layout/bottomtabs/BottomTabsLayout.java View File

@@ -11,7 +11,7 @@ import java.util.List;
11 11
 
12 12
 public class BottomTabsLayout extends RelativeLayout implements BottomTabs.BottomTabsSelectionListener, StackLayout {
13 13
 
14
-    private List<View> tabsContent;
14
+    private List<StackLayout> tabsContent;
15 15
     private BottomTabs bottomTabs;
16 16
     private int currentTab;
17 17
 
@@ -26,7 +26,7 @@ public class BottomTabsLayout extends RelativeLayout implements BottomTabs.Botto
26 26
         }
27 27
         bottomTabs.add(label);
28 28
         attachTabContent(tabContent);
29
-        tabsContent.add(tabContent);
29
+        tabsContent.add((StackLayout) tabContent);
30 30
 
31 31
         if (tabsContent.size() > 1) {
32 32
             tabContent.setVisibility(View.GONE);
@@ -55,21 +55,21 @@ public class BottomTabsLayout extends RelativeLayout implements BottomTabs.Botto
55 55
     }
56 56
 
57 57
     private void showTab(int tabId) {
58
-        tabsContent.get(tabId).setVisibility(View.VISIBLE);
58
+        tabsContent.get(tabId).asView().setVisibility(View.VISIBLE);
59 59
     }
60 60
 
61 61
     private void hideTab(int tabId) {
62
-        tabsContent.get(tabId).setVisibility(View.GONE);
62
+        tabsContent.get(tabId).asView().setVisibility(View.GONE);
63 63
     }
64 64
 
65 65
     @Override
66 66
     public void push(View view) {
67
-
67
+        tabsContent.get(currentTab).push(view);
68 68
     }
69 69
 
70 70
     @Override
71 71
     public void pop() {
72
-
72
+        tabsContent.get(currentTab).pop();
73 73
     }
74 74
 
75 75
     @Override

+ 15
- 14
android/app/src/test/java/com/reactnativenavigation/BottomTabsContainerTest.java View File

@@ -3,6 +3,7 @@ package com.reactnativenavigation;
3 3
 import android.app.Activity;
4 4
 import android.view.View;
5 5
 
6
+import com.reactnativenavigation.layout.ContainerStackLayout;
6 7
 import com.reactnativenavigation.layout.bottomtabs.BottomTabs;
7 8
 import com.reactnativenavigation.layout.bottomtabs.BottomTabsLayout;
8 9
 import com.reactnativenavigation.layout.bottomtabs.TooManyTabsException;
@@ -34,7 +35,7 @@ public class BottomTabsContainerTest {
34 35
 
35 36
     @Test
36 37
     public void addsTabToBottomTabs() throws Exception {
37
-        View tabContent = new View(activity);
38
+        ContainerStackLayout tabContent = new ContainerStackLayout(activity);
38 39
 
39 40
         BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
40 41
         bottomTabsContainer.addTabContent(TAB_NAME, tabContent);
@@ -44,19 +45,19 @@ public class BottomTabsContainerTest {
44 45
 
45 46
     @Test
46 47
     public void addsTabContentToLayout() throws Exception {
47
-        View tabContent = new View(activity);
48
+        ContainerStackLayout stackLayout = new ContainerStackLayout(activity);
48 49
 
49 50
         BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
50
-        bottomTabsContainer.addTabContent(TAB_NAME, tabContent);
51
+        bottomTabsContainer.addTabContent(TAB_NAME, stackLayout);
51 52
 
52 53
         verify(bottomTabs).attach(bottomTabsContainer);
53
-        TestUtils.assertViewChildren(bottomTabsContainer, tabContent);
54
+        TestUtils.assertViewChildren(bottomTabsContainer, stackLayout);
54 55
     }
55 56
 
56 57
     @Test
57 58
     public void addsTwoTabsContentToLayout() throws Exception {
58
-        View tabContent = new View(activity);
59
-        View otherTabContent = new View(activity);
59
+        View tabContent = new ContainerStackLayout(activity);
60
+        View otherTabContent = new ContainerStackLayout(activity);
60 61
 
61 62
         BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
62 63
         bottomTabsContainer.addTabContent(TAB_NAME, tabContent);
@@ -69,7 +70,7 @@ public class BottomTabsContainerTest {
69 70
     public void throwsExceptionWhenMoreThenFiveTabs() throws Exception {
70 71
         BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
71 72
         for (int i = 0; i < 6; i++) {
72
-            View content = new View(activity);
73
+            ContainerStackLayout content = new ContainerStackLayout(activity);
73 74
             bottomTabsContainer.addTabContent("#" + i, content);
74 75
         }
75 76
     }
@@ -78,15 +79,15 @@ public class BottomTabsContainerTest {
78 79
     public void addFiveTabs() throws Exception {
79 80
         BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
80 81
         for (int i = 0; i < 5; i++) {
81
-            View content = new View(activity);
82
+            ContainerStackLayout content = new ContainerStackLayout(activity);
82 83
             bottomTabsContainer.addTabContent("#" + i, content);
83 84
         }
84 85
     }
85 86
 
86 87
     @Test
87 88
     public void onlyFirstTabShouldBeVisible() throws Exception {
88
-        View tabContent = new View(activity);
89
-        View otherTabContent = new View(activity);
89
+        ContainerStackLayout tabContent = new ContainerStackLayout(activity);
90
+        ContainerStackLayout otherTabContent = new ContainerStackLayout(activity);
90 91
 
91 92
         BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
92 93
         bottomTabsContainer.addTabContent(TAB_NAME, tabContent);
@@ -104,8 +105,8 @@ public class BottomTabsContainerTest {
104 105
 
105 106
     @Test
106 107
     public void switchesBetweenFirstAndSecondTabs() throws Exception {
107
-        View tabContent = new View(activity);
108
-        View otherTabContent = new View(activity);
108
+        ContainerStackLayout tabContent = new ContainerStackLayout(activity);
109
+        ContainerStackLayout otherTabContent = new ContainerStackLayout(activity);
109 110
 
110 111
         BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
111 112
         bottomTabsContainer.addTabContent(TAB_NAME, tabContent);
@@ -118,8 +119,8 @@ public class BottomTabsContainerTest {
118 119
 
119 120
     @Test
120 121
     public void switchesBetweenSecondAndFirstTabs() throws Exception {
121
-        View tabContent = new View(activity);
122
-        View otherTabContent = new View(activity);
122
+        ContainerStackLayout tabContent = new ContainerStackLayout(activity);
123
+        ContainerStackLayout otherTabContent = new ContainerStackLayout(activity);
123 124
 
124 125
         BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
125 126
         bottomTabsContainer.addTabContent(TAB_NAME, tabContent);

+ 55
- 3
android/app/src/test/java/com/reactnativenavigation/LayoutFactoryTest.java View File

@@ -139,7 +139,7 @@ public class LayoutFactoryTest {
139 139
         when(bottomTabsMock.size()).thenReturn(0);
140 140
 
141 141
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
142
-        final LayoutNode containerNode = createContainerNode();
142
+        final LayoutNode containerNode = createContainerStackNode(createContainerNode());
143 143
         final LayoutNode tabNode = createBottomTabNode(containerNode);
144 144
 
145 145
         final View result = createLayoutFactory(bottomTabsMock).create(tabNode);
@@ -147,7 +147,8 @@ public class LayoutFactoryTest {
147 147
         verify(bottomTabsMock).add("#0");
148 148
 
149 149
         assertThat(result).isInstanceOf(BottomTabsLayout.class);
150
-        Container container = (Container) TestUtils.assertViewChildrenCount((BottomTabsLayout) result, 1).get(0);
150
+        ViewGroup containerStack = (ViewGroup) TestUtils.assertViewChildrenCount((BottomTabsLayout) result, 1).get(0);
151
+        Container container = (Container)  TestUtils.assertViewChildrenCount(containerStack, 1).get(0);
151 152
         View view = TestUtils.assertViewChildrenCount(container, 1).get(0);
152 153
         assertThat(view).isEqualTo(mockView);
153 154
     }
@@ -158,7 +159,7 @@ public class LayoutFactoryTest {
158 159
         when(bottomTabsMock.size()).thenReturn(0, 1);
159 160
 
160 161
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
161
-        final LayoutNode firstTabRootNode = createContainerNode(NODE_ID, REACT_ROOT_VIEW_KEY);
162
+        final LayoutNode firstTabRootNode = createContainerStackNode(createContainerNode(NODE_ID, REACT_ROOT_VIEW_KEY));
162 163
 
163 164
         when(reactRootViewCreator.create(eq(OTHER_NODE_ID), eq(OTHER_REACT_ROOT_VIEW_KEY))).thenReturn(otherMockView);
164 165
         final LayoutNode secondTabRootNode = createContainerStackNode(createContainerNode(OTHER_NODE_ID, OTHER_REACT_ROOT_VIEW_KEY));
@@ -172,6 +173,51 @@ public class LayoutFactoryTest {
172 173
         verify(bottomTabsMock).add(eq("#1"));
173 174
     }
174 175
 
176
+    @Test
177
+    public void pushScreenToFirstBottomTab() {
178
+        BottomTabs bottomTabsMock = mock(BottomTabs.class);
179
+        when(bottomTabsMock.size()).thenReturn(0, 1);
180
+
181
+        when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
182
+        final LayoutNode firstTabContainerStack = createContainerStackNode(createContainerNode(NODE_ID, REACT_ROOT_VIEW_KEY));
183
+
184
+        final LayoutNode tabNode = createBottomTabNode(firstTabContainerStack);
185
+        final BottomTabsLayout bottomTabsContainer = (BottomTabsLayout) createLayoutFactory(bottomTabsMock).create(tabNode);
186
+        verify(bottomTabsMock).add(eq("#0"));
187
+
188
+        View pushedReactView = new View(activity);
189
+        when(reactRootViewCreator.create(eq("pushedReactViewId"), eq("pushedReactViewKey"))).thenReturn(pushedReactView);
190
+        View view = createLayoutFactory().create(createContainerNode("pushedReactViewId", "pushedReactViewKey"));
191
+        bottomTabsContainer.push(view);
192
+
193
+        ViewGroup containerStack = (ViewGroup) TestUtils.assertViewChildrenCount(bottomTabsContainer, 1).get(0);
194
+        ViewGroup container = (ViewGroup) TestUtils.assertViewChildrenCount(containerStack, 1).get(0);
195
+        View result = TestUtils.assertViewChildrenCount(container, 1).get(0);
196
+        assertThat(result).isEqualTo(pushedReactView);
197
+    }
198
+
199
+    @Test
200
+    public void popScreenFromFirstBottomTab() {
201
+        BottomTabs bottomTabsMock = mock(BottomTabs.class);
202
+        when(bottomTabsMock.size()).thenReturn(0, 1);
203
+
204
+        when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
205
+        final LayoutNode firstTabContainerStack = createContainerStackNode(createContainerNode(NODE_ID, REACT_ROOT_VIEW_KEY));
206
+
207
+        final LayoutNode tabNode = createBottomTabNode(firstTabContainerStack);
208
+        final BottomTabsLayout bottomTabsContainer = (BottomTabsLayout) createLayoutFactory(bottomTabsMock).create(tabNode);
209
+        verify(bottomTabsMock).add(eq("#0"));
210
+
211
+        pushContainer(bottomTabsContainer, "pushedReactViewId", "pushedReactViewKey", new View(activity));
212
+
213
+        bottomTabsContainer.pop();
214
+
215
+        ViewGroup containerStack = (ViewGroup) TestUtils.assertViewChildrenCount(bottomTabsContainer, 1).get(0);
216
+        ViewGroup container = (ViewGroup) TestUtils.assertViewChildrenCount(containerStack, 1).get(0);
217
+        View result = TestUtils.assertViewChildrenCount(container, 1).get(0);
218
+        assertThat(result).isEqualTo(mockView);
219
+    }
220
+
175 221
     @Test
176 222
     public void pushScreenToScreenStackLayout() throws Exception {
177 223
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
@@ -227,6 +273,12 @@ public class LayoutFactoryTest {
227 273
         containerStackLayout.push(pushedContainer);
228 274
     }
229 275
 
276
+    private void pushContainer(BottomTabsLayout containerStackLayout, String screenId, String reactRootViewKey, View rootView) {
277
+        when(reactRootViewCreator.create(eq(screenId), eq(reactRootViewKey))).thenReturn(rootView);
278
+        View pushedContainer = createLayoutFactory().create(createContainerNode(screenId, reactRootViewKey));
279
+        containerStackLayout.push(pushedContainer);
280
+    }
281
+
230 282
     @Test
231 283
     public void popScreenFromScreenStackLayout() {
232 284
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);