summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/ui/PreferencesTest.java
blob: 74414240f4c7b6015e5d4ba4f644515ac16bf556 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package de.test.antennapod.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;

import androidx.annotation.StringRes;
import androidx.preference.PreferenceManager;
import androidx.test.espresso.matcher.RootMatchers;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;

import org.awaitility.Awaitility;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.PreferenceActivity;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences.EnqueueLocation;
import de.danoeh.antennapod.core.storage.APCleanupAlgorithm;
import de.danoeh.antennapod.core.storage.APNullCleanupAlgorithm;
import de.danoeh.antennapod.core.storage.APQueueCleanupAlgorithm;
import de.danoeh.antennapod.core.storage.EpisodeCleanupAlgorithm;
import de.danoeh.antennapod.core.storage.ExceptFavoriteCleanupAlgorithm;
import de.danoeh.antennapod.fragment.EpisodesFragment;
import de.danoeh.antennapod.fragment.QueueFragment;
import de.danoeh.antennapod.fragment.SubscriptionFragment;
import de.test.antennapod.EspressoTestUtils;

import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.action.ViewActions.swipeDown;
import static androidx.test.espresso.action.ViewActions.swipeUp;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isChecked;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static de.test.antennapod.EspressoTestUtils.clickPreference;
import static de.test.antennapod.EspressoTestUtils.waitForView;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertTrue;

@LargeTest
public class PreferencesTest {
    private Resources res;

    @Rule
    public ActivityTestRule<PreferenceActivity> activityTestRule =
            new ActivityTestRule<>(PreferenceActivity.class,
                    false,
                    false);


    @Before
    public void setUp() {
        EspressoTestUtils.clearDatabase();
        EspressoTestUtils.clearPreferences();
        activityTestRule.launchActivity(new Intent());
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activityTestRule.getActivity());
        prefs.edit().putBoolean(UserPreferences.PREF_ENABLE_AUTODL, true).commit();

