mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2025-01-23 07:14:24 +01:00
ap algorithm cleanup tests
This commit is contained in:
parent
0beeeb4351
commit
e61c7efde4
@ -0,0 +1,177 @@
|
||||
package de.test.antennapod.storage;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.test.FlakyTest;
|
||||
import android.test.InstrumentationTestCase;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import de.danoeh.antennapod.core.feed.Feed;
|
||||
import de.danoeh.antennapod.core.feed.FeedItem;
|
||||
import de.danoeh.antennapod.core.feed.FeedMedia;
|
||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||
import de.danoeh.antennapod.core.storage.DBTasks;
|
||||
import de.danoeh.antennapod.core.storage.PodDBAdapter;
|
||||
|
||||
import static de.test.antennapod.storage.DBTestUtils.saveFeedlist;
|
||||
|
||||
/**
|
||||
* Tests that the APQueueCleanupAlgorithm is working correctly.
|
||||
*/
|
||||
public class DBQueueCleanupAlgorithmTest extends InstrumentationTestCase {
|
||||
|
||||
private static final String TAG = "DBQueueCleanupAlgorithmTest";
|
||||
private static final int EPISODE_CACHE_SIZE = 5;
|
||||
|
||||
private Context context;
|
||||
|
||||
private File destFolder;
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
|
||||
assertTrue(PodDBAdapter.deleteDatabase());
|
||||
|
||||
cleanupDestFolder(destFolder);
|
||||
assertTrue(destFolder.delete());
|
||||
}
|
||||
|
||||
private void cleanupDestFolder(File destFolder) {
|
||||
for (File f : destFolder.listFiles()) {
|
||||
assertTrue(f.delete());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
context = getInstrumentation().getTargetContext();
|
||||
destFolder = context.getExternalCacheDir();
|
||||
cleanupDestFolder(destFolder);
|
||||
assertNotNull(destFolder);
|
||||
assertTrue(destFolder.exists());
|
||||
assertTrue(destFolder.canWrite());
|
||||
|
||||
// create new database
|
||||
PodDBAdapter.deleteDatabase();
|
||||
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
||||
adapter.open();
|
||||
adapter.close();
|
||||
|
||||
SharedPreferences.Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).edit();
|
||||
prefEdit.putString(UserPreferences.PREF_EPISODE_CACHE_SIZE, Integer.toString(EPISODE_CACHE_SIZE));
|
||||
prefEdit.putInt(UserPreferences.PREF_EPISODE_CLEANUP, UserPreferences.EPISODE_CLEANUP_QUEUE);
|
||||
prefEdit.commit();
|
||||
|
||||
UserPreferences.init(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* A test with no items in the queue, but multiple items downloaded. A certain
|
||||
* number of items should be removed to make room in in the cache/queue.
|
||||
* @throws IOException
|
||||
*/
|
||||
@FlakyTest(tolerance = 3)
|
||||
public void testPerformAutoCleanupShouldDelete() throws IOException {
|
||||
final int NUM_ITEMS = EPISODE_CACHE_SIZE * 2;
|
||||
|
||||
Feed feed = new Feed("url", new Date(), "title");
|
||||
List<FeedItem> items = new ArrayList<>();
|
||||
feed.setItems(items);
|
||||
List<File> files = new ArrayList<>();
|
||||
for (int i = 0; i < NUM_ITEMS; i++) {
|
||||
FeedItem item = new FeedItem(0, "title", "id", "link", new Date(NUM_ITEMS - i), FeedItem.UNPLAYED, feed);
|
||||
|
||||
File f = new File(destFolder, "file " + i);
|
||||
assertTrue(f.createNewFile());
|
||||
files.add(f);
|
||||
item.setMedia(new FeedMedia(0, item, 1, 0, 1L, "m", f.getAbsolutePath(), "url", true, null, 0));
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
||||
adapter.open();
|
||||
adapter.setCompleteFeed(feed);
|
||||
adapter.close();
|
||||
|
||||
assertTrue(feed.getId() != 0);
|
||||
for (FeedItem item : items) {
|
||||
assertTrue(item.getId() != 0);
|
||||
assertTrue(item.getMedia().getId() != 0);
|
||||
}
|
||||
DBTasks.performAutoCleanup(context);
|
||||
for (int i = 0; i < files.size(); i++) {
|
||||
if (i < EPISODE_CACHE_SIZE) {
|
||||
assertTrue(files.get(i).exists());
|
||||
} else {
|
||||
assertFalse(files.get(i).exists());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@FlakyTest(tolerance = 3)
|
||||
public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue() throws IOException {
|
||||
final int NUM_ITEMS = EPISODE_CACHE_SIZE * 2;
|
||||
|
||||
Feed feed = new Feed("url", new Date(), "title");
|
||||
List<FeedItem> items = new ArrayList<>();
|
||||
feed.setItems(items);
|
||||
List<File> files = new ArrayList<>();
|
||||
for (int i = 0; i < NUM_ITEMS; i++) {
|
||||
FeedItem item = new FeedItem(0, "title", "id", "link", new Date(NUM_ITEMS - i), FeedItem.PLAYED, feed);
|
||||
|
||||
File f = new File(destFolder, "file " + i);
|
||||
assertTrue(f.createNewFile());
|
||||
assertTrue(f.exists());
|
||||
files.add(f);
|
||||
item.setMedia(new FeedMedia(0, item, 1, 0, 1L, "m", f.getAbsolutePath(), "url", true, new Date(NUM_ITEMS - i), 0));
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
||||
adapter.open();
|
||||
adapter.setCompleteFeed(feed);
|
||||
adapter.setQueue(items);
|
||||
adapter.close();
|
||||
|
||||
assertTrue(feed.getId() != 0);
|
||||
for (FeedItem item : items) {
|
||||
assertTrue(item.getId() != 0);
|
||||
assertTrue(item.getMedia().getId() != 0);
|
||||
}
|
||||
DBTasks.performAutoCleanup(context);
|
||||
for (File file : files) {
|
||||
assertTrue(file.exists());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reproduces a bug where DBTasks.performAutoCleanup(android.content.Context) would use the ID of the FeedItem in the
|
||||
* call to DBWriter.deleteFeedMediaOfItem instead of the ID of the FeedMedia. This would cause the wrong item to be deleted.
|
||||
* @throws IOException
|
||||
*/
|
||||
@FlakyTest(tolerance = 3)
|
||||
public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue_withFeedsWithNoMedia() throws IOException {
|
||||
// add feed with no enclosures so that item ID != media ID
|
||||
saveFeedlist(1, 10, false);
|
||||
|
||||
// add candidate for performAutoCleanup
|
||||
List<Feed> feeds = saveFeedlist(1, 1, true);
|
||||
FeedMedia m = feeds.get(0).getItems().get(0).getMedia();
|
||||
m.setDownloaded(true);
|
||||
m.setFile_url("file");
|
||||
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
||||
adapter.open();
|
||||
adapter.setMedia(m);
|
||||
adapter.close();
|
||||
|
||||
testPerformAutoCleanupShouldNotDeleteBecauseInQueue();
|
||||
}
|
||||
}
|
@ -110,7 +110,7 @@ public class DBTasksTest extends InstrumentationTestCase {
|
||||
}
|
||||
|
||||
@FlakyTest(tolerance = 3)
|
||||
public void testPerformAutoCleanupShouldNotDeleteBecauseUnread() throws IOException {
|
||||
public void testPerformAutoCleanupShouldNotDeleteBecauseUnplayed() throws IOException {
|
||||
final int NUM_ITEMS = EPISODE_CACHE_SIZE * 2;
|
||||
|
||||
Feed feed = new Feed("url", new Date(), "title");
|
||||
|
Loading…
Reference in New Issue
Block a user