From a7f662fe45e04434bb2213fd1f01368f03ea68a1 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Thu, 17 Oct 2024 13:16:50 +0530 Subject: [PATCH] Fixed: The search fragment test was failing in both the custom and app modules due to a socket exception during the ZIM file download. --- .../reader/KiwixReaderFragmentTest.kt | 4 ++-- .../kiwixmobile/search/SearchFragmentTest.kt | 6 ++--- .../kiwix/kiwixmobile/testutils/TestUtils.kt | 20 +++++++++++++++++ .../search/SearchFragmentTestForCustomApp.kt | 22 +++++++++++++++++-- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/KiwixReaderFragmentTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/KiwixReaderFragmentTest.kt index d2d1ae3b3..94fe11ba0 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/KiwixReaderFragmentTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/KiwixReaderFragmentTest.kt @@ -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.checks.TouchTargetSizeCheck import leakcanary.LeakAssertions -import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.ResponseBody 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.TestUtils import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs +import org.kiwix.kiwixmobile.testutils.TestUtils.getOkkHttpClientForTesting import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible import java.io.File import java.io.FileOutputStream @@ -147,7 +147,7 @@ class KiwixReaderFragmentTest : BaseActivityTest() { kiwixMainActivity.navigate(R.id.libraryFragment) } val downloadingZimFile = getDownloadingZimFile() - OkHttpClient().newCall(downloadRequest()).execute().use { response -> + getOkkHttpClientForTesting().newCall(downloadRequest()).execute().use { response -> if (response.isSuccessful) { response.body?.let { responseBody -> writeZimFileData(responseBody, downloadingZimFile) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt index 32f9d77b6..cb0c0c1d2 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt @@ -36,7 +36,6 @@ import com.google.android.apps.common.testing.accessibility.framework.checks.Tou import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import leakcanary.LeakAssertions -import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.ResponseBody 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.TestUtils import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs +import org.kiwix.kiwixmobile.testutils.TestUtils.getOkkHttpClientForTesting import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible import java.io.File import java.io.FileOutputStream @@ -169,7 +169,7 @@ class SearchFragmentTest : BaseActivityTest() { UiThreadStatement.runOnUiThread { kiwixMainActivity.navigate(R.id.libraryFragment) } // test with a large ZIM file to properly test the scenario downloadingZimFile = getDownloadingZimFile() - OkHttpClient().newCall(downloadRequest()).execute().use { response -> + getOkkHttpClientForTesting().newCall(downloadRequest()).execute().use { response -> if (response.isSuccessful) { response.body?.let { responseBody -> writeZimFileData(responseBody, downloadingZimFile) @@ -242,7 +242,7 @@ class SearchFragmentTest : BaseActivityTest() { kiwixMainActivity.navigate(R.id.libraryFragment) } downloadingZimFile = getDownloadingZimFile() - OkHttpClient().newCall(downloadRequest()).execute().use { response -> + getOkkHttpClientForTesting().newCall(downloadRequest()).execute().use { response -> if (response.isSuccessful) { response.body?.let { responseBody -> writeZimFileData(responseBody, downloadingZimFile) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.kt index dfb27e45f..3b7127692 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.kt @@ -37,8 +37,14 @@ import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiObject import androidx.test.uiautomator.UiObjectNotFoundException import androidx.test.uiautomator.UiSelector +import okhttp3.OkHttpClient import org.hamcrest.Description 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.utils.files.Log import java.io.File @@ -47,6 +53,8 @@ import java.io.FileOutputStream import java.io.OutputStream import java.text.SimpleDateFormat import java.util.Date +import java.util.concurrent.TimeUnit +import javax.inject.Singleton /** * 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() } diff --git a/custom/src/androidTest/java/org/kiwix/kiwixmobile/custom/search/SearchFragmentTestForCustomApp.kt b/custom/src/androidTest/java/org/kiwix/kiwixmobile/custom/search/SearchFragmentTestForCustomApp.kt index ec50e9227..8802aab8e 100644 --- a/custom/src/androidTest/java/org/kiwix/kiwixmobile/custom/search/SearchFragmentTestForCustomApp.kt +++ b/custom/src/androidTest/java/org/kiwix/kiwixmobile/custom/search/SearchFragmentTestForCustomApp.kt @@ -43,6 +43,11 @@ import org.junit.Before import org.junit.Rule import org.junit.Test 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.search.SearchFragment import org.kiwix.kiwixmobile.core.search.viewmodel.Action @@ -58,6 +63,8 @@ import java.io.File import java.io.FileOutputStream import java.io.IOException import java.net.URI +import java.util.concurrent.TimeUnit +import javax.inject.Singleton @RunWith(AndroidJUnit4::class) class SearchFragmentTestForCustomApp { @@ -123,7 +130,7 @@ class SearchFragmentTestForCustomApp { } // test with a large ZIM file to properly test the scenario downloadingZimFile = getDownloadingZimFile() - OkHttpClient().newCall(downloadRequest()).execute().use { response -> + getOkkHttpClientForTesting().newCall(downloadRequest()).execute().use { response -> if (response.isSuccessful) { response.body?.let { responseBody -> writeZimFileData(responseBody, downloadingZimFile) @@ -201,7 +208,7 @@ class SearchFragmentTestForCustomApp { } // test with a large ZIM file to properly test the scenario downloadingZimFile = getDownloadingZimFile() - OkHttpClient().newCall(downloadRequest()).execute().use { response -> + getOkkHttpClientForTesting().newCall(downloadRequest()).execute().use { response -> if (response.isSuccessful) { response.body?.let { responseBody -> writeZimFileData(responseBody, downloadingZimFile) @@ -321,6 +328,17 @@ class SearchFragmentTestForCustomApp { 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 fun finish() { TestUtils.deleteTemporaryFilesOfTestCases(context)