Added tests for PlaybackServiceTaskManager

This commit is contained in:
daniel oeh 2013-12-20 22:46:05 +01:00
parent 5448983288
commit 5645509853
2 changed files with 343 additions and 7 deletions

View File

@ -36,7 +36,7 @@ public class PlaybackServiceTaskManager {
private ScheduledFuture positionSaverFuture; private ScheduledFuture positionSaverFuture;
private ScheduledFuture widgetUpdaterFuture; private ScheduledFuture widgetUpdaterFuture;
private Future sleepTimerFuture; private ScheduledFuture sleepTimerFuture;
private volatile Future<List<FeedItem>> queueFuture; private volatile Future<List<FeedItem>> queueFuture;
private volatile Future chapterLoaderFuture; private volatile Future chapterLoaderFuture;
@ -75,6 +75,7 @@ public class PlaybackServiceTaskManager {
@Override @Override
public void update(EventDistributor eventDistributor, Integer arg) { public void update(EventDistributor eventDistributor, Integer arg) {
if ((EventDistributor.QUEUE_UPDATE & arg) != 0) { if ((EventDistributor.QUEUE_UPDATE & arg) != 0) {
cancelQueueLoader();
loadQueue(); loadQueue();
} }
} }
@ -102,7 +103,7 @@ public class PlaybackServiceTaskManager {
} }
/** /**
* Returns the queue or waits until the PSTM hasloaded the queue from the database. * Returns the queue or waits until the PSTM has loaded the queue from the database.
*/ */
public synchronized List<FeedItem> getQueue() throws InterruptedException { public synchronized List<FeedItem> getQueue() throws InterruptedException {
try { try {
@ -187,14 +188,14 @@ public class PlaybackServiceTaskManager {
sleepTimerFuture.cancel(true); sleepTimerFuture.cancel(true);
} }
sleepTimer = new SleepTimer(waitingTime); sleepTimer = new SleepTimer(waitingTime);
sleepTimerFuture = schedExecutor.submit(sleepTimer); sleepTimerFuture = schedExecutor.schedule(sleepTimer, 0, TimeUnit.MILLISECONDS);
} }
/** /**
* Returns true if the sleep timer is currently active. * Returns true if the sleep timer is currently active.
*/ */
public synchronized boolean isSleepTimerActive() { public synchronized boolean isSleepTimerActive() {
return sleepTimer != null && sleepTimer.isWaiting() && sleepTimerFuture != null; return sleepTimer != null && sleepTimerFuture != null && !sleepTimerFuture.isCancelled() && !sleepTimerFuture.isDone();
} }
/** /**
@ -307,16 +308,16 @@ public class PlaybackServiceTaskManager {
private static final String TAG = "SleepTimer"; private static final String TAG = "SleepTimer";
private static final long UPDATE_INTERVALL = 1000L; private static final long UPDATE_INTERVALL = 1000L;
private volatile long waitingTime; private volatile long waitingTime;
private boolean isWaiting; private volatile boolean isWaiting;
public SleepTimer(long waitingTime) { public SleepTimer(long waitingTime) {
super(); super();
this.waitingTime = waitingTime; this.waitingTime = waitingTime;
isWaiting = true;
} }
@Override @Override
public void run() { public void run() {
isWaiting = true;
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Starting"); Log.d(TAG, "Starting");
while (waitingTime > 0) { while (waitingTime > 0) {
@ -327,7 +328,9 @@ public class PlaybackServiceTaskManager {
if (waitingTime <= 0) { if (waitingTime <= 0) {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Waiting completed"); Log.d(TAG, "Waiting completed");
callback.onSleepTimerExpired(); if (!Thread.currentThread().isInterrupted()) {
callback.onSleepTimerExpired();
}
postExecute(); postExecute();
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@ -0,0 +1,333 @@
package instrumentationTest.de.test.antennapod.service.playback;
import android.content.Context;
import android.test.InstrumentationTestCase;
import de.danoeh.antennapod.feed.EventDistributor;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.service.playback.PlaybackServiceTaskManager;
import de.danoeh.antennapod.storage.PodDBAdapter;
import de.danoeh.antennapod.util.playback.Playable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Test class for PlaybackServiceTaskManager
*/
public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
@Override
protected void tearDown() throws Exception {
super.tearDown();
assertTrue(PodDBAdapter.deleteDatabase(getInstrumentation().getTargetContext()));
}
@Override
protected void setUp() throws Exception {
super.setUp();
final Context context = getInstrumentation().getTargetContext();
context.deleteDatabase(PodDBAdapter.DATABASE_NAME);
// make sure database is created
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
adapter.close();
}
public void testInit() {
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(getInstrumentation().getTargetContext(), defaultPSTM);
pstm.shutdown();
}
private List<FeedItem> writeTestQueue(String pref) {
final Context c = getInstrumentation().getTargetContext();
final int NUM_ITEMS = 10;
Feed f = new Feed(0, new Date(), "title", "link", "d", null, null, null, null, "id", null, "null", "url", false);
f.setItems(new ArrayList<FeedItem>());
for (int i = 0; i < NUM_ITEMS; i++) {
f.getItems().add(new FeedItem(0, pref + i, pref + i, "link", new Date(), true, f));
}
PodDBAdapter adapter = new PodDBAdapter(c);
adapter.open();
adapter.setCompleteFeed(f);
adapter.setQueue(f.getItems());
adapter.close();
for (FeedItem item : f.getItems()) {
assertTrue(item.getId() != 0);
}
return f.getItems();
}
public void testGetQueueWriteBeforeCreation() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext();
List<FeedItem> queue = writeTestQueue("a");
assertNotNull(queue);
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
List<FeedItem> testQueue = pstm.getQueue();
assertNotNull(testQueue);
assertTrue(queue.size() == testQueue.size());
for (int i = 0; i < queue.size(); i++) {
assertTrue(queue.get(i).getId() == testQueue.get(i).getId());
}
pstm.shutdown();
}
public void testGetQueueWriteAfterCreation() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
List<FeedItem> testQueue = pstm.getQueue();
assertNotNull(testQueue);
assertTrue(testQueue.isEmpty());
final CountDownLatch countDownLatch = new CountDownLatch(1);
EventDistributor.EventListener queueListener = new EventDistributor.EventListener() {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
countDownLatch.countDown();
}
};
EventDistributor.getInstance().register(queueListener);
List<FeedItem> queue = writeTestQueue("a");
EventDistributor.getInstance().sendQueueUpdateBroadcast();
countDownLatch.await(5000, TimeUnit.MILLISECONDS);
assertNotNull(queue);
testQueue = pstm.getQueue();
assertNotNull(testQueue);
assertTrue(queue.size() == testQueue.size());
for (int i = 0; i < queue.size(); i++) {
assertTrue(queue.get(i).getId() == testQueue.get(i).getId());
}
pstm.shutdown();
}
public void testStartPositionSaver() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext();
final int NUM_COUNTDOWNS = 2;
final int TIMEOUT = 3 * PlaybackServiceTaskManager.POSITION_SAVER_WAITING_INTERVAL;
final CountDownLatch countDownLatch = new CountDownLatch(NUM_COUNTDOWNS);
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, new PlaybackServiceTaskManager.PSTMCallback() {
@Override
public void positionSaverTick() {
countDownLatch.countDown();
}
@Override
public void onSleepTimerExpired() {
}
@Override
public void onWidgetUpdaterTick() {
}
@Override
public void onChapterLoaded(Playable media) {
}
});
pstm.startPositionSaver();
countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
pstm.shutdown();
}
public void testIsPositionSaverActive() {
final Context c = getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.startPositionSaver();
assertTrue(pstm.isPositionSaverActive());
pstm.shutdown();
}
public void testCancelPositionSaver() {
final Context c = getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.startPositionSaver();
pstm.cancelPositionSaver();
assertFalse(pstm.isPositionSaverActive());
pstm.shutdown();
}
public void testStartWidgetUpdater() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext();
final int NUM_COUNTDOWNS = 2;
final int TIMEOUT = 3 * PlaybackServiceTaskManager.WIDGET_UPDATER_NOTIFICATION_INTERVAL;
final CountDownLatch countDownLatch = new CountDownLatch(NUM_COUNTDOWNS);
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, new PlaybackServiceTaskManager.PSTMCallback() {
@Override
public void positionSaverTick() {
}
@Override
public void onSleepTimerExpired() {
}
@Override
public void onWidgetUpdaterTick() {
countDownLatch.countDown();
}
@Override
public void onChapterLoaded(Playable media) {
}
});
pstm.startWidgetUpdater();
countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
pstm.shutdown();
}
public void testIsWidgetUpdaterActive() {
final Context c = getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.startWidgetUpdater();
assertTrue(pstm.isWidgetUpdaterActive());
pstm.shutdown();
}
public void testCancelWidgetUpdater() {
final Context c = getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.startWidgetUpdater();
pstm.cancelWidgetUpdater();
assertFalse(pstm.isWidgetUpdaterActive());
pstm.shutdown();
}
public void testCancelAllTasksNoTasksStarted() {
final Context c = getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.cancelAllTasks();
assertFalse(pstm.isPositionSaverActive());
assertFalse(pstm.isWidgetUpdaterActive());
assertFalse(pstm.isSleepTimerActive());
pstm.shutdown();
}
public void testCancelAllTasksAllTasksStarted() {
final Context c = getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.startWidgetUpdater();
pstm.startPositionSaver();
pstm.setSleepTimer(100000);
pstm.cancelAllTasks();
assertFalse(pstm.isPositionSaverActive());
assertFalse(pstm.isWidgetUpdaterActive());
assertFalse(pstm.isSleepTimerActive());
pstm.shutdown();
}
public void testSetSleepTimer() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext();
final long TIME = 2000;
final long TIMEOUT = 2 * TIME;
final CountDownLatch countDownLatch = new CountDownLatch(1);
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, new PlaybackServiceTaskManager.PSTMCallback() {
@Override
public void positionSaverTick() {
}
@Override
public void onSleepTimerExpired() {
if (countDownLatch.getCount() == 0) {
fail();
}
countDownLatch.countDown();
}
@Override
public void onWidgetUpdaterTick() {
}
@Override
public void onChapterLoaded(Playable media) {
}
});
pstm.setSleepTimer(TIME);
countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
pstm.shutdown();
}
public void testDisableSleepTimer() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext();
final long TIME = 1000;
final long TIMEOUT = 2 * TIME;
final CountDownLatch countDownLatch = new CountDownLatch(1);
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, new PlaybackServiceTaskManager.PSTMCallback() {
@Override
public void positionSaverTick() {
}
@Override
public void onSleepTimerExpired() {
fail("Sleeptimer expired");
}
@Override
public void onWidgetUpdaterTick() {
}
@Override
public void onChapterLoaded(Playable media) {
}
});
pstm.setSleepTimer(TIME);
pstm.disableSleepTimer();
assertFalse(countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS));
pstm.shutdown();
}
public void testIsSleepTimerActivePositive() {
final Context c = getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.setSleepTimer(10000);
assertTrue(pstm.isSleepTimerActive());
pstm.shutdown();
}
public void testIsSleepTimerActiveNegative() {
final Context c = getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.setSleepTimer(10000);
pstm.disableSleepTimer();
assertFalse(pstm.isSleepTimerActive());
pstm.shutdown();
}
private final PlaybackServiceTaskManager.PSTMCallback defaultPSTM = new PlaybackServiceTaskManager.PSTMCallback() {
@Override
public void positionSaverTick() {
}
@Override
public void onSleepTimerExpired() {
}
@Override
public void onWidgetUpdaterTick() {
}
@Override
public void onChapterLoaded(Playable media) {
}
};
}