Improved SearchFragmentTest:

After addressing the issue documented in https://github.com/kiwix/java-libkiwix/pull/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.
This commit is contained in:
MohitMali 2023-08-08 18:11:05 +05:30
parent c4dbe478b8
commit a1b5f36c59

View File

@ -18,27 +18,28 @@
package org.kiwix.kiwixmobile.search package org.kiwix.kiwixmobile.search
import androidx.core.content.edit import androidx.core.content.edit
import androidx.core.net.toUri
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ActivityScenario
import androidx.test.internal.runner.junit4.statement.UiThreadStatement import androidx.test.internal.runner.junit4.statement.UiThreadStatement
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import leakcanary.LeakAssertions import leakcanary.LeakAssertions
import org.junit.After import org.junit.After
import org.junit.Assert
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.R import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.download.downloadRobot
import org.kiwix.kiwixmobile.main.KiwixMainActivity 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.RetryRule
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.isSystemUINotRespondingDialogVisible import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
class SearchFragmentTest : BaseActivityTest() { class SearchFragmentTest : BaseActivityTest() {
@ -67,29 +68,40 @@ class SearchFragmentTest : BaseActivityTest() {
fun searchFragmentSimple() { fun searchFragmentSimple() {
ActivityScenario.launch(KiwixMainActivity::class.java).onActivity { ActivityScenario.launch(KiwixMainActivity::class.java).onActivity {
kiwixMainActivity = it kiwixMainActivity = it
kiwixMainActivity.navigate(R.id.libraryFragment)
} }
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong()) val loadFileStream =
try { SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim")
downloadRobot { val zimFile = File(context.cacheDir, "testzim.zim")
clickLibraryOnBottomNav() if (zimFile.exists()) zimFile.delete()
deleteZimIfExists(false) zimFile.createNewFile()
clickDownloadOnBottomNav() loadFileStream.use { inputStream ->
waitForDataToLoad() val outputStream: OutputStream = FileOutputStream(zimFile)
downloadZimFile() outputStream.use { it ->
assertDownloadStart() val buffer = ByteArray(inputStream.available())
waitUntilDownloadComplete() var length: Int
clickLibraryOnBottomNav() while (inputStream.read(buffer).also { length = it } > 0) {
checkIfZimFileDownloaded() it.write(buffer, 0, length)
downloadZimFile()
} }
} 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) } search { checkZimFileSearchSuccessful(R.id.readerFragment) }
UiThreadStatement.runOnUiThread { 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 { search {
clickOnSearchItemInSearchList() clickOnSearchItemInSearchList()