Skip to content

Commit 49af9ed

Browse files
committed
Updated ERefactor ErrorPanel to use ErrorInfo directly and remove UI models
1 parent 6a985e0 commit 49af9ed

16 files changed

Lines changed: 257 additions & 326 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package org.schabi.newpipe.ui.components.video.comment
2+
3+
import android.content.Context
4+
import android.content.Intent
5+
import androidx.paging.LoadState
6+
import androidx.test.core.app.ApplicationProvider
7+
import androidx.test.ext.junit.runners.AndroidJUnit4
8+
import org.junit.Assert.assertEquals
9+
import org.junit.Assert.assertTrue
10+
import org.junit.Before
11+
import org.junit.Test
12+
import org.junit.runner.RunWith
13+
import org.schabi.newpipe.error.ErrorInfo
14+
import org.schabi.newpipe.error.UserAction
15+
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
16+
import org.schabi.newpipe.ui.components.common.ErrorAction
17+
import org.schabi.newpipe.ui.components.common.determineErrorAction
18+
import org.schabi.newpipe.viewmodels.util.Resource
19+
import java.io.IOException
20+
import java.net.SocketTimeoutException
21+
22+
@RunWith(AndroidJUnit4::class)
23+
class CommentSectionErrorIntegrationTest {
24+
25+
private lateinit var context: Context
26+
27+
@Before
28+
fun setUp() {
29+
context = ApplicationProvider.getApplicationContext()
30+
}
31+
32+
// Test 1: Network error on initial load (Resource.Error)
33+
@Test
34+
fun testInitialCommentNetworkError() {
35+
val expectedMessage = "Connection timeout"
36+
val networkError = SocketTimeoutException(expectedMessage)
37+
val resourceError = Resource.Error(networkError)
38+
39+
val errorInfo = ErrorInfo(
40+
throwable = resourceError.throwable,
41+
userAction = UserAction.REQUESTED_COMMENTS,
42+
request = "comments"
43+
)
44+
assertEquals(networkError, errorInfo.throwable)
45+
assertEquals(ErrorAction.REPORT, determineErrorAction(errorInfo))
46+
assertEquals(expectedMessage, errorInfo.getExplanation())
47+
}
48+
49+
// Test 2: Network error on paging (LoadState.Error)
50+
@Test
51+
fun testPagingNetworkError() {
52+
val expectedMessage = "Paging failed"
53+
val pagingError = IOException(expectedMessage)
54+
val loadStateError = LoadState.Error(pagingError)
55+
56+
val errorInfo = ErrorInfo(
57+
throwable = loadStateError.error,
58+
userAction = UserAction.REQUESTED_COMMENTS,
59+
request = "comments"
60+
)
61+
assertEquals(pagingError, errorInfo.throwable)
62+
assertEquals(ErrorAction.REPORT, determineErrorAction(errorInfo))
63+
assertEquals(expectedMessage, errorInfo.getExplanation())
64+
}
65+
66+
// Test 3: ReCaptcha during comments load
67+
@Test
68+
fun testReCaptchaDuringComments() {
69+
val url = "https://www.google.com/recaptcha/api/fallback?k=test"
70+
val expectedMessage = "ReCaptcha needed"
71+
val captcha = ReCaptchaException(expectedMessage, url)
72+
val errorInfo = ErrorInfo(
73+
throwable = captcha,
74+
userAction = UserAction.REQUESTED_COMMENTS,
75+
request = "comments"
76+
)
77+
assertEquals(ErrorAction.SOLVE_CAPTCHA, determineErrorAction(errorInfo))
78+
assertEquals(expectedMessage, errorInfo.getExplanation())
79+
80+
val intent = Intent(context, org.schabi.newpipe.error.ReCaptchaActivity::class.java).apply {
81+
putExtra(org.schabi.newpipe.error.ReCaptchaActivity.RECAPTCHA_URL_EXTRA, url)
82+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
83+
}
84+
assertEquals(url, intent.getStringExtra(org.schabi.newpipe.error.ReCaptchaActivity.RECAPTCHA_URL_EXTRA))
85+
}
86+
87+
// Test 4: Retry functionality integration with ErrorPanel
88+
@Test
89+
fun testRetryIntegrationWithErrorPanel() {
90+
val expectedMessage = "Network request failed"
91+
val networkError = IOException(expectedMessage)
92+
val loadStateError = LoadState.Error(networkError)
93+
94+
val errorInfo = ErrorInfo(
95+
throwable = loadStateError.error,
96+
userAction = UserAction.REQUESTED_COMMENTS,
97+
request = "comments"
98+
)
99+
100+
val errorAction = determineErrorAction(errorInfo)
101+
assertEquals("Network errors should get REPORT action", ErrorAction.REPORT, errorAction)
102+
var retryCallbackInvoked = false
103+
val mockCommentRetry = {
104+
retryCallbackInvoked = true
105+
}
106+
107+
mockCommentRetry()
108+
assertTrue("Retry callback should be invoked when user clicks retry", retryCallbackInvoked)
109+
110+
assertEquals(
111+
"Error explanation should be available for retry scenarios",
112+
expectedMessage, errorInfo.getExplanation()
113+
)
114+
assertEquals(
115+
"Error should maintain comment context for retry",
116+
UserAction.REQUESTED_COMMENTS, errorInfo.userAction
117+
)
118+
}
119+
}

