11package org.schabi.newpipe.error
22
3+ import android.content.Context
4+ import android.os.Parcel
5+ import android.os.Parcelable
6+ import androidx.test.core.app.ApplicationProvider
7+ import androidx.test.ext.junit.runners.AndroidJUnit4
8+ import androidx.test.filters.LargeTest
9+ import org.junit.Assert.assertEquals
10+ import org.junit.Assert.assertFalse
11+ import org.junit.Assert.assertNull
12+ import org.junit.Assert.assertTrue
13+ import org.junit.Test
14+ import org.junit.runner.RunWith
15+ import org.schabi.newpipe.R
16+ import org.schabi.newpipe.extractor.ServiceList
17+ import org.schabi.newpipe.extractor.exceptions.ParsingException
18+ import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
19+ import java.io.IOException
20+ import java.net.SocketTimeoutException
21+
22+ /* *
23+ * Instrumented tests for {@link ErrorInfo}.
24+ */
25+ @RunWith(AndroidJUnit4 ::class )
26+ @LargeTest
327class ErrorInfoTest {
4- }
28+ private val context: Context by lazy { ApplicationProvider .getApplicationContext<Context >() }
29+
30+ /* *
31+ * @param errorInfo the error info to access
32+ * @return the private field errorInfo.message.stringRes using reflection
33+ */
34+ @Throws(NoSuchFieldException ::class , IllegalAccessException ::class )
35+ private fun getMessageFromErrorInfo (errorInfo : ErrorInfo ): Int {
36+ val message = ErrorInfo ::class .java.getDeclaredField(" message" )
37+ message.isAccessible = true
38+ val messageValue = message.get(errorInfo) as ErrorInfo .Companion .ErrorMessage
39+
40+ val stringRes = ErrorInfo .Companion .ErrorMessage ::class .java.getDeclaredField(" stringRes" )
41+ stringRes.isAccessible = true
42+ return stringRes.get(messageValue) as Int
43+ }
44+
45+ @Test
46+ @Throws(NoSuchFieldException ::class , IllegalAccessException ::class )
47+ fun errorInfoTestParcelable () {
48+ val info = ErrorInfo (
49+ ParsingException (" Hello" ),
50+ UserAction .USER_REPORT ,
51+ " request" ,
52+ ServiceList .YouTube .serviceId
53+ )
54+ // Obtain a Parcel object and write the parcelable object to it:
55+ val parcel = Parcel .obtain()
56+ info.writeToParcel(parcel, 0 )
57+ parcel.setDataPosition(0 )
58+ val creatorField = ErrorInfo ::class .java.getDeclaredField(" CREATOR" )
59+ val creator = creatorField.get(null )
60+ check(creator is Parcelable .Creator <* >)
61+ val infoFromParcel = requireNotNull(
62+ creator.createFromParcel(parcel) as ? ErrorInfo
63+ )
64+ assertTrue(
65+ infoFromParcel.stackTraces.contentToString()
66+ .contains(ErrorInfoTest ::class .java.simpleName)
67+ )
68+ assertEquals(UserAction .USER_REPORT , infoFromParcel.userAction)
69+ assertEquals(
70+ ServiceList .YouTube .serviceInfo.name,
71+ infoFromParcel.getServiceName()
72+ )
73+ assertEquals(" request" , infoFromParcel.request)
74+ assertEquals(R .string.parsing_error, getMessageFromErrorInfo(infoFromParcel))
75+
76+ parcel.recycle()
77+ }
78+
79+ /* *
80+ * Test: Network error on initial load (Resource.Error)
81+ */
82+
83+ @Test
84+ fun testInitialCommentNetworkError () {
85+ val errorInfo = ErrorInfo (
86+ throwable = SocketTimeoutException (" Connection timeout" ),
87+ userAction = UserAction .REQUESTED_COMMENTS ,
88+ request = " comments"
89+ )
90+ assertEquals(context.getString(R .string.network_error), errorInfo.getMessage(context))
91+ assertTrue(errorInfo.isReportable)
92+ assertTrue(errorInfo.isRetryable)
93+ assertNull(errorInfo.recaptchaUrl)
94+ }
95+
96+ /* *
97+ * Test: Network error on paging (LoadState.Error)
98+ */
99+ @Test
100+ fun testPagingNetworkError () {
101+ val errorInfo = ErrorInfo (
102+ throwable = IOException (" Paging failed" ),
103+ userAction = UserAction .REQUESTED_COMMENTS ,
104+ request = " comments"
105+ )
106+ assertEquals(context.getString(R .string.network_error), errorInfo.getMessage(context))
107+ assertTrue(errorInfo.isReportable)
108+ assertTrue(errorInfo.isRetryable)
109+ assertNull(errorInfo.recaptchaUrl)
110+ }
111+
112+ /* *
113+ * Test: ReCaptcha during comments load
114+ */
115+ @Test
116+ fun testReCaptchaDuringComments () {
117+ val url = " https://www.google.com/recaptcha/api/fallback?k=test"
118+ val errorInfo = ErrorInfo (
119+ throwable = ReCaptchaException (" ReCaptcha needed" , url),
120+ userAction = UserAction .REQUESTED_COMMENTS ,
121+ request = " comments"
122+ )
123+ assertEquals(context.getString(R .string.recaptcha_request_toast), errorInfo.getMessage(context))
124+ assertEquals(url, errorInfo.recaptchaUrl)
125+ assertFalse(errorInfo.isReportable)
126+ assertTrue(errorInfo.isRetryable)
127+ }
128+ }
0 commit comments