|
| 1 | +package org.schabi.newpipe.ui.components.menu |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.annotation.StringRes |
| 5 | +import androidx.preference.PreferenceManager |
| 6 | +import androidx.test.core.app.ApplicationProvider |
| 7 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 8 | +import org.junit.Assert.assertEquals |
| 9 | +import org.junit.Test |
| 10 | +import org.junit.runner.RunWith |
| 11 | +import org.schabi.newpipe.R |
| 12 | +import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Background |
| 13 | +import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Enqueue |
| 14 | +import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.EnqueueNext |
| 15 | +import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.MarkAsWatched |
| 16 | +import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.PlayWithKodi |
| 17 | +import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.ShowChannelDetails |
| 18 | +import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.ShowDetails |
| 19 | + |
| 20 | +@RunWith(AndroidJUnit4::class) |
| 21 | +class LongPressMenuSettingsTest { |
| 22 | + |
| 23 | + val ctx: Context = ApplicationProvider.getApplicationContext<Context>() |
| 24 | + |
| 25 | + private fun putBooleanInPrefs(@StringRes key: Int, value: Boolean) { |
| 26 | + PreferenceManager.getDefaultSharedPreferences(ctx) |
| 27 | + .edit().putBoolean(ctx.getString(key), value).apply() |
| 28 | + } |
| 29 | + |
| 30 | + private fun putStringInPrefs(@StringRes key: Int, value: String) { |
| 31 | + PreferenceManager.getDefaultSharedPreferences(ctx) |
| 32 | + .edit().putString(ctx.getString(key), value).apply() |
| 33 | + } |
| 34 | + |
| 35 | + private fun clearPrefs() { |
| 36 | + PreferenceManager.getDefaultSharedPreferences(ctx) |
| 37 | + .edit().clear().apply() |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun testStoringAndLoadingPreservesIsHeaderEnabled() { |
| 42 | + for (enabled in arrayOf(false, true)) { |
| 43 | + storeIsHeaderEnabledToSettings(ctx, enabled) |
| 44 | + assertEquals(enabled, loadIsHeaderEnabledFromSettings(ctx)) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun testStoringAndLoadingPreservesActionArrangement() { |
| 50 | + for (actions in listOf( |
| 51 | + listOf(), |
| 52 | + LongPressAction.Type.entries.toList(), |
| 53 | + listOf(Enqueue, EnqueueNext, MarkAsWatched, ShowChannelDetails), |
| 54 | + listOf(PlayWithKodi) |
| 55 | + )) { |
| 56 | + storeLongPressActionArrangementToSettings(ctx, actions) |
| 57 | + assertEquals(actions, loadLongPressActionArrangementFromSettings(ctx)) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun testLoadingActionArrangementUnset() { |
| 63 | + clearPrefs() |
| 64 | + assertEquals(getDefaultEnabledLongPressActions(ctx), loadLongPressActionArrangementFromSettings(ctx)) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun testLoadingActionArrangementInvalid() { |
| 69 | + putStringInPrefs(R.string.long_press_menu_action_arrangement_key, "0,1,whatever,3") |
| 70 | + assertEquals(getDefaultEnabledLongPressActions(ctx), loadLongPressActionArrangementFromSettings(ctx)) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun testLoadingActionArrangementEmpty() { |
| 75 | + putStringInPrefs(R.string.long_press_menu_action_arrangement_key, "") |
| 76 | + assertEquals(listOf<LongPressAction.Type>(), loadLongPressActionArrangementFromSettings(ctx)) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun testLoadingActionArrangementDuplicates() { |
| 81 | + putStringInPrefs(R.string.long_press_menu_action_arrangement_key, "0,1,0,3,2,3,3,3,0") |
| 82 | + assertEquals( |
| 83 | + // deduplicates items but retains order |
| 84 | + listOf(ShowDetails, Enqueue, Background, EnqueueNext), |
| 85 | + loadLongPressActionArrangementFromSettings(ctx) |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun testDefaultActionsIncludeKodiIffShowKodiEnabled() { |
| 91 | + for (enabled in arrayOf(false, true)) { |
| 92 | + putBooleanInPrefs(R.string.show_play_with_kodi_key, enabled) |
| 93 | + val actions = getDefaultEnabledLongPressActions(ctx) |
| 94 | + assertEquals(enabled, actions.contains(PlayWithKodi)) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun testAddOrRemoveKodiLongPressAction() { |
| 100 | + for (enabled in arrayOf(false, true)) { |
| 101 | + putBooleanInPrefs(R.string.show_play_with_kodi_key, enabled) |
| 102 | + for (actions in listOf(listOf(Enqueue), listOf(Enqueue, PlayWithKodi))) { |
| 103 | + storeLongPressActionArrangementToSettings(ctx, actions) |
| 104 | + addOrRemoveKodiLongPressAction(ctx) |
| 105 | + val newActions = getDefaultEnabledLongPressActions(ctx) |
| 106 | + assertEquals(enabled, newActions.contains(PlayWithKodi)) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments