Skip to content

Commit

Permalink
Added instrumentation test cases to test the search functionality.
Browse files Browse the repository at this point in the history
* Testing the render method with different scenarios to ensure that libzim do not crash due to broken call stack.
  • Loading branch information
MohitMaliDeveloper committed Jun 19, 2024
1 parent eb95bdd commit 1fcbe67
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.core.net.toUri
import androidx.lifecycle.Lifecycle
import androidx.navigation.fragment.NavHostFragment
import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.accessibility.AccessibilityChecks
Expand All @@ -31,21 +32,23 @@ import androidx.test.uiautomator.UiDevice
import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResultUtils.matchesCheck
import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResultUtils.matchesViews
import com.google.android.apps.common.testing.accessibility.framework.checks.TouchTargetSizeCheck
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
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.search.SearchFragment
import org.kiwix.kiwixmobile.core.search.viewmodel.Action
import org.kiwix.kiwixmobile.core.utils.LanguageUtils.Companion.handleLocaleChange
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.nav.destination.library.LocalLibraryFragmentDirections.actionNavigationLibraryToNavigationReader
import org.kiwix.kiwixmobile.testutils.RetryRule
import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs
import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible
import java.io.File
Expand All @@ -58,9 +61,9 @@ class SearchFragmentTest : BaseActivityTest() {
private val rayCharlesZimFileUrl =
"https://dev.kiwix.org/kiwix-android/test/wikipedia_en_ray_charles_maxi_2023-12.zim"

@Rule
@JvmField
var retryRule = RetryRule()
// @Rule
// @JvmField
// var retryRule = RetryRule()

private lateinit var kiwixMainActivity: KiwixMainActivity
private lateinit var uiDevice: UiDevice
Expand Down Expand Up @@ -205,9 +208,60 @@ class SearchFragmentTest : BaseActivityTest() {
LeakAssertions.assertNoLeaks()
}

@Test
fun testConcurrencyOfSearch() = runBlocking {
val searchTerms = listOf(
"A Song",
"The Ra",
"The Ge",
"Wish",
"WIFI",
"Woman",
"Big Ba",
"My Wor",
"100"
)
activityScenario.onActivity {
kiwixMainActivity = it
kiwixMainActivity.navigate(R.id.libraryFragment)
}
downloadingZimFile = getDownloadingZimFile(false)
openKiwixReaderFragmentWithFile(downloadingZimFile)
search { checkZimFileSearchSuccessful(R.id.readerFragment) }
openSearchWithQuery(searchTerms[0], downloadingZimFile)
// wait for searchFragment become visible on screen.
delay(2000)
val navHostFragment: NavHostFragment =
kiwixMainActivity.supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val searchFragment = navHostFragment.childFragmentManager.fragments[0] as SearchFragment
for (i in 1..100) {
// This will execute the render method 100 times frequently.
val searchTerm = searchTerms[i % searchTerms.size]
searchFragment.searchViewModel.actions.trySend(Action.Filter(searchTerm)).isSuccess
}
for (i in 1..100) {
// this will execute the render method 100 times with 100MS delay.
delay(100)
val searchTerm = searchTerms[i % searchTerms.size]
searchFragment.searchViewModel.actions.trySend(Action.Filter(searchTerm)).isSuccess
}
for (i in 1..100) {
// this will execute the render method 100 times with 200MS delay.
delay(200)
val searchTerm = searchTerms[i % searchTerms.size]
searchFragment.searchViewModel.actions.trySend(Action.Filter(searchTerm)).isSuccess
}
for (i in 1..100) {
// this will execute the render method 100 times with 200MS delay.
delay(300)
val searchTerm = searchTerms[i % searchTerms.size]
searchFragment.searchViewModel.actions.trySend(Action.Filter(searchTerm)).isSuccess
}
}

private fun removeTemporaryZimFilesToFreeUpDeviceStorage() {
testZimFile.delete()
downloadingZimFile.delete()
}

private fun openKiwixReaderFragmentWithFile(zimFile: File) {
Expand Down Expand Up @@ -273,10 +327,12 @@ class SearchFragmentTest : BaseActivityTest() {
return zimFile
}

private fun getDownloadingZimFile(): File {
private fun getDownloadingZimFile(isDeletePreviousZimFile: Boolean = true): File {
val zimFile = File(context.cacheDir, "ray_charles.zim")
if (zimFile.exists()) zimFile.delete()
zimFile.createNewFile()
if (isDeletePreviousZimFile) {
if (zimFile.exists()) zimFile.delete()
zimFile.createNewFile()
}
return zimFile
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class SearchFragment : BaseFragment() {
private var findInPageTextView: TextView? = null
private var fragmentSearchBinding: FragmentSearchBinding? = null

private val searchViewModel by lazy { viewModel<SearchViewModel>(viewModelFactory) }
val searchViewModel by lazy { viewModel<SearchViewModel>(viewModelFactory) }
private var searchAdapter: SearchAdapter? = null
private var isDataLoading = false
private var renderingJob: Job? = null
Expand Down Expand Up @@ -284,7 +284,7 @@ class SearchFragment : BaseFragment() {
it.value.equals(query, ignoreCase = true)
}

private suspend fun render(state: SearchState) {
suspend fun render(state: SearchState) {
renderingJob?.apply {
// cancel the children job. Since we are getting the result on IO thread
// with `withContext` that is child for this job
Expand Down

0 comments on commit 1fcbe67

Please sign in to comment.