Skip to content

Commit ff29b82

Browse files
committed
Extract some common test methods to InstrumentedTestUtil
1 parent c61c5ea commit ff29b82

3 files changed

Lines changed: 58 additions & 38 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.schabi.newpipe
2+
3+
import android.content.Context
4+
import androidx.annotation.StringRes
5+
import androidx.compose.ui.test.SemanticsNodeInteraction
6+
import androidx.compose.ui.test.SemanticsNodeInteractionsProvider
7+
import androidx.compose.ui.test.onNodeWithText
8+
import androidx.preference.PreferenceManager
9+
import androidx.test.core.app.ApplicationProvider
10+
11+
val ctx: Context
12+
get() = ApplicationProvider.getApplicationContext<Context>()
13+
14+
fun putBooleanInPrefs(@StringRes key: Int, value: Boolean) {
15+
PreferenceManager.getDefaultSharedPreferences(ctx)
16+
.edit().putBoolean(ctx.getString(key), value).apply()
17+
}
18+
19+
fun putStringInPrefs(@StringRes key: Int, value: String) {
20+
PreferenceManager.getDefaultSharedPreferences(ctx)
21+
.edit().putString(ctx.getString(key), value).apply()
22+
}
23+
24+
fun clearPrefs() {
25+
PreferenceManager.getDefaultSharedPreferences(ctx)
26+
.edit().clear().apply()
27+
}
28+
29+
/**
30+
* Same as the original `onNodeWithText` except that this takes a [StringRes] instead of a [String].
31+
*/
32+
fun SemanticsNodeInteractionsProvider.onNodeWithText(
33+
@StringRes text: Int,
34+
substring: Boolean = false,
35+
ignoreCase: Boolean = false,
36+
useUnmergedTree: Boolean = false
37+
): SemanticsNodeInteraction {
38+
return this.onNodeWithText(ctx.getString(text), substring, ignoreCase, useUnmergedTree)
39+
}

app/src/androidTest/java/org/schabi/newpipe/ui/components/common/ErrorPanelTest.kt

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.schabi.newpipe.ui.components.common
22

33
import androidx.activity.ComponentActivity
4-
import androidx.annotation.StringRes
54
import androidx.compose.ui.test.assertIsDisplayed
65
import androidx.compose.ui.test.junit4.createAndroidComposeRule
7-
import androidx.compose.ui.test.onNodeWithText
86
import androidx.compose.ui.test.performClick
97
import androidx.test.ext.junit.runners.AndroidJUnit4
108
import java.net.UnknownHostException
@@ -16,6 +14,7 @@ import org.schabi.newpipe.error.ErrorInfo
1614
import org.schabi.newpipe.error.UserAction
1715
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
1816
import org.schabi.newpipe.extractor.exceptions.UnsupportedContentInCountryException
17+
import org.schabi.newpipe.onNodeWithText
1918
import org.schabi.newpipe.ui.theme.AppTheme
2019

2120
@RunWith(AndroidJUnit4::class)
@@ -30,7 +29,6 @@ class ErrorPanelTest {
3029
}
3130
}
3231
}
33-
private fun text(@StringRes id: Int) = composeRule.activity.getString(id)
3432

