Browse Source

Throw exception when adding more than 5 tabs

Guy Carmeli 7 years ago
parent
commit
886672b24c

+ 4
- 4
android/app/src/main/java/com/reactnativenavigation/layout/bottomtabs/BottomTabs.java View File

16
 
16
 
17
 public class BottomTabs implements BottomNavigationView.OnNavigationItemSelectedListener {
17
 public class BottomTabs implements BottomNavigationView.OnNavigationItemSelectedListener {
18
 
18
 
19
-    public interface BottomTabsSelectionListener {
19
+    interface BottomTabsSelectionListener {
20
         void onTabSelected(int index);
20
         void onTabSelected(int index);
21
     }
21
     }
22
 
22
 
35
     }
35
     }
36
 
36
 
37
     public void add(String label) {
37
     public void add(String label) {
38
-        int tabId = getTabsCount();
38
+        int tabId = size();
39
         bottomNavigationView.getMenu().add(0, tabId, Menu.NONE, label);
39
         bottomNavigationView.getMenu().add(0, tabId, Menu.NONE, label);
40
     }
40
     }
41
 
41
 
42
-    public int getTabsCount() {
42
+    public int size() {
43
         return bottomNavigationView.getMenu().size();
43
         return bottomNavigationView.getMenu().size();
44
     }
44
     }
45
 
45
 
46
-    public int getViewId() {
46
+    int getViewId() {
47
         return bottomNavigationView.getId();
47
         return bottomNavigationView.getId();
48
     }
48
     }
49
 
49
 

+ 3
- 1
android/app/src/main/java/com/reactnativenavigation/layout/bottomtabs/BottomTabsContainer.java View File

15
 
15
 
16
     public BottomTabsContainer(Activity activity, BottomTabs bottomTabs) {
16
     public BottomTabsContainer(Activity activity, BottomTabs bottomTabs) {
17
         super(activity);
17
         super(activity);
18
-
19
         initBottomTabs(bottomTabs);
18
         initBottomTabs(bottomTabs);
20
     }
19
     }
21
 
20
 
22
     public void addTabContent(String label, View tabContent) {
21
     public void addTabContent(String label, View tabContent) {
22
+        if (tabsContent.size() > 5) {
23
+            throw new TooManyTabsException();
24
+        }
23
         bottomTabs.add(label);
25
         bottomTabs.add(label);
24
         attachTabContent(tabContent);
26
         attachTabContent(tabContent);
25
         tabsContent.add(tabContent);
27
         tabsContent.add(tabContent);

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/layout/bottomtabs/TooManyTabsException.java View File

1
+package com.reactnativenavigation.layout.bottomtabs;
2
+
3
+public class TooManyTabsException extends RuntimeException {
4
+}

+ 10
- 0
android/app/src/test/java/com/reactnativenavigation/BottomTabsContainerTest.java View File

5
 
5
 
6
 import com.reactnativenavigation.layout.bottomtabs.BottomTabs;
6
 import com.reactnativenavigation.layout.bottomtabs.BottomTabs;
7
 import com.reactnativenavigation.layout.bottomtabs.BottomTabsContainer;
7
 import com.reactnativenavigation.layout.bottomtabs.BottomTabsContainer;
8
+import com.reactnativenavigation.layout.bottomtabs.TooManyTabsException;
8
 
9
 
9
 import org.junit.Before;
10
 import org.junit.Before;
10
 import org.junit.Test;
11
 import org.junit.Test;
64
         TestUtils.assertViewChildren(bottomTabsContainer, tabContent, otherTabContent);
65
         TestUtils.assertViewChildren(bottomTabsContainer, tabContent, otherTabContent);
65
     }
66
     }
66
 
67
 
68
+    @Test (expected = TooManyTabsException.class)
69
+    public void throwsExceptionWhenMoreThenFiveTabs() throws Exception {
70
+        BottomTabsContainer bottomTabsContainer = createBottomTabsContainer();
71
+        for (int i = 0; i <= 6; i++) {
72
+            View content = new View(activity);
73
+            bottomTabsContainer.addTabContent("#" + i, content);
74
+        }
75
+    }
76
+
67
     @Test
77
     @Test
68
     public void onlyFirstTabShouldBeVisible() throws Exception {
78
     public void onlyFirstTabShouldBeVisible() throws Exception {
69
         View tabContent = new View(activity);
79
         View tabContent = new View(activity);

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

100
     @Test
100
     @Test
101
     public void returnsSingleTabContent() throws Exception {
101
     public void returnsSingleTabContent() throws Exception {
102
         BottomTabs bottomTabsMock = mock(BottomTabs.class);
102
         BottomTabs bottomTabsMock = mock(BottomTabs.class);
103
-        when(bottomTabsMock.getTabsCount()).thenReturn(0);
103
+        when(bottomTabsMock.size()).thenReturn(0);
104
 
104
 
105
         when(rootViewCreator.createRootView(eq(VIEW_ID), eq(VIEW_NAME))).thenReturn(mockView);
105
         when(rootViewCreator.createRootView(eq(VIEW_ID), eq(VIEW_NAME))).thenReturn(mockView);
106
         final LayoutNode containerNode = createContainerNode();
106
         final LayoutNode containerNode = createContainerNode();
119
     @Test
119
     @Test
120
     public void returnsTwoTabContent() throws Exception {
120
     public void returnsTwoTabContent() throws Exception {
121
         BottomTabs bottomTabsMock = mock(BottomTabs.class);
121
         BottomTabs bottomTabsMock = mock(BottomTabs.class);
122
-        when(bottomTabsMock.getTabsCount()).thenReturn(0, 1);
122
+        when(bottomTabsMock.size()).thenReturn(0, 1);
123
 
123
 
124
         when(rootViewCreator.createRootView(eq(VIEW_ID), eq(VIEW_NAME))).thenReturn(mockView);
124
         when(rootViewCreator.createRootView(eq(VIEW_ID), eq(VIEW_NAME))).thenReturn(mockView);
125
         final LayoutNode firstTabRootNode = createContainerNode(VIEW_ID, VIEW_NAME);
125
         final LayoutNode firstTabRootNode = createContainerNode(VIEW_ID, VIEW_NAME);
136
         verify(bottomTabsMock).add(eq("#1"));
136
         verify(bottomTabsMock).add(eq("#1"));
137
     }
137
     }
138
 
138
 
139
+
139
     @Test(expected = IllegalArgumentException.class)
140
     @Test(expected = IllegalArgumentException.class)
140
     public void throwsExceptionForUnknownType() throws Exception {
141
     public void throwsExceptionForUnknownType() throws Exception {
141
         when(rootViewCreator.createRootView(eq(VIEW_ID), eq(VIEW_NAME))).thenReturn(mockView);
142
         when(rootViewCreator.createRootView(eq(VIEW_ID), eq(VIEW_NAME))).thenReturn(mockView);