Add tests for LongPressMenuSettings
This commit is contained in:
parent
3ee031efb9
commit
35e673a5ce
@ -0,0 +1,110 @@
|
||||
package org.schabi.newpipe.ui.components.menu
|
||||
|
||||
import android.content.Context
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.schabi.newpipe.R
|
||||
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Background
|
||||
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Enqueue
|
||||
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.EnqueueNext
|
||||
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.MarkAsWatched
|
||||
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.PlayWithKodi
|
||||
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.ShowChannelDetails
|
||||
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.ShowDetails
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class LongPressMenuSettingsTest {
|
||||
|
||||
val ctx: Context = ApplicationProvider.getApplicationContext<Context>()
|
||||
|
||||
private fun putBooleanInPrefs(@StringRes key: Int, value: Boolean) {
|
||||
PreferenceManager.getDefaultSharedPreferences(ctx)
|
||||
.edit().putBoolean(ctx.getString(key), value).apply()
|
||||
}
|
||||
|
||||
private fun putStringInPrefs(@StringRes key: Int, value: String) {
|
||||
PreferenceManager.getDefaultSharedPreferences(ctx)
|
||||
.edit().putString(ctx.getString(key), value).apply()
|
||||
}
|
||||
|
||||
private fun clearPrefs() {
|
||||
PreferenceManager.getDefaultSharedPreferences(ctx)
|
||||
.edit().clear().apply()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStoringAndLoadingPreservesIsHeaderEnabled() {
|
||||
for (enabled in arrayOf(false, true)) {
|
||||
storeIsHeaderEnabledToSettings(ctx, enabled)
|
||||
assertEquals(enabled, loadIsHeaderEnabledFromSettings(ctx))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStoringAndLoadingPreservesActionArrangement() {
|
||||
for (actions in listOf(
|
||||
listOf(),
|
||||
LongPressAction.Type.entries.toList(),
|
||||
listOf(Enqueue, EnqueueNext, MarkAsWatched, ShowChannelDetails),
|
||||
listOf(PlayWithKodi)
|
||||
)) {
|
||||
storeLongPressActionArrangementToSettings(ctx, actions)
|
||||
assertEquals(actions, loadLongPressActionArrangementFromSettings(ctx))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadingActionArrangementUnset() {
|
||||
clearPrefs()
|
||||
assertEquals(getDefaultEnabledLongPressActions(ctx), loadLongPressActionArrangementFromSettings(ctx))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadingActionArrangementInvalid() {
|
||||
putStringInPrefs(R.string.long_press_menu_action_arrangement_key, "0,1,whatever,3")
|
||||
assertEquals(getDefaultEnabledLongPressActions(ctx), loadLongPressActionArrangementFromSettings(ctx))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadingActionArrangementEmpty() {
|
||||
putStringInPrefs(R.string.long_press_menu_action_arrangement_key, "")
|
||||
assertEquals(listOf<LongPressAction.Type>(), loadLongPressActionArrangementFromSettings(ctx))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadingActionArrangementDuplicates() {
|
||||
putStringInPrefs(R.string.long_press_menu_action_arrangement_key, "0,1,0,3,2,3,3,3,0")
|
||||
assertEquals(
|
||||
// deduplicates items but retains order
|
||||
listOf(ShowDetails, Enqueue, Background, EnqueueNext),
|
||||
loadLongPressActionArrangementFromSettings(ctx)
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDefaultActionsIncludeKodiIffShowKodiEnabled() {
|
||||
for (enabled in arrayOf(false, true)) {
|
||||
putBooleanInPrefs(R.string.show_play_with_kodi_key, enabled)
|
||||
val actions = getDefaultEnabledLongPressActions(ctx)
|
||||
assertEquals(enabled, actions.contains(PlayWithKodi))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddOrRemoveKodiLongPressAction() {
|
||||
for (enabled in arrayOf(false, true)) {
|
||||
putBooleanInPrefs(R.string.show_play_with_kodi_key, enabled)
|
||||
for (actions in listOf(listOf(Enqueue), listOf(Enqueue, PlayWithKodi))) {
|
||||
storeLongPressActionArrangementToSettings(ctx, actions)
|
||||
addOrRemoveKodiLongPressAction(ctx)
|
||||
val newActions = getDefaultEnabledLongPressActions(ctx)
|
||||
assertEquals(enabled, newActions.contains(PlayWithKodi))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -64,17 +64,19 @@ fun getDefaultEnabledLongPressActions(context: Context): List<LongPressAction.Ty
|
||||
|
||||
fun loadLongPressActionArrangementFromSettings(context: Context): List<LongPressAction.Type> {
|
||||
val key = context.getString(R.string.long_press_menu_action_arrangement_key)
|
||||
val items = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
val ids = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
.getString(key, null)
|
||||
if (items == null) {
|
||||
if (ids == null) {
|
||||
return getDefaultEnabledLongPressActions(context)
|
||||
} else if (ids.isEmpty()) {
|
||||
return emptyList() // apparently the user has disabled all buttons
|
||||
}
|
||||
|
||||
try {
|
||||
val actions = items.split(',')
|
||||
.map { item ->
|
||||
val actions = ids.split(',')
|
||||
.map { id ->
|
||||
LongPressAction.Type.entries.first { entry ->
|
||||
entry.id.toString() == item
|
||||
entry.id.toString() == id
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user