3533
/**
3634
* Test Network Error
@@ -44,11 +42,11 @@ class ErrorPanelTest {
4442
)
4543

4644
setErrorPanel(networkErrorInfo, onRetry = {})
47-
composeRule.onNodeWithText(text(R.string.network_error)).assertIsDisplayed()
48-
composeRule.onNodeWithText(text(R.string.retry), ignoreCase = true).assertIsDisplayed()
49-
composeRule.onNodeWithText(text(R.string.error_snackbar_action), ignoreCase = true)
45+
composeRule.onNodeWithText(R.string.network_error).assertIsDisplayed()
46+
composeRule.onNodeWithText(R.string.retry, ignoreCase = true).assertIsDisplayed()
47+
composeRule.onNodeWithText(R.string.error_snackbar_action, ignoreCase = true)
5048
.assertDoesNotExist()
51-
composeRule.onNodeWithText(text(R.string.recaptcha_solve), ignoreCase = true)
49+
composeRule.onNodeWithText(R.string.recaptcha_solve, ignoreCase = true)
5250
.assertDoesNotExist()
5351
}
5452

@@ -64,9 +62,9 @@ class ErrorPanelTest {
6462
)
6563

6664
setErrorPanel(unexpectedErrorInfo, onRetry = {})
67-
composeRule.onNodeWithText(text(R.string.error_snackbar_message)).assertIsDisplayed()
68-
composeRule.onNodeWithText(text(R.string.retry), ignoreCase = true).assertIsDisplayed()
69-
composeRule.onNodeWithText(text(R.string.error_snackbar_action), ignoreCase = true)
65+
composeRule.onNodeWithText(R.string.error_snackbar_message).assertIsDisplayed()
66+
composeRule.onNodeWithText(R.string.retry, ignoreCase = true).assertIsDisplayed()
67+
composeRule.onNodeWithText(R.string.error_snackbar_action, ignoreCase = true)
7068
.assertIsDisplayed()
7169
}
7270

@@ -91,14 +89,14 @@ class ErrorPanelTest {
9189
onRetry = { retryClicked = true }
9290

9391
)
94-
composeRule.onNodeWithText(text(R.string.recaptcha_solve), ignoreCase = true)
92+
composeRule.onNodeWithText(R.string.recaptcha_solve, ignoreCase = true)
9593
.assertIsDisplayed()
96-
composeRule.onNodeWithText(text(R.string.retry), ignoreCase = true)
94+
composeRule.onNodeWithText(R.string.retry, ignoreCase = true)
9795
.assertIsDisplayed()
9896
.performClick()
99-
composeRule.onNodeWithText(text(R.string.open_in_browser), ignoreCase = true)
97+
composeRule.onNodeWithText(R.string.open_in_browser, ignoreCase = true)
10098
.assertIsDisplayed()
101-
composeRule.onNodeWithText(text(R.string.error_snackbar_action), ignoreCase = true)
99+
composeRule.onNodeWithText(R.string.error_snackbar_action, ignoreCase = true)
102100
.assertIsDisplayed()
103101
assert(retryClicked) { "onRetry callback should have been invoked" }
104102
}
@@ -116,11 +114,11 @@ class ErrorPanelTest {
116114

117115
setErrorPanel(contentNotAvailable)
118116

119-
composeRule.onNodeWithText(text(R.string.unsupported_content_in_country))
117+
composeRule.onNodeWithText(R.string.unsupported_content_in_country)
120118
.assertIsDisplayed()
121-
composeRule.onNodeWithText(text(R.string.retry), ignoreCase = true)
119+
composeRule.onNodeWithText(R.string.retry, ignoreCase = true)
122120
.assertDoesNotExist()
123-
composeRule.onNodeWithText(text(R.string.error_snackbar_action), ignoreCase = true)
121+
composeRule.onNodeWithText(R.string.error_snackbar_action, ignoreCase = true)
124122
.assertDoesNotExist()
125123
}
126124
}

app/src/androidTest/java/org/schabi/newpipe/ui/components/menu/LongPressMenuSettingsTest.kt

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.schabi.newpipe.ui.components.menu
22

3-
import android.content.Context
4-
import androidx.annotation.StringRes
5-
import androidx.preference.PreferenceManager
6-
import androidx.test.core.app.ApplicationProvider
73
import androidx.test.ext.junit.runners.AndroidJUnit4
84
import org.junit.Assert.assertEquals
95
import org.junit.Test
106
import org.junit.runner.RunWith
117
import org.schabi.newpipe.R
8+
import org.schabi.newpipe.clearPrefs
9+
import org.schabi.newpipe.ctx
10+
import org.schabi.newpipe.putBooleanInPrefs
11+
import org.schabi.newpipe.putStringInPrefs
1212
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Background
1313
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Enqueue
1414
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.EnqueueNext
@@ -20,23 +20,6 @@ import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.ShowDetails
2020
@RunWith(AndroidJUnit4::class)
2121
class LongPressMenuSettingsTest {
2222

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-
4023
@Test
4124
fun testStoringAndLoadingPreservesIsHeaderEnabled() {
4225
for (enabled in arrayOf(false, true)) {

0 commit comments

Comments
 (0)