1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-11-12 13:13:00 +00:00

Convert CommentSectionErrorIntegrationTest to unit test

- Moved from androidTest to test directory
- Removed Android test runner dependencies
- Renamed to CommentSectionErrorTest
- Addresses PR feedback until Compose testing is in place
This commit is contained in:
Su TT
2025-08-10 18:12:55 -04:00
parent 9cbcaecb92
commit 2d3a4b67d7
4 changed files with 62 additions and 122 deletions

View File

@@ -1,119 +0,0 @@
package org.schabi.newpipe.ui.components.video.comment
import android.content.Context
import android.content.Intent
import androidx.paging.LoadState
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.schabi.newpipe.error.ErrorInfo
import org.schabi.newpipe.error.UserAction
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
import org.schabi.newpipe.ui.components.common.ErrorAction
import org.schabi.newpipe.ui.components.common.determineErrorAction
import org.schabi.newpipe.viewmodels.util.Resource
import java.io.IOException
import java.net.SocketTimeoutException
@RunWith(AndroidJUnit4::class)
class CommentSectionErrorIntegrationTest {
private lateinit var context: Context
@Before
fun setUp() {
context = ApplicationProvider.getApplicationContext()
}
// Test 1: Network error on initial load (Resource.Error)
@Test
fun testInitialCommentNetworkError() {
val expectedMessage = "Connection timeout"
val networkError = SocketTimeoutException(expectedMessage)
val resourceError = Resource.Error(networkError)
val errorInfo = ErrorInfo(
throwable = resourceError.throwable,
userAction = UserAction.REQUESTED_COMMENTS,
request = "comments"
)
assertEquals(networkError, errorInfo.throwable)
assertEquals(ErrorAction.REPORT, determineErrorAction(errorInfo))
assertEquals(expectedMessage, errorInfo.getExplanation())
}
// Test 2: Network error on paging (LoadState.Error)
@Test
fun testPagingNetworkError() {
val expectedMessage = "Paging failed"
val pagingError = IOException(expectedMessage)
val loadStateError = LoadState.Error(pagingError)
val errorInfo = ErrorInfo(
throwable = loadStateError.error,
userAction = UserAction.REQUESTED_COMMENTS,
request = "comments"
)
assertEquals(pagingError, errorInfo.throwable)
assertEquals(ErrorAction.REPORT, determineErrorAction(errorInfo))
assertEquals(expectedMessage, errorInfo.getExplanation())
}
// Test 3: ReCaptcha during comments load
@Test
fun testReCaptchaDuringComments() {
val url = "https://www.google.com/recaptcha/api/fallback?k=test"
val expectedMessage = "ReCaptcha needed"
val captcha = ReCaptchaException(expectedMessage, url)
val errorInfo = ErrorInfo(
throwable = captcha,
userAction = UserAction.REQUESTED_COMMENTS,
request = "comments"
)
assertEquals(ErrorAction.SOLVE_CAPTCHA, determineErrorAction(errorInfo))
assertEquals(expectedMessage, errorInfo.getExplanation())
val intent = Intent(context, org.schabi.newpipe.error.ReCaptchaActivity::class.java).apply {
putExtra(org.schabi.newpipe.error.ReCaptchaActivity.RECAPTCHA_URL_EXTRA, url)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
assertEquals(url, intent.getStringExtra(org.schabi.newpipe.error.ReCaptchaActivity.RECAPTCHA_URL_EXTRA))
}
// Test 4: Retry functionality integration with ErrorPanel
@Test
fun testRetryIntegrationWithErrorPanel() {
val expectedMessage = "Network request failed"
val networkError = IOException(expectedMessage)
val loadStateError = LoadState.Error(networkError)
val errorInfo = ErrorInfo(
throwable = loadStateError.error,
userAction = UserAction.REQUESTED_COMMENTS,
request = "comments"
)
val errorAction = determineErrorAction(errorInfo)
assertEquals("Network errors should get REPORT action", ErrorAction.REPORT, errorAction)
var retryCallbackInvoked = false
val mockCommentRetry = {
retryCallbackInvoked = true
}
mockCommentRetry()
assertTrue("Retry callback should be invoked when user clicks retry", retryCallbackInvoked)
assertEquals(
"Error explanation should be available for retry scenarios",
expectedMessage, errorInfo.getExplanation()
)
assertEquals(
"Error should maintain comment context for retry",
UserAction.REQUESTED_COMMENTS, errorInfo.userAction
)
}
}

View File

@@ -9,7 +9,6 @@ import org.schabi.newpipe.extractor.Page
import org.schabi.newpipe.extractor.comments.CommentsInfo
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
import org.schabi.newpipe.ui.components.video.comment.CommentInfo
import java.io.IOException
class CommentsSource(private val commentInfo: CommentInfo) : PagingSource<Page, CommentsInfoItem>() {
private val service = NewPipe.getService(commentInfo.serviceId)

View File

@@ -21,7 +21,6 @@ import org.schabi.newpipe.error.ErrorUtil
import org.schabi.newpipe.error.ReCaptchaActivity
import org.schabi.newpipe.extractor.exceptions.AccountTerminatedException
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
import org.schabi.newpipe.extractor.timeago.patterns.it
import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.ui.theme.SizeTokens.SpacingExtraLarge
import org.schabi.newpipe.ui.theme.SizeTokens.SpacingMedium
@@ -48,8 +47,8 @@ fun determineErrorAction(errorInfo: ErrorInfo): ErrorAction {
@Composable
fun ErrorPanel(
errorInfo: ErrorInfo,
modifier: Modifier = Modifier,
onRetry: (() -> Unit)? = null,
modifier: Modifier = Modifier
) {
val explanation = errorInfo.getExplanation()

View File

@@ -0,0 +1,61 @@
package org.schabi.newpipe.ui.components.common
import androidx.paging.LoadState
import org.junit.Assert
import org.junit.Test
import org.schabi.newpipe.error.ErrorInfo
import org.schabi.newpipe.error.UserAction
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
import java.io.IOException
import java.net.SocketTimeoutException
class CommentSectionErrorTest {
// Test 1: Network error on initial load (Resource.Error)
@Test
fun testInitialCommentNetworkError() {
val expectedMessage = "Connection timeout"
val networkError = SocketTimeoutException(expectedMessage)
val errorInfo = ErrorInfo(
throwable = networkError,
userAction = UserAction.REQUESTED_COMMENTS,
request = "comments"
)
Assert.assertEquals(networkError, errorInfo.throwable)
Assert.assertEquals(ErrorAction.REPORT, determineErrorAction(errorInfo))
Assert.assertEquals(expectedMessage, errorInfo.getExplanation())
}
// Test 2: Network error on paging (LoadState.Error)
@Test
fun testPagingNetworkError() {
val expectedMessage = "Paging failed"
val pagingError = IOException(expectedMessage)
val loadStateError = LoadState.Error(pagingError)
val errorInfo = ErrorInfo(
throwable = loadStateError.error,
userAction = UserAction.REQUESTED_COMMENTS,
request = "comments"
)
Assert.assertEquals(pagingError, errorInfo.throwable)
Assert.assertEquals(ErrorAction.REPORT, determineErrorAction(errorInfo))
Assert.assertEquals(expectedMessage, errorInfo.getExplanation())
}
// Test 3: ReCaptcha during comments load
@Test
fun testReCaptchaDuringComments() {
val url = "https://www.google.com/recaptcha/api/fallback?k=test"
val expectedMessage = "ReCaptcha needed"
val captcha = ReCaptchaException(expectedMessage, url)
val errorInfo = ErrorInfo(
throwable = captcha,
userAction = UserAction.REQUESTED_COMMENTS,
request = "comments"
)
Assert.assertEquals(ErrorAction.SOLVE_CAPTCHA, determineErrorAction(errorInfo))
Assert.assertEquals(expectedMessage, errorInfo.getExplanation())
}
}