From e51a107083101b64614b30324ae6cc2226a3f835 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Thu, 28 Nov 2019 23:34:26 +0100 Subject: Fixed tests on small screens In general, made some tests more stable --- .../de/test/antennapod/service/download/DownloadServiceTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'app/src/androidTest/java/de/test/antennapod/service/download') diff --git a/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java b/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java index d0e14d70a..6b020efcb 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java @@ -5,6 +5,7 @@ import androidx.annotation.Nullable; import androidx.test.InstrumentationRegistry; import androidx.test.runner.AndroidJUnit4; +import de.test.antennapod.EspressoTestUtils; import org.awaitility.Awaitility; import org.awaitility.core.ConditionTimeoutException; import org.junit.After; @@ -53,6 +54,8 @@ public class DownloadServiceTest { @Before public void setUp() throws Exception { + EspressoTestUtils.clearDatabase(); + EspressoTestUtils.clearPreferences(); origFactory = DownloadService.getDownloaderFactory(); testFeed = setUpTestFeeds(); testMedia11 = testFeed.getItemAtIndex(0).getMedia(); @@ -171,7 +174,7 @@ public class DownloadServiceTest { final int totalNumEventsExpected = itemAlreadyInQueue ? 1 : 3; Awaitility.await("item dequeue event + download termination event") .atMost(1000, TimeUnit.MILLISECONDS) - .until(() ->feedItemEventListener.getEvents().size() >= totalNumEventsExpected); + .until(() -> feedItemEventListener.getEvents().size() >= totalNumEventsExpected); assertFalse("The download should have been canceled", DBReader.getFeedMedia(testMedia11.getId()).isDownloaded()); if (itemAlreadyInQueue) { -- cgit v1.2.3 From e77ecda206c36cc0743b470e4f9f286325ceaba6 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Mon, 2 Dec 2019 09:37:33 +0100 Subject: Fixed cases where one failing test broke all following tests Select a random port for the http server, so we do not get EADDRINUSE --- .../service/download/HttpDownloaderTest.java | 29 ++++++++++------------ 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'app/src/androidTest/java/de/test/antennapod/service/download') diff --git a/app/src/androidTest/java/de/test/antennapod/service/download/HttpDownloaderTest.java b/app/src/androidTest/java/de/test/antennapod/service/download/HttpDownloaderTest.java index 4530b7679..7d9ec7c98 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/download/HttpDownloaderTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/download/HttpDownloaderTest.java @@ -29,10 +29,9 @@ public class HttpDownloaderTest { private static final String TAG = "HttpDownloaderTest"; private static final String DOWNLOAD_DIR = "testdownloads"; - private static boolean successful = true; - + private String url404; + private String urlAuth; private File destDir; - private HTTPBin httpServer; public HttpDownloaderTest() { @@ -57,6 +56,8 @@ public class HttpDownloaderTest { assertTrue(destDir.exists()); httpServer = new HTTPBin(); httpServer.start(); + url404 = httpServer.getBaseUrl() + "/status/404"; + urlAuth = httpServer.getBaseUrl() + "/basic-auth/user/passwd"; } private FeedFileImpl setupFeedFile(String downloadUrl, String title, boolean deleteExisting) { @@ -88,33 +89,29 @@ public class HttpDownloaderTest { return downloader; } - - private static final String URL_404 = HTTPBin.BASE_URL + "/status/404"; - private static final String URL_AUTH = HTTPBin.BASE_URL + "/basic-auth/user/passwd"; - @Test public void testPassingHttp() { - download(HTTPBin.BASE_URL + "/status/200", "test200", true); + download(httpServer.getBaseUrl() + "/status/200", "test200", true); } @Test public void testRedirect() { - download(HTTPBin.BASE_URL + "/redirect/4", "testRedirect", true); + download(httpServer.getBaseUrl() + "/redirect/4", "testRedirect", true); } @Test public void testGzip() { - download(HTTPBin.BASE_URL + "/gzip/100", "testGzip", true); + download(httpServer.getBaseUrl() + "/gzip/100", "testGzip", true); } @Test public void test404() { - download(URL_404, "test404", false); + download(url404, "test404", false); } @Test public void testCancel() { - final String url = HTTPBin.BASE_URL + "/delay/3"; + final String url = httpServer.getBaseUrl() + "/delay/3"; FeedFileImpl feedFile = setupFeedFile(url, "delay", true); final Downloader downloader = new HttpDownloader(new DownloadRequest(feedFile.getFile_url(), url, "delay", 0, feedFile.getTypeAsInt())); Thread t = new Thread() { @@ -139,7 +136,7 @@ public class HttpDownloaderTest { @Test public void testDeleteOnFailShouldDelete() { - Downloader downloader = download(URL_404, "testDeleteOnFailShouldDelete", false, true, null, null, true); + Downloader downloader = download(url404, "testDeleteOnFailShouldDelete", false, true, null, null, true); assertFalse(new File(downloader.getDownloadRequest().getDestination()).exists()); } @@ -149,18 +146,18 @@ public class HttpDownloaderTest { File dest = new File(destDir, filename); dest.delete(); assertTrue(dest.createNewFile()); - Downloader downloader = download(URL_404, filename, false, false, null, null, false); + Downloader downloader = download(url404, filename, false, false, null, null, false); assertTrue(new File(downloader.getDownloadRequest().getDestination()).exists()); } @Test public void testAuthenticationShouldSucceed() throws InterruptedException { - download(URL_AUTH, "testAuthSuccess", true, true, "user", "passwd", true); + download(urlAuth, "testAuthSuccess", true, true, "user", "passwd", true); } @Test public void testAuthenticationShouldFail() { - Downloader downloader = download(URL_AUTH, "testAuthSuccess", false, true, "user", "Wrong passwd", true); + Downloader downloader = download(urlAuth, "testAuthSuccess", false, true, "user", "Wrong passwd", true); assertEquals(DownloadError.ERROR_UNAUTHORIZED, downloader.getResult().getReason()); } -- cgit v1.2.3 From 376600d5b4c41f6167d5d4161a7ef8e3a132d715 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Mon, 2 Dec 2019 15:49:57 +0100 Subject: Read current media from preferences instead of activity->fragment->controller --- .../service/download/DownloadServiceTest.java | 68 +++++++++++----------- 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'app/src/androidTest/java/de/test/antennapod/service/download') diff --git a/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java b/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java index 6b020efcb..1ca4de1ad 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java @@ -1,5 +1,7 @@ package de.test.antennapod.service.download; +import android.content.Context; +import android.content.Intent; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.test.InstrumentationRegistry; @@ -80,6 +82,10 @@ public class DownloadServiceTest { @After public void tearDown() throws Exception { DownloadService.setDownloaderFactory(origFactory); + Context context = InstrumentationRegistry.getTargetContext(); + DownloadRequester.getInstance().cancelAllDownloads(context); + context.stopService(new Intent(context, DownloadService.class)); + EspressoTestUtils.tryKillDownloadService(); } @Test @@ -139,8 +145,9 @@ public class DownloadServiceTest { } private void doTestCancelDownload_UndoEnqueue(boolean itemAlreadyInQueue) throws Exception { + Context context = InstrumentationRegistry.getTargetContext(); // let download takes longer to ensure the test can cancel the download in time - DownloadService.setDownloaderFactory(new StubDownloaderFactory(150, downloadStatus -> { + DownloadService.setDownloaderFactory(new StubDownloaderFactory(10000, downloadStatus -> { downloadStatus.setSuccessful(); })); UserPreferences.setEnqueueDownloadedEpisodes(true); @@ -149,45 +156,38 @@ public class DownloadServiceTest { final long item1Id = testMedia11.getItem().getId(); if (itemAlreadyInQueue) { // simulate item already in queue condition - DBWriter.addQueueItem(InstrumentationRegistry.getTargetContext(), false, item1Id).get(); + DBWriter.addQueueItem(context, false, item1Id).get(); assertTrue(DBReader.getQueueIDList().contains(item1Id)); } else { assertFalse(DBReader.getQueueIDList().contains(item1Id)); } withFeedItemEventListener(feedItemEventListener -> { - try { - DownloadRequester.getInstance().downloadMedia(false, InstrumentationRegistry.getTargetContext(), - testMedia11.getItem()); - if (itemAlreadyInQueue) { - Awaitility.await("download service receives the request - " - + "no event is expected before cancel is issued") - .atLeast(100, TimeUnit.MILLISECONDS) - .until(() -> true); - } else { - Awaitility.await("item enqueue event") - .atMost(1000, TimeUnit.MILLISECONDS) - .until(() -> feedItemEventListener.getEvents().size() >= 1); - } - DownloadRequester.getInstance().cancelDownload(InstrumentationRegistry.getTargetContext(), - testMedia11); - final int totalNumEventsExpected = itemAlreadyInQueue ? 1 : 3; - Awaitility.await("item dequeue event + download termination event") - .atMost(1000, TimeUnit.MILLISECONDS) - .until(() -> feedItemEventListener.getEvents().size() >= totalNumEventsExpected); - assertFalse("The download should have been canceled", - DBReader.getFeedMedia(testMedia11.getId()).isDownloaded()); - if (itemAlreadyInQueue) { - assertTrue("The FeedItem should still be in the queue after the download is cancelled." - + " It's there before download.", - DBReader.getQueueIDList().contains(item1Id)); - } else { - assertFalse("The FeedItem should not be in the queue after the download is cancelled.", - DBReader.getQueueIDList().contains(item1Id)); - } - } catch (ConditionTimeoutException cte) { - fail("The expected FeedItemEvent (for media download complete) has not been posted. " - + cte.getMessage()); + DownloadRequester.getInstance().downloadMedia(false, context, testMedia11.getItem()); + if (itemAlreadyInQueue) { + Awaitility.await("download service receives the request - " + + "no event is expected before cancel is issued") + .atLeast(100, TimeUnit.MILLISECONDS) + .until(() -> true); + } else { + Awaitility.await("item enqueue event") + .atMost(2000, TimeUnit.MILLISECONDS) + .until(() -> feedItemEventListener.getEvents().size() >= 1); + } + DownloadRequester.getInstance().cancelDownload(context, testMedia11); + final int totalNumEventsExpected = itemAlreadyInQueue ? 1 : 3; + Awaitility.await("item dequeue event + download termination event") + .atMost(1000, TimeUnit.MILLISECONDS) + .until(() -> feedItemEventListener.getEvents().size() >= totalNumEventsExpected); + assertFalse("The download should have been canceled", + DBReader.getFeedMedia(testMedia11.getId()).isDownloaded()); + if (itemAlreadyInQueue) { + assertTrue("The FeedItem should still be in the queue after the download is cancelled." + + " It's there before download.", + DBReader.getQueueIDList().contains(item1Id)); + } else { + assertFalse("The FeedItem should not be in the queue after the download is cancelled.", + DBReader.getQueueIDList().contains(item1Id)); } }); } -- cgit v1.2.3