Skip to content

Commit fab0d35

Browse files
committed
Update ErrorPanel and retest
1 parent 5ba95a2 commit fab0d35

3 files changed

Lines changed: 94 additions & 126 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.schabi.newpipe.ui.components.common
2+
3+
import android.content.Context
4+
import androidx.test.core.app.ApplicationProvider
5+
import androidx.test.ext.junit.runners.AndroidJUnit4
6+
import org.junit.Assert
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import org.schabi.newpipe.R
10+
import org.schabi.newpipe.error.ErrorInfo
11+
import org.schabi.newpipe.error.UserAction
12+
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
13+
import java.io.IOException
14+
import java.net.SocketTimeoutException
15+
16+
@RunWith(AndroidJUnit4::class)
17+
class CommentSectionErrorTest {
18+
private val context: Context by lazy { ApplicationProvider.getApplicationContext<Context>() }
19+
// Test 1: Network error on initial load (Resource.Error)
20+
@Test
21+
fun testInitialCommentNetworkError() {
22+
val errorInfo = ErrorInfo(
23+
throwable = SocketTimeoutException("Connection timeout"),
24+
userAction = UserAction.REQUESTED_COMMENTS,
25+
request = "comments"
26+
)
27+
Assert.assertEquals(context.getString(R.string.network_error), errorInfo.getMessage(context))
28+
Assert.assertTrue(errorInfo.isReportable)
29+
Assert.assertTrue(errorInfo.isRetryable)
30+
Assert.assertNull(errorInfo.recaptchaUrl)
31+
}
32+
33+
// Test 2: Network error on paging (LoadState.Error)
34+
@Test
35+
fun testPagingNetworkError() {
36+
val errorInfo = ErrorInfo(
37+
throwable = IOException("Paging failed"),
38+
userAction = UserAction.REQUESTED_COMMENTS,
39+
request = "comments"
40+
)
41+
Assert.assertEquals(context.getString(R.string.network_error), errorInfo.getMessage(context))
42+
Assert.assertTrue(errorInfo.isReportable)
43+
Assert.assertTrue(errorInfo.isRetryable)
44+
Assert.assertNull(errorInfo.recaptchaUrl)
45+
}
46+
47+
// Test 3: ReCaptcha during comments load
48+
@Test
49+
fun testReCaptchaDuringComments() {
50+
val url = "https://www.google.com/recaptcha/api/fallback?k=test"
51+
val errorInfo = ErrorInfo(
52+
throwable = ReCaptchaException("ReCaptcha needed", url),
53+
userAction = UserAction.REQUESTED_COMMENTS,
54+
request = "comments"
55+
)
56+
Assert.assertEquals(context.getString(R.string.recaptcha_request_toast), errorInfo.getMessage(context))
57+
Assert.assertEquals(url, errorInfo.recaptchaUrl)
58+
Assert.assertTrue(errorInfo.isReportable)
59+
Assert.assertTrue(errorInfo.isRetryable)
60+
}
61+
}

app/src/main/java/org/schabi/newpipe/ui/components/common/ErrorPanel.kt

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.schabi.newpipe.ui.components.common
22

33
import android.content.Intent
4-
import androidx.annotation.StringRes
54
import androidx.compose.foundation.layout.Column
65
import androidx.compose.foundation.layout.Spacer
76
import androidx.compose.foundation.layout.height
@@ -11,6 +10,7 @@ import androidx.compose.runtime.Composable
1110
import androidx.compose.ui.Alignment
1211
import androidx.compose.ui.Modifier
1312
import androidx.compose.ui.platform.LocalContext
13+
import androidx.compose.ui.platform.LocalInspectionMode
1414
import androidx.compose.ui.res.stringResource
1515
import androidx.compose.ui.text.font.FontWeight
1616
import androidx.compose.ui.text.style.TextAlign
@@ -19,105 +19,73 @@ import org.schabi.newpipe.R
1919
import org.schabi.newpipe.error.ErrorInfo
2020
import org.schabi.newpipe.error.ErrorUtil
2121
import org.schabi.newpipe.error.ReCaptchaActivity
22-
import org.schabi.newpipe.extractor.exceptions.AccountTerminatedException
23-
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
2422
import org.schabi.newpipe.ui.theme.AppTheme
25-
import org.schabi.newpipe.ui.theme.SizeTokens.SpacingExtraLarge
26-
import org.schabi.newpipe.ui.theme.SizeTokens.SpacingMedium
27-
import org.schabi.newpipe.ui.theme.SizeTokens.SpacingSmall
23+
import org.schabi.newpipe.ui.theme.SizeTokens
2824
import org.schabi.newpipe.util.external_communication.ShareUtils
2925

