Merge pull request #3166 from ByteHamster/fix-some-tests

Fixed some integration tests
This commit is contained in:
H. Lehmann 2019-05-08 19:30:04 +02:00 committed by GitHub
commit 96f582a4d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 27 deletions

View File

@ -1,6 +1,9 @@
package de.test.antennapod.service.playback; package de.test.antennapod.service.playback;
import android.content.Context; 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 android.test.InstrumentationTestCase;
import java.util.ArrayList; 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.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.playback.Playable; import de.danoeh.antennapod.core.util.playback.Playable;
import de.greenrobot.event.EventBus; 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 * Test class for PlaybackServiceTaskManager
*/ */
public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase { @RunWith(AndroidJUnit4.class)
public class PlaybackServiceTaskManagerTest {
@Override @After
protected void tearDown() throws Exception { public void tearDown() {
super.tearDown();
PodDBAdapter.deleteDatabase(); PodDBAdapter.deleteDatabase();
} }
@Override @Before
protected void setUp() throws Exception { public void setUp() {
super.setUp();
// create new database // create new database
PodDBAdapter.init(getInstrumentation().getTargetContext()); Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
PodDBAdapter.init(context);
PodDBAdapter.deleteDatabase(); PodDBAdapter.deleteDatabase();
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
adapter.close(); adapter.close();
} }
@Test
public void testInit() { public void testInit() {
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(getInstrumentation().getTargetContext(), defaultPSTM); Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(context, defaultPSTM);
pstm.shutdown(); pstm.shutdown();
} }
private List<FeedItem> writeTestQueue(String pref) { private List<FeedItem> writeTestQueue(String pref) {
final Context c = getInstrumentation().getTargetContext();
final int NUM_ITEMS = 10; final int NUM_ITEMS = 10;
Feed f = new Feed(0, null, "title", "link", "d", null, null, null, null, "id", null, "null", "url", false); Feed f = new Feed(0, null, "title", "link", "d", null, null, null, null, "id", null, "null", "url", false);
f.setItems(new ArrayList<>()); f.setItems(new ArrayList<>());
@ -66,8 +78,9 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
return f.getItems(); return f.getItems();
} }
@Test
public void testGetQueueWriteBeforeCreation() throws InterruptedException { public void testGetQueueWriteBeforeCreation() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
List<FeedItem> queue = writeTestQueue("a"); List<FeedItem> queue = writeTestQueue("a");
assertNotNull(queue); assertNotNull(queue);
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
@ -80,8 +93,9 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
pstm.shutdown(); pstm.shutdown();
} }
@Test
public void testGetQueueWriteAfterCreation() throws InterruptedException { public void testGetQueueWriteAfterCreation() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
List<FeedItem> testQueue = pstm.getQueue(); List<FeedItem> testQueue = pstm.getQueue();
@ -111,8 +125,9 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
pstm.shutdown(); pstm.shutdown();
} }
@Test
public void testStartPositionSaver() throws InterruptedException { public void testStartPositionSaver() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
final int NUM_COUNTDOWNS = 2; final int NUM_COUNTDOWNS = 2;
final int TIMEOUT = 3 * PlaybackServiceTaskManager.POSITION_SAVER_WAITING_INTERVAL; final int TIMEOUT = 3 * PlaybackServiceTaskManager.POSITION_SAVER_WAITING_INTERVAL;
final CountDownLatch countDownLatch = new CountDownLatch(NUM_COUNTDOWNS); final CountDownLatch countDownLatch = new CountDownLatch(NUM_COUNTDOWNS);
@ -152,16 +167,18 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
pstm.shutdown(); pstm.shutdown();
} }
@Test
public void testIsPositionSaverActive() { public void testIsPositionSaverActive() {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.startPositionSaver(); pstm.startPositionSaver();
assertTrue(pstm.isPositionSaverActive()); assertTrue(pstm.isPositionSaverActive());
pstm.shutdown(); pstm.shutdown();
} }
@Test
public void testCancelPositionSaver() { public void testCancelPositionSaver() {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.startPositionSaver(); pstm.startPositionSaver();
pstm.cancelPositionSaver(); pstm.cancelPositionSaver();
@ -169,8 +186,9 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
pstm.shutdown(); pstm.shutdown();
} }
@Test
public void testStartWidgetUpdater() throws InterruptedException { public void testStartWidgetUpdater() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
final int NUM_COUNTDOWNS = 2; final int NUM_COUNTDOWNS = 2;
final int TIMEOUT = 3 * PlaybackServiceTaskManager.WIDGET_UPDATER_NOTIFICATION_INTERVAL; final int TIMEOUT = 3 * PlaybackServiceTaskManager.WIDGET_UPDATER_NOTIFICATION_INTERVAL;
final CountDownLatch countDownLatch = new CountDownLatch(NUM_COUNTDOWNS); final CountDownLatch countDownLatch = new CountDownLatch(NUM_COUNTDOWNS);
@ -210,16 +228,18 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
pstm.shutdown(); pstm.shutdown();
} }
@Test
public void testIsWidgetUpdaterActive() { public void testIsWidgetUpdaterActive() {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.startWidgetUpdater(); pstm.startWidgetUpdater();
assertTrue(pstm.isWidgetUpdaterActive()); assertTrue(pstm.isWidgetUpdaterActive());
pstm.shutdown(); pstm.shutdown();
} }
@Test
public void testCancelWidgetUpdater() { public void testCancelWidgetUpdater() {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.startWidgetUpdater(); pstm.startWidgetUpdater();
pstm.cancelWidgetUpdater(); pstm.cancelWidgetUpdater();
@ -227,8 +247,9 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
pstm.shutdown(); pstm.shutdown();
} }
@Test
public void testCancelAllTasksNoTasksStarted() { public void testCancelAllTasksNoTasksStarted() {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.cancelAllTasks(); pstm.cancelAllTasks();
assertFalse(pstm.isPositionSaverActive()); assertFalse(pstm.isPositionSaverActive());
@ -237,8 +258,10 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
pstm.shutdown(); pstm.shutdown();
} }
@Test
@UiThreadTest
public void testCancelAllTasksAllTasksStarted() { public void testCancelAllTasksAllTasksStarted() {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.startWidgetUpdater(); pstm.startWidgetUpdater();
pstm.startPositionSaver(); pstm.startPositionSaver();
@ -250,8 +273,10 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
pstm.shutdown(); pstm.shutdown();
} }
@Test
@UiThreadTest
public void testSetSleepTimer() throws InterruptedException { public void testSetSleepTimer() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
final long TIME = 2000; final long TIME = 2000;
final long TIMEOUT = 2 * TIME; final long TIMEOUT = 2 * TIME;
final CountDownLatch countDownLatch = new CountDownLatch(1); final CountDownLatch countDownLatch = new CountDownLatch(1);
@ -294,8 +319,10 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
pstm.shutdown(); pstm.shutdown();
} }
@Test
@UiThreadTest
public void testDisableSleepTimer() throws InterruptedException { public void testDisableSleepTimer() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
final long TIME = 1000; final long TIME = 1000;
final long TIMEOUT = 2 * TIME; final long TIMEOUT = 2 * TIME;
final CountDownLatch countDownLatch = new CountDownLatch(1); final CountDownLatch countDownLatch = new CountDownLatch(1);
@ -336,16 +363,20 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
pstm.shutdown(); pstm.shutdown();
} }
@Test
@UiThreadTest
public void testIsSleepTimerActivePositive() { public void testIsSleepTimerActivePositive() {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.setSleepTimer(10000, false, false); pstm.setSleepTimer(10000, false, false);
assertTrue(pstm.isSleepTimerActive()); assertTrue(pstm.isSleepTimerActive());
pstm.shutdown(); pstm.shutdown();
} }
@Test
@UiThreadTest
public void testIsSleepTimerActiveNegative() { public void testIsSleepTimerActiveNegative() {
final Context c = getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.setSleepTimer(10000, false, false); pstm.setSleepTimer(10000, false, false);
pstm.disableSleepTimer(); pstm.disableSleepTimer();

View File

@ -7,6 +7,7 @@ import android.support.test.espresso.intent.Intents;
import android.support.test.filters.FlakyTest; import android.support.test.filters.FlakyTest;
import android.support.test.rule.ActivityTestRule; import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import android.view.Gravity;
import android.widget.ListView; import android.widget.ListView;
import com.robotium.solo.Solo; import com.robotium.solo.Solo;
import com.robotium.solo.Timeout; 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.feed.Feed;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.PodDBAdapter; import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.dialog.RatingDialog;
import de.danoeh.antennapod.fragment.DownloadsFragment; import de.danoeh.antennapod.fragment.DownloadsFragment;
import de.danoeh.antennapod.fragment.EpisodesFragment; import de.danoeh.antennapod.fragment.EpisodesFragment;
import de.danoeh.antennapod.fragment.PlaybackHistoryFragment; 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.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.longClick; 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.Intents.intended;
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent; import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static android.support.test.espresso.matcher.ViewMatchers.withId; 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 = context.getSharedPreferences(MainActivity.PREF_NAME, Context.MODE_PRIVATE);
prefs.edit().putBoolean(MainActivity.PREF_IS_FIRST_LAUNCH, false).commit(); prefs.edit().putBoolean(MainActivity.PREF_IS_FIRST_LAUNCH, false).commit();
RatingDialog.init(context);
RatingDialog.saveRated();
solo = new Solo(getInstrumentation(), mActivityRule.getActivity()); solo = new Solo(getInstrumentation(), mActivityRule.getActivity());
} }
@ -89,7 +96,9 @@ public class MainActivityTest {
} }
private void openNavDrawer() { 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 @Test

View File

@ -6,6 +6,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.util.Log; import android.util.Log;
import com.afollestad.materialdialogs.MaterialDialog; import com.afollestad.materialdialogs.MaterialDialog;
@ -73,7 +74,8 @@ public class RatingDialog {
return mPreferences.getBoolean(KEY_RATED, false); return mPreferences.getBoolean(KEY_RATED, false);
} }
private static void saveRated() { @VisibleForTesting
public static void saveRated() {
mPreferences mPreferences
.edit() .edit()
.putBoolean(KEY_RATED, true) .putBoolean(KEY_RATED, true)