app/src/androidTest/java/org/schabi/newpipe/ui/components/video/comments/CommentSectionErrorTest.kt

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

app/src/main/java/org/schabi/newpipe/error/AcraReportSender.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public void send(@NonNull final Context context, @NonNull final CrashReportData
3838
UserAction.UI_ERROR,
3939
ErrorInfo.SERVICE_NONE,
4040
"ACRA report",
41-
R.string.app_ui_crash));
41+
R.string.app_ui_crash,
42+
null));
4243
}
4344
}

app/src/main/java/org/schabi/newpipe/error/ErrorInfo.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class ErrorInfo(
2020
val userAction: UserAction,
2121
val serviceName: String,
2222
val request: String,
23-
val messageStringId: Int
23+
val messageStringId: Int,
24+
val openInBrowserUrl: String? = null
2425
) : Parcelable {
2526

2627
// no need to store throwable, all data for report is in other variables
@@ -113,4 +114,9 @@ class ErrorInfo(
113114
}
114115
}
115116
}
117+
118+
// fun to extract service explanation
119+
fun getExplanation(): String {
120+
return throwable?.message ?: return ""
121+
}
116122
}

app/src/main/java/org/schabi/newpipe/error/ErrorUIMapper.kt

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

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,13 +1352,14 @@ public void handleError() {
13521352
super.handleError();
13531353
setErrorImage();
13541354

1355-
if (binding.relatedItemsLayout != null) { // hide related streams for tablets
1355+
// hide related streams for tablets
1356+
if (binding.relatedItemsLayout != null) {
13561357
binding.relatedItemsLayout.setVisibility(View.INVISIBLE);
13571358
}
1358-
13591359
// hide comments / related streams / description tabs
13601360
binding.viewPager.setVisibility(View.GONE);
13611361
binding.tabLayout.setVisibility(View.GONE);
1362+
13621363
}
13631364

13641365
private void hideAgeRestrictedContent() {

app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public void onCreate(final Bundle savedInstanceState) {
8787
new ErrorInfo(new String[]{}, UserAction.SUBSCRIPTION_IMPORT_EXPORT,
8888
ServiceHelper.getNameOfServiceById(currentServiceId),
8989
"Service does not support importing subscriptions",
90-
R.string.general_error));
90+
R.string.general_error,
91+
null));
9192
activity.finish();
9293
}
9394
}

app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CommentsSource(private val commentInfo: CommentInfo) : PagingSource<Page,
1717
override suspend fun load(params: LoadParams<Page>): LoadResult<Page, CommentsInfoItem> {
1818
// params.key is null the first time the load() function is called, so we need to return the
1919
// first batch of already-loaded comments
20-
return LoadResult.Error(IOException("💥 forced test error"))
20+
2121
if (params.key == null) {
2222
return LoadResult.Page(commentInfo.comments, null, commentInfo.nextPage)
2323
} else {

app/src/main/java/org/schabi/newpipe/ui/UiModel/ErrorUiModel.kt

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

0 commit comments

Comments
 (0)