Merge branch 'ui-tests' into develop

This commit is contained in:
daniel oeh 2014-06-01 17:31:40 +02:00
commit d08888300a
7 changed files with 415 additions and 8 deletions

View File

@ -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.1'
}
android {

View File

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

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

@ -0,0 +1,120 @@
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
*/
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
private Solo solo;
private UITestUtils uiTestUtils;
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();
// create database
PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getTargetContext());
adapter.open();
adapter.close();
// override first launch preference
SharedPreferences prefs = getInstrumentation().getTargetContext().getSharedPreferences(MainActivity.PREF_NAME, Context.MODE_PRIVATE);
prefs.edit().putBoolean(MainActivity.PREF_IS_FIRST_LAUNCH, false).commit();
}
@Override
protected void tearDown() throws Exception {
uiTestUtils.tearDown();
solo.finishOpenedActivities();
PodDBAdapter.deleteDatabase(getInstrumentation().getTargetContext());
super.tearDown();
}
public void testAddFeed() throws Exception {
uiTestUtils.addHostedFeedData();
final Feed feed = uiTestUtils.hostedFeeds.get(0);
solo.setNavigationDrawer(Solo.OPENED);
solo.clickOnText(solo.getString(R.string.add_feed_label));
solo.enterText(0, feed.getDownload_url());
solo.clickOnButton(0);
solo.waitForActivity(DefaultOnlineFeedViewActivity.class);
solo.waitForView(R.id.butSubscribe);
assertEquals(solo.getString(R.string.subscribe_label), solo.getButton(0).getText().toString());
solo.clickOnButton(0);
solo.waitForText(solo.getString(R.string.subscribed_label));
}
public void testClickNavDrawer() throws Exception {
uiTestUtils.addLocalFeedData(false);
EventDistributor.getInstance().sendFeedUpdateBroadcast();
EventDistributor.getInstance().sendQueueUpdateBroadcast();
final View home = solo.getView(android.R.id.home);
// all episodes
solo.waitForView(android.R.id.list);
assertEquals(solo.getString(R.string.all_episodes_label), getActionbarTitle());
// queue
solo.clickOnView(home);
solo.clickOnText(solo.getString(R.string.queue_label));
solo.waitForView(android.R.id.list);
assertEquals(solo.getString(R.string.queue_label), getActionbarTitle());
// downloads
solo.clickOnView(home);
solo.clickOnText(solo.getString(R.string.downloads_label));
solo.waitForView(android.R.id.list);
assertEquals(solo.getString(R.string.downloads_label), getActionbarTitle());
// playback history
solo.clickOnView(home);
solo.clickOnText(solo.getString(R.string.playback_history_label));
solo.waitForView(android.R.id.list);
assertEquals(solo.getString(R.string.playback_history_label), getActionbarTitle());
// add podcast
solo.clickOnView(home);
solo.clickOnText(solo.getString(R.string.add_feed_label));
solo.waitForView(R.id.txtvFeedurl);
assertEquals(solo.getString(R.string.add_feed_label), getActionbarTitle());
// podcasts
for (int i = 0; i < uiTestUtils.hostedFeeds.size(); i++) {
Feed f = uiTestUtils.hostedFeeds.get(i);
solo.clickOnView(home);
solo.clickOnText(f.getTitle());
solo.waitForView(android.R.id.list);
assertEquals("", getActionbarTitle());
}
}
private String getActionbarTitle() {
return ((MainActivity)solo.getCurrentActivity()).getMainActivtyActionBar().getTitle().toString();
}
public void testGoToPreferences() {
solo.setNavigationDrawer(Solo.CLOSED);
solo.clickOnMenuItem(solo.getString(R.string.settings_label));
solo.waitForActivity(PreferenceActivity.class);
}
}

View File

@ -0,0 +1,193 @@
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 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.
* 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 = 5;
public static final int NUM_ITEMS_PER_FEED = 10;
private Context context;
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;
}
public void setup() throws IOException {
destDir = context.getExternalFilesDir(DATA_FOLDER);
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 {
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("%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 {
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;
}
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++) {
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);
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";