Skip to content

Commit

Permalink
Fixed: SearchFragmentTest for custom apps was failing.
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitMaliDeveloper committed Dec 6, 2024
1 parent 52527b7 commit c33fc58
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ package org.kiwix.kiwixmobile.custom.search

import android.Manifest
import android.content.Context
import android.content.res.AssetFileDescriptor
import android.os.ParcelFileDescriptor
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.lifecycle.Lifecycle
import androidx.navigation.fragment.NavHostFragment
Expand Down Expand Up @@ -61,7 +60,6 @@ import org.kiwix.kiwixmobile.custom.testutils.TestUtils.closeSystemDialogs
import org.kiwix.kiwixmobile.custom.testutils.TestUtils.isSystemUINotRespondingDialogVisible
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
Expand Down Expand Up @@ -145,7 +143,7 @@ class SearchFragmentTestForCustomApp {
UiThreadStatement.runOnUiThread {
customMainActivity.navigate(customMainActivity.readerFragmentResId)
}
openZimFileInReaderWithAssetFileDescriptor(downloadingZimFile)
openZimFileInReader(downloadingZimFile)
openSearchWithQuery()
val searchTerm = "gard"
val searchedItem = "Gardanta Spirito"
Expand Down Expand Up @@ -223,7 +221,7 @@ class SearchFragmentTestForCustomApp {
UiThreadStatement.runOnUiThread {
customMainActivity.navigate(customMainActivity.readerFragmentResId)
}
openZimFileInReaderWithAssetFileDescriptor(downloadingZimFile)
openZimFileInReader(downloadingZimFile)
openSearchWithQuery(searchTerms[0])
// wait for searchFragment become visible on screen.
delay(2000)
Expand Down Expand Up @@ -264,13 +262,7 @@ class SearchFragmentTestForCustomApp {
}
}

private fun openZimFileInReaderWithAssetFileDescriptor(downloadingZimFile: File) {
getAssetFileDescriptorFromFile(downloadingZimFile)?.let(::openZimFileInReader) ?: run {
throw RuntimeException("Unable to get fileDescriptor from file. Original exception")
}
}

private fun openZimFileInReader(assetFileDescriptor: AssetFileDescriptor) {
private fun openZimFileInReader(file: File) {
UiThreadStatement.runOnUiThread {
val navHostFragment: NavHostFragment =
customMainActivity.supportFragmentManager
Expand All @@ -281,30 +273,13 @@ class SearchFragmentTestForCustomApp {
navHostFragment.childFragmentManager.fragments[0] as CustomReaderFragment
runBlocking {
customReaderFragment.openZimFile(
ZimReaderSource(null, null, listOf(assetFileDescriptor)),
ZimReaderSource(file, null, null),
true
)
}
}
}

private fun getAssetFileDescriptorFromFile(file: File): AssetFileDescriptor? {
val parcelFileDescriptor = getFileDescriptor(file)
if (parcelFileDescriptor != null) {
return AssetFileDescriptor(parcelFileDescriptor, 0, file.length())
}
return null
}

private fun getFileDescriptor(file: File?): ParcelFileDescriptor? {
try {
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
} catch (e: IOException) {
e.printStackTrace()
return null
}
}

private fun writeZimFileData(responseBody: ResponseBody, file: File) {
FileOutputStream(file).use { outputStream ->
responseBody.byteStream().use { inputStream ->
Expand All @@ -324,7 +299,7 @@ class SearchFragmentTestForCustomApp {
.build()

private fun getDownloadingZimFile(): File {
val zimFile = File(context.cacheDir, "ray_charles.zim")
val zimFile = File(ContextCompat.getExternalFilesDirs(context, null)[0], "ray_charles.zim")
if (zimFile.exists()) zimFile.delete()
zimFile.createNewFile()
return zimFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class SearchRobot {
clickOnSearchItemInSearchList()
}

fun clickOnSearchItemInSearchList() {
private fun clickOnSearchItemInSearchList() {
testFlakyView({
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS_FOR_SEARCH_TEST.toLong())
Espresso.onView(ViewMatchers.withId(R.id.search_list)).perform(
Expand Down

0 comments on commit c33fc58

Please sign in to comment.