summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/ui/PlaybackSonicTest.java
blob: 55ed998bb387716f52025f0ec54306d5fe9349b4 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package de.test.antennapod.ui;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.test.ActivityInstrumentationTestCase2;
import android.test.FlakyTest;
import android.view.View;
import android.widget.ListView;

import com.robotium.solo.Solo;
import com.robotium.solo.Timeout;

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.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;

/**
 * test cases for starting and ending playback from the MainActivity and AudioPlayerActivity
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class PlaybackSonicTest extends ActivityInstrumentationTestCase2<MainActivity> {

    private static final String TAG = PlaybackTest.class.getSimpleName();
    private static final int EPISODES_DRAWER_LIST_INDEX = 1;
    private static final int QUEUE_DRAWER_LIST_INDEX = 0;

    private Solo solo;
    private UITestUtils uiTestUtils;

    private Context context;

    public PlaybackSonicTest() {
        super(MainActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();

        context = getInstrumentation().getTargetContext();

        PodDBAdapter.init(context);
        PodDBAdapter.deleteDatabase();

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit()
                .clear()
                .putBoolean(UserPreferences.PREF_UNPAUSE_ON_HEADSET_RECONNECT, false)
                .putBoolean(UserPreferences.PREF_PAUSE_ON_HEADSET_DISCONNECT, false)
                .putString(UserPreferences.PREF_MEDIA_PLAYER, "sonic")
                .commit();

        solo = new Solo(getInstrumentation(), getActivity());

        uiTestUtils = new UITestUtils(context);
        uiTestUtils.setup();

        // create database
        PodDBAdapter adapter = PodDBAdapter.getInstance();
        adapter.open();
        adapter.close();
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
        uiTestUtils.tearDown();

        // shut down playback service
        skipEpisode();
        context.sendBroadcast(new Intent(PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));

        super.tearDown();
    }

    private void openNavDrawer() {
        solo.clickOnImageButton(0);
        getInstrumentation().waitForIdleSync();
    }

    private void setContinuousPlaybackPreference(boolean value) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putBoolean(UserPreferences.PREF_FOLLOW_QUEUE, value).commit();
    }

    private void skipEpisode() {
        Intent skipIntent = new Intent(PlaybackService.ACTION_SKIP_CURRENT_EPISODE);
        context.sendBroadcast(skipIntent);
    }

    private void startLocalPlayback() {
        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.
        ListView drawerView = (ListView)solo.getView(R.id.nav_list);
        // this should be 'Episodes'
        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)));

        solo.clickOnView(solo.getView(R.id.butSecondaryAction));
        long mediaId = episodes.get(0).getMedia().getId();
        boolean playing = solo.waitForCondition(() -> {
            if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
                return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
            } else {
                return false;
            }
        }, Timeout.getSmallTimeout());
        assertTrue(playing);
    }

    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.
        ListView drawerView = (ListView)solo.getView(R.id.nav_list);
        // 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(uiTestUtils.getCurrentMedia(getActivity()) != null) {
                return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
            } else {
                return false;
            }
        }, Timeout.getSmallTimeout());
        assertTrue(playing);
    }

    public void testStartLocal() throws Exception {
        uiTestUtils.addLocalFeedData(true);
        DBWriter.clearQueue().get();
        startLocalPlayback();
    }

    public void testContinousPlaybackOffSingleEpisode() throws Exception {
        setContinuousPlaybackPreference(false);
        uiTestUtils.addLocalFeedData(true);
        DBWriter.clearQueue().get();
        startLocalPlayback();
    }

    @FlakyTest(tolerance = 3)
    public void testContinousPlaybackOffMultipleEpisodes() throws Exception {
        setContinuousPlaybackPreference(false);
        uiTestUtils.addLocalFeedData(true);
        List<FeedItem> queue = DBReader.getQueue();
        final FeedItem first = queue.get(0);

        startLocalPlaybackFromQueue();
        boolean stopped = solo.waitForCondition(() -> {
            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 true;
            }
        }, Timeout.getSmallTimeout());
        assertTrue(stopped);
        Thread.sleep(1000);
        PlayerStatus status = uiTestUtils.getPlaybackController(getActivity()).getStatus();
        assertFalse(status.equals(PlayerStatus.PLAYING));
    }

    @FlakyTest(tolerance = 3)
    public void testContinuousPlaybackOnMultipleEpisodes() throws Exception {
        setContinuousPlaybackPreference(true);
        uiTestUtils.addLocalFeedData(true);
        List<FeedItem> queue = DBReader.getQueue();
        final FeedItem first = queue.get(0);
        final FeedItem second = queue.get(1);

        startLocalPlaybackFromQueue();
        boolean firstPlaying = solo.waitForCondition(() -> {
            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 (uiTestUtils.getCurrentMedia(getActivity()) != null) {
                return uiTestUtils.getCurrentMedia(getActivity()).getId()
                        == second.getMedia().getId();
            } else {
                return false;
            }
        }, Timeout.getLargeTimeout());
        assertTrue(secondPlaying);
    }

    /**
     * Check if an episode can be played twice without problems.
     */
    private void replayEpisodeCheck(boolean followQueue) throws Exception {
        setContinuousPlaybackPreference(followQueue);
        uiTestUtils.addLocalFeedData(true);
        DBWriter.clearQueue().get();
        final List<FeedItem> episodes = DBReader.getRecentlyPublishedEpisodes(10);

        startLocalPlayback();
        long mediaId = episodes.get(0).getMedia().getId();
        boolean startedPlaying = solo.waitForCondition(() -> {
            if (uiTestUtils.getCurrentMedia(getActivity()) != null) {
                return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
            } else {
                return false;
            }
        }, Timeout.getSmallTimeout());
        assertTrue(startedPlaying);

        boolean stoppedPlaying = solo.waitForCondition(() ->
                uiTestUtils.getCurrentMedia(getActivity()) == null
                    || uiTestUtils.getCurrentMedia(getActivity()).getId() != mediaId
        , Timeout.getLargeTimeout());
        assertTrue(stoppedPlaying);

        startLocalPlayback();
        boolean startedReplay = solo.waitForCondition(() -> {
            if(uiTestUtils.getCurrentMedia(getActivity()) != null) {
                return uiTestUtils.getCurrentMedia(getActivity()).getId() == mediaId;
            } else {
                return false;
            }
        }, Timeout.getLargeTimeout());
        assertTrue(startedReplay);
    }

    public void testReplayEpisodeContinuousPlaybackOn() throws Exception {
        replayEpisodeCheck(true);
    }

    public void testReplayEpisodeContinuousPlaybackOff() throws Exception {
        replayEpisodeCheck(false);
    }


}