From 731b0a584345896d0caf52f69d00c7e05409e198 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Wed, 26 Jul 2023 20:21:12 +0530 Subject: [PATCH] Refactored SearchFragmentTest * As we can not search in that zim file which is not contains the FT Xapian index and testzim.zim file is not contain it so we have improved our test case. * Now we are downloading the `off the grid` zim file and performing the search functionality inside it. --- .../kiwixmobile/search/SearchFragmentTest.kt | 54 ++++++++----------- .../kiwix/kiwixmobile/search/SearchRobot.kt | 12 ++++- 2 files changed, 32 insertions(+), 34 deletions(-) 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 8bee43c52..621c5cdd4 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchFragmentTest.kt @@ -18,28 +18,27 @@ 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() { @@ -68,40 +67,29 @@ class SearchFragmentTest : BaseActivityTest() { fun searchFragmentSimple() { ActivityScenario.launch(KiwixMainActivity::class.java).onActivity { kiwixMainActivity = it - kiwixMainActivity.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(inputStream.available()) - var length: Int - while (inputStream.read(buffer).also { length = it } > 0) { - it.write(buffer, 0, length) - } + BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong()) + try { + downloadRobot { + clickLibraryOnBottomNav() + deleteZimIfExists(false) + clickDownloadOnBottomNav() + waitForDataToLoad() + downloadZimFile() + assertDownloadStart() + waitUntilDownloadComplete() + clickLibraryOnBottomNav() + checkIfZimFileDownloaded() + downloadZimFile() } - } - UiThreadStatement.runOnUiThread { - kiwixMainActivity.navigate( - actionNavigationLibraryToNavigationReader() - .apply { zimFileUri = zimFile.toUri().toString() } + } catch (e: Exception) { + Assert.fail( + "Couldn't find downloaded file ' Off the Grid ' Original Exception: ${e.message}" ) } search { checkZimFileSearchSuccessful(R.id.readerFragment) } UiThreadStatement.runOnUiThread { - 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()}" - ) - } + kiwixMainActivity.openSearch(searchString = "100R") } search { clickOnSearchItemInSearchList() diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchRobot.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchRobot.kt index 8cbe2957f..1a09dfc58 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchRobot.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/search/SearchRobot.kt @@ -18,6 +18,11 @@ package org.kiwix.kiwixmobile.search +import androidx.recyclerview.widget.RecyclerView +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition +import androidx.test.espresso.matcher.ViewMatchers.withId import applyWithViewHierarchyPrinting import com.adevinta.android.barista.interaction.BaristaSleepInteractions import org.kiwix.kiwixmobile.BaseRobot @@ -32,7 +37,12 @@ class SearchRobot : BaseRobot() { fun clickOnSearchItemInSearchList() { BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS_FOR_SEARCH_TEST.toLong()) isVisible(ViewId(R.id.search_list)) - clickOn(ViewId(R.id.list_item_search_text)) + onView(withId(R.id.search_list)).perform( + actionOnItemAtPosition( + 0, + click() + ) + ) } fun checkZimFileSearchSuccessful(readerFragment: Int) {