Skip to content

Commit a1b5f36

Browse files
Improved SearchFragmentTest:
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.
1 parent c4dbe47 commit a1b5f36

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,28 @@
1818
package org.kiwix.kiwixmobile.search
1919

2020
import androidx.core.content.edit
21+
import androidx.core.net.toUri
2122
import androidx.preference.PreferenceManager
2223
import androidx.test.core.app.ActivityScenario
2324
import androidx.test.internal.runner.junit4.statement.UiThreadStatement
2425
import androidx.test.platform.app.InstrumentationRegistry
2526
import androidx.test.uiautomator.UiDevice
26-
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
2727
import leakcanary.LeakAssertions
2828
import org.junit.After
29-
import org.junit.Assert
3029
import org.junit.Before
3130
import org.junit.Rule
3231
import org.junit.Test
3332
import org.kiwix.kiwixmobile.BaseActivityTest
3433
import org.kiwix.kiwixmobile.R
3534
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
36-
import org.kiwix.kiwixmobile.download.downloadRobot
3735
import org.kiwix.kiwixmobile.main.KiwixMainActivity
36+
import org.kiwix.kiwixmobile.nav.destination.library.LocalLibraryFragmentDirections.actionNavigationLibraryToNavigationReader
3837
import org.kiwix.kiwixmobile.testutils.RetryRule
39-
import org.kiwix.kiwixmobile.testutils.TestUtils
4038
import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs
4139
import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible
40+
import java.io.File
41+
import java.io.FileOutputStream
42+
import java.io.OutputStream
4243

4344
class SearchFragmentTest : BaseActivityTest() {
4445

@@ -67,29 +68,40 @@ class SearchFragmentTest : BaseActivityTest() {
6768
fun searchFragmentSimple() {
6869
ActivityScenario.launch(KiwixMainActivity::class.java).onActivity {
6970
kiwixMainActivity = it
71+
kiwixMainActivity.navigate(R.id.libraryFragment)
7072
}
71-
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
72-
try {
73-
downloadRobot {
74-
clickLibraryOnBottomNav()
75-
deleteZimIfExists(false)
76-
clickDownloadOnBottomNav()
77-
waitForDataToLoad()
78-
downloadZimFile()
79-
assertDownloadStart()
80-
waitUntilDownloadComplete()
81-
clickLibraryOnBottomNav()
82-
checkIfZimFileDownloaded()
83-
downloadZimFile()
73+
val loadFileStream =
74+
SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim")
75+
val zimFile = File(context.cacheDir, "testzim.zim")
76+
if (zimFile.exists()) zimFile.delete()
77+
zimFile.createNewFile()
78+
loadFileStream.use { inputStream ->
79+
val outputStream: OutputStream = FileOutputStream(zimFile)
80+
outputStream.use { it ->
81+
val buffer = ByteArray(inputStream.available())
82+
var length: Int
83+
while (inputStream.read(buffer).also { length = it } > 0) {
84+
it.write(buffer, 0, length)
85+
}
8486
}
85-
} catch (e: Exception) {
86-
Assert.fail(
87-
"Couldn't find downloaded file ' Off the Grid ' Original Exception: ${e.message}"
87+
}
88+
UiThreadStatement.runOnUiThread {
89+
kiwixMainActivity.navigate(
90+
actionNavigationLibraryToNavigationReader()
91+
.apply { zimFileUri = zimFile.toUri().toString() }
8892
)
8993
}
9094
search { checkZimFileSearchSuccessful(R.id.readerFragment) }
9195
UiThreadStatement.runOnUiThread {
92-
kiwixMainActivity.openSearch(searchString = "100R")
96+
if (zimFile.canRead()) {
97+
kiwixMainActivity.openSearch(searchString = "Android")
98+
} else {
99+
throw RuntimeException(
100+
"File $zimFile is not readable." +
101+
" Original File $zimFile is readable = ${zimFile.canRead()}" +
102+
" Size ${zimFile.length()}"
103+
)
104+
}
93105
}
94106
search {
95107
clickOnSearchItemInSearchList()

0 commit comments

Comments
 (0)