Finished UITestUtils implementation

This commit is contained in:
daniel oeh 2014-06-01 13:13:17 +02:00
parent 6407e680dd
commit 662f8a4679
6 changed files with 219 additions and 17 deletions

View File

@ -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 {

View File

@ -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() {

View File

@ -10,14 +10,24 @@ import de.danoeh.antennapod.activity.MainActivity;
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
private Solo solo;
private UITestUtils uiTestUtils;
public MainActivityTest(Class<MainActivity> 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();
}

View File

@ -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<Feed> hostedFeeds = new ArrayList<Feed>();
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<FeedItem> items = new ArrayList<FeedItem>();
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<FeedItem> queue = new ArrayList<FeedItem>();
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();
}
}

View File

@ -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<Feed> 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);
}
}

View File

@ -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";