react-native-navigation的迁移库

BottomTabsContainerTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.reactnativenavigation.layout;
  2. import android.app.Activity;
  3. import android.view.View;
  4. import com.reactnativenavigation.BaseTest;
  5. import com.reactnativenavigation.layout.bottomtabs.BottomTabs;
  6. import com.reactnativenavigation.layout.bottomtabs.BottomTabsLayout;
  7. import com.reactnativenavigation.layout.bottomtabs.TooManyTabsException;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import org.robolectric.Robolectric;
  11. import static org.assertj.core.api.Java6Assertions.assertThat;
  12. import static org.mockito.Mockito.mock;
  13. import static org.mockito.Mockito.verify;
  14. public class BottomTabsContainerTest extends BaseTest {
  15. private static final String TAB_NAME = "myTab";
  16. private static final String OTHER_TAB_NAME = "myOtherTab";
  17. private BottomTabs bottomTabs;
  18. private Activity activity;
  19. @Before
  20. public void setUp() throws Exception {
  21. bottomTabs = mock(BottomTabs.class);
  22. activity = Robolectric.buildActivity(Activity.class).get();
  23. }
  24. @Test
  25. public void addsTabToBottomTabs() throws Exception {
  26. ContainerStackLayout tabContent = new ContainerStackLayout(activity);
  27. BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
  28. bottomTabsContainer.addTabContent(TAB_NAME, tabContent);
  29. verify(bottomTabs).add(TAB_NAME);
  30. }
  31. @Test
  32. public void addsTabContentToLayout() throws Exception {
  33. ContainerStackLayout stackLayout = new ContainerStackLayout(activity);
  34. BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
  35. bottomTabsContainer.addTabContent(TAB_NAME, stackLayout);
  36. verify(bottomTabs).attach(bottomTabsContainer);
  37. TestUtils.assertViewChildren(bottomTabsContainer, stackLayout);
  38. }
  39. @Test
  40. public void addsTwoTabsContentToLayout() throws Exception {
  41. View tabContent = new ContainerStackLayout(activity);
  42. View otherTabContent = new ContainerStackLayout(activity);
  43. BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
  44. bottomTabsContainer.addTabContent(TAB_NAME, tabContent);
  45. bottomTabsContainer.addTabContent(OTHER_TAB_NAME, otherTabContent);
  46. TestUtils.assertViewChildren(bottomTabsContainer, tabContent, otherTabContent);
  47. }
  48. @Test(expected = TooManyTabsException.class)
  49. public void throwsExceptionWhenMoreThenFiveTabs() throws Exception {
  50. BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
  51. for (int i = 0; i < 6; i++) {
  52. ContainerStackLayout content = new ContainerStackLayout(activity);
  53. bottomTabsContainer.addTabContent("#" + i, content);
  54. }
  55. }
  56. @Test
  57. public void addFiveTabs() throws Exception {
  58. BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
  59. for (int i = 0; i < 5; i++) {
  60. ContainerStackLayout content = new ContainerStackLayout(activity);
  61. bottomTabsContainer.addTabContent("#" + i, content);
  62. }
  63. }
  64. @Test
  65. public void onlyFirstTabShouldBeVisible() throws Exception {
  66. ContainerStackLayout tabContent = new ContainerStackLayout(activity);
  67. ContainerStackLayout otherTabContent = new ContainerStackLayout(activity);
  68. BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
  69. bottomTabsContainer.addTabContent(TAB_NAME, tabContent);
  70. bottomTabsContainer.addTabContent(OTHER_TAB_NAME, otherTabContent);
  71. assertThat(tabContent.getVisibility()).isEqualTo(View.VISIBLE);
  72. assertThat(otherTabContent.getVisibility()).isEqualTo(View.GONE);
  73. }
  74. @Test
  75. public void listensToTabsSwitchingEvents() throws Exception {
  76. BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
  77. verify(bottomTabs).setSelectionListener(bottomTabsContainer);
  78. }
  79. @Test
  80. public void switchesBetweenFirstAndSecondTabs() throws Exception {
  81. ContainerStackLayout tabContent = new ContainerStackLayout(activity);
  82. ContainerStackLayout otherTabContent = new ContainerStackLayout(activity);
  83. BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
  84. bottomTabsContainer.addTabContent(TAB_NAME, tabContent);
  85. bottomTabsContainer.addTabContent(OTHER_TAB_NAME, otherTabContent);
  86. bottomTabsContainer.onTabSelected(1);
  87. assertThat(tabContent.getVisibility()).isEqualTo(View.GONE);
  88. assertThat(otherTabContent.getVisibility()).isEqualTo(View.VISIBLE);
  89. }
  90. @Test
  91. public void switchesBetweenSecondAndFirstTabs() throws Exception {
  92. ContainerStackLayout tabContent = new ContainerStackLayout(activity);
  93. ContainerStackLayout otherTabContent = new ContainerStackLayout(activity);
  94. BottomTabsLayout bottomTabsContainer = createBottomTabsContainer();
  95. bottomTabsContainer.addTabContent(TAB_NAME, tabContent);
  96. bottomTabsContainer.addTabContent(OTHER_TAB_NAME, otherTabContent);
  97. bottomTabsContainer.onTabSelected(1);
  98. bottomTabsContainer.onTabSelected(0);
  99. assertThat(tabContent.getVisibility()).isEqualTo(View.VISIBLE);
  100. assertThat(otherTabContent.getVisibility()).isEqualTo(View.GONE);
  101. }
  102. private BottomTabsLayout createBottomTabsContainer() {
  103. return new BottomTabsLayout(activity, bottomTabs);
  104. }
  105. }