Fixed: The search fragment test was failing in both the custom and app modules due to a socket exception during the ZIM file download.

This commit is contained in:
MohitMaliFtechiz 2024-10-17 13:16:50 +05:30 committed by MohitMaliFtechiz
parent 5c25b20ad7
commit a7f662fe45
4 changed files with 45 additions and 7 deletions

View File

@ -33,7 +33,6 @@ import com.google.android.apps.common.testing.accessibility.framework.Accessibil
import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResultUtils.matchesViews import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResultUtils.matchesViews
import com.google.android.apps.common.testing.accessibility.framework.checks.TouchTargetSizeCheck import com.google.android.apps.common.testing.accessibility.framework.checks.TouchTargetSizeCheck
import leakcanary.LeakAssertions import leakcanary.LeakAssertions
import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.ResponseBody import okhttp3.ResponseBody
import org.hamcrest.Matchers.allOf import org.hamcrest.Matchers.allOf
@ -50,6 +49,7 @@ import org.kiwix.kiwixmobile.nav.destination.library.LocalLibraryFragmentDirecti
import org.kiwix.kiwixmobile.testutils.RetryRule import org.kiwix.kiwixmobile.testutils.RetryRule
import org.kiwix.kiwixmobile.testutils.TestUtils import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs
import org.kiwix.kiwixmobile.testutils.TestUtils.getOkkHttpClientForTesting
import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
@ -147,7 +147,7 @@ class KiwixReaderFragmentTest : BaseActivityTest() {
kiwixMainActivity.navigate(R.id.libraryFragment) kiwixMainActivity.navigate(R.id.libraryFragment)
} }
val downloadingZimFile = getDownloadingZimFile() val downloadingZimFile = getDownloadingZimFile()
OkHttpClient().newCall(downloadRequest()).execute().use { response -> getOkkHttpClientForTesting().newCall(downloadRequest()).execute().use { response ->
if (response.isSuccessful) { if (response.isSuccessful) {
response.body?.let { responseBody -> response.body?.let { responseBody ->
writeZimFileData(responseBody, downloadingZimFile) writeZimFileData(responseBody, downloadingZimFile)

View File

@ -36,7 +36,6 @@ import com.google.android.apps.common.testing.accessibility.framework.checks.Tou
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import leakcanary.LeakAssertions import leakcanary.LeakAssertions
import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.ResponseBody import okhttp3.ResponseBody
import org.hamcrest.Matchers.allOf import org.hamcrest.Matchers.allOf
@ -57,6 +56,7 @@ import org.kiwix.kiwixmobile.nav.destination.library.LocalLibraryFragmentDirecti
import org.kiwix.kiwixmobile.testutils.RetryRule import org.kiwix.kiwixmobile.testutils.RetryRule
import org.kiwix.kiwixmobile.testutils.TestUtils import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs
import org.kiwix.kiwixmobile.testutils.TestUtils.getOkkHttpClientForTesting
import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
@ -169,7 +169,7 @@ class SearchFragmentTest : BaseActivityTest() {
UiThreadStatement.runOnUiThread { kiwixMainActivity.navigate(R.id.libraryFragment) } UiThreadStatement.runOnUiThread { kiwixMainActivity.navigate(R.id.libraryFragment) }
// test with a large ZIM file to properly test the scenario // test with a large ZIM file to properly test the scenario
downloadingZimFile = getDownloadingZimFile() downloadingZimFile = getDownloadingZimFile()
OkHttpClient().newCall(downloadRequest()).execute().use { response -> getOkkHttpClientForTesting().newCall(downloadRequest()).execute().use { response ->
if (response.isSuccessful) { if (response.isSuccessful) {
response.body?.let { responseBody -> response.body?.let { responseBody ->
writeZimFileData(responseBody, downloadingZimFile) writeZimFileData(responseBody, downloadingZimFile)
@ -242,7 +242,7 @@ class SearchFragmentTest : BaseActivityTest() {
kiwixMainActivity.navigate(R.id.libraryFragment) kiwixMainActivity.navigate(R.id.libraryFragment)
} }
downloadingZimFile = getDownloadingZimFile() downloadingZimFile = getDownloadingZimFile()
OkHttpClient().newCall(downloadRequest()).execute().use { response -> getOkkHttpClientForTesting().newCall(downloadRequest()).execute().use { response ->
if (response.isSuccessful) { if (response.isSuccessful) {
response.body?.let { responseBody -> response.body?.let { responseBody ->
writeZimFileData(responseBody, downloadingZimFile) writeZimFileData(responseBody, downloadingZimFile)

View File

@ -37,8 +37,14 @@ import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiObject import androidx.test.uiautomator.UiObject
import androidx.test.uiautomator.UiObjectNotFoundException import androidx.test.uiautomator.UiObjectNotFoundException
import androidx.test.uiautomator.UiSelector import androidx.test.uiautomator.UiSelector
import okhttp3.OkHttpClient
import org.hamcrest.Description import org.hamcrest.Description
import org.hamcrest.Matcher import org.hamcrest.Matcher
import org.kiwix.kiwixmobile.core.data.remote.UserAgentInterceptor
import org.kiwix.kiwixmobile.core.di.modules.CALL_TIMEOUT
import org.kiwix.kiwixmobile.core.di.modules.CONNECTION_TIMEOUT
import org.kiwix.kiwixmobile.core.di.modules.READ_TIMEOUT
import org.kiwix.kiwixmobile.core.di.modules.USER_AGENT
import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity
import org.kiwix.kiwixmobile.core.utils.files.Log import org.kiwix.kiwixmobile.core.utils.files.Log
import java.io.File import java.io.File
@ -47,6 +53,8 @@ import java.io.FileOutputStream
import java.io.OutputStream import java.io.OutputStream
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
import java.util.concurrent.TimeUnit
import javax.inject.Singleton
/** /**
* Created by mhutti1 on 07/04/17. * Created by mhutti1 on 07/04/17.
@ -234,4 +242,16 @@ object TestUtils {
} }
} }
} }
@JvmStatic
@Singleton
fun getOkkHttpClientForTesting(): OkHttpClient =
OkHttpClient().newBuilder()
.followRedirects(true)
.followSslRedirects(true)
.connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
.callTimeout(CALL_TIMEOUT, TimeUnit.SECONDS)
.addNetworkInterceptor(UserAgentInterceptor(USER_AGENT))
.build()
} }

View File

@ -43,6 +43,11 @@ import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.kiwix.kiwixmobile.core.data.remote.UserAgentInterceptor
import org.kiwix.kiwixmobile.core.di.modules.CALL_TIMEOUT
import org.kiwix.kiwixmobile.core.di.modules.CONNECTION_TIMEOUT
import org.kiwix.kiwixmobile.core.di.modules.READ_TIMEOUT
import org.kiwix.kiwixmobile.core.di.modules.USER_AGENT
import org.kiwix.kiwixmobile.core.reader.ZimReaderSource import org.kiwix.kiwixmobile.core.reader.ZimReaderSource
import org.kiwix.kiwixmobile.core.search.SearchFragment import org.kiwix.kiwixmobile.core.search.SearchFragment
import org.kiwix.kiwixmobile.core.search.viewmodel.Action import org.kiwix.kiwixmobile.core.search.viewmodel.Action
@ -58,6 +63,8 @@ import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
import java.net.URI import java.net.URI
import java.util.concurrent.TimeUnit
import javax.inject.Singleton
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class SearchFragmentTestForCustomApp { class SearchFragmentTestForCustomApp {
@ -123,7 +130,7 @@ class SearchFragmentTestForCustomApp {
} }
// test with a large ZIM file to properly test the scenario // test with a large ZIM file to properly test the scenario
downloadingZimFile = getDownloadingZimFile() downloadingZimFile = getDownloadingZimFile()
OkHttpClient().newCall(downloadRequest()).execute().use { response -> getOkkHttpClientForTesting().newCall(downloadRequest()).execute().use { response ->
if (response.isSuccessful) { if (response.isSuccessful) {
response.body?.let { responseBody -> response.body?.let { responseBody ->
writeZimFileData(responseBody, downloadingZimFile) writeZimFileData(responseBody, downloadingZimFile)
@ -201,7 +208,7 @@ class SearchFragmentTestForCustomApp {
} }
// test with a large ZIM file to properly test the scenario // test with a large ZIM file to properly test the scenario
downloadingZimFile = getDownloadingZimFile() downloadingZimFile = getDownloadingZimFile()
OkHttpClient().newCall(downloadRequest()).execute().use { response -> getOkkHttpClientForTesting().newCall(downloadRequest()).execute().use { response ->
if (response.isSuccessful) { if (response.isSuccessful) {
response.body?.let { responseBody -> response.body?.let { responseBody ->
writeZimFileData(responseBody, downloadingZimFile) writeZimFileData(responseBody, downloadingZimFile)
@ -321,6 +328,17 @@ class SearchFragmentTestForCustomApp {
return zimFile return zimFile
} }
@Singleton
private fun getOkkHttpClientForTesting(): OkHttpClient =
OkHttpClient().newBuilder()
.followRedirects(true)
.followSslRedirects(true)
.connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
.callTimeout(CALL_TIMEOUT, TimeUnit.SECONDS)
.addNetworkInterceptor(UserAgentInterceptor(USER_AGENT))
.build()
@After @After
fun finish() { fun finish() {
TestUtils.deleteTemporaryFilesOfTestCases(context) TestUtils.deleteTemporaryFilesOfTestCases(context)