From 2aec26aa8e7a3d01928518eab85d5b5c07c2655d Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Thu, 26 Mar 2015 00:19:46 +0100 Subject: [PATCH] Fixes non-working app tests, especially GUI tests --- .../service/download/HttpDownloaderTest.java | 11 ++++--- .../test/antennapod/ui/MainActivityTest.java | 29 ++++++++++++------- .../de/test/antennapod/ui/PlaybackTest.java | 23 +++++++++++---- .../de/test/antennapod/ui/UITestUtils.java | 19 ++++++++---- .../util/service/download/HTTPBin.java | 28 +++++++++++++----- .../antennapod/core/feed/FeedImage.java | 6 ++-- 6 files changed, 81 insertions(+), 35 deletions(-) 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 65b1145a2..443fbed7e 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 @@ -2,7 +2,12 @@ package de.test.antennapod.service.download; import android.test.InstrumentationTestCase; import android.util.Log; + +import java.io.File; +import java.io.IOException; + import de.danoeh.antennapod.core.feed.FeedFile; +import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.download.DownloadRequest; import de.danoeh.antennapod.core.service.download.DownloadStatus; import de.danoeh.antennapod.core.service.download.Downloader; @@ -10,9 +15,6 @@ import de.danoeh.antennapod.core.service.download.HttpDownloader; import de.danoeh.antennapod.core.util.DownloadError; import de.test.antennapod.util.service.download.HTTPBin; -import java.io.File; -import java.io.IOException; - public class HttpDownloaderTest extends InstrumentationTestCase { private static final String TAG = "HttpDownloaderTest"; private static final String DOWNLOAD_DIR = "testdownloads"; @@ -41,6 +43,7 @@ public class HttpDownloaderTest extends InstrumentationTestCase { @Override protected void setUp() throws Exception { super.setUp(); + UserPreferences.createInstance(getInstrumentation().getTargetContext()); destDir = getInstrumentation().getTargetContext().getExternalFilesDir(DOWNLOAD_DIR); assertNotNull(destDir); assertTrue(destDir.exists()); @@ -90,7 +93,7 @@ public class HttpDownloaderTest extends InstrumentationTestCase { } public void testGzip() { - download("http://httpbin.org/gzip", "testGzip", true); + download(HTTPBin.BASE_URL + "/gzip/100", "testGzip", true); } public void test404() { diff --git a/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java b/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java index ce7b790e0..a8e93183c 100644 --- a/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java +++ b/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java @@ -3,12 +3,13 @@ package de.test.antennapod.ui; import android.content.Context; import android.content.SharedPreferences; import android.test.ActivityInstrumentationTestCase2; -import android.view.View; +import android.widget.ListView; + import com.robotium.solo.Solo; + import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.DefaultOnlineFeedViewActivity; import de.danoeh.antennapod.activity.MainActivity; -import de.danoeh.antennapod.activity.PreferenceActivityGingerbread; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.storage.PodDBAdapter; import de.danoeh.antennapod.preferences.PreferenceController; @@ -49,10 +50,14 @@ public class MainActivityTest extends ActivityInstrumentationTestCase2 PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getTargetContext()); adapter.open(); adapter.close(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getInstrumentation().getTargetContext()); prefs.edit().putBoolean(UserPreferences.PREF_UNPAUSE_ON_HEADSET_RECONNECT, false).commit(); prefs.edit().putBoolean(UserPreferences.PREF_PAUSE_ON_HEADSET_DISCONNECT, false).commit(); @@ -59,6 +62,10 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2 super.tearDown(); } + private void openNavDrawer() { + solo.clickOnScreen(50, 50); + } + private void setContinuousPlaybackPreference(boolean value) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getInstrumentation().getTargetContext()); prefs.edit().putBoolean(UserPreferences.PREF_FOLLOW_QUEUE, value).commit(); @@ -71,7 +78,9 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2 private void startLocalPlayback() { assertTrue(solo.waitForActivity(MainActivity.class)); - solo.setNavigationDrawer(Solo.CLOSED); + openNavDrawer(); + solo.clickOnText(solo.getString(R.string.all_episodes_label)); + solo.waitForView(android.R.id.list); solo.clickOnView(solo.getView(R.id.butSecondaryAction)); assertTrue(solo.waitForActivity(AudioplayerActivity.class)); assertTrue(solo.waitForView(solo.getView(R.id.butPlay))); @@ -79,10 +88,10 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2 private void startLocalPlaybackFromQueue() { assertTrue(solo.waitForActivity(MainActivity.class)); - solo.clickOnView(solo.getView(UITestUtils.HOME_VIEW)); + openNavDrawer(); solo.clickOnText(solo.getString(R.string.queue_label)); assertTrue(solo.waitForView(solo.getView(R.id.butSecondaryAction))); - solo.clickOnImageButton(0); + solo.clickOnImageButton(1); assertTrue(solo.waitForActivity(AudioplayerActivity.class)); assertTrue(solo.waitForView(solo.getView(R.id.butPlay))); } @@ -108,7 +117,7 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2 setContinuousPlaybackPreference(false); uiTestUtils.addLocalFeedData(true); List queue = DBReader.getQueue(getInstrumentation().getTargetContext()); - FeedItem second = queue.get(1); + FeedItem second = queue.get(0); startLocalPlaybackFromQueue(); assertTrue(solo.waitForText(second.getTitle())); @@ -147,4 +156,6 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2 public void testReplayEpisodeContinuousPlaybackOff() throws Exception { replayEpisodeCheck(false); } + + } diff --git a/app/src/androidTest/java/de/test/antennapod/ui/UITestUtils.java b/app/src/androidTest/java/de/test/antennapod/ui/UITestUtils.java index 55fffb80a..249cb0dd6 100644 --- a/app/src/androidTest/java/de/test/antennapod/ui/UITestUtils.java +++ b/app/src/androidTest/java/de/test/antennapod/ui/UITestUtils.java @@ -5,12 +5,8 @@ import android.content.Context; import android.graphics.Bitmap; import android.os.Build; -import de.danoeh.antennapod.R; -import de.danoeh.antennapod.core.feed.*; -import de.danoeh.antennapod.core.storage.PodDBAdapter; -import de.test.antennapod.util.service.download.HTTPBin; -import de.test.antennapod.util.syndication.feedgenerator.RSS2Generator; import junit.framework.Assert; + import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; @@ -23,6 +19,16 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.feed.EventDistributor; +import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.feed.FeedImage; +import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.feed.FeedMedia; +import de.danoeh.antennapod.core.storage.PodDBAdapter; +import de.test.antennapod.util.service.download.HTTPBin; +import de.test.antennapod.util.syndication.feedgenerator.RSS2Generator; + /** * Utility methods for UI tests. * Starts a web server that hosts feeds, episodes and images. @@ -174,7 +180,8 @@ public class UITestUtils { feed.setDownloaded(true); if (feed.getImage() != null) { FeedImage image = feed.getImage(); - image.setFile_url(image.getDownload_url()); + int fileId = Integer.parseInt(StringUtils.substringAfter(image.getDownload_url(), "files/")); + image.setFile_url(server.accessFile(fileId).getAbsolutePath()); image.setDownloaded(true); } if (downloadEpisodes) { diff --git a/app/src/androidTest/java/de/test/antennapod/util/service/download/HTTPBin.java b/app/src/androidTest/java/de/test/antennapod/util/service/download/HTTPBin.java index 5cb723446..2f2c3fe5b 100644 --- a/app/src/androidTest/java/de/test/antennapod/util/service/download/HTTPBin.java +++ b/app/src/androidTest/java/de/test/antennapod/util/service/download/HTTPBin.java @@ -2,15 +2,28 @@ package de.test.antennapod.util.service.download; import android.util.Base64; import android.util.Log; -import de.danoeh.antennapod.BuildConfig; + import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.net.URLConnection; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Random; import java.util.zip.GZIPOutputStream; +import de.danoeh.antennapod.BuildConfig; + /** * Http server for testing purposes *

@@ -264,7 +277,7 @@ public class HTTPBin extends NanoHTTPD { private Response getGzippedResponse(int size) throws IOException { try { - Thread.sleep(5000); + Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } @@ -272,14 +285,15 @@ public class HTTPBin extends NanoHTTPD { Random random = new Random(System.currentTimeMillis()); random.nextBytes(buffer); - ByteArrayOutputStream compressed = new ByteArrayOutputStream(); + ByteArrayOutputStream compressed = new ByteArrayOutputStream(buffer.length); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(compressed); gzipOutputStream.write(buffer); + gzipOutputStream.close(); InputStream inputStream = new ByteArrayInputStream(compressed.toByteArray()); Response response = new Response(Response.Status.OK, MIME_PLAIN, inputStream); - response.addHeader("Content-encoding", "gzip"); - response.addHeader("Content-length", String.valueOf(compressed.size())); + response.addHeader("Content-Encoding", "gzip"); + response.addHeader("Content-Length", String.valueOf(compressed.size())); return response; } diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedImage.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedImage.java index b01747f7f..c6f24367e 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedImage.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedImage.java @@ -2,10 +2,10 @@ package de.danoeh.antennapod.core.feed; import android.net.Uri; -import de.danoeh.antennapod.core.asynctask.PicassoImageResource; - import java.io.File; +import de.danoeh.antennapod.core.asynctask.PicassoImageResource; + public class FeedImage extends FeedFile implements PicassoImageResource { public static final int FEEDFILETYPE_FEEDIMAGE = 1; @@ -65,6 +65,8 @@ public class FeedImage extends FeedFile implements PicassoImageResource { public Uri getImageUri() { if (file_url != null && downloaded) { return Uri.fromFile(new File(file_url)); + } else if(download_url != null) { + return Uri.parse(download_url); } else { return null; }