30-
enum class ErrorAction(@StringRes val actionStringId: Int) {
31-
REPORT(R.string.error_snackbar_action),
32-
SOLVE_CAPTCHA(R.string.recaptcha_solve)
33-
}
34-
35-
/**
36-
* Determines the error action type based on the throwable in ErrorInfo
37-
*
38-
*/
39-
fun determineErrorAction(errorInfo: ErrorInfo): ErrorAction {
40-
return when (errorInfo.throwable) {
41-
is ReCaptchaException -> ErrorAction.SOLVE_CAPTCHA
42-
is AccountTerminatedException -> ErrorAction.REPORT
43-
else -> ErrorAction.REPORT
44-
}
45-
}
46-
4726
@Composable
4827
fun ErrorPanel(
4928
errorInfo: ErrorInfo,
5029
modifier: Modifier = Modifier,
5130
onRetry: (() -> Unit)? = null,
5231

5332
) {
54-
val explanation = errorInfo.getExplanation()
55-
val canOpenInBrowser = errorInfo.openInBrowserUrl != null
56-
val errorActionType = determineErrorAction(errorInfo)
57-
5833
val context = LocalContext.current
34+
val isPreview = LocalInspectionMode.current
35+
val messageText = if (isPreview) {
36+
stringResource(R.string.error_snackbar_message)
37+
} else {
38+
errorInfo.getMessage(context)
39+
}
5940

6041
Column(
6142
horizontalAlignment = Alignment.CenterHorizontally,
6243
modifier = modifier
6344
) {
6445

6546
Text(
66-
text = stringResource(errorInfo.messageStringId),
47+
text = messageText,
6748
style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold),
6849
textAlign = TextAlign.Center
6950
)
7051

71-
if (explanation.isNotBlank()) {
72-
Spacer(Modifier.height(SpacingSmall))
73-
Text(
74-
text = explanation,
75-
style = MaterialTheme.typography.bodyMedium,
76-
textAlign = TextAlign.Center
77-
)
52+
Spacer(Modifier.height(SizeTokens.SpacingMedium))
53+
if (errorInfo.isReportable) {
54+
ServiceColoredButton(onClick = {
55+
ErrorUtil.openActivity(context, errorInfo)
56+
}) {
57+
Text(stringResource(R.string.error_snackbar_action).uppercase())
58+
}
7859
}
7960

80-
Spacer(Modifier.height(SpacingMedium))
81-
when (errorActionType) {
82-
ErrorAction.REPORT -> {
83-
ServiceColoredButton(onClick = {
84-
ErrorUtil.openActivity(context, errorInfo)
85-
}) {
86-
Text(stringResource(errorActionType.actionStringId).uppercase())
87-
}
88-
}
89-
ErrorAction.SOLVE_CAPTCHA -> {
90-
ServiceColoredButton(onClick = {
91-
// Starting ReCaptcha Challenge Activity
92-
val intent = Intent(context, ReCaptchaActivity::class.java)
93-
.putExtra(
94-
ReCaptchaActivity.RECAPTCHA_URL_EXTRA,
95-
(errorInfo.throwable as ReCaptchaException).url
96-
)
97-
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
98-
context.startActivity(intent)
99-
}) {
100-
Text(stringResource(errorActionType.actionStringId).uppercase())
101-
}
61+
errorInfo.recaptchaUrl?.let { recaptchaUrl ->
62+
ServiceColoredButton(onClick = {
63+
val intent = Intent(context, ReCaptchaActivity::class.java)
64+
.putExtra(ReCaptchaActivity.RECAPTCHA_URL_EXTRA, recaptchaUrl)
65+
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
66+
context.startActivity(intent)
67+
}) {
68+
Text(stringResource(R.string.recaptcha_solve).uppercase())
10269
}
10370
}
10471

105-
onRetry?.let {
106-
ServiceColoredButton(onClick = it) {
107-
Text(stringResource(R.string.retry).uppercase())
72+
if (errorInfo.isRetryable) {
73+
onRetry?.let {
74+
ServiceColoredButton(onClick = it) {
75+
Text(stringResource(R.string.retry).uppercase())
76+
}
10877
}
10978
}
110-
if (canOpenInBrowser) {
79+
80+
errorInfo.openInBrowserUrl?.let { url ->
11181
ServiceColoredButton(onClick = {
112-
errorInfo.openInBrowserUrl?.let { url ->
113-
ShareUtils.openUrlInBrowser(context, url)
114-
}
82+
ShareUtils.openUrlInBrowser(context, url)
11583
}) {
11684
Text(stringResource(R.string.open_in_browser).uppercase())
11785
}
11886
}
11987

120-
Spacer(Modifier.height(SpacingExtraLarge))
88+
Spacer(Modifier.height(SizeTokens.SpacingExtraLarge))
12189
}
12290
}
12391

app/src/test/java/org/schabi/newpipe/ui/components/common/CommentSectionErrorTest.kt

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)