diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt index 71bc2df83..07a3387b8 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt @@ -17,19 +17,67 @@ */ 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 com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertDisplayed -import org.junit.Before +import androidx.test.rule.ActivityTestRule 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.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() { - @Before fun setUp() { - UiThreadStatement.runOnUiThread { activityRule.activity.navigate(R.id.searchFragment) } + override var activityRule: ActivityTestRule = activityTestRule { + PreferenceManager.getDefaultSharedPreferences(context).edit { + putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false) + putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false) + } } @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) + } } } diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchRobot.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchRobot.kt new file mode 100644 index 000000000..4bd66f521 --- /dev/null +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchRobot.kt @@ -0,0 +1,45 @@ +/* + * Kiwix Android + * Copyright (c) 2022 Kiwix + * 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 . + * + */ + +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)) + } +} diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.java b/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.java index 7f76c0257..cac9de228 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.java +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/testutils/TestUtils.java @@ -49,6 +49,7 @@ import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book; public class TestUtils { private static final String TAG = "TESTUTILS"; public static int TEST_PAUSE_MS = 250; + public static int TEST_PAUSE_MS_FOR_SEARCH_TEST = 1000; /* TEST_PAUSE_MS is used as such: BaristaSleepInteractions.sleep(TEST_PAUSE_MS); @@ -143,4 +144,3 @@ public class TestUtils { return targetContext.getResources().getString(id); } } - diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ActivityExtensions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ActivityExtensions.kt index 02770accb..3a46ffeca 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ActivityExtensions.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ActivityExtensions.kt @@ -130,4 +130,11 @@ object ActivityExtensions { result ) } + + fun Activity.setNavigationResultOnCurrent(result: T, key: String = "result") { + coreMainActivity.navController.currentBackStackEntry?.savedStateHandle?.set( + key, + result + ) + } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModel.kt index d37317b6a..d639f85b6 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModel.kt @@ -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.ProcessActivityResult 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.SearchInPreviousScreen 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.StartSpeechInput @@ -140,7 +140,7 @@ class SearchViewModel @Inject constructor( private fun saveSearchAndOpenItem(searchListItem: SearchListItem, openInNewTab: Boolean) { _effects.offer(SaveSearchToRecents(recentSearchDao, searchListItem, zimReaderContainer.id)) - _effects.offer(OpenSearchItem(searchListItem, openInNewTab)) + _effects.sendBlocking(OpenSearchItem(searchListItem, openInNewTab)) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/OpenSearchItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/OpenSearchItem.kt index e6ce4b8e7..6cf9159ad 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/OpenSearchItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/OpenSearchItem.kt @@ -22,7 +22,7 @@ import android.os.Parcelable import androidx.appcompat.app.AppCompatActivity import kotlinx.android.parcel.Parcelize 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.search.adapter.SearchListItem import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED @@ -32,12 +32,12 @@ data class OpenSearchItem( private val openInNewTab: Boolean = false ) : SideEffect { override fun invokeWith(activity: AppCompatActivity) { - activity.setNavigationResult( + val readerFragmentResId = (activity as CoreMainActivity).readerFragmentResId + activity.navigate(readerFragmentResId) + activity.setNavigationResultOnCurrent( SearchItemToOpen(searchListItem.value, openInNewTab), TAG_FILE_SEARCHED ) - val readerFragmentResId = (activity as CoreMainActivity).readerFragmentResId - activity.navigate(readerFragmentResId) } } diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/OpenSearchItemTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/OpenSearchItemTest.kt index f0aa2cd68..49e7409a2 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/OpenSearchItemTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/OpenSearchItemTest.kt @@ -24,7 +24,7 @@ import io.mockk.mockk import io.mockk.mockkConstructor import io.mockk.verify 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.search.adapter.SearchListItem.RecentSearchListItem import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED @@ -44,11 +44,11 @@ internal class OpenSearchItemTest { } returns intent OpenSearchItem(searchListItem, false).invokeWith(activity) verify { - activity.setNavigationResult( + activity.navigate(activity.readerFragmentResId) + activity.setNavigationResultOnCurrent( SearchItemToOpen(searchListItem.value, false), TAG_FILE_SEARCHED ) - activity.navigate(activity.readerFragmentResId) } } @@ -64,11 +64,11 @@ internal class OpenSearchItemTest { } returns intent OpenSearchItem(searchListItem, true).invokeWith(activity) verify { - activity.setNavigationResult( + activity.navigate(activity.readerFragmentResId) + activity.setNavigationResultOnCurrent( SearchItemToOpen(searchListItem.value, true), TAG_FILE_SEARCHED ) - activity.navigate(activity.readerFragmentResId) } } } diff --git a/lintConfig.xml b/lintConfig.xml index 6e3f77fa0..4ab2543df 100644 --- a/lintConfig.xml +++ b/lintConfig.xml @@ -17,6 +17,7 @@ +