mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-07 22:31:17 -04:00
Search functionality fix and Test Added
This commit is contained in:
parent
cbbb72bd56
commit
a581b4ef0e
@ -17,19 +17,67 @@
|
|||||||
*/
|
*/
|
||||||
package org.kiwix.kiwixmobile.search
|
package org.kiwix.kiwixmobile.search
|
||||||
|
|
||||||
|
import androidx.core.content.edit
|
||||||
|
import androidx.core.net.toUri
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
import androidx.test.internal.runner.junit4.statement.UiThreadStatement
|
import androidx.test.internal.runner.junit4.statement.UiThreadStatement
|
||||||
import com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertDisplayed
|
import androidx.test.rule.ActivityTestRule
|
||||||
import org.junit.Before
|
|
||||||
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.main.KiwixMainActivity
|
||||||
|
import org.kiwix.kiwixmobile.nav.destination.library.LocalLibraryFragmentDirections.actionNavigationLibraryToNavigationReader
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.io.OutputStream
|
||||||
|
|
||||||
class SearchFragmentTest : BaseActivityTest() {
|
class SearchFragmentTest : BaseActivityTest() {
|
||||||
@Before fun setUp() {
|
override var activityRule: ActivityTestRule<KiwixMainActivity> = activityTestRule {
|
||||||
UiThreadStatement.runOnUiThread { activityRule.activity.navigate(R.id.searchFragment) }
|
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||||
|
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
|
||||||
|
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun searchFragmentSimple() {
|
@Test fun searchFragmentSimple() {
|
||||||
assertDisplayed(R.string.menu_search_in_text)
|
UiThreadStatement.runOnUiThread { activityRule.activity.navigate(R.id.libraryFragment) }
|
||||||
|
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(1024)
|
||||||
|
var length: Int
|
||||||
|
while (inputStream.read(buffer).also { length = it } > 0) {
|
||||||
|
it.write(buffer, 0, length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UiThreadStatement.runOnUiThread {
|
||||||
|
activityRule.activity.navigate(
|
||||||
|
actionNavigationLibraryToNavigationReader()
|
||||||
|
.apply { zimFileUri = zimFile.toUri().toString() }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
search { checkZimFileSearchSuccessful(R.id.readerFragment) }
|
||||||
|
UiThreadStatement.runOnUiThread {
|
||||||
|
if (zimFile.canRead()) {
|
||||||
|
activityRule.activity.openSearch(searchString = "Android")
|
||||||
|
} else {
|
||||||
|
throw RuntimeException(
|
||||||
|
"File $zimFile is not readable." +
|
||||||
|
" Original File $zimFile is readable = ${zimFile.canRead()}" +
|
||||||
|
" Size ${zimFile.length()}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
search {
|
||||||
|
clickOnSearchItemInSearchList()
|
||||||
|
checkZimFileSearchSuccessful(R.id.readerFragment)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Kiwix Android
|
||||||
|
* Copyright (c) 2022 Kiwix <android.kiwix.org>
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.kiwixmobile.search
|
||||||
|
|
||||||
|
import android.os.Build
|
||||||
|
import applyWithViewHierarchyPrinting
|
||||||
|
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
|
||||||
|
import org.kiwix.kiwixmobile.BaseRobot
|
||||||
|
import org.kiwix.kiwixmobile.Findable.ViewId
|
||||||
|
import org.kiwix.kiwixmobile.core.R
|
||||||
|
import org.kiwix.kiwixmobile.testutils.TestUtils
|
||||||
|
|
||||||
|
fun search(func: SearchRobot.() -> Unit) = SearchRobot().applyWithViewHierarchyPrinting(func)
|
||||||
|
|
||||||
|
class SearchRobot : BaseRobot() {
|
||||||
|
|
||||||
|
fun clickOnSearchItemInSearchList() {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
||||||
|
pressBack()
|
||||||
|
}
|
||||||
|
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS_FOR_SEARCH_TEST.toLong())
|
||||||
|
isVisible(ViewId(R.id.search_list))
|
||||||
|
clickOn(ViewId(R.id.list_item_search_text))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun checkZimFileSearchSuccessful(readerFragment: Int) {
|
||||||
|
isVisible(ViewId(readerFragment))
|
||||||
|
}
|
||||||
|
}
|
@ -49,6 +49,7 @@ import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book;
|
|||||||
public class TestUtils {
|
public class TestUtils {
|
||||||
private static final String TAG = "TESTUTILS";
|
private static final String TAG = "TESTUTILS";
|
||||||
public static int TEST_PAUSE_MS = 250;
|
public static int TEST_PAUSE_MS = 250;
|
||||||
|
public static int TEST_PAUSE_MS_FOR_SEARCH_TEST = 1000;
|
||||||
/*
|
/*
|
||||||
TEST_PAUSE_MS is used as such:
|
TEST_PAUSE_MS is used as such:
|
||||||
BaristaSleepInteractions.sleep(TEST_PAUSE_MS);
|
BaristaSleepInteractions.sleep(TEST_PAUSE_MS);
|
||||||
@ -143,4 +144,3 @@ public class TestUtils {
|
|||||||
return targetContext.getResources().getString(id);
|
return targetContext.getResources().getString(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,4 +130,11 @@ object ActivityExtensions {
|
|||||||
result
|
result
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T> Activity.setNavigationResultOnCurrent(result: T, key: String = "result") {
|
||||||
|
coreMainActivity.navController.currentBackStackEntry?.savedStateHandle?.set(
|
||||||
|
key,
|
||||||
|
result
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,8 +55,8 @@ import org.kiwix.kiwixmobile.core.search.viewmodel.effects.OpenSearchItem
|
|||||||
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.PopFragmentBackstack
|
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.PopFragmentBackstack
|
||||||
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.ProcessActivityResult
|
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.ProcessActivityResult
|
||||||
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SaveSearchToRecents
|
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SaveSearchToRecents
|
||||||
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchInPreviousScreen
|
|
||||||
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchArgumentProcessing
|
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchArgumentProcessing
|
||||||
|
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchInPreviousScreen
|
||||||
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.ShowDeleteSearchDialog
|
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.ShowDeleteSearchDialog
|
||||||
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.ShowToast
|
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.ShowToast
|
||||||
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.StartSpeechInput
|
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.StartSpeechInput
|
||||||
@ -140,7 +140,7 @@ class SearchViewModel @Inject constructor(
|
|||||||
|
|
||||||
private fun saveSearchAndOpenItem(searchListItem: SearchListItem, openInNewTab: Boolean) {
|
private fun saveSearchAndOpenItem(searchListItem: SearchListItem, openInNewTab: Boolean) {
|
||||||
_effects.offer(SaveSearchToRecents(recentSearchDao, searchListItem, zimReaderContainer.id))
|
_effects.offer(SaveSearchToRecents(recentSearchDao, searchListItem, zimReaderContainer.id))
|
||||||
_effects.offer(OpenSearchItem(searchListItem, openInNewTab))
|
_effects.sendBlocking(OpenSearchItem(searchListItem, openInNewTab))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ import android.os.Parcelable
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import kotlinx.android.parcel.Parcelize
|
import kotlinx.android.parcel.Parcelize
|
||||||
import org.kiwix.kiwixmobile.core.base.SideEffect
|
import org.kiwix.kiwixmobile.core.base.SideEffect
|
||||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setNavigationResult
|
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setNavigationResultOnCurrent
|
||||||
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
||||||
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem
|
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem
|
||||||
import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED
|
import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED
|
||||||
@ -32,12 +32,12 @@ data class OpenSearchItem(
|
|||||||
private val openInNewTab: Boolean = false
|
private val openInNewTab: Boolean = false
|
||||||
) : SideEffect<Unit> {
|
) : SideEffect<Unit> {
|
||||||
override fun invokeWith(activity: AppCompatActivity) {
|
override fun invokeWith(activity: AppCompatActivity) {
|
||||||
activity.setNavigationResult(
|
val readerFragmentResId = (activity as CoreMainActivity).readerFragmentResId
|
||||||
|
activity.navigate(readerFragmentResId)
|
||||||
|
activity.setNavigationResultOnCurrent(
|
||||||
SearchItemToOpen(searchListItem.value, openInNewTab),
|
SearchItemToOpen(searchListItem.value, openInNewTab),
|
||||||
TAG_FILE_SEARCHED
|
TAG_FILE_SEARCHED
|
||||||
)
|
)
|
||||||
val readerFragmentResId = (activity as CoreMainActivity).readerFragmentResId
|
|
||||||
activity.navigate(readerFragmentResId)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ import io.mockk.mockk
|
|||||||
import io.mockk.mockkConstructor
|
import io.mockk.mockkConstructor
|
||||||
import io.mockk.verify
|
import io.mockk.verify
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setNavigationResult
|
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setNavigationResultOnCurrent
|
||||||
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
||||||
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem
|
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem
|
||||||
import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED
|
import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED
|
||||||
@ -44,11 +44,11 @@ internal class OpenSearchItemTest {
|
|||||||
} returns intent
|
} returns intent
|
||||||
OpenSearchItem(searchListItem, false).invokeWith(activity)
|
OpenSearchItem(searchListItem, false).invokeWith(activity)
|
||||||
verify {
|
verify {
|
||||||
activity.setNavigationResult(
|
activity.navigate(activity.readerFragmentResId)
|
||||||
|
activity.setNavigationResultOnCurrent(
|
||||||
SearchItemToOpen(searchListItem.value, false),
|
SearchItemToOpen(searchListItem.value, false),
|
||||||
TAG_FILE_SEARCHED
|
TAG_FILE_SEARCHED
|
||||||
)
|
)
|
||||||
activity.navigate(activity.readerFragmentResId)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,11 +64,11 @@ internal class OpenSearchItemTest {
|
|||||||
} returns intent
|
} returns intent
|
||||||
OpenSearchItem(searchListItem, true).invokeWith(activity)
|
OpenSearchItem(searchListItem, true).invokeWith(activity)
|
||||||
verify {
|
verify {
|
||||||
activity.setNavigationResult(
|
activity.navigate(activity.readerFragmentResId)
|
||||||
|
activity.setNavigationResultOnCurrent(
|
||||||
SearchItemToOpen(searchListItem.value, true),
|
SearchItemToOpen(searchListItem.value, true),
|
||||||
TAG_FILE_SEARCHED
|
TAG_FILE_SEARCHED
|
||||||
)
|
)
|
||||||
activity.navigate(activity.readerFragmentResId)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
<ignore regexp=".*SettingsRobot.kt.*"/>
|
<ignore regexp=".*SettingsRobot.kt.*"/>
|
||||||
<ignore regexp=".*TopLevelDestinationRobot.kt.*"/>
|
<ignore regexp=".*TopLevelDestinationRobot.kt.*"/>
|
||||||
<ignore regexp=".*ZimHostRobot.kt.*"/>
|
<ignore regexp=".*ZimHostRobot.kt.*"/>
|
||||||
|
<ignore regexp=".*SearchRobot.kt.*"/>
|
||||||
</issue>
|
</issue>
|
||||||
<issue id="TypographyEllipsis">
|
<issue id="TypographyEllipsis">
|
||||||
<ignore path="**-iw/**.xml" />
|
<ignore path="**-iw/**.xml" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user