Skip to content

Commit

Permalink
Improved SearchFragmentTest:
Browse files Browse the repository at this point in the history
After addressing the issue documented in kiwix/java-libkiwix#61, we now have the ability to search within zim files that do not have a Xapian index. As a result, we have enhanced our test to utilize pre-existing zim files. This improvement leads to reduced time consumption, improved memory efficiency, and minimized network usage impact.
  • Loading branch information
MohitMaliDeveloper authored and kelson42 committed Aug 31, 2023
1 parent bc7c86e commit 6e94f8d
Showing 1 changed file with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@
package org.kiwix.kiwixmobile.search

import androidx.core.content.edit
import androidx.core.net.toUri
import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario
import androidx.test.internal.runner.junit4.statement.UiThreadStatement
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import leakcanary.LeakAssertions
import org.junit.After
import org.junit.Assert
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.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.download.downloadRobot
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
import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs
import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream

class SearchFragmentTest : BaseActivityTest() {

Expand Down Expand Up @@ -67,29 +68,40 @@ class SearchFragmentTest : BaseActivityTest() {
fun searchFragmentSimple() {
ActivityScenario.launch(KiwixMainActivity::class.java).onActivity {
kiwixMainActivity = it
kiwixMainActivity.navigate(R.id.libraryFragment)
}
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
try {
downloadRobot {
clickLibraryOnBottomNav()
deleteZimIfExists(false)
clickDownloadOnBottomNav()
waitForDataToLoad()
downloadZimFile()
assertDownloadStart()
waitUntilDownloadComplete()
clickLibraryOnBottomNav()
checkIfZimFileDownloaded()
downloadZimFile()
val loadFileStream =
SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim")
val zimFile = File(context.cacheDir, "testzim.zim")
if (zimFile.exists()) zimFile.delete()
zimFile.createNewFile()
loadFileStream.use { inputStream ->
val outputStream: OutputStream = FileOutputStream(zimFile)
outputStream.use { it ->
val buffer = ByteArray(inputStream.available())
var length: Int
while (inputStream.read(buffer).also { length = it } > 0) {
it.write(buffer, 0, length)
}
}
} catch (e: Exception) {
Assert.fail(
"Couldn't find downloaded file ' Off the Grid ' Original Exception: ${e.message}"
}
UiThreadStatement.runOnUiThread {
kiwixMainActivity.navigate(
actionNavigationLibraryToNavigationReader()
.apply { zimFileUri = zimFile.toUri().toString() }
)
}
search { checkZimFileSearchSuccessful(R.id.readerFragment) }
UiThreadStatement.runOnUiThread {
kiwixMainActivity.openSearch(searchString = "100R")
if (zimFile.canRead()) {
kiwixMainActivity.openSearch(searchString = "Android")
} else {
throw RuntimeException(
"File $zimFile is not readable." +
" Original File $zimFile is readable = ${zimFile.canRead()}" +
" Size ${zimFile.length()}"
)
}
}
search {
clickOnSearchItemInSearchList()
Expand Down

0 comments on commit 6e94f8d

Please sign in to comment.