From 6407e680ddf503e9e1dad9ca94ad081febb1a4c7 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Fri, 30 May 2014 21:11:44 +0200 Subject: [PATCH 1/3] Added UI test classes --- build.gradle | 2 + .../test/antennapod/ui/MainActivityTest.java | 24 +++++ .../de/test/antennapod/ui/UITestUtils.java | 93 +++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java create mode 100644 src/instrumentationTest/de/test/antennapod/ui/UITestUtils.java diff --git a/build.gradle b/build.gradle index 9046f2648..b1a2a46ca 100644 --- a/build.gradle +++ b/build.gradle @@ -28,6 +28,8 @@ dependencies { compile 'commons-io:commons-io:2.4' compile 'com.nineoldandroids:library:2.4.0' compile project(':submodules:dslv:library') + + compile 'com.jayway.android.robotium:robotium-solo:5.0.1' } android { diff --git a/src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java b/src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java new file mode 100644 index 000000000..5dc119a88 --- /dev/null +++ b/src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java @@ -0,0 +1,24 @@ +package instrumentationTest.de.test.antennapod.ui; + +import android.test.ActivityInstrumentationTestCase2; +import com.robotium.solo.Solo; +import de.danoeh.antennapod.activity.MainActivity; + +/** + * User interface tests for MainActivity + */ +public class MainActivityTest extends ActivityInstrumentationTestCase2 { + + private Solo solo; + + public MainActivityTest(Class activityClass) { + super(activityClass); + } + + @Override + protected void setUp() throws Exception { + solo = new Solo(getInstrumentation(), getActivity()); + } + + +} diff --git a/src/instrumentationTest/de/test/antennapod/ui/UITestUtils.java b/src/instrumentationTest/de/test/antennapod/ui/UITestUtils.java new file mode 100644 index 000000000..9fd5c4d77 --- /dev/null +++ b/src/instrumentationTest/de/test/antennapod/ui/UITestUtils.java @@ -0,0 +1,93 @@ +package instrumentationTest.de.test.antennapod.ui; + +import android.content.Context; +import android.graphics.Bitmap; +import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.feed.FeedImage; +import de.danoeh.antennapod.feed.FeedItem; +import de.danoeh.antennapod.feed.FeedMedia; +import instrumentationTest.de.test.antennapod.util.service.download.HTTPBin; +import instrumentationTest.de.test.antennapod.util.syndication.feedgenerator.RSS2Generator; +import junit.framework.Assert; +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * Utility methods for UI tests + */ +public class UITestUtils { + + private static final String DATA_FOLDER = "test/UITestUtils"; + + public static final int NUM_FEEDS = 10; + public static final int NUM_ITEMS_PER_FEED = 20; + + + private Context context; + private HTTPBin server; + private File destDir; + private File hostedFeedDir; + + public UITestUtils(Context context) { + this.context = context; + } + + + public void setup() throws IOException { + destDir = context.getExternalFilesDir(DATA_FOLDER); + destDir.mkdir(); + hostedFeedDir = new File(destDir, "hostedFeeds"); + hostedFeedDir.mkdir(); + Assert.assertTrue(destDir.exists()); + Assert.assertTrue(hostedFeedDir.exists()); + server.start(); + } + + public void tearDown() throws IOException { + FileUtils.deleteDirectory(destDir); + server.stop(); + } + + private String hostFeed(Feed feed) throws IOException { + File feedFile = new File(hostedFeedDir, feed.getTitle()); + FileOutputStream out = new FileOutputStream(feedFile); + RSS2Generator generator = new RSS2Generator(); + generator.writeFeed(feed, out, "UTF-8", 0); + out.close(); + int id = server.serveFile(feedFile); + Assert.assertTrue(id != -1); + return String.format("http://127.0.0.1/files/%d", id); + } + + private File newBitmapFile(String name) throws IOException { + File imgFile = new File(destDir, name); + Bitmap bitmap = Bitmap.createBitmap(128, 128, Bitmap.Config.ARGB_8888); + FileOutputStream out = new FileOutputStream(imgFile); + bitmap.compress(Bitmap.CompressFormat.PNG, 1, out); + out.close(); + return imgFile; + } + + public void addFeedData() throws IOException { + for (int i = 0; i < NUM_FEEDS; i++) { + FeedImage image = new FeedImage(0, "image " + i, newBitmapFile("image" + i).getAbsolutePath(), "http://example.com/feed" + i + "/image", true); + Feed feed = new Feed(0, new Date(), "Title " + i, "http://example.com/" + i, "Description of feed " + i, + "http://example.com/pay/feed" + i, "author " + i, "en", Feed.TYPE_RSS2, "feed" + i, image, null, + "http://example.com/feed/src/" + i, false); + feed.setDownload_url(hostFeed(feed)); + + // create items + List items = new ArrayList(); + for (int j = 0; j < NUM_ITEMS_PER_FEED; j++) { + FeedItem item = new FeedItem(0, "item" + j, "item" + j, "http://example.com/feed" + i + "/item/" + j, new Date(), true, feed); + items.add(item); + } + } + } +} From 662f8a46794f33276582783ccc2f24486651eb18 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Sun, 1 Jun 2014 13:13:17 +0200 Subject: [PATCH 2/3] Finished UITestUtils implementation --- build.gradle | 2 +- .../service/download/HttpDownloaderTest.java | 11 +- .../test/antennapod/ui/MainActivityTest.java | 14 ++- .../de/test/antennapod/ui/UITestUtils.java | 116 ++++++++++++++++-- .../test/antennapod/ui/UITestUtilsTest.java | 91 ++++++++++++++ .../util/service/download/HTTPBin.java | 2 + 6 files changed, 219 insertions(+), 17 deletions(-) create mode 100644 src/instrumentationTest/de/test/antennapod/ui/UITestUtilsTest.java diff --git a/build.gradle b/build.gradle index b1a2a46ca..c350673c6 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ dependencies { compile 'com.nineoldandroids:library:2.4.0' compile project(':submodules:dslv:library') - compile 'com.jayway.android.robotium:robotium-solo:5.0.1' + compile 'com.jayway.android.robotium:robotium-solo:5.1' } android { diff --git a/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java b/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java index f4726504e..6e44c8a0f 100644 --- a/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java +++ b/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java @@ -22,7 +22,6 @@ public class HttpDownloaderTest extends InstrumentationTestCase { private File destDir; private HTTPBin httpServer; - private static final String BASE_URL = "http://127.0.0.1:" + HTTPBin.PORT; public HttpDownloaderTest() { super(); @@ -79,15 +78,15 @@ public class HttpDownloaderTest extends InstrumentationTestCase { } - private static final String URL_404 = BASE_URL + "/status/404"; - private static final String URL_AUTH = BASE_URL + "/basic-auth/user/passwd"; + private static final String URL_404 = HTTPBin.BASE_URL + "/status/404"; + private static final String URL_AUTH = HTTPBin.BASE_URL + "/basic-auth/user/passwd"; public void testPassingHttp() { - download(BASE_URL + "/status/200", "test200", true); + download(HTTPBin.BASE_URL + "/status/200", "test200", true); } public void testRedirect() { - download(BASE_URL + "/redirect/4", "testRedirect", true); + download(HTTPBin.BASE_URL + "/redirect/4", "testRedirect", true); } public void testGzip() { @@ -99,7 +98,7 @@ public class HttpDownloaderTest extends InstrumentationTestCase { } public void testCancel() { - final String url = BASE_URL + "/delay/3"; + final String url = HTTPBin.BASE_URL + "/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() { diff --git a/src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java b/src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java index 5dc119a88..319664806 100644 --- a/src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java +++ b/src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java @@ -10,14 +10,24 @@ import de.danoeh.antennapod.activity.MainActivity; public class MainActivityTest extends ActivityInstrumentationTestCase2 { private Solo solo; + private UITestUtils uiTestUtils; - public MainActivityTest(Class activityClass) { - super(activityClass); + public MainActivityTest() { + super(MainActivity.class); } @Override protected void setUp() throws Exception { + super.setUp(); solo = new Solo(getInstrumentation(), getActivity()); + uiTestUtils = new UITestUtils(getInstrumentation().getTargetContext()); + uiTestUtils.setup(); + } + + @Override + protected void tearDown() throws Exception { + uiTestUtils.tearDown(); + super.tearDown(); } diff --git a/src/instrumentationTest/de/test/antennapod/ui/UITestUtils.java b/src/instrumentationTest/de/test/antennapod/ui/UITestUtils.java index 9fd5c4d77..4d1ca093a 100644 --- a/src/instrumentationTest/de/test/antennapod/ui/UITestUtils.java +++ b/src/instrumentationTest/de/test/antennapod/ui/UITestUtils.java @@ -6,33 +6,41 @@ import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.FeedImage; import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedMedia; +import de.danoeh.antennapod.storage.DBWriter; +import de.danoeh.antennapod.storage.PodDBAdapter; import instrumentationTest.de.test.antennapod.util.service.download.HTTPBin; import instrumentationTest.de.test.antennapod.util.syndication.feedgenerator.RSS2Generator; import junit.framework.Assert; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; /** - * Utility methods for UI tests + * Utility methods for UI tests. + * Starts a web server that hosts feeds, episodes and images. */ public class UITestUtils { private static final String DATA_FOLDER = "test/UITestUtils"; - public static final int NUM_FEEDS = 10; - public static final int NUM_ITEMS_PER_FEED = 20; + public static final int NUM_FEEDS = 5; + public static final int NUM_ITEMS_PER_FEED = 10; private Context context; - private HTTPBin server; + private HTTPBin server = new HTTPBin(); private File destDir; private File hostedFeedDir; + private File hostedMediaDir; + + public List hostedFeeds = new ArrayList(); public UITestUtils(Context context) { this.context = context; @@ -44,14 +52,23 @@ public class UITestUtils { destDir.mkdir(); hostedFeedDir = new File(destDir, "hostedFeeds"); hostedFeedDir.mkdir(); + hostedMediaDir = new File(destDir, "hostedMediaDir"); + hostedMediaDir.mkdir(); Assert.assertTrue(destDir.exists()); Assert.assertTrue(hostedFeedDir.exists()); + Assert.assertTrue(hostedMediaDir.exists()); server.start(); } public void tearDown() throws IOException { FileUtils.deleteDirectory(destDir); + FileUtils.deleteDirectory(hostedMediaDir); + FileUtils.deleteDirectory(hostedFeedDir); server.stop(); + + if (localFeedDataAdded) { + PodDBAdapter.deleteDatabase(context); + } } private String hostFeed(Feed feed) throws IOException { @@ -62,7 +79,13 @@ public class UITestUtils { out.close(); int id = server.serveFile(feedFile); Assert.assertTrue(id != -1); - return String.format("http://127.0.0.1/files/%d", id); + return String.format("%s/files/%d", HTTPBin.BASE_URL, id); + } + + private String hostFile(File file) { + int id = server.serveFile(file); + Assert.assertTrue(id != -1); + return String.format("%s/files/%d", HTTPBin.BASE_URL, id); } private File newBitmapFile(String name) throws IOException { @@ -74,20 +97,97 @@ public class UITestUtils { return imgFile; } - public void addFeedData() throws IOException { + private File newMediaFile(String name) throws IOException { + File mediaFile = new File(hostedMediaDir, name); + Assert.assertFalse(mediaFile.exists()); + + InputStream in = context.getAssets().open("testfile.mp3"); + Assert.assertNotNull(in); + + FileOutputStream out = new FileOutputStream(mediaFile); + IOUtils.copy(in, out); + out.close(); + + return mediaFile; + } + + private boolean feedDataHosted = false; + + /** + * Adds feeds, images and episodes to the webserver for testing purposes. + */ + public void addHostedFeedData() throws IOException { + if (feedDataHosted) throw new IllegalStateException("addHostedFeedData was called twice on the same instance"); for (int i = 0; i < NUM_FEEDS; i++) { - FeedImage image = new FeedImage(0, "image " + i, newBitmapFile("image" + i).getAbsolutePath(), "http://example.com/feed" + i + "/image", true); + File bitmapFile = newBitmapFile("image" + i); + FeedImage image = new FeedImage(0, "image " + i, null, hostFile(bitmapFile), false); Feed feed = new Feed(0, new Date(), "Title " + i, "http://example.com/" + i, "Description of feed " + i, "http://example.com/pay/feed" + i, "author " + i, "en", Feed.TYPE_RSS2, "feed" + i, image, null, "http://example.com/feed/src/" + i, false); - feed.setDownload_url(hostFeed(feed)); + image.setOwner(feed); // create items List items = new ArrayList(); for (int j = 0; j < NUM_ITEMS_PER_FEED; j++) { FeedItem item = new FeedItem(0, "item" + j, "item" + j, "http://example.com/feed" + i + "/item/" + j, new Date(), true, feed); items.add(item); + + File mediaFile = newMediaFile("feed-" + i + "-episode-" + j + ".mp3"); + item.setMedia(new FeedMedia(0, item, 0, 0, mediaFile.length(), "audio/mp3", null, hostFile(mediaFile), false, null, 0)); + } + feed.setItems(items); + feed.setDownload_url(hostFeed(feed)); + hostedFeeds.add(feed); } + feedDataHosted = true; + } + + + private boolean localFeedDataAdded = false; + + /** + * Adds feeds, images and episodes to the local database. This method will also call addHostedFeedData if it has not + * been called yet. + * + * Adds one item of each feed to the queue and to the playback history. + * + * This method should NOT be called if the testing class wants to download the hosted feed data. + * + * @param downloadEpisodes true if episodes should also be marked as downloaded. + */ + public void addLocalFeedData(boolean downloadEpisodes) throws Exception { + if (localFeedDataAdded) throw new IllegalStateException("addLocalFeedData was called twice on the same instance"); + if (!feedDataHosted) { + addHostedFeedData(); + } + + List queue = new ArrayList(); + + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + for (Feed feed : hostedFeeds) { + feed.setDownloaded(true); + if (feed.getImage() != null) { + FeedImage image = feed.getImage(); + image.setFile_url(image.getDownload_url()); + image.setDownloaded(true); + } + if (downloadEpisodes) { + for (FeedItem item : feed.getItems()) { + if (item.hasMedia()) { + FeedMedia media = item.getMedia(); + media.setFile_url(media.getDownload_url()); + media.setDownloaded(true); + } + } + } + + queue.add(feed.getItems().get(0)); + feed.getItems().get(1).getMedia().setPlaybackCompletionDate(new Date()); + } + adapter.setCompleteFeed(hostedFeeds.toArray(new Feed[hostedFeeds.size()])); + adapter.setQueue(queue); + adapter.close(); } } diff --git a/src/instrumentationTest/de/test/antennapod/ui/UITestUtilsTest.java b/src/instrumentationTest/de/test/antennapod/ui/UITestUtilsTest.java new file mode 100644 index 000000000..9628c5522 --- /dev/null +++ b/src/instrumentationTest/de/test/antennapod/ui/UITestUtilsTest.java @@ -0,0 +1,91 @@ +package instrumentationTest.de.test.antennapod.ui; + +import android.test.InstrumentationTestCase; +import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.feed.FeedItem; +import org.apache.http.HttpStatus; + +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.List; + +/** + * Test for the UITestUtils. Makes sure that all URLs are reachable and that the class does not cause any crashes. + */ +public class UITestUtilsTest extends InstrumentationTestCase { + + private UITestUtils uiTestUtils; + + @Override + protected void setUp() throws Exception { + super.setUp(); + uiTestUtils = new UITestUtils(getInstrumentation().getTargetContext()); + uiTestUtils.setup(); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + uiTestUtils.tearDown(); + } + + public void testAddHostedFeeds() throws Exception { + uiTestUtils.addHostedFeedData(); + final List feeds = uiTestUtils.hostedFeeds; + assertNotNull(feeds); + assertFalse(feeds.isEmpty()); + + for (Feed feed : feeds) { + testUrlReachable(feed.getDownload_url()); + if (feed.getImage() != null) { + testUrlReachable(feed.getImage().getDownload_url()); + } + for (FeedItem item : feed.getItems()) { + if (item.hasMedia()) { + testUrlReachable(item.getMedia().getDownload_url()); + } + } + } + } + + private void testUrlReachable(String strUtl) throws Exception { + URL url = new URL(strUtl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.connect(); + int rc = conn.getResponseCode(); + assertEquals(HttpStatus.SC_OK, rc); + conn.disconnect(); + } + + private void addLocalFeedDataCheck(boolean downloadEpisodes) throws Exception { + uiTestUtils.addLocalFeedData(downloadEpisodes); + assertNotNull(uiTestUtils.hostedFeeds); + assertFalse(uiTestUtils.hostedFeeds.isEmpty()); + + for (Feed feed : uiTestUtils.hostedFeeds) { + assertTrue(feed.getId() != 0); + if (feed.getImage() != null) { + assertTrue(feed.getImage().getId() != 0); + } + for (FeedItem item : feed.getItems()) { + assertTrue(item.getId() != 0); + if (item.hasMedia()) { + assertTrue(item.getMedia().getId() != 0); + if (downloadEpisodes) { + assertTrue(item.getMedia().isDownloaded()); + assertNotNull(item.getMedia().getFile_url()); + } + } + } + } + } + + public void testAddLocalFeedDataNoDownload() throws Exception { + addLocalFeedDataCheck(false); + } + + public void testAddLocalFeedDataDownload() throws Exception { + addLocalFeedDataCheck(true); + } +} diff --git a/src/instrumentationTest/de/test/antennapod/util/service/download/HTTPBin.java b/src/instrumentationTest/de/test/antennapod/util/service/download/HTTPBin.java index 272bcda5e..adde08ef0 100644 --- a/src/instrumentationTest/de/test/antennapod/util/service/download/HTTPBin.java +++ b/src/instrumentationTest/de/test/antennapod/util/service/download/HTTPBin.java @@ -26,6 +26,8 @@ import java.util.zip.GZIPOutputStream; public class HTTPBin extends NanoHTTPD { private static final String TAG = "HTTPBin"; public static final int PORT = 8124; + public static final String BASE_URL = "http://127.0.0.1:" + HTTPBin.PORT; + private static final String MIME_HTML = "text/html"; private static final String MIME_PLAIN = "text/plain"; From 78f725d50d9ae374c356fc9458ee79ccc752454a Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Sun, 1 Jun 2014 14:32:48 +0200 Subject: [PATCH 3/3] Added basic MainActivity tests --- .../antennapod/activity/MainActivity.java | 4 +- .../test/antennapod/ui/MainActivityTest.java | 86 +++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/src/de/danoeh/antennapod/activity/MainActivity.java b/src/de/danoeh/antennapod/activity/MainActivity.java index 92afea77c..898897bd8 100644 --- a/src/de/danoeh/antennapod/activity/MainActivity.java +++ b/src/de/danoeh/antennapod/activity/MainActivity.java @@ -41,8 +41,8 @@ public class MainActivity extends ActionBarActivity { | EventDistributor.FEED_LIST_UPDATE | EventDistributor.UNREAD_ITEMS_UPDATE; - private static final String PREF_NAME = "MainActivityPrefs"; - private static final String PREF_IS_FIRST_LAUNCH = "prefMainActivityIsFirstLaunch"; + public static final String PREF_NAME = "MainActivityPrefs"; + public static final String PREF_IS_FIRST_LAUNCH = "prefMainActivityIsFirstLaunch"; public static final String EXTRA_NAV_INDEX = "nav_index"; public static final String EXTRA_NAV_TYPE = "nav_type"; diff --git a/src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java b/src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java index 319664806..26f38da98 100644 --- a/src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java +++ b/src/instrumentationTest/de/test/antennapod/ui/MainActivityTest.java @@ -1,8 +1,18 @@ package instrumentationTest.de.test.antennapod.ui; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Resources; import android.test.ActivityInstrumentationTestCase2; +import android.view.View; 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.PreferenceActivity; +import de.danoeh.antennapod.feed.EventDistributor; +import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.storage.PodDBAdapter; /** * User interface tests for MainActivity @@ -22,13 +32,89 @@ public class MainActivityTest extends ActivityInstrumentationTestCase2