Fixed: Test cases are frequently failing on API level 33.

* Refreshed the ZIM files list before deleting/checking for downloaded ZIM files in `DownloadTest` and `InitialDownloadTest` because sometimes data doesn't show on the `LocalLibraryScreen` after downloading the ZIM file, leading to test failures.
* Hiding the `Storage Restriction` dialog in `LibkiwixBookTest` and `MimeTypeTest` to not show this dialog in these test cases.
* Improved the temporary ZIM file path in `NoteFragmentTest` to properly display the ZIM file in the LocalLibrary screen so that it can be deleted in our test case.
* Enhanced the loading of ZIM files in `LocalLibraryTest`.
This commit is contained in:
MohitMaliFtechiz 2024-03-05 14:40:16 +05:30
parent 28945412f3
commit 291a063747
8 changed files with 37 additions and 15 deletions

View File

@ -28,11 +28,13 @@ import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.espresso.matcher.ViewMatchers.withText
import applyWithViewHierarchyPrinting import applyWithViewHierarchyPrinting
import com.adevinta.android.barista.interaction.BaristaSleepInteractions import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh
import junit.framework.AssertionFailedError import junit.framework.AssertionFailedError
import org.kiwix.kiwixmobile.BaseRobot import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.Text import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.Findable.ViewId import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.download.DownloadTest.Companion.KIWIX_DOWNLOAD_TEST
import org.kiwix.kiwixmobile.testutils.TestUtils import org.kiwix.kiwixmobile.testutils.TestUtils
fun downloadRobot(func: DownloadRobot.() -> Unit) = fun downloadRobot(func: DownloadRobot.() -> Unit) =
@ -185,4 +187,13 @@ class DownloadRobot : BaseRobot() {
) )
} }
} }
fun refreshLocalLibraryData() {
try {
refresh(R.id.zim_swiperefresh)
pauseForBetterTestPerformance()
} catch (e: RuntimeException) {
Log.w(KIWIX_DOWNLOAD_TEST, "Failed to refresh ZIM list: " + e.localizedMessage)
}
}
} }

View File