        res = activityTestRule.getActivity().getResources();
        UserPreferences.init(activityTestRule.getActivity());
    }

    @Test
    public void testSwitchTheme() {
        final int theme = UserPreferences.getTheme();
        int otherTheme;
        if (theme == de.danoeh.antennapod.core.R.style.Theme_AntennaPod_Light) {
            otherTheme = R.string.pref_theme_title_dark;
        } else {
            otherTheme = R.string.pref_theme_title_light;
        }
        clickPreference(R.string.user_interface_label);
        clickPreference(R.string.pref_set_theme_title);
        onView(withText(otherTheme)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getTheme() != theme);
    }

    @Test
    public void testSwitchThemeBack() {
        final int theme = UserPreferences.getTheme();
        int otherTheme;
        if (theme == de.danoeh.antennapod.core.R.style.Theme_AntennaPod_Light) {
            otherTheme = R.string.pref_theme_title_dark;
        } else {
            otherTheme = R.string.pref_theme_title_light;
        }
        clickPreference(R.string.user_interface_label);
        clickPreference(R.string.pref_set_theme_title);
        onView(withText(otherTheme)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getTheme() != theme);
    }

    @Test
    public void testEnablePersistentPlaybackControls() {
        final boolean persistNotify = UserPreferences.isPersistNotify();
        clickPreference(R.string.user_interface_label);
        clickPreference(R.string.pref_persistNotify_title);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> persistNotify != UserPreferences.isPersistNotify());
        clickPreference(R.string.pref_persistNotify_title);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> persistNotify == UserPreferences.isPersistNotify());
    }

    @Test
    public void testSetLockscreenButtons() {
        clickPreference(R.string.user_interface_label);
        String[] buttons = res.getStringArray(R.array.compact_notification_buttons_options);
        clickPreference(R.string.pref_compact_notification_buttons_title);
        // First uncheck checkboxes
        onView(withText(buttons[0])).perform(click());
        onView(withText(buttons[1])).perform(click());

        // Now try to check all checkboxes
        onView(withText(buttons[0])).perform(click());
        onView(withText(buttons[1])).perform(click());
        onView(withText(buttons[2])).perform(click());

        // Make sure that the third checkbox is unchecked
        onView(withText(buttons[2])).check(matches(not(isChecked())));

        String snackBarText = String.format(res.getString(
                R.string.pref_compact_notification_buttons_dialog_error), 2);
        Awaitility.await().ignoreExceptions().atMost(4000, MILLISECONDS)
                .until(() -> {
                    onView(withText(snackBarText)).check(doesNotExist());
                    return true;
                });

        onView(withText(R.string.confirm_label)).perform(click());

        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(UserPreferences::showRewindOnCompactNotification);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(UserPreferences::showFastForwardOnCompactNotification);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> !UserPreferences.showSkipOnCompactNotification());
    }

    @Test
    public void testEnqueueLocation() {
        clickPreference(R.string.playback_pref);
        doTestEnqueueLocation(R.string.enqueue_location_after_current, EnqueueLocation.AFTER_CURRENTLY_PLAYING);
        doTestEnqueueLocation(R.string.enqueue_location_front, EnqueueLocation.FRONT);
        doTestEnqueueLocation(R.string.enqueue_location_back, EnqueueLocation.BACK);
    }

    private void doTestEnqueueLocation(@StringRes int optionResId, EnqueueLocation expected) {
        clickPreference(R.string.pref_enqueue_location_title);
        onView(withText(optionResId)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> expected == UserPreferences.getEnqueueLocation());
    }

    @Test
    public void testHeadPhonesDisconnect() {
        clickPreference(R.string.playback_pref);
        final boolean pauseOnHeadsetDisconnect = UserPreferences.isPauseOnHeadsetDisconnect();
        onView(withText(R.string.pref_pauseOnHeadsetDisconnect_title)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> pauseOnHeadsetDisconnect != UserPreferences.isPauseOnHeadsetDisconnect());
        onView(withText(R.string.pref_pauseOnHeadsetDisconnect_title)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> pauseOnHeadsetDisconnect == UserPreferences.isPauseOnHeadsetDisconnect());
    }

    @Test
    public void testHeadPhonesReconnect() {
        clickPreference(R.string.playback_pref);
        if (!UserPreferences.isPauseOnHeadsetDisconnect()) {
            onView(withText(R.string.pref_pauseOnHeadsetDisconnect_title)).perform(click());
            Awaitility.await().atMost(1000, MILLISECONDS)
                    .until(UserPreferences::isPauseOnHeadsetDisconnect);
        }
        final boolean unpauseOnHeadsetReconnect = UserPreferences.isUnpauseOnHeadsetReconnect();
        onView(withText(R.string.pref_unpauseOnHeadsetReconnect_title)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> unpauseOnHeadsetReconnect != UserPreferences.isUnpauseOnHeadsetReconnect());
        onView(withText(R.string.pref_unpauseOnHeadsetReconnect_title)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> unpauseOnHeadsetReconnect == UserPreferences.isUnpauseOnHeadsetReconnect());
    }

    @Test
    public void testBluetoothReconnect() {
        clickPreference(R.string.playback_pref);
        if (!UserPreferences.isPauseOnHeadsetDisconnect()) {
            onView(withText(R.string.pref_pauseOnHeadsetDisconnect_title)).perform(click());
            Awaitility.await().atMost(1000, MILLISECONDS)
                    .until(UserPreferences::isPauseOnHeadsetDisconnect);
        }
        final boolean unpauseOnBluetoothReconnect = UserPreferences.isUnpauseOnBluetoothReconnect();
        onView(withText(R.string.pref_unpauseOnBluetoothReconnect_title)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> unpauseOnBluetoothReconnect != UserPreferences.isUnpauseOnBluetoothReconnect());
        onView(withText(R.string.pref_unpauseOnBluetoothReconnect_title)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> unpauseOnBluetoothReconnect == UserPreferences.isUnpauseOnBluetoothReconnect());
    }

    @Test
    public void testContinuousPlayback() {
        clickPreference(R.string.playback_pref);
        final boolean continuousPlayback = UserPreferences.isFollowQueue();
        clickPreference(R.string.pref_followQueue_title);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> continuousPlayback != UserPreferences.isFollowQueue());
        clickPreference(R.string.pref_followQueue_title);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> continuousPlayback == UserPreferences.isFollowQueue());
    }

    @Test
    public void testAutoDelete() {
        clickPreference(R.string.storage_pref);
        final boolean autoDelete = UserPreferences.isAutoDelete();
        onView(withText(R.string.pref_auto_delete_title)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> autoDelete != UserPreferences.isAutoDelete());
        onView(withText(R.string.pref_auto_delete_title)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> autoDelete == UserPreferences.isAutoDelete());
    }

    @Test
    public void testPlaybackSpeeds() {
        clickPreference(R.string.playback_pref);
        clickPreference(R.string.playback_speed);
        onView(isRoot()).perform(waitForView(withText("1.25"), 1000));
        onView(withText("1.25")).check(matches(isDisplayed()));
    }

    @Test
    public void testPauseForInterruptions() {
        clickPreference(R.string.playback_pref);
        final boolean pauseForFocusLoss = UserPreferences.shouldPauseForFocusLoss();
        clickPreference(R.string.pref_pausePlaybackForFocusLoss_title);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> pauseForFocusLoss != UserPreferences.shouldPauseForFocusLoss());
        clickPreference(R.string.pref_pausePlaybackForFocusLoss_title);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> pauseForFocusLoss == UserPreferences.shouldPauseForFocusLoss());
    }

    @Test
    public void testDisableUpdateInterval() {
        clickPreference(R.string.network_pref);
        clickPreference(R.string.feed_refresh_title);
        onView(withText(R.string.feed_refresh_never)).perform(click());
        onView(withId(R.id.disableRadioButton)).perform(click());
        onView(withText(R.string.confirm_label)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getUpdateInterval() == 0);
    }

    @Test
    public void testSetUpdateInterval() {
        clickPreference(R.string.network_pref);
        clickPreference(R.string.feed_refresh_title);
        onView(withId(R.id.intervalRadioButton)).perform(click());
        onView(withId(R.id.spinner)).perform(click());
        int position = 1; // an arbitrary position
        onData(anything()).inRoot(RootMatchers.isPlatformPopup()).atPosition(position).perform(click());
        onView(withText(R.string.confirm_label)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getUpdateInterval() == TimeUnit.HOURS.toMillis(2));
    }

    @Test
    public void testSetSequentialDownload() {
        clickPreference(R.string.network_pref);
        clickPreference(R.string.pref_parallel_downloads_title);
        onView(isRoot()).perform(waitForView(withClassName(endsWith("EditText")), 1000));
        onView(withClassName(endsWith("EditText"))).perform(replaceText("1"));
        onView(withText(android.R.string.ok)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getParallelDownloads() == 1);
    }

    @Test
    public void testSetParallelDownloads() {
        clickPreference(R.string.network_pref);
        clickPreference(R.string.pref_parallel_downloads_title);
        onView(isRoot()).perform(waitForView(withClassName(endsWith("EditText")), 1000));
        onView(withClassName(endsWith("EditText"))).perform(replaceText("10"));
        onView(withText(android.R.string.ok)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getParallelDownloads() == 10);
    }

    @Test
    public void testSetParallelDownloadsInvalidInput() {
        clickPreference(R.string.network_pref);
        clickPreference(R.string.pref_parallel_downloads_title);
        onView(isRoot()).perform(waitForView(withClassName(endsWith("EditText")), 1000));
        onView(withClassName(endsWith("EditText"))).perform(replaceText("0"));
        onView(withClassName(endsWith("EditText"))).check(matches(withText("")));
        onView(withClassName(endsWith("EditText"))).perform(replaceText("100"));
        onView(withClassName(endsWith("EditText"))).check(matches(withText("")));
    }

    @Test
    public void testSetEpisodeCache() {
        String[] entries = res.getStringArray(R.array.episode_cache_size_entries);
        String[] values = res.getStringArray(R.array.episode_cache_size_values);
        String entry = entries[entries.length / 2];
        final int value = Integer.parseInt(values[values.length / 2]);
        clickPreference(R.string.network_pref);
        clickPreference(R.string.pref_automatic_download_title);
        clickPreference(R.string.pref_episode_cache_title);
        onView(isRoot()).perform(waitForView(withText(entry), 1000));
        onView(withText(entry)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getEpisodeCacheSize() == value);
    }

    @Test
    public void testSetEpisodeCacheMin() {
        String[] entries = res.getStringArray(R.array.episode_cache_size_entries);
        String[] values = res.getStringArray(R.array.episode_cache_size_values);
        String minEntry = entries[0];
        final int minValue = Integer.parseInt(values[0]);

        clickPreference(R.string.network_pref);
        clickPreference(R.string.pref_automatic_download_title);
        clickPreference(R.string.pref_episode_cache_title);
        onView(withId(R.id.select_dialog_listview)).perform(swipeDown());
        onView(withText(minEntry)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getEpisodeCacheSize() == minValue);
    }

    @Test
    public void testSetEpisodeCacheMax() {
        String[] entries = res.getStringArray(R.array.episode_cache_size_entries);
        String[] values = res.getStringArray(R.array.episode_cache_size_values);
        String maxEntry = entries[entries.length - 1];
        final int maxValue = Integer.parseInt(values[values.length - 1]);
        onView(withText(R.string.network_pref)).perform(click());
        onView(withText(R.string.pref_automatic_download_title)).perform(click());
        onView(withText(R.string.pref_episode_cache_title)).perform(click());
        onView(withId(R.id.select_dialog_listview)).perform(swipeUp());
        onView(withText(maxEntry)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getEpisodeCacheSize() == maxValue);
    }

    @Test
    public void testAutomaticDownload() {
        final boolean automaticDownload = UserPreferences.isEnableAutodownload();
        clickPreference(R.string.network_pref);
        clickPreference(R.string.pref_automatic_download_title);
        clickPreference(R.string.pref_automatic_download_title);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> automaticDownload != UserPreferences.isEnableAutodownload());
        if (!UserPreferences.isEnableAutodownload()) {
            clickPreference(R.string.pref_automatic_download_title);
        }
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(UserPreferences::isEnableAutodownload);
        final boolean enableAutodownloadOnBattery = UserPreferences.isEnableAutodownloadOnBattery();
        clickPreference(R.string.pref_automatic_download_on_battery_title);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> enableAutodownloadOnBattery != UserPreferences.isEnableAutodownloadOnBattery());
        clickPreference(R.string.pref_automatic_download_on_battery_title);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> enableAutodownloadOnBattery == UserPreferences.isEnableAutodownloadOnBattery());
    }

    @Test
    public void testEpisodeCleanupFavoriteOnly() {
        clickPreference(R.string.network_pref);
        onView(withText(R.string.pref_automatic_download_title)).perform(click());
        onView(withText(R.string.pref_episode_cleanup_title)).perform(click());
        onView(isRoot()).perform(waitForView(withText(R.string.episode_cleanup_except_favorite_removal), 1000));
        onView(withText(R.string.episode_cleanup_except_favorite_removal)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getEpisodeCleanupAlgorithm() instanceof ExceptFavoriteCleanupAlgorithm);
    }

    @Test
    public void testEpisodeCleanupQueueOnly() {
        clickPreference(R.string.network_pref);
        onView(withText(R.string.pref_automatic_download_title)).perform(click());
        onView(withText(R.string.pref_episode_cleanup_title)).perform(click());
        onView(isRoot()).perform(waitForView(withText(R.string.episode_cleanup_queue_removal), 1000));
        onView(withText(R.string.episode_cleanup_queue_removal)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getEpisodeCleanupAlgorithm() instanceof APQueueCleanupAlgorithm);
    }

    @Test
    public void testEpisodeCleanupNeverAlg() {
        clickPreference(R.string.network_pref);
        onView(withText(R.string.pref_automatic_download_title)).perform(click());
        onView(withText(R.string.pref_episode_cleanup_title)).perform(click());
        onView(withId(R.id.select_dialog_listview)).perform(swipeUp());
        onView(withText(R.string.episode_cleanup_never)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getEpisodeCleanupAlgorithm() instanceof APNullCleanupAlgorithm);
    }

    @Test
    public void testEpisodeCleanupClassic() {
        clickPreference(R.string.network_pref);
        onView(withText(R.string.pref_automatic_download_title)).perform(click());
        onView(withText(R.string.pref_episode_cleanup_title)).perform(click());
        onView(isRoot()).perform(waitForView(withText(R.string.episode_cleanup_after_listening), 1000));
        onView(withText(R.string.episode_cleanup_after_listening)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> {
                    EpisodeCleanupAlgorithm alg = UserPreferences.getEpisodeCleanupAlgorithm();
                    if (alg instanceof APCleanupAlgorithm) {
                        APCleanupAlgorithm cleanupAlg = (APCleanupAlgorithm) alg;
                        return cleanupAlg.getNumberOfHoursAfterPlayback() == 0;
                    }
                    return false;
                });
    }

    @Test
    public void testEpisodeCleanupNumDays() {
        clickPreference(R.string.network_pref);
        clickPreference(R.string.pref_automatic_download_title);
        clickPreference(R.string.pref_episode_cleanup_title);
        String search = res.getQuantityString(R.plurals.episode_cleanup_days_after_listening, 3, 3);
        onView(isRoot()).perform(waitForView(withText(search), 1000));
        onView(withText(search)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> {
                    EpisodeCleanupAlgorithm alg = UserPreferences.getEpisodeCleanupAlgorithm();
                    if (alg instanceof APCleanupAlgorithm) {
                        APCleanupAlgorithm cleanupAlg = (APCleanupAlgorithm) alg;
                        return cleanupAlg.getNumberOfHoursAfterPlayback() == 72; // 5 days
                    }
                    return false;
                });
    }

    @Test
    public void testRewindChange() {
        int seconds = UserPreferences.getRewindSecs();
        int[] deltas = res.getIntArray(R.array.seek_delta_values);

        clickPreference(R.string.playback_pref);
        clickPreference(R.string.pref_rewind);

        int currentIndex = Arrays.binarySearch(deltas, seconds);
        assertTrue(currentIndex >= 0 && currentIndex < deltas.length);  // found?

        // Find next value (wrapping around to next)
        int newIndex = (currentIndex + 1) % deltas.length;
        onView(withText(deltas[newIndex] + " seconds")).perform(click());
        onView(withText("Confirm")).perform(click());

        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getRewindSecs() == deltas[newIndex]);
    }

    @Test
    public void testFastForwardChange() {
        clickPreference(R.string.playback_pref);
        for (int i = 2; i > 0; i--) { // repeat twice to catch any error where fastforward is tracking rewind
            int seconds = UserPreferences.getFastForwardSecs();
            int[] deltas = res.getIntArray(R.array.seek_delta_values);

            clickPreference(R.string.pref_fast_forward);

            int currentIndex = Arrays.binarySearch(deltas, seconds);
            assertTrue(currentIndex >= 0 && currentIndex < deltas.length);  // found?

            // Find next value (wrapping around to next)
            int newIndex = (currentIndex + 1) % deltas.length;

            onView(withText(deltas[newIndex] + " seconds")).perform(click());
            onView(withText("Confirm")).perform(click());

            Awaitility.await().atMost(1000, MILLISECONDS)
                    .until(() -> UserPreferences.getFastForwardSecs() == deltas[newIndex]);
        }
    }

    @Test
    public void testBackButtonBehaviorGoToPageSelector() {
        clickPreference(R.string.user_interface_label);
        clickPreference(R.string.pref_back_button_behavior_title);
        onView(withText(R.string.back_button_go_to_page)).perform(click());
        onView(withText(R.string.queue_label)).perform(click());
        onView(withText(R.string.confirm_label)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getBackButtonBehavior() == UserPreferences.BackButtonBehavior.GO_TO_PAGE);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getBackButtonGoToPage().equals(QueueFragment.TAG));
        clickPreference(R.string.pref_back_button_behavior_title);
        onView(withText(R.string.back_button_go_to_page)).perform(click());
        onView(withText(R.string.episodes_label)).perform(click());
        onView(withText(R.string.confirm_label)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getBackButtonBehavior() == UserPreferences.BackButtonBehavior.GO_TO_PAGE);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getBackButtonGoToPage().equals(EpisodesFragment.TAG));
        clickPreference(R.string.pref_back_button_behavior_title);
        onView(withText(R.string.back_button_go_to_page)).perform(click());
        onView(withText(R.string.subscriptions_label)).perform(click());
        onView(withText(R.string.confirm_label)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getBackButtonBehavior() == UserPreferences.BackButtonBehavior.GO_TO_PAGE);
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> UserPreferences.getBackButtonGoToPage().equals(SubscriptionFragment.TAG));
    }

    @Test
    public void testDeleteRemovesFromQueue() {
        clickPreference(R.string.storage_pref);
        if (!UserPreferences.shouldDeleteRemoveFromQueue()) {
            clickPreference(R.string.pref_delete_removes_from_queue_title);
            Awaitility.await().atMost(1000, MILLISECONDS)
                    .until(UserPreferences::shouldDeleteRemoveFromQueue);
        }
        final boolean deleteRemovesFromQueue = UserPreferences.shouldDeleteRemoveFromQueue();
        onView(withText(R.string.pref_delete_removes_from_queue_title)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> deleteRemovesFromQueue != UserPreferences.shouldDeleteRemoveFromQueue());
        onView(withText(R.string.pref_delete_removes_from_queue_title)).perform(click());
        Awaitility.await().atMost(1000, MILLISECONDS)
                .until(() -> deleteRemovesFromQueue == UserPreferences.shouldDeleteRemoveFromQueue());
    }
}