|
| 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 | +} |
0 commit comments