@ -17,7 +17,6 @@
*/ */
package org.kiwix.kiwixmobile.download package org.kiwix.kiwixmobile.download
import android.util.Log
import androidx.core.content.edit import androidx.core.content.edit
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
@ -29,7 +28,6 @@ import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
import com.adevinta.android.barista.interaction.BaristaSleepInteractions import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh
import leakcanary.LeakAssertions import leakcanary.LeakAssertions
import org.junit.After import org.junit.After
import org.junit.Assert import org.junit.Assert
@ -39,7 +37,6 @@ import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.main.KiwixMainActivity import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.testutils.RetryRule import org.kiwix.kiwixmobile.testutils.RetryRule
@ -86,6 +83,7 @@ class DownloadTest : BaseActivityTest() {
try { try {
downloadRobot { downloadRobot {
clickLibraryOnBottomNav() clickLibraryOnBottomNav()
refreshLocalLibraryData()
deleteZimIfExists(false) deleteZimIfExists(false)
clickDownloadOnBottomNav() clickDownloadOnBottomNav()
waitForDataToLoad() waitForDataToLoad()
@ -98,19 +96,17 @@ class DownloadTest : BaseActivityTest() {
assertDownloadResumed() assertDownloadResumed()
waitUntilDownloadComplete() waitUntilDownloadComplete()
clickLibraryOnBottomNav() clickLibraryOnBottomNav()
// refresh the local library list to show the downloaded zim file
refreshLocalLibraryData()
checkIfZimFileDownloaded() checkIfZimFileDownloaded()
deleteZimIfExists(true) deleteZimIfExists(true)
refreshLocalLibraryData()
} }
} catch (e: Exception) { } catch (e: Exception) {
Assert.fail( Assert.fail(
"Couldn't find downloaded file ' Off the Grid ' Original Exception: ${e.message}" "Couldn't find downloaded file ' Off the Grid ' Original Exception: ${e.message}"
) )
} }
try {
refresh(R.id.zim_swiperefresh)
} catch (e: RuntimeException) {
Log.w(KIWIX_DOWNLOAD_TEST, "Failed to refresh ZIM list: " + e.localizedMessage)
}
LeakAssertions.assertNoLeaks() LeakAssertions.assertNoLeaks()
} }
@ -124,7 +120,7 @@ class DownloadTest : BaseActivityTest() {
} }
companion object { companion object {
private const val KIWIX_DOWNLOAD_TEST = "kiwixDownloadTest" const val KIWIX_DOWNLOAD_TEST = "kiwixDownloadTest"
@BeforeClass @BeforeClass
fun beforeClass() { fun beforeClass() {

View File

@ -91,10 +91,19 @@ class InitialDownloadRobot : BaseRobot() {
} }
} }
fun refreshList() { fun refreshOnlineList() {
refresh(R.id.librarySwipeRefresh) refresh(R.id.librarySwipeRefresh)
} }
fun refreshLocalLibraryData() {
try {
refresh(R.id.zim_swiperefresh)
pauseForBetterTestPerformance()
} catch (e: RuntimeException) {
Log.w("InitialDownloadTest", "Failed to refresh ZIM list: " + e.localizedMessage)
}
}
fun waitForDataToLoad() { fun waitForDataToLoad() {
try { try {
isVisible(Text(zimFileTitle)) isVisible(Text(zimFileTitle))

View File

@ -75,11 +75,12 @@ class InitialDownloadTest : BaseActivityTest() {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS_FOR_SEARCH_TEST.toLong()) BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS_FOR_SEARCH_TEST.toLong())
initialDownload { initialDownload {
clickLibraryOnBottomNav() clickLibraryOnBottomNav()
refreshLocalLibraryData()
// This is for if download test fails for some reason after downloading the zim file // This is for if download test fails for some reason after downloading the zim file
deleteZimIfExists() deleteZimIfExists()
clickDownloadOnBottomNav() clickDownloadOnBottomNav()
assertLibraryListDisplayed() assertLibraryListDisplayed()
refreshList() refreshOnlineList()
waitForDataToLoad() waitForDataToLoad()
stopDownloadIfAlreadyStarted() stopDownloadIfAlreadyStarted()
downloadZimFile() downloadZimFile()

View File

@ -27,7 +27,6 @@ import androidx.test.uiautomator.UiDevice
import org.junit.Assert import org.junit.Assert
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.kiwix.libzim.Archive
import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.core.NightModeConfig import org.kiwix.kiwixmobile.core.NightModeConfig
import org.kiwix.kiwixmobile.core.reader.ZimFileReader import org.kiwix.kiwixmobile.core.reader.ZimFileReader
@ -35,6 +34,7 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.main.KiwixMainActivity import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs
import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible
import org.kiwix.libzim.Archive
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.OutputStream import java.io.OutputStream
@ -53,6 +53,7 @@ class MimeTypeTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false) putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false) putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false) putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
} }
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply { activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED) moveToState(Lifecycle.State.RESUMED)

View File

@ -34,7 +34,6 @@ 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.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.main.KiwixMainActivity import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.search.SearchFragmentTest
import org.kiwix.kiwixmobile.testutils.RetryRule import org.kiwix.kiwixmobile.testutils.RetryRule
import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs
import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible
@ -88,7 +87,7 @@ class LocalLibraryTest : BaseActivityTest() {
} }
// load a zim file to test, After downloading zim file library list is visible or not // load a zim file to test, After downloading zim file library list is visible or not
val loadFileStream = val loadFileStream =
SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim") LocalLibraryTest::class.java.classLoader.getResourceAsStream("testzim.zim")
val zimFile = val zimFile =
File( File(
ContextCompat.getExternalFilesDirs(context, null)[0], ContextCompat.getExternalFilesDirs(context, null)[0],

View File

@ -18,6 +18,7 @@
package org.kiwix.kiwixmobile.note package org.kiwix.kiwixmobile.note
import androidx.core.content.ContextCompat
import androidx.core.content.edit import androidx.core.content.edit
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
@ -93,7 +94,10 @@ class NoteFragmentTest : BaseActivityTest() {
val loadFileStream = val loadFileStream =
NoteFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim") NoteFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim")
val zimFile = File(context.cacheDir, "testzim.zim") val zimFile = File(
ContextCompat.getExternalFilesDirs(context, null)[0],
"testzim.zim"
)
if (zimFile.exists()) zimFile.delete() if (zimFile.exists()) zimFile.delete()
zimFile.createNewFile() zimFile.createNewFile()
loadFileStream.use { inputStream -> loadFileStream.use { inputStream ->

View File

@ -60,6 +60,7 @@ class LibkiwixBookmarkTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false) putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false) putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true) putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
} }
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply { activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED) moveToState(Lifecycle.State.RESUMED)