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;
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<FeedItem> 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<FeedItem> 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<FeedItem> 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();

View File

@ -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

View File

@ -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)