Merge pull request #3653 from ByteHamster/test-playback-speed

Added test for changing playback speed
This commit is contained in:
H. Lehmann 2019-12-01 23:02:07 +01:00 committed by GitHub
commit 55453c543f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 129 additions and 6 deletions

Binary file not shown.

View File

@ -81,7 +81,7 @@ public class PlaybackServiceMediaPlayerTest {
assertTrue(cacheDir.canWrite());
assertTrue(cacheDir.canRead());
if (!dest.exists()) {
InputStream i = InstrumentationRegistry.getContext().getAssets().open("testfile.mp3");
InputStream i = InstrumentationRegistry.getContext().getAssets().open("3sec.mp3");
OutputStream o = new FileOutputStream(new File(cacheDir, PLAYABLE_DEST_URL));
IOUtils.copy(i, o);
o.flush();

View File

@ -0,0 +1,121 @@
package de.test.antennapod.ui;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.AudioplayerActivity;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
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.fragment.QueueFragment;
import de.test.antennapod.EspressoTestUtils;
import org.awaitility.Awaitility;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static de.test.antennapod.EspressoTestUtils.waitForView;
/**
* User interface tests for changing the playback speed.
*/
@RunWith(AndroidJUnit4.class)
public class SpeedChangeTest {
@Rule
public ActivityTestRule<AudioplayerActivity> activityRule
= new ActivityTestRule<>(AudioplayerActivity.class, false, false);
private UITestUtils uiTestUtils;
private String[] availableSpeeds;
@Before
public void setUp() throws Exception {
EspressoTestUtils.clearPreferences();
EspressoTestUtils.makeNotFirstRun();
EspressoTestUtils.clearDatabase();
EspressoTestUtils.setLastNavFragment(QueueFragment.TAG);
Context context = getInstrumentation().getTargetContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putBoolean(UserPreferences.PREF_FOLLOW_QUEUE, true).commit();
uiTestUtils = new UITestUtils(context);
uiTestUtils.setup();
uiTestUtils.setMediaFileName("30sec.mp3");
uiTestUtils.addLocalFeedData(true);
List<FeedItem> queue = DBReader.getQueue();
PlaybackPreferences.writeMediaPlaying(queue.get(0).getMedia(), PlayerStatus.PAUSED, false);
UserPreferences.setPlaybackSpeedArray(new String[] {"1.00", "2.00", "3.00"});
availableSpeeds = UserPreferences.getPlaybackSpeedArray();
context.sendBroadcast(new Intent(PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
Awaitility.await().until(() -> !PlaybackService.isRunning);
activityRule.launchActivity(new Intent());
}
@After
public void tearDown() throws Exception {
uiTestUtils.tearDown();
}
@Test
public void testChangeSpeedServiceNotRunning() {
clickThroughSpeeds();
}
@Test
public void testChangeSpeedPlaying() {
onView(isRoot()).perform(waitForView(withId(R.id.butPlay), 1000));
onView(withId(R.id.butPlay)).perform(click());
Awaitility.await().atMost(1, TimeUnit.SECONDS).until(()
-> activityRule.getActivity().getPlaybackController().getStatus() == PlayerStatus.PLAYING);
clickThroughSpeeds();
}
@Test
public void testChangeSpeedPaused() {
onView(isRoot()).perform(waitForView(withId(R.id.butPlay), 1000));
onView(withId(R.id.butPlay)).perform(click());
Awaitility.await().atMost(1, TimeUnit.SECONDS).until(()
-> activityRule.getActivity().getPlaybackController().getStatus() == PlayerStatus.PLAYING);
onView(withId(R.id.butPlay)).perform(click());
Awaitility.await().atMost(1, TimeUnit.SECONDS).until(()
-> activityRule.getActivity().getPlaybackController().getStatus() == PlayerStatus.PAUSED);
clickThroughSpeeds();
}
private void clickThroughSpeeds() {
onView(isRoot()).perform(waitForView(withText(availableSpeeds[0]), 1000));
onView(withId(R.id.txtvPlaybackSpeed)).check(matches(withText(availableSpeeds[0])));
onView(withId(R.id.butPlaybackSpeed)).perform(click());
onView(isRoot()).perform(waitForView(withText(availableSpeeds[1]), 1000));
onView(withId(R.id.txtvPlaybackSpeed)).check(matches(withText(availableSpeeds[1])));
onView(withId(R.id.butPlaybackSpeed)).perform(click());
onView(isRoot()).perform(waitForView(withText(availableSpeeds[2]), 1000));
onView(withId(R.id.txtvPlaybackSpeed)).check(matches(withText(availableSpeeds[2])));
onView(withId(R.id.butPlaybackSpeed)).perform(click());
onView(isRoot()).perform(waitForView(withText(availableSpeeds[0]), 1000));
onView(withId(R.id.txtvPlaybackSpeed)).check(matches(withText(availableSpeeds[0])));
}
}

View File

@ -41,9 +41,7 @@ public class UITestUtils {
private static final int NUM_FEEDS = 5;
private static final int NUM_ITEMS_PER_FEED = 10;
private static final String TEST_FILE_NAME = "3sec.mp3";
private String testFileName = "3sec.mp3";
private final Context context;
private final HTTPBin server = new HTTPBin();
private File destDir;
@ -109,12 +107,12 @@ public class UITestUtils {
private File newMediaFile(String name) throws IOException {
File mediaFile = new File(hostedMediaDir, name);
if(mediaFile.exists()) {
if (mediaFile.exists()) {
mediaFile.delete();
}
Assert.assertFalse(mediaFile.exists());
InputStream in = context.getAssets().open(TEST_FILE_NAME);
InputStream in = context.getAssets().open(testFileName);
Assert.assertNotNull(in);
FileOutputStream out = new FileOutputStream(mediaFile);
@ -213,4 +211,8 @@ public class UITestUtils {
public FeedMedia getCurrentMedia(MainActivity mainActivity) {
return (FeedMedia)getPlaybackController(mainActivity).getMedia();
}
public void setMediaFileName(String filename) {
testFileName = filename;
}
}

Binary file not shown.

Binary file not shown.