From ae3cef3bd20dca4d079fe44de4b824c52b96db25 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Mon, 6 May 2019 23:52:02 +0200 Subject: [PATCH] Fixed some integration tests --- .../PlaybackServiceTaskManagerTest.java | 81 +++++++++++++------ .../test/antennapod/ui/MainActivityTest.java | 11 ++- .../antennapod/dialog/RatingDialog.java | 4 +- 3 files changed, 69 insertions(+), 27 deletions(-) diff --git a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java index 8be57a074..08e7f6fc5 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java @@ -1,6 +1,9 @@ package de.test.antennapod.service.playback; import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.annotation.UiThreadTest; +import android.support.test.runner.AndroidJUnit4; import android.test.InstrumentationTestCase; import java.util.ArrayList; @@ -17,37 +20,46 @@ import de.danoeh.antennapod.core.service.playback.PlaybackServiceTaskManager; import de.danoeh.antennapod.core.storage.PodDBAdapter; import de.danoeh.antennapod.core.util.playback.Playable; import de.greenrobot.event.EventBus; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static junit.framework.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * Test class for PlaybackServiceTaskManager */ -public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { +@RunWith(AndroidJUnit4.class) +public class PlaybackServiceTaskManagerTest { - @Override - protected void tearDown() throws Exception { - super.tearDown(); + @After + public void tearDown() { PodDBAdapter.deleteDatabase(); } - @Override - protected void setUp() throws Exception { - super.setUp(); - + @Before + public void setUp() { // create new database - PodDBAdapter.init(getInstrumentation().getTargetContext()); + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + PodDBAdapter.init(context); PodDBAdapter.deleteDatabase(); PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); adapter.close(); } + @Test public void testInit() { - PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(getInstrumentation().getTargetContext(), defaultPSTM); + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(context, defaultPSTM); pstm.shutdown(); } private List writeTestQueue(String pref) { - final Context c = getInstrumentation().getTargetContext(); final int NUM_ITEMS = 10; Feed f = new Feed(0, null, "title", "link", "d", null, null, null, null, "id", null, "null", "url", false); f.setItems(new ArrayList<>()); @@ -66,8 +78,9 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { return f.getItems(); } + @Test public void testGetQueueWriteBeforeCreation() throws InterruptedException { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); List queue = writeTestQueue("a"); assertNotNull(queue); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); @@ -80,8 +93,9 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { pstm.shutdown(); } + @Test public void testGetQueueWriteAfterCreation() throws InterruptedException { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); List testQueue = pstm.getQueue(); @@ -111,8 +125,9 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { pstm.shutdown(); } + @Test public void testStartPositionSaver() throws InterruptedException { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); final int NUM_COUNTDOWNS = 2; final int TIMEOUT = 3 * PlaybackServiceTaskManager.POSITION_SAVER_WAITING_INTERVAL; final CountDownLatch countDownLatch = new CountDownLatch(NUM_COUNTDOWNS); @@ -152,16 +167,18 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { pstm.shutdown(); } + @Test public void testIsPositionSaverActive() { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); pstm.startPositionSaver(); assertTrue(pstm.isPositionSaverActive()); pstm.shutdown(); } + @Test public void testCancelPositionSaver() { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); pstm.startPositionSaver(); pstm.cancelPositionSaver(); @@ -169,8 +186,9 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { pstm.shutdown(); } + @Test public void testStartWidgetUpdater() throws InterruptedException { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); final int NUM_COUNTDOWNS = 2; final int TIMEOUT = 3 * PlaybackServiceTaskManager.WIDGET_UPDATER_NOTIFICATION_INTERVAL; final CountDownLatch countDownLatch = new CountDownLatch(NUM_COUNTDOWNS); @@ -210,16 +228,18 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { pstm.shutdown(); } + @Test public void testIsWidgetUpdaterActive() { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); pstm.startWidgetUpdater(); assertTrue(pstm.isWidgetUpdaterActive()); pstm.shutdown(); } + @Test public void testCancelWidgetUpdater() { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); pstm.startWidgetUpdater(); pstm.cancelWidgetUpdater(); @@ -227,8 +247,9 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { pstm.shutdown(); } + @Test public void testCancelAllTasksNoTasksStarted() { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); pstm.cancelAllTasks(); assertFalse(pstm.isPositionSaverActive()); @@ -237,8 +258,10 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { pstm.shutdown(); } + @Test + @UiThreadTest public void testCancelAllTasksAllTasksStarted() { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); pstm.startWidgetUpdater(); pstm.startPositionSaver(); @@ -250,8 +273,10 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { pstm.shutdown(); } + @Test + @UiThreadTest public void testSetSleepTimer() throws InterruptedException { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); final long TIME = 2000; final long TIMEOUT = 2 * TIME; final CountDownLatch countDownLatch = new CountDownLatch(1); @@ -294,8 +319,10 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { pstm.shutdown(); } + @Test + @UiThreadTest public void testDisableSleepTimer() throws InterruptedException { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); final long TIME = 1000; final long TIMEOUT = 2 * TIME; final CountDownLatch countDownLatch = new CountDownLatch(1); @@ -336,16 +363,20 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { pstm.shutdown(); } + @Test + @UiThreadTest public void testIsSleepTimerActivePositive() { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); pstm.setSleepTimer(10000, false, false); assertTrue(pstm.isSleepTimerActive()); pstm.shutdown(); } + @Test + @UiThreadTest public void testIsSleepTimerActiveNegative() { - final Context c = getInstrumentation().getTargetContext(); + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); pstm.setSleepTimer(10000, false, false); pstm.disableSleepTimer(); diff --git a/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java b/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java index 9a60b04b8..8e0064079 100644 --- a/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java +++ b/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java @@ -7,6 +7,7 @@ import android.support.test.espresso.intent.Intents; import android.support.test.filters.FlakyTest; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; +import android.view.Gravity; import android.widget.ListView; import com.robotium.solo.Solo; import com.robotium.solo.Timeout; @@ -17,6 +18,7 @@ import de.danoeh.antennapod.activity.PreferenceActivity; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.storage.PodDBAdapter; +import de.danoeh.antennapod.dialog.RatingDialog; import de.danoeh.antennapod.fragment.DownloadsFragment; import de.danoeh.antennapod.fragment.EpisodesFragment; import de.danoeh.antennapod.fragment.PlaybackHistoryFragment; @@ -36,6 +38,8 @@ import static android.support.test.InstrumentationRegistry.getInstrumentation; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.longClick; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.contrib.DrawerMatchers.isClosed; import static android.support.test.espresso.intent.Intents.intended; import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent; import static android.support.test.espresso.matcher.ViewMatchers.withId; @@ -76,6 +80,9 @@ public class MainActivityTest { prefs = context.getSharedPreferences(MainActivity.PREF_NAME, Context.MODE_PRIVATE); prefs.edit().putBoolean(MainActivity.PREF_IS_FIRST_LAUNCH, false).commit(); + RatingDialog.init(context); + RatingDialog.saveRated(); + solo = new Solo(getInstrumentation(), mActivityRule.getActivity()); } @@ -89,7 +96,9 @@ public class MainActivityTest { } private void openNavDrawer() { - onView(withId(R.id.drawer_layout)).perform(DrawerActions.open()); + onView(withId(R.id.drawer_layout)) + .check(matches(isClosed(Gravity.LEFT))) + .perform(DrawerActions.open()); } @Test diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/RatingDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/RatingDialog.java index ece184035..24656ed29 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/RatingDialog.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/RatingDialog.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; import android.util.Log; import com.afollestad.materialdialogs.MaterialDialog; @@ -73,7 +74,8 @@ public class RatingDialog { return mPreferences.getBoolean(KEY_RATED, false); } - private static void saveRated() { + @VisibleForTesting + public static void saveRated() { mPreferences .edit() .putBoolean(KEY_RATED, true)