From 2d3a4b67d74556646ea9940426f096d20aa36b96 Mon Sep 17 00:00:00 2001 From: Su TT Date: Sun, 10 Aug 2025 18:12:55 -0400 Subject: [PATCH] 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 --- .../CommentSectionErrorIntegrationTest.kt | 119 ------------------ .../schabi/newpipe/paging/CommentsSource.kt | 1 - .../ui/components/common/ErrorPanel.kt | 3 +- .../common/CommentSectionErrorTest.kt | 61 +++++++++ 4 files changed, 62 insertions(+), 122 deletions(-) delete mode 100644 app/src/androidTest/java/org/schabi/newpipe/ui/components/video/comment/CommentSectionErrorIntegrationTest.kt create mode 100644 app/src/test/java/org/schabi/newpipe/ui/components/common/CommentSectionErrorTest.kt diff --git a/app/src/androidTest/java/org/schabi/newpipe/ui/components/video/comment/CommentSectionErrorIntegrationTest.kt b/app/src/androidTest/java/org/schabi/newpipe/ui/components/video/comment/CommentSectionErrorIntegrationTest.kt deleted file mode 100644 index 12bcd85ac..000000000 --- a/app/src/androidTest/java/org/schabi/newpipe/ui/components/video/comment/CommentSectionErrorIntegrationTest.kt +++ /dev/null @@ -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 - ) - } -} diff --git a/app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt b/app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt index 21963640d..20d67a283 100644 --- a/app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt +++ b/app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt @@ -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() { private val service = NewPipe.getService(commentInfo.serviceId) diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/common/ErrorPanel.kt b/app/src/main/java/org/schabi/newpipe/ui/components/common/ErrorPanel.kt index 9fe521651..5224c2c2f 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/common/ErrorPanel.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/common/ErrorPanel.kt @@ -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() diff --git a/app/src/test/java/org/schabi/newpipe/ui/components/common/CommentSectionErrorTest.kt b/app/src/test/java/org/schabi/newpipe/ui/components/common/CommentSectionErrorTest.kt new file mode 100644 index 000000000..42e45a3c5 --- /dev/null +++ b/app/src/test/java/org/schabi/newpipe/ui/components/common/CommentSectionErrorTest.kt @@ -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()) + } +}