summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/ui/SpeedChangeTest.java
blob: ac58870694bc27d193dcbe7ca80c2350a9e5e602 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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.ext.junit.runners.AndroidJUnit4;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
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.PlayerStatus;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
import de.danoeh.antennapod.fragment.ExternalPlayerFragment;
import de.danoeh.antennapod.fragment.QueueFragment;
import de.test.antennapod.EspressoTestUtils;
import de.test.antennapod.IgnoreOnCi;
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.Arrays;
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;
import static de.test.antennapod.NthMatcher.first;
import static org.hamcrest.Matchers.allOf;

/**
 * User interface tests for changing the playback speed.
 */
@RunWith(AndroidJUnit4.class)
@IgnoreOnCi
public class SpeedChangeTest {

    @Rule
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class, false, false);
    private UITestUtils uiTestUtils;
    private String[] availableSpeeds;
    private PlaybackController controller;

    @Before
    public void setUp() throws Exception {
        EspressoTestUtils.clearPreferences();
        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);
        availableSpeeds = new String[] {"1.00", "2.00", "3.00"};
        UserPreferences.setPlaybackSpeedArray(Arrays.asList(1.0f, 2.0f, 3.0f));

        EspressoTestUtils.tryKillPlaybackService();
        activityRule.launchActivity(new Intent().putExtra(MainActivity.EXTRA_OPEN_PLAYER, true));
        controller = new PlaybackController(activityRule.getActivity());
        controller.init();
        controller.getMedia(); // To load media
    }

    @After
    public void tearDown() throws Exception {
        uiTestUtils.tearDown();
        controller.release();
    }

    @Test
    public void testChangeSpeedServiceNotRunning() {
        clickThroughSpeeds();
    }

    @Test
    public void testChangeSpeedPlaying() {
        onView(isRoot()).perform(waitForView(withId(R.id.butPlay), 1000));
        controller.playPause();
        Awaitility.await().atMost(5, TimeUnit.SECONDS).until(()
                -> controller.getStatus() == PlayerStatus.PLAYING);
        clickThroughSpeeds();
    }

    @Test
    public void testChangeSpeedPaused() {
        onView(isRoot()).perform(waitForView(withId(R.id.butPlay), 1000));
        controller.playPause();
        Awaitility.await().atMost(5, TimeUnit.SECONDS).until(()
                -> controller.getStatus() == PlayerStatus.PLAYING);
        controller.playPause();
        Awaitility.await().atMost(5, TimeUnit.SECONDS).until(()
                -> controller.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])));
    }
}