Refactored the test case to use the scientific-alliance zim file since this zim can reproduce this bug.

This commit is contained in:
MohitMaliFtechiz 2024-07-03 18:09:30 +05:30
parent 32e1ba69de
commit 01cce5307e
2 changed files with 44 additions and 69 deletions

View File

@ -28,21 +28,16 @@ import androidx.navigation.fragment.NavHostFragment
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.pressBack import androidx.test.espresso.Espresso.pressBack
import androidx.test.espresso.accessibility.AccessibilityChecks
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
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.rule.GrantPermissionRule import androidx.test.rule.GrantPermissionRule
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResultUtils
import com.google.android.apps.common.testing.accessibility.framework.checks.TouchTargetSizeCheck
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.ResponseBody import okhttp3.ResponseBody
import org.hamcrest.Matchers
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
@ -86,7 +81,7 @@ class SearchFragmentTestForCustomApp {
private lateinit var activityScenario: ActivityScenario<CustomMainActivity> private lateinit var activityScenario: ActivityScenario<CustomMainActivity>
private val rayCharlesZimFileUrl = private val rayCharlesZimFileUrl =
"https://dev.kiwix.org/kiwix-android/test/wikipedia_en_ray_charles_maxi_2023-12.zim" "https://download.kiwix.org/zim/zimit/scientific-alliance.obscurative.ru_ru_all_2024-06.zim"
@Before @Before
fun waitForIdle() { fun waitForIdle() {
@ -115,20 +110,6 @@ class SearchFragmentTestForCustomApp {
} }
} }
init {
AccessibilityChecks.enable().apply {
setRunChecksFromRootView(true)
setSuppressingResultMatcher(
Matchers.allOf(
AccessibilityCheckResultUtils.matchesCheck(TouchTargetSizeCheck::class.java),
AccessibilityCheckResultUtils.matchesViews(
ViewMatchers.withId(org.kiwix.kiwixmobile.core.R.id.menu_searchintext)
)
)
)
}
}
@Test @Test
fun searchFragment() { fun searchFragment() {
activityScenario.onActivity { activityScenario.onActivity {
@ -136,18 +117,16 @@ class SearchFragmentTestForCustomApp {
} }
// test with a large ZIM file to properly test the scenario // test with a large ZIM file to properly test the scenario
downloadingZimFile = getDownloadingZimFile() downloadingZimFile = getDownloadingZimFile()
if (downloadingZimFile.length() == 0L) { OkHttpClient().newCall(downloadRequest()).execute().use { response ->
OkHttpClient().newCall(downloadRequest()).execute().use { response -> if (response.isSuccessful) {
if (response.isSuccessful) { response.body?.let { responseBody ->
response.body?.let { responseBody -> writeZimFileData(responseBody, downloadingZimFile)
writeZimFileData(responseBody, downloadingZimFile)
}
} else {
throw RuntimeException(
"Download Failed. Error: ${response.message}\n" +
" Status Code: ${response.code}"
)
} }
} else {
throw RuntimeException(
"Download Failed. Error: ${response.message}\n" +
" Status Code: ${response.code}"
)
} }
} }
UiThreadStatement.runOnUiThread { UiThreadStatement.runOnUiThread {
@ -155,8 +134,8 @@ class SearchFragmentTestForCustomApp {
} }
openZimFileInReaderWithAssetFileDescriptor(downloadingZimFile) openZimFileInReaderWithAssetFileDescriptor(downloadingZimFile)
openSearchWithQuery() openSearchWithQuery()
val searchTerm = "A Fool" val searchTerm = "gard"
val searchedItem = "A Fool for You" val searchedItem = "Gardanta Spirito"
search { search {
// test with fast typing/deleting // test with fast typing/deleting
searchWithFrequentlyTypedWords(searchTerm) searchWithFrequentlyTypedWords(searchTerm)
@ -185,15 +164,15 @@ class SearchFragmentTestForCustomApp {
search { search {
// test by searching 10 article and clicking on them // test by searching 10 article and clicking on them
searchAndClickOnArticle(searchTerm) searchAndClickOnArticle(searchTerm)
searchAndClickOnArticle("A Song") searchAndClickOnArticle("eilum")
searchAndClickOnArticle("The Ra") searchAndClickOnArticle("page")
searchAndClickOnArticle("The Ge") searchAndClickOnArticle("list")
searchAndClickOnArticle("Wish") searchAndClickOnArticle("ladder")
searchAndClickOnArticle("WIFI") searchAndClickOnArticle("welc")
searchAndClickOnArticle("Woman") searchAndClickOnArticle("js")
searchAndClickOnArticle("Big Ba") searchAndClickOnArticle("hizo")
searchAndClickOnArticle("My Wor") searchAndClickOnArticle("fad")
searchAndClickOnArticle("100") searchAndClickOnArticle("forum")
assertArticleLoaded() assertArticleLoaded()
} }
} }
@ -201,33 +180,31 @@ class SearchFragmentTestForCustomApp {
@Test @Test
fun testConcurrencyOfSearch() = runBlocking { fun testConcurrencyOfSearch() = runBlocking {
val searchTerms = listOf( val searchTerms = listOf(
"A Song", "eilum",
"The Ra", "page",
"The Ge", "list",
"Wish", "ladder",
"WIFI", "welc",
"Woman", "js",
"Big Ba", "hizo",
"My Wor", "fad",
"100" "forum"
) )
activityScenario.onActivity { activityScenario.onActivity {
customMainActivity = it customMainActivity = it
} }
// test with a large ZIM file to properly test the scenario // test with a large ZIM file to properly test the scenario
downloadingZimFile = getDownloadingZimFile() downloadingZimFile = getDownloadingZimFile()
if (downloadingZimFile.length() == 0L) { OkHttpClient().newCall(downloadRequest()).execute().use { response ->
OkHttpClient().newCall(downloadRequest()).execute().use { response -> if (response.isSuccessful) {
if (response.isSuccessful) { response.body?.let { responseBody ->
response.body?.let { responseBody -> writeZimFileData(responseBody, downloadingZimFile)
writeZimFileData(responseBody, downloadingZimFile)
}
} else {
throw RuntimeException(
"Download Failed. Error: ${response.message}\n" +
" Status Code: ${response.code}"
)
} }
} else {
throw RuntimeException(
"Download Failed. Error: ${response.message}\n" +
" Status Code: ${response.code}"
)
} }
} }
UiThreadStatement.runOnUiThread { UiThreadStatement.runOnUiThread {
@ -328,12 +305,10 @@ class SearchFragmentTestForCustomApp {
.url(URI.create(rayCharlesZimFileUrl).toURL()) .url(URI.create(rayCharlesZimFileUrl).toURL())
.build() .build()
private fun getDownloadingZimFile(isDeletePreviousZimFile: Boolean = true): File { private fun getDownloadingZimFile(): File {
val zimFile = File(context.cacheDir, "ray_charles.zim") val zimFile = File(context.cacheDir, "ray_charles.zim")
if (isDeletePreviousZimFile) { if (zimFile.exists()) zimFile.delete()
if (zimFile.exists()) zimFile.delete() zimFile.createNewFile()
zimFile.createNewFile()
}
return zimFile return zimFile
} }
} }

View File

@ -58,7 +58,7 @@ class SearchRobot {
ViewAssertions.matches( ViewAssertions.matches(
HelperMatchers.atPosition( HelperMatchers.atPosition(
0, 0,
ViewMatchers.hasDescendant(ViewMatchers.withText(searchResult)) ViewMatchers.hasDescendant(ViewMatchers.withSubstring(searchResult))
) )
) )
) )
@ -113,7 +113,7 @@ class SearchRobot {
.withElement( .withElement(
DriverAtoms.findElement( DriverAtoms.findElement(
Locator.XPATH, Locator.XPATH,
"//*[contains(text(), 'Big Baby DRAM')]" "//*[contains(text(), 'Forum Category')]"
) )
) )
}) })