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,7 +16,7 @@ import static android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM;
16 16
 
17 17
 public class BottomTabs implements BottomNavigationView.OnNavigationItemSelectedListener {
18 18
 
19
-    public interface BottomTabsSelectionListener {
19
+    interface BottomTabsSelectionListener {
20 20
         void onTabSelected(int index);
21 21
     }
22 22
 
@@ -35,15 +35,15 @@ public class BottomTabs implements BottomNavigationView.OnNavigationItemSelected
35 35
     }
36 36
 
37 37
     public void add(String label) {
38
-        int tabId = getTabsCount();
38
+        int tabId = size();
39 39
         bottomNavigationView.getMenu().add(0, tabId, Menu.NONE, label);
40 40
     }
41 41
 
42
-    public int getTabsCount() {
42
+    public int size() {
43 43
         return bottomNavigationView.getMenu().size();
44 44
     }
45 45
 
46
-    public int getViewId() {
46
+    int getViewId() {
47 47
         return bottomNavigationView.getId();
48 48
     }
49 49
 

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

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

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

@@ -0,0 +1,4 @@
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,6 +5,7 @@ import android.view.View;
5 5
 
6 6
 import com.reactnativenavigation.layout.bottomtabs.BottomTabs;
7 7
 import com.reactnativenavigation.layout.bottomtabs.BottomTabsContainer;
8
+import com.reactnativenavigation.layout.bottomtabs.TooManyTabsException;
8 9
 
9 10
 import org.junit.Before;
10 11
 import org.junit.Test;
@@ -64,6 +65,15 @@ public class BottomTabsContainerTest {
64 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 77
     @Test
68 78
     public void onlyFirstTabShouldBeVisible() throws Exception {
69 79
         View tabContent = new View(activity);

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

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