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

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

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

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

139
         when(bottomTabsMock.size()).thenReturn(0);
139
         when(bottomTabsMock.size()).thenReturn(0);
140
 
140
 
141
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
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
         final LayoutNode tabNode = createBottomTabNode(containerNode);
143
         final LayoutNode tabNode = createBottomTabNode(containerNode);
144
 
144
 
145
         final View result = createLayoutFactory(bottomTabsMock).create(tabNode);
145
         final View result = createLayoutFactory(bottomTabsMock).create(tabNode);
147
         verify(bottomTabsMock).add("#0");
147
         verify(bottomTabsMock).add("#0");
148
 
148
 
149
         assertThat(result).isInstanceOf(BottomTabsLayout.class);
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
         View view = TestUtils.assertViewChildrenCount(container, 1).get(0);
152
         View view = TestUtils.assertViewChildrenCount(container, 1).get(0);
152
         assertThat(view).isEqualTo(mockView);
153
         assertThat(view).isEqualTo(mockView);
153
     }
154
     }
158
         when(bottomTabsMock.size()).thenReturn(0, 1);
159
         when(bottomTabsMock.size()).thenReturn(0, 1);
159
 
160
 
160
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
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
         when(reactRootViewCreator.create(eq(OTHER_NODE_ID), eq(OTHER_REACT_ROOT_VIEW_KEY))).thenReturn(otherMockView);
164
         when(reactRootViewCreator.create(eq(OTHER_NODE_ID), eq(OTHER_REACT_ROOT_VIEW_KEY))).thenReturn(otherMockView);
164
         final LayoutNode secondTabRootNode = createContainerStackNode(createContainerNode(OTHER_NODE_ID, OTHER_REACT_ROOT_VIEW_KEY));
165
         final LayoutNode secondTabRootNode = createContainerStackNode(createContainerNode(OTHER_NODE_ID, OTHER_REACT_ROOT_VIEW_KEY));
172
         verify(bottomTabsMock).add(eq("#1"));
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
     @Test
221
     @Test
176
     public void pushScreenToScreenStackLayout() throws Exception {
222
     public void pushScreenToScreenStackLayout() throws Exception {
177
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
223
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
227
         containerStackLayout.push(pushedContainer);
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
     @Test
282
     @Test
231
     public void popScreenFromScreenStackLayout() {
283
     public void popScreenFromScreenStackLayout() {
232
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
284
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);