Skip to content

Commit c1ac055

Browse files
SttApolloStypox
authored andcommitted
Fix CommentSectionErrorTest to use named NetworkException for instrumented test compatibility
1 parent 2894f32 commit c1ac055

11 files changed

Lines changed: 232 additions & 318 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 & 64 deletions
This file was deleted.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,9 @@ class ErrorInfo private constructor(
302302
}
303303
}
304304
}
305+
306+
// fun to extract service explanation
307+
fun getExplanation(): String {
308+
return ""
309+
}
305310
}

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/paging/CommentsSource.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ 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"))
2120
if (params.key == null) {
2221
return LoadResult.Page(commentInfo.comments, null, commentInfo.nextPage)
2322
} else {
2423
val info = withContext(Dispatchers.IO) {
2524
CommentsInfo.getMoreItems(service, commentInfo.url, params.key)
2625
}
27-
2826
return LoadResult.Page(info.items, null, info.nextPage)
2927
}
3028
}

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)