From 967dc1e52877ce895602ae1e3b8a55d8e4e9346a Mon Sep 17 00:00:00 2001 From: Iiro Krankka Date: Mon, 22 Aug 2016 01:24:17 +0300 Subject: [PATCH] Moved the tests to the library module. --- .idea/runConfigurations/All_Tests.xml | 8 +- CHANGELOG.md | 1 + app/app.iml | 2 - app/build.gradle | 2 - app/src/androidTest/res/xml/dummy_tab_xml.xml | 8 -- bottom-bar/bottom-bar.iml | 29 +++---- bottom-bar/build.gradle | 9 +++ .../com/roughike/bottombar/BadgeTest.java | 31 ++++---- .../com/roughike/bottombar/TabParserTest.java | 6 +- .../bottombar/ThreeFixedTabsTest.java | 72 ++++++++---------- .../res/drawable-nodpi/empty_icon.png | Bin .../src/androidTest/res/values/colors.xml | 0 .../src/androidTest/res/values/strings.xml | 0 .../androidTest/res/xml/dummy_tabs_five.xml | 8 ++ .../androidTest/res/xml/dummy_tabs_three.xml | 15 ++++ 15 files changed, 103 insertions(+), 88 deletions(-) delete mode 100644 app/src/androidTest/res/xml/dummy_tab_xml.xml rename app/src/androidTest/java/com/roughike/bottombar/BadgeActivityTest.java => bottom-bar/src/androidTest/java/com/roughike/bottombar/BadgeTest.java (67%) rename {app => bottom-bar}/src/androidTest/java/com/roughike/bottombar/TabParserTest.java (93%) rename app/src/androidTest/java/com/roughike/bottombar/ThreeTabsActivityTest.java => bottom-bar/src/androidTest/java/com/roughike/bottombar/ThreeFixedTabsTest.java (66%) rename {app => bottom-bar}/src/androidTest/res/drawable-nodpi/empty_icon.png (100%) rename {app => bottom-bar}/src/androidTest/res/values/colors.xml (100%) rename {app => bottom-bar}/src/androidTest/res/values/strings.xml (100%) create mode 100644 bottom-bar/src/androidTest/res/xml/dummy_tabs_five.xml create mode 100644 bottom-bar/src/androidTest/res/xml/dummy_tabs_three.xml diff --git a/.idea/runConfigurations/All_Tests.xml b/.idea/runConfigurations/All_Tests.xml index b3de3041..ae5d55e2 100644 --- a/.idea/runConfigurations/All_Tests.xml +++ b/.idea/runConfigurations/All_Tests.xml @@ -1,11 +1,11 @@ - - - \ No newline at end of file diff --git a/bottom-bar/build.gradle b/bottom-bar/build.gradle index 1a106d36..1bbfcf09 100644 --- a/bottom-bar/build.gradle +++ b/bottom-bar/build.gradle @@ -33,6 +33,8 @@ android { targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName "1.0" + + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } } @@ -42,6 +44,13 @@ dependencies { compile 'com.android.support:design:' + rootProject.ext.supportLibraryVersion testCompile 'junit:junit:' + rootProject.ext.junitVersion + + androidTestCompile 'junit:junit:4.12' + androidTestCompile 'com.android.support.test:runner:0.5' + androidTestCompile 'com.android.support.test:rules:0.5' + androidTestCompile 'org.mockito:mockito-core:1.+' + androidTestCompile "com.google.dexmaker:dexmaker:1.2" + androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2" } apply plugin: 'com.github.dcendents.android-maven' diff --git a/app/src/androidTest/java/com/roughike/bottombar/BadgeActivityTest.java b/bottom-bar/src/androidTest/java/com/roughike/bottombar/BadgeTest.java similarity index 67% rename from app/src/androidTest/java/com/roughike/bottombar/BadgeActivityTest.java rename to bottom-bar/src/androidTest/java/com/roughike/bottombar/BadgeTest.java index 6ff41242..64315bbb 100644 --- a/app/src/androidTest/java/com/roughike/bottombar/BadgeActivityTest.java +++ b/bottom-bar/src/androidTest/java/com/roughike/bottombar/BadgeTest.java @@ -1,16 +1,12 @@ package com.roughike.bottombar; import android.os.Bundle; +import android.support.test.InstrumentationRegistry; import android.support.test.annotation.UiThreadTest; import android.support.test.filters.LargeTest; -import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; -import com.example.bottombar.sample.BadgeActivity; -import com.example.bottombar.sample.R; - import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -24,24 +20,22 @@ */ @RunWith(AndroidJUnit4.class) @LargeTest -public class BadgeActivityTest { - @Rule - public ActivityTestRule badgeActivityRule = - new ActivityTestRule<>(BadgeActivity.class); - +public class BadgeTest { private BottomBar bottomBar; private BottomBarTab nearby; @Before public void setUp() { - bottomBar = (BottomBar) badgeActivityRule.getActivity().findViewById(R.id.bottomBar); - nearby = bottomBar.getTabWithId(R.id.tab_nearby); + bottomBar = new BottomBar(InstrumentationRegistry.getContext()); + bottomBar.setItems(com.roughike.bottombar.test.R.xml.dummy_tabs_three); + nearby = bottomBar.getTabWithId(com.roughike.bottombar.test.R.id.tab_nearby); + nearby.setBadgeCount(5); } @Test public void hasNoBadges_ExceptNearby() { - assertNull(bottomBar.getTabWithId(R.id.tab_favorites).badge); - assertNull(bottomBar.getTabWithId(R.id.tab_friends).badge); + assertNull(bottomBar.getTabWithId(com.roughike.bottombar.test.R.id.tab_favorites).badge); + assertNull(bottomBar.getTabWithId(com.roughike.bottombar.test.R.id.tab_friends).badge); assertNotNull(nearby.badge); } @@ -49,8 +43,13 @@ public void hasNoBadges_ExceptNearby() { @Test @UiThreadTest public void whenTabWithBadgeClicked_BadgeIsHidden() { - bottomBar.selectTabWithId(R.id.tab_nearby); - assertFalse(nearby.badge.isVisible()); + InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { + @Override + public void run() { + bottomBar.selectTabWithId(com.roughike.bottombar.test.R.id.tab_nearby); + assertFalse(nearby.badge.isVisible()); + } + }); } @Test diff --git a/app/src/androidTest/java/com/roughike/bottombar/TabParserTest.java b/bottom-bar/src/androidTest/java/com/roughike/bottombar/TabParserTest.java similarity index 93% rename from app/src/androidTest/java/com/roughike/bottombar/TabParserTest.java rename to bottom-bar/src/androidTest/java/com/roughike/bottombar/TabParserTest.java index b3bbbdc5..a7a508e7 100644 --- a/app/src/androidTest/java/com/roughike/bottombar/TabParserTest.java +++ b/bottom-bar/src/androidTest/java/com/roughike/bottombar/TabParserTest.java @@ -27,7 +27,7 @@ public class TabParserTest { @Before public void setUp() throws Exception { context = InstrumentationRegistry.getContext(); - tabs = new TabParser(context, new TabParser.Config.Builder().build(), com.example.bottombar.sample.test.R.xml.dummy_tab_xml) + tabs = new TabParser(context, new TabParser.Config.Builder().build(), com.roughike.bottombar.test.R.xml.dummy_tabs_five) .getTabs(); } @@ -68,7 +68,7 @@ public void correctActiveColors() { assertEquals(Color.parseColor("#FF0000"), tabs.get(0).getActiveColor()); assertEquals( - ContextCompat.getColor(context, com.example.bottombar.sample.test.R.color.test_random_color), + ContextCompat.getColor(context, com.roughike.bottombar.test.R.color.test_random_color), tabs.get(1).getActiveColor() ); @@ -88,7 +88,7 @@ public void iconResourcesExist() { @Test public void iconResourceIdsAsExpected() { - int expectedId = com.example.bottombar.sample.test.R.drawable.empty_icon; + int expectedId = com.roughike.bottombar.test.R.drawable.empty_icon; assertEquals(expectedId, tabs.get(0).getIconResId()); assertEquals(expectedId, tabs.get(1).getIconResId()); diff --git a/app/src/androidTest/java/com/roughike/bottombar/ThreeTabsActivityTest.java b/bottom-bar/src/androidTest/java/com/roughike/bottombar/ThreeFixedTabsTest.java similarity index 66% rename from app/src/androidTest/java/com/roughike/bottombar/ThreeTabsActivityTest.java rename to bottom-bar/src/androidTest/java/com/roughike/bottombar/ThreeFixedTabsTest.java index e99a49f1..5d9b1924 100644 --- a/app/src/androidTest/java/com/roughike/bottombar/ThreeTabsActivityTest.java +++ b/bottom-bar/src/androidTest/java/com/roughike/bottombar/ThreeFixedTabsTest.java @@ -1,16 +1,12 @@ package com.roughike.bottombar; import android.os.Bundle; +import android.support.test.InstrumentationRegistry; import android.support.test.annotation.UiThreadTest; import android.support.test.filters.LargeTest; -import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; -import com.example.bottombar.sample.R; -import com.example.bottombar.sample.ThreeTabsActivity; - import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InOrder; @@ -30,13 +26,8 @@ */ @RunWith(AndroidJUnit4.class) @LargeTest -public class ThreeTabsActivityTest { - @Rule - public ActivityTestRule threeTabsActivityRule = - new ActivityTestRule<>(ThreeTabsActivity.class); - +public class ThreeFixedTabsTest { private OnTabSelectListener selectListener; - private OnTabReselectListener reselectListener; private BottomBar bottomBar; @@ -46,7 +37,8 @@ public void setUp() { selectListener = Mockito.mock(OnTabSelectListener.class); reselectListener = Mockito.mock(OnTabReselectListener.class); - bottomBar = (BottomBar) threeTabsActivityRule.getActivity().findViewById(R.id.bottomBar); + bottomBar = new BottomBar(InstrumentationRegistry.getContext()); + bottomBar.setItems(com.roughike.bottombar.test.R.xml.dummy_tabs_three); bottomBar.setOnTabSelectListener(selectListener); bottomBar.setOnTabReselectListener(reselectListener); } @@ -60,50 +52,50 @@ public void tabCount_IsCorrect() { @Test @UiThreadTest public void findingPositionForTabs_ReturnsCorrectPositions() { - assertEquals(0, bottomBar.findPositionForTabWithId(R.id.tab_favorites)); - assertEquals(1, bottomBar.findPositionForTabWithId(R.id.tab_nearby)); - assertEquals(2, bottomBar.findPositionForTabWithId(R.id.tab_friends)); + assertEquals(0, bottomBar.findPositionForTabWithId(com.roughike.bottombar.test.R.id.tab_favorites)); + assertEquals(1, bottomBar.findPositionForTabWithId(com.roughike.bottombar.test.R.id.tab_nearby)); + assertEquals(2, bottomBar.findPositionForTabWithId(com.roughike.bottombar.test.R.id.tab_friends)); } @Test @UiThreadTest public void whenTabIsSelected_SelectionListenerIsFired() { - bottomBar.selectTabWithId(R.id.tab_friends); - bottomBar.selectTabWithId(R.id.tab_nearby); - bottomBar.selectTabWithId(R.id.tab_favorites); + bottomBar.selectTabWithId(com.roughike.bottombar.test.R.id.tab_friends); + bottomBar.selectTabWithId(com.roughike.bottombar.test.R.id.tab_nearby); + bottomBar.selectTabWithId(com.roughike.bottombar.test.R.id.tab_favorites); InOrder inOrder = inOrder(selectListener); - inOrder.verify(selectListener, times(1)).onTabSelected(R.id.tab_friends); - inOrder.verify(selectListener, times(1)).onTabSelected(R.id.tab_nearby); - inOrder.verify(selectListener, times(1)).onTabSelected(R.id.tab_favorites); + inOrder.verify(selectListener, times(1)).onTabSelected(com.roughike.bottombar.test.R.id.tab_friends); + inOrder.verify(selectListener, times(1)).onTabSelected(com.roughike.bottombar.test.R.id.tab_nearby); + inOrder.verify(selectListener, times(1)).onTabSelected(com.roughike.bottombar.test.R.id.tab_favorites); inOrder.verifyNoMoreInteractions(); } @Test @UiThreadTest public void afterConfigurationChanged_SavedStateRestored_AndSelectedTabPersists() { - bottomBar.selectTabWithId(R.id.tab_favorites); + bottomBar.selectTabWithId(com.roughike.bottombar.test.R.id.tab_favorites); Bundle savedState = bottomBar.saveState(); - bottomBar.selectTabWithId(R.id.tab_nearby); + bottomBar.selectTabWithId(com.roughike.bottombar.test.R.id.tab_nearby); bottomBar.restoreState(savedState); - assertEquals(R.id.tab_favorites, bottomBar.getCurrentTabId()); + assertEquals(com.roughike.bottombar.test.R.id.tab_favorites, bottomBar.getCurrentTabId()); } @Test @UiThreadTest public void whenTabIsReselected_ReselectionListenerIsFired() { - int firstTabId = R.id.tab_favorites; + int firstTabId = com.roughike.bottombar.test.R.id.tab_favorites; bottomBar.selectTabWithId(firstTabId); verify(reselectListener, times(1)).onTabReSelected(firstTabId); - int secondTabId = R.id.tab_nearby; + int secondTabId = com.roughike.bottombar.test.R.id.tab_nearby; bottomBar.selectTabWithId(secondTabId); bottomBar.selectTabWithId(secondTabId); verify(reselectListener, times(1)).onTabReSelected(secondTabId); - int thirdTabId = R.id.tab_friends; + int thirdTabId = com.roughike.bottombar.test.R.id.tab_friends; bottomBar.selectTabWithId(thirdTabId); bottomBar.selectTabWithId(thirdTabId); verify(reselectListener, times(1)).onTabReSelected(thirdTabId); @@ -112,7 +104,7 @@ public void whenTabIsReselected_ReselectionListenerIsFired() { @Test @UiThreadTest public void whenDefaultTabIsSet_ItsSelectedAtFirst() { - int defaultTabId = R.id.tab_friends; + int defaultTabId = com.roughike.bottombar.test.R.id.tab_friends; bottomBar.setDefaultTab(defaultTabId); verify(selectListener).onTabSelected(defaultTabId); @@ -121,37 +113,37 @@ public void whenDefaultTabIsSet_ItsSelectedAtFirst() { @Test @UiThreadTest public void afterConfigurationChanged_UserSelectedTabPersistsWhenResettingDefaultTab() { - int defaultTabId = R.id.tab_friends; + int defaultTabId = com.roughike.bottombar.test.R.id.tab_friends; bottomBar.setDefaultTab(defaultTabId); - bottomBar.selectTabWithId(R.id.tab_nearby); + bottomBar.selectTabWithId(com.roughike.bottombar.test.R.id.tab_nearby); Bundle savedState = bottomBar.saveState(); bottomBar.restoreState(savedState); bottomBar.setDefaultTab(defaultTabId); assertNotSame(defaultTabId, bottomBar.getCurrentTabId()); - assertEquals(R.id.tab_nearby, bottomBar.getCurrentTabId()); + assertEquals(com.roughike.bottombar.test.R.id.tab_nearby, bottomBar.getCurrentTabId()); } @Test @UiThreadTest public void whenGettingCurrentTab_ReturnsCorrectOne() { - int firstTabId = R.id.tab_favorites; + int firstTabId = com.roughike.bottombar.test.R.id.tab_favorites; bottomBar.selectTabWithId(firstTabId); assertEquals(firstTabId, bottomBar.getCurrentTabId()); assertEquals(bottomBar.findPositionForTabWithId(firstTabId), bottomBar.getCurrentTabPosition()); assertEquals(bottomBar.getTabWithId(firstTabId), bottomBar.getCurrentTab()); - int secondTabId = R.id.tab_nearby; + int secondTabId = com.roughike.bottombar.test.R.id.tab_nearby; bottomBar.selectTabWithId(secondTabId); assertEquals(secondTabId, bottomBar.getCurrentTabId()); assertEquals(bottomBar.findPositionForTabWithId(secondTabId), bottomBar.getCurrentTabPosition()); assertEquals(bottomBar.getTabWithId(secondTabId), bottomBar.getCurrentTab()); - int thirdTabId = R.id.tab_friends; + int thirdTabId = com.roughike.bottombar.test.R.id.tab_friends; bottomBar.selectTabWithId(thirdTabId); assertEquals(thirdTabId, bottomBar.getCurrentTabId()); @@ -165,9 +157,9 @@ public void whenSelectionChanges_AndHasNoListeners_onlyOneTabIsSelectedAtATime() bottomBar.setOnTabSelectListener(null); bottomBar.setOnTabReselectListener(null); - int firstTabId = R.id.tab_favorites; - int secondTabId = R.id.tab_nearby; - int thirdTabId = R.id.tab_friends; + int firstTabId = com.roughike.bottombar.test.R.id.tab_favorites; + int secondTabId = com.roughike.bottombar.test.R.id.tab_nearby; + int thirdTabId = com.roughike.bottombar.test.R.id.tab_friends; bottomBar.selectTabWithId(secondTabId); assertOnlyHasOnlyOneSelectedTabWithId(secondTabId); @@ -195,9 +187,9 @@ private void assertOnlyHasOnlyOneSelectedTabWithId(int tabId) { @UiThreadTest public void whenTabIsSelectedOnce_AndNoSelectionListenerSet_ReselectionListenerIsNotFired() { bottomBar.setOnTabSelectListener(null); - bottomBar.selectTabWithId(R.id.tab_friends); - bottomBar.selectTabWithId(R.id.tab_nearby); - bottomBar.selectTabWithId(R.id.tab_favorites); + bottomBar.selectTabWithId(com.roughike.bottombar.test.R.id.tab_friends); + bottomBar.selectTabWithId(com.roughike.bottombar.test.R.id.tab_nearby); + bottomBar.selectTabWithId(com.roughike.bottombar.test.R.id.tab_favorites); verifyZeroInteractions(reselectListener); } diff --git a/app/src/androidTest/res/drawable-nodpi/empty_icon.png b/bottom-bar/src/androidTest/res/drawable-nodpi/empty_icon.png similarity index 100% rename from app/src/androidTest/res/drawable-nodpi/empty_icon.png rename to bottom-bar/src/androidTest/res/drawable-nodpi/empty_icon.png diff --git a/app/src/androidTest/res/values/colors.xml b/bottom-bar/src/androidTest/res/values/colors.xml similarity index 100% rename from app/src/androidTest/res/values/colors.xml rename to bottom-bar/src/androidTest/res/values/colors.xml diff --git a/app/src/androidTest/res/values/strings.xml b/bottom-bar/src/androidTest/res/values/strings.xml similarity index 100% rename from app/src/androidTest/res/values/strings.xml rename to bottom-bar/src/androidTest/res/values/strings.xml diff --git a/bottom-bar/src/androidTest/res/xml/dummy_tabs_five.xml b/bottom-bar/src/androidTest/res/xml/dummy_tabs_five.xml new file mode 100644 index 00000000..c16abc04 --- /dev/null +++ b/bottom-bar/src/androidTest/res/xml/dummy_tabs_five.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/bottom-bar/src/androidTest/res/xml/dummy_tabs_three.xml b/bottom-bar/src/androidTest/res/xml/dummy_tabs_three.xml new file mode 100644 index 00000000..29dcedca --- /dev/null +++ b/bottom-bar/src/androidTest/res/xml/dummy_tabs_three.xml @@ -0,0 +1,15 @@ + + + + + + \ No newline at end of file