Merge pull request #1258 from TomHennen/fix_playback_tests

Updated playback tests to make them more reliable
This commit is contained in:
Tom Hennen 2015-10-15 17:09:42 -04:00
commit 0e2b056d4e
7 changed files with 75 additions and 238 deletions

Binary file not shown.

View File

@ -46,98 +46,6 @@ public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActi
private Context context;
private PlaybackController controller;
protected FeedMedia currentMedia;
private PlaybackController createController(Activity activity) {
return new PlaybackController(activity, false) {
@Override
public void setupGUI() {
}
@Override
public void onPositionObserverUpdate() {
}
@Override
public void onBufferStart() {
}
@Override
public void onBufferEnd() {
}
@Override
public void onBufferUpdate(float progress) {
}
@Override
public void handleError(int code) {
}
@Override
public void onReloadNotification(int code) {
}
@Override
public void onSleepTimerUpdate() {
}
@Override
public ImageButton getPlayButton() {
return null;
}
@Override
public void postStatusMsg(int msg) {
}
@Override
public void clearStatusMsg() {
}
@Override
public boolean loadMediaInfo() {
Playable playable = controller.getMedia();
if(playable == null) {
currentMedia = null;
return true;
} else if(playable instanceof FeedMedia) {
currentMedia = (FeedMedia) playable;
return true;
} else {
return false;
}
}
@Override
public void onAwaitingVideoSurface() {
}
@Override
public void onServiceQueried() {
}
@Override
public void onShutdownNotification() {
}
@Override
public void onPlaybackEnd() {
currentMedia = null;
}
@Override
public void onPlaybackSpeedChange() {
}
@Override
protected void setScreenOn(boolean enable) {
}
};
}
public PlaybackSonicTest() {
super(MainActivity.class);
}
@ -158,9 +66,6 @@ public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActi
.putBoolean(UserPreferences.PREF_SONIC, true)
.commit();
controller = createController(getActivity());
controller.init();
solo = new Solo(getInstrumentation(), getActivity());
uiTestUtils = new UITestUtils(context);
@ -174,7 +79,6 @@ public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActi
@Override
public void tearDown() throws Exception {
controller.release();
solo.finishOpenedActivities();
uiTestUtils.tearDown();
@ -187,6 +91,7 @@ public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActi
private void openNavDrawer() {
solo.clickOnScreen(50, 50);
getInstrumentation().waitForIdleSync();
}
private void setContinuousPlaybackPreference(boolean value) {
@ -209,8 +114,10 @@ public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActi
View targetView = drawerView.getChildAt(EPISODES_DRAWER_LIST_INDEX);
solo.waitForView(targetView);
solo.clickOnView(targetView);
getInstrumentation().waitForIdleSync();
solo.waitForText(solo.getString(R.string.all_episodes_short_label));
solo.clickOnText(solo.getString(R.string.all_episodes_short_label));
getInstrumentation().waitForIdleSync();
final List<FeedItem> episodes = DBReader.getRecentlyPublishedEpisodes(10);
assertTrue(solo.waitForView(solo.getView(R.id.butSecondaryAction)));
@ -218,8 +125,8 @@ public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActi
solo.clickOnView(solo.getView(R.id.butSecondaryAction));
long mediaId = episodes.get(0).getMedia().getId();
boolean playing = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == mediaId;
if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
} else {
return false;
}
@ -237,16 +144,17 @@ public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActi
// this should be 'Queue'
View targetView = drawerView.getChildAt(QUEUE_DRAWER_LIST_INDEX);
solo.waitForView(targetView);
getInstrumentation().waitForIdleSync();
solo.clickOnView(targetView);
assertTrue(solo.waitForView(solo.getView(R.id.butSecondaryAction)));
final List<FeedItem> queue = DBReader.getQueue();
solo.clickOnImageButton(1);
assertTrue(solo.waitForView(solo.getView(R.id.butPlay)));
long mediaId = queue.get(0).getMedia().getId();
boolean playing = solo.waitForCondition(() -> {
if(currentMedia != null) {
return currentMedia.getId() == mediaId;
if(uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
} else {
return false;
}
@ -276,15 +184,19 @@ public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActi
startLocalPlaybackFromQueue();
boolean stopped = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() != first.getMedia().getId();
if (uiTestUtils.getPlaybackController(getActivity()).getStatus()
!= PlayerStatus.PLAYING) {
return true;
} else if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId()
!= first.getMedia().getId();
} else {
return false;
return true;
}
}, Timeout.getSmallTimeout());
assertTrue(stopped);
Thread.sleep(1000);
PlayerStatus status = controller.getStatus();
PlayerStatus status = uiTestUtils.getPlaybackController(getActivity()).getStatus();
assertFalse(status.equals(PlayerStatus.PLAYING));
}
@ -298,16 +210,18 @@ public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActi
startLocalPlaybackFromQueue();
boolean firstPlaying = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == first.getMedia().getId();
if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId()
== first.getMedia().getId();
} else {
return false;
}
}, Timeout.getSmallTimeout());
assertTrue(firstPlaying);
boolean secondPlaying = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == second.getMedia().getId();
if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId()
== second.getMedia().getId();
} else {
return false;
}
@ -327,8 +241,8 @@ public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActi
startLocalPlayback();
long mediaId = episodes.get(0).getMedia().getId();
boolean startedPlaying = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == mediaId;
if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
} else {
return false;
}
@ -336,14 +250,15 @@ public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActi
assertTrue(startedPlaying);
boolean stoppedPlaying = solo.waitForCondition(() -> {
return currentMedia == null || currentMedia.getId() != mediaId;
return uiTestUtils.getCurrentMedia(getActivity()) == null
|| uiTestUtils.getCurrentMedia(getActivity()).getId() != mediaId;
}, Timeout.getLargeTimeout());
assertTrue(stoppedPlaying);
startLocalPlayback();
boolean startedReplay = solo.waitForCondition(() -> {
if(currentMedia != null) {
return currentMedia.getId() == mediaId;
if(uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
} else {
return false;
}

View File

@ -1,6 +1,5 @@
package de.test.antennapod.ui;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@ -8,7 +7,6 @@ import android.preference.PreferenceManager;
import android.test.ActivityInstrumentationTestCase2;
import android.test.FlakyTest;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import com.robotium.solo.Solo;
@ -19,15 +17,12 @@ import java.util.List;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
/**
* test cases for starting and ending playback from the MainActivity and AudioPlayerActivity
@ -43,98 +38,6 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
private Context context;
private PlaybackController controller;
protected FeedMedia currentMedia;
private PlaybackController createController(Activity activity) {
return new PlaybackController(activity, false) {
@Override
public void setupGUI() {
}
@Override
public void onPositionObserverUpdate() {
}
@Override
public void onBufferStart() {
}
@Override
public void onBufferEnd() {
}
@Override
public void onBufferUpdate(float progress) {
}
@Override
public void handleError(int code) {
}
@Override
public void onReloadNotification(int code) {
}
@Override
public void onSleepTimerUpdate() {
}
@Override
public ImageButton getPlayButton() {
return null;
}
@Override
public void postStatusMsg(int msg) {
}
@Override
public void clearStatusMsg() {
}
@Override
public boolean loadMediaInfo() {
Playable playable = controller.getMedia();
if(playable == null) {
currentMedia = null;
return true;
} else if(playable instanceof FeedMedia) {
currentMedia = (FeedMedia) playable;
return true;
} else {
return false;
}
}
@Override
public void onAwaitingVideoSurface() {
}
@Override
public void onServiceQueried() {
}
@Override
public void onShutdownNotification() {
}
@Override
public void onPlaybackEnd() {
currentMedia = null;
}
@Override
public void onPlaybackSpeedChange() {
}
@Override
protected void setScreenOn(boolean enable) {
}
};
}
public PlaybackTest() {
super(MainActivity.class);
}
@ -154,9 +57,6 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
.putBoolean(UserPreferences.PREF_PAUSE_ON_HEADSET_DISCONNECT, false)
.commit();
controller = createController(getActivity());
controller.init();
solo = new Solo(getInstrumentation(), getActivity());
uiTestUtils = new UITestUtils(context);
@ -170,7 +70,6 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
@Override
public void tearDown() throws Exception {
controller.release();
solo.finishOpenedActivities();
uiTestUtils.tearDown();
@ -214,8 +113,8 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
solo.clickOnView(solo.getView(R.id.butSecondaryAction));
long mediaId = episodes.get(0).getMedia().getId();
boolean playing = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == mediaId;
if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
} else {
return false;
}
@ -225,7 +124,6 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
private void startLocalPlaybackFromQueue() {
openNavDrawer();
// if we try to just click on plain old text then
// we might wind up clicking on the fragment title and not
// the drawer element like we want.
@ -234,19 +132,20 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
View targetView = drawerView.getChildAt(QUEUE_DRAWER_LIST_INDEX);
solo.waitForView(targetView);
solo.clickOnView(targetView);
assertTrue(solo.waitForView(solo.getView(R.id.butSecondaryAction)));
final List<FeedItem> queue = DBReader.getQueue();
solo.clickOnImageButton(1);
assertTrue(solo.waitForView(solo.getView(R.id.butPlay)));
long mediaId = queue.get(0).getMedia().getId();
boolean playing = solo.waitForCondition(() -> {
if(currentMedia != null) {
return currentMedia.getId() == mediaId;
if(uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
} else {
return false;
}
}, Timeout.getSmallTimeout());
assertTrue(playing);
}
@ -269,18 +168,21 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
uiTestUtils.addLocalFeedData(true);
List<FeedItem> queue = DBReader.getQueue();
final FeedItem first = queue.get(0);
startLocalPlaybackFromQueue();
boolean stopped = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() != first.getMedia().getId();
if (uiTestUtils.getPlaybackController(getActivity()).getStatus()
!= PlayerStatus.PLAYING) {
return true;
} else if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId()
!= first.getMedia().getId();
} else {
return false;
return true;
}
}, Timeout.getSmallTimeout());
assertTrue(stopped);
Thread.sleep(1000);
PlayerStatus status = controller.getStatus();
PlayerStatus status = uiTestUtils.getPlaybackController(getActivity()).getStatus();
assertFalse(status.equals(PlayerStatus.PLAYING));
}
@ -294,16 +196,18 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
startLocalPlaybackFromQueue();
boolean firstPlaying = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == first.getMedia().getId();
if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId()
== first.getMedia().getId();
} else {
return false;
}
}, Timeout.getSmallTimeout());
assertTrue(firstPlaying);
boolean secondPlaying = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == second.getMedia().getId();
if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId()
== second.getMedia().getId();
} else {
return false;
}
@ -323,8 +227,8 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
startLocalPlayback();
long mediaId = episodes.get(0).getMedia().getId();
boolean startedPlaying = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == mediaId;
if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
} else {
return false;
}
@ -332,14 +236,15 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
assertTrue(startedPlaying);
boolean stoppedPlaying = solo.waitForCondition(() -> {
return currentMedia == null || currentMedia.getId() != mediaId;
return uiTestUtils.getCurrentMedia(getActivity()) == null
|| uiTestUtils.getCurrentMedia(getActivity()).getId() != mediaId;
}, Timeout.getLargeTimeout());
assertTrue(stoppedPlaying);
startLocalPlayback();
boolean startedReplay = solo.waitForCondition(() -> {
if(currentMedia != null) {
return currentMedia.getId() == mediaId;
if(uiTestUtils.getCurrentMedia(getActivity()) != null) {
return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
} else {
return false;
}

View File

@ -21,6 +21,7 @@ import java.util.Date;
import java.util.List;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedImage;
@ -28,6 +29,8 @@ import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.event.QueueEvent;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
import de.danoeh.antennapod.fragment.ExternalPlayerFragment;
import de.greenrobot.event.EventBus;
import de.test.antennapod.util.service.download.HTTPBin;
import de.test.antennapod.util.syndication.feedgenerator.RSS2Generator;
@ -47,6 +50,7 @@ public class UITestUtils {
public static final int NUM_ITEMS_PER_FEED = 10;
public static final int HOME_VIEW = (Build.VERSION.SDK_INT >= 11) ? android.R.id.home : R.id.home;
public static final String TEST_FILE_NAME = "3sec.mp3";
private Context context;
@ -119,7 +123,7 @@ public class UITestUtils {
}
Assert.assertFalse(mediaFile.exists());
InputStream in = context.getAssets().open("testfile.mp3");
InputStream in = context.getAssets().open(TEST_FILE_NAME);
Assert.assertNotNull(in);
FileOutputStream out = new FileOutputStream(mediaFile);
@ -218,4 +222,13 @@ public class UITestUtils {
EventDistributor.getInstance().sendFeedUpdateBroadcast();
EventBus.getDefault().post(new QueueEvent(QueueEvent.Action.ADDED_ITEMS, queue));
}
public PlaybackController getPlaybackController(MainActivity mainActivity) {
ExternalPlayerFragment fragment = (ExternalPlayerFragment)mainActivity.getSupportFragmentManager().findFragmentByTag(ExternalPlayerFragment.TAG);
return fragment.getPlaybackControllerTestingOnly();
}
public FeedMedia getCurrentMedia(MainActivity mainActivity) {
return (FeedMedia)getPlaybackController(mainActivity).getMedia();
}
}

Binary file not shown.

View File

@ -191,7 +191,7 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
}
}
externalPlayerFragment = new ExternalPlayerFragment();
transaction.replace(R.id.playerFragment, externalPlayerFragment);
transaction.replace(R.id.playerFragment, externalPlayerFragment, ExternalPlayerFragment.TAG);
transaction.commit();
checkFirstLaunch();

View File

@ -26,7 +26,7 @@ import de.danoeh.antennapod.core.util.playback.PlaybackController;
* if the PlaybackService is running
*/
public class ExternalPlayerFragment extends Fragment {
private static final String TAG = "ExternalPlayerFragment";
public static final String TAG = "ExternalPlayerFragment";
private ViewGroup fragmentLayout;
private ImageView imgvCover;
@ -230,4 +230,8 @@ public class ExternalPlayerFragment extends Fragment {
return Converter.getDurationStringLong(position) + " / "
+ Converter.getDurationStringLong(duration);
}
public PlaybackController getPlaybackControllerTestingOnly() {
return controller;
}
}