diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2013-08-21 15:23:47 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2013-08-21 15:23:47 +0200 |
commit | 4291a96f2f8d83aa8571d404f9a8143695785780 (patch) | |
tree | 320a671e4702b0bc11f11858e6faa76299b16f7d /src/instrumentationTest/de/test/antennapod | |
parent | fd5f1fcc0b52f5aefcfcc979a1d7d07e26f9eb76 (diff) | |
download | AntennaPod-4291a96f2f8d83aa8571d404f9a8143695785780.zip |
Fixed problems with auto-cleanup
Diffstat (limited to 'src/instrumentationTest/de/test/antennapod')
3 files changed, 173 insertions, 6 deletions
diff --git a/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java b/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java index f91592869..8df35ce67 100644 --- a/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java +++ b/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java @@ -2,18 +2,21 @@ package instrumentationTest.de.test.antennapod.service.download; import java.io.File; +import android.test.InstrumentationTestCase; import de.danoeh.antennapod.feed.FeedFile; import de.danoeh.antennapod.service.download.*; import android.test.AndroidTestCase; import android.util.Log; -public class HttpDownloaderTest extends AndroidTestCase { +public class HttpDownloaderTest extends InstrumentationTestCase { private static final String TAG = "HttpDownloaderTest"; private static final String DOWNLOAD_DIR = "testdownloads"; private static boolean successful = true; + private File destDir; + public HttpDownloaderTest() { super(); } @@ -21,17 +24,23 @@ public class HttpDownloaderTest extends AndroidTestCase { @Override protected void tearDown() throws Exception { super.tearDown(); - File externalDir = getContext().getExternalFilesDir(DOWNLOAD_DIR); - assertNotNull(externalDir); - File[] contents = externalDir.listFiles(); + File[] contents = destDir.listFiles(); for (File f : contents) { assertTrue(f.delete()); } } + @Override + protected void setUp() throws Exception { + super.setUp(); + destDir = getInstrumentation().getTargetContext().getExternalFilesDir(DOWNLOAD_DIR); + assertNotNull(destDir); + assertTrue(destDir.exists()); + } + private FeedFileImpl setupFeedFile(String downloadUrl, String title) { FeedFileImpl feedfile = new FeedFileImpl(downloadUrl); - String fileUrl = new File(getContext().getExternalFilesDir(DOWNLOAD_DIR).getAbsolutePath(), title).getAbsolutePath(); + String fileUrl = new File(destDir, title).getAbsolutePath(); File file = new File(fileUrl); Log.d(TAG, "Deleting file: " + file.delete()); feedfile.setFile_url(fileUrl); diff --git a/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java b/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java index ef48b8d5f..6dd9ef92b 100644 --- a/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java +++ b/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java @@ -1,9 +1,167 @@ package instrumentationTest.de.test.antennapod.storage; +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.test.InstrumentationTestCase; +import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.feed.FeedItem; +import de.danoeh.antennapod.feed.FeedMedia; +import de.danoeh.antennapod.preferences.UserPreferences; +import de.danoeh.antennapod.storage.DBTasks; +import de.danoeh.antennapod.storage.PodDBAdapter; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; /** * Test class for DBTasks */ public class DBTasksTest extends InstrumentationTestCase { + private static final String TEST_FOLDER = "testDBTasks"; + private static final int EPISODE_CACHE_SIZE = 5; + + private File destFolder; + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + final Context context = getInstrumentation().getTargetContext(); + assertTrue(PodDBAdapter.deleteDatabase(context)); + + for (File f : destFolder.listFiles()) { + assertTrue(f.delete()); + } + assertTrue(destFolder.delete()); + + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + destFolder = getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER); + assertNotNull(destFolder); + assertTrue(destFolder.exists()); + assertTrue(destFolder.canWrite()); + + final Context context = getInstrumentation().getTargetContext(); + context.deleteDatabase(PodDBAdapter.DATABASE_NAME); + // make sure database is created + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + adapter.close(); + + SharedPreferences.Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(getInstrumentation().getTargetContext().getApplicationContext()).edit(); + prefEdit.putString(UserPreferences.PREF_EPISODE_CACHE_SIZE, Integer.toString(EPISODE_CACHE_SIZE)); + prefEdit.commit(); + } + + 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<FeedItem>(); + feed.setItems(items); + List<File> files = new ArrayList<File>(); + for (int i = 0; i < NUM_ITEMS; i++) { + FeedItem item = new FeedItem(0, "title", "id", "link", new Date(), true, 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, new Date(NUM_ITEMS - i))); + items.add(item); + } + + PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getTargetContext()); + 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(getInstrumentation().getTargetContext()); + for (int i = 0; i < files.size(); i++) { + if (i < EPISODE_CACHE_SIZE) { + assertTrue(files.get(i).exists()); + } else { + assertFalse(files.get(i).exists()); + } + } + } + + public void testPerformAutoCleanupShouldNotDeleteBecauseUnread() throws IOException { + final int NUM_ITEMS = EPISODE_CACHE_SIZE * 2; + + Feed feed = new Feed("url", new Date(), "title"); + List<FeedItem> items = new ArrayList<FeedItem>(); + feed.setItems(items); + List<File> files = new ArrayList<File>(); + for (int i = 0; i < NUM_ITEMS; i++) { + FeedItem item = new FeedItem(0, "title", "id", "link", new Date(), false, 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))); + items.add(item); + } + + PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getTargetContext()); + 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(getInstrumentation().getTargetContext()); + for (File file : files) { + assertTrue(file.exists()); + } + } + + 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<FeedItem>(); + feed.setItems(items); + List<File> files = new ArrayList<File>(); + for (int i = 0; i < NUM_ITEMS; i++) { + FeedItem item = new FeedItem(0, "title", "id", "link", new Date(), true, 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))); + items.add(item); + } + + PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getTargetContext()); + 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(getInstrumentation().getTargetContext()); + for (File file : files) { + assertTrue(file.exists()); + } + } } diff --git a/src/instrumentationTest/de/test/antennapod/storage/DBWriterTest.java b/src/instrumentationTest/de/test/antennapod/storage/DBWriterTest.java index 37fff1de7..04ec265d7 100644 --- a/src/instrumentationTest/de/test/antennapod/storage/DBWriterTest.java +++ b/src/instrumentationTest/de/test/antennapod/storage/DBWriterTest.java @@ -31,7 +31,7 @@ public class DBWriterTest extends InstrumentationTestCase { protected void tearDown() throws Exception { super.tearDown(); final Context context = getInstrumentation().getTargetContext(); - context.deleteDatabase(PodDBAdapter.DATABASE_NAME); + assertTrue(PodDBAdapter.deleteDatabase(getInstrumentation().getTargetContext())); File testDir = context.getExternalFilesDir(TEST_FOLDER); assertNotNull(testDir); |