Extract some common test methods to InstrumentedTestUtil

This commit is contained in:
Stypox 2026-02-07 18:22:58 +01:00
parent 35e673a5ce
commit 48010d014b
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
3 changed files with 58 additions and 38 deletions

View File

@ -0,0 +1,39 @@
package org.schabi.newpipe
import android.content.Context
import androidx.annotation.StringRes
import androidx.compose.ui.test.SemanticsNodeInteraction
import androidx.compose.ui.test.SemanticsNodeInteractionsProvider
import androidx.compose.ui.test.onNodeWithText
import androidx.preference.PreferenceManager
import androidx.test.core.app.ApplicationProvider
val ctx: Context
get() = ApplicationProvider.getApplicationContext<Context>()
fun putBooleanInPrefs(@StringRes key: Int, value: Boolean) {
PreferenceManager.getDefaultSharedPreferences(ctx)
.edit().putBoolean(ctx.getString(key), value).apply()
}
fun putStringInPrefs(@StringRes key: Int, value: String) {
PreferenceManager.getDefaultSharedPreferences(ctx)
.edit().putString(ctx.getString(key), value).apply()
}
fun clearPrefs() {
PreferenceManager.getDefaultSharedPreferences(ctx)
.edit().clear().apply()
}
/**
* Same as the original `onNodeWithText` except that this takes a [StringRes] instead of a [String].
*/
fun SemanticsNodeInteractionsProvider.onNodeWithText(
@StringRes text: Int,
substring: Boolean = false,
ignoreCase: Boolean = false,
useUnmergedTree: Boolean = false
): SemanticsNodeInteraction {
return this.onNodeWithText(ctx.getString(text), substring, ignoreCase, useUnmergedTree)
}

View File

@ -1,10 +1,8 @@
package org.schabi.newpipe.ui.components.common
import androidx.activity.ComponentActivity
import androidx.annotation.StringRes
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import java.net.UnknownHostException
@ -16,6 +14,7 @@ import org.schabi.newpipe.error.ErrorInfo
import org.schabi.newpipe.error.UserAction
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
import org.schabi.newpipe.extractor.exceptions.UnsupportedContentInCountryException
import org.schabi.newpipe.onNodeWithText
import org.schabi.newpipe.ui.theme.AppTheme
@RunWith(AndroidJUnit4::class)
@ -30,7 +29,6 @@ class ErrorPanelTest {
}
}
}
private fun text(@StringRes id: Int) = composeRule.activity.getString(id)
/**
* Test Network Error
@ -44,11 +42,11 @@ class ErrorPanelTest {
)
setErrorPanel(networkErrorInfo, onRetry = {})
composeRule.onNodeWithText(text(R.string.network_error)).assertIsDisplayed()
composeRule.onNodeWithText(text(R.string.retry), ignoreCase = true).assertIsDisplayed()
composeRule.onNodeWithText(text(R.string.error_snackbar_action), ignoreCase = true)
composeRule.onNodeWithText(R.string.network_error).assertIsDisplayed()
composeRule.onNodeWithText(R.string.retry, ignoreCase = true).assertIsDisplayed()
composeRule.onNodeWithText(R.string.error_snackbar_action, ignoreCase = true)
.assertDoesNotExist()
composeRule.onNodeWithText(text(R.string.recaptcha_solve), ignoreCase = true)
composeRule.onNodeWithText(R.string.recaptcha_solve, ignoreCase = true)
.assertDoesNotExist()
}
@ -64,9 +62,9 @@ class ErrorPanelTest {
)
setErrorPanel(unexpectedErrorInfo, onRetry = {})
composeRule.onNodeWithText(text(R.string.error_snackbar_message)).assertIsDisplayed()
composeRule.onNodeWithText(text(R.string.retry), ignoreCase = true).assertIsDisplayed()
composeRule.onNodeWithText(text(R.string.error_snackbar_action), ignoreCase = true)
composeRule.onNodeWithText(R.string.error_snackbar_message).assertIsDisplayed()
composeRule.onNodeWithText(R.string.retry, ignoreCase = true).assertIsDisplayed()
composeRule.onNodeWithText(R.string.error_snackbar_action, ignoreCase = true)
.assertIsDisplayed()
}
@ -91,14 +89,14 @@ class ErrorPanelTest {
onRetry = { retryClicked = true }
)
composeRule.onNodeWithText(text(R.string.recaptcha_solve), ignoreCase = true)
composeRule.onNodeWithText(R.string.recaptcha_solve, ignoreCase = true)
.assertIsDisplayed()
composeRule.onNodeWithText(text(R.string.retry), ignoreCase = true)
composeRule.onNodeWithText(R.string.retry, ignoreCase = true)
.assertIsDisplayed()
.performClick()
composeRule.onNodeWithText(text(R.string.open_in_browser), ignoreCase = true)
composeRule.onNodeWithText(R.string.open_in_browser, ignoreCase = true)
.assertIsDisplayed()
composeRule.onNodeWithText(text(R.string.error_snackbar_action), ignoreCase = true)
composeRule.onNodeWithText(R.string.error_snackbar_action, ignoreCase = true)
.assertIsDisplayed()
assert(retryClicked) { "onRetry callback should have been invoked" }
}
@ -116,11 +114,11 @@ class ErrorPanelTest {
setErrorPanel(contentNotAvailable)
composeRule.onNodeWithText(text(R.string.unsupported_content_in_country))
composeRule.onNodeWithText(R.string.unsupported_content_in_country)
.assertIsDisplayed()
composeRule.onNodeWithText(text(R.string.retry), ignoreCase = true)
composeRule.onNodeWithText(R.string.retry, ignoreCase = true)
.assertDoesNotExist()
composeRule.onNodeWithText(text(R.string.error_snackbar_action), ignoreCase = true)
composeRule.onNodeWithText(R.string.error_snackbar_action, ignoreCase = true)
.assertDoesNotExist()
}
}

View File

@ -1,14 +1,14 @@
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.clearPrefs
import org.schabi.newpipe.ctx
import org.schabi.newpipe.putBooleanInPrefs
import org.schabi.newpipe.putStringInPrefs
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
@ -20,23 +20,6 @@ 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)) {