From 35e673a5ceff25663d8f8afb03d4b16e7e6b2af2 Mon Sep 17 00:00:00 2001 From: Stypox Date: Sat, 7 Feb 2026 18:06:38 +0100 Subject: [PATCH] Add tests for LongPressMenuSettings --- .../menu/LongPressMenuSettingsTest.kt | 110 ++++++++++++++++++ .../components/menu/LongPressMenuSettings.kt | 12 +- 2 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 app/src/androidTest/java/org/schabi/newpipe/ui/components/menu/LongPressMenuSettingsTest.kt diff --git a/app/src/androidTest/java/org/schabi/newpipe/ui/components/menu/LongPressMenuSettingsTest.kt b/app/src/androidTest/java/org/schabi/newpipe/ui/components/menu/LongPressMenuSettingsTest.kt new file mode 100644 index 000000000..0c57bee72 --- /dev/null +++ b/app/src/androidTest/java/org/schabi/newpipe/ui/components/menu/LongPressMenuSettingsTest.kt @@ -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() + + 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(), 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)) + } + } + } +} diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenuSettings.kt b/app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenuSettings.kt index 16eaddd16..f6d4621b9 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenuSettings.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenuSettings.kt @@ -64,17 +64,19 @@ fun getDefaultEnabledLongPressActions(context: Context): List { 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 } }