From 291a063747712f98aa009b7cd8b9727c7e89c02e Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Tue, 5 Mar 2024 14:40:16 +0530 Subject: [PATCH] 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`. --- .../kiwix/kiwixmobile/download/DownloadRobot.kt | 11 +++++++++++ .../org/kiwix/kiwixmobile/download/DownloadTest.kt | 14 +++++--------- .../initial/download/InitialDownloadRobot.kt | 11 ++++++++++- .../initial/download/InitialDownloadTest.kt | 3 ++- .../org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt | 3 ++- .../nav/destination/library/LocalLibraryTest.kt | 3 +-- .../org/kiwix/kiwixmobile/note/NoteFragmentTest.kt | 6 +++++- .../page/bookmarks/LibkiwixBookmarkTest.kt | 1 + 8 files changed, 37 insertions(+), 15 deletions(-) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadRobot.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadRobot.kt index fd987765f..28d644aba 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadRobot.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadRobot.kt @@ -28,11 +28,13 @@ import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.matcher.ViewMatchers.withText import applyWithViewHierarchyPrinting import com.adevinta.android.barista.interaction.BaristaSleepInteractions +import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh import junit.framework.AssertionFailedError import org.kiwix.kiwixmobile.BaseRobot import org.kiwix.kiwixmobile.Findable.Text import org.kiwix.kiwixmobile.Findable.ViewId import org.kiwix.kiwixmobile.R +import org.kiwix.kiwixmobile.download.DownloadTest.Companion.KIWIX_DOWNLOAD_TEST import org.kiwix.kiwixmobile.testutils.TestUtils 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) + } + } } diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt index 08e5a1bd9..831308144 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt @@ -17,7 +17,6 @@ */ package org.kiwix.kiwixmobile.download -import android.util.Log import androidx.core.content.edit import androidx.lifecycle.Lifecycle import androidx.preference.PreferenceManager @@ -29,7 +28,6 @@ import androidx.test.filters.LargeTest import androidx.test.platform.app.InstrumentationRegistry import androidx.test.uiautomator.UiDevice import com.adevinta.android.barista.interaction.BaristaSleepInteractions -import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh import leakcanary.LeakAssertions import org.junit.After import org.junit.Assert @@ -39,7 +37,6 @@ import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith 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.testutils.RetryRule @@ -86,6 +83,7 @@ class DownloadTest : BaseActivityTest() { try { downloadRobot { clickLibraryOnBottomNav() + refreshLocalLibraryData() deleteZimIfExists(false) clickDownloadOnBottomNav() waitForDataToLoad() @@ -98,19 +96,17 @@ class DownloadTest : BaseActivityTest() { assertDownloadResumed() waitUntilDownloadComplete() clickLibraryOnBottomNav() + // refresh the local library list to show the downloaded zim file + refreshLocalLibraryData() checkIfZimFileDownloaded() deleteZimIfExists(true) + refreshLocalLibraryData() } } catch (e: Exception) { Assert.fail( "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() } @@ -124,7 +120,7 @@ class DownloadTest : BaseActivityTest() { } companion object { - private const val KIWIX_DOWNLOAD_TEST = "kiwixDownloadTest" + const val KIWIX_DOWNLOAD_TEST = "kiwixDownloadTest" @BeforeClass fun beforeClass() { diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadRobot.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadRobot.kt index f73a69fb0..2667856d3 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadRobot.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadRobot.kt @@ -91,10 +91,19 @@ class InitialDownloadRobot : BaseRobot() { } } - fun refreshList() { + fun refreshOnlineList() { 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() { try { isVisible(Text(zimFileTitle)) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadTest.kt index bed20071d..7fa872b6c 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadTest.kt @@ -75,11 +75,12 @@ class InitialDownloadTest : BaseActivityTest() { BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS_FOR_SEARCH_TEST.toLong()) initialDownload { clickLibraryOnBottomNav() + refreshLocalLibraryData() // This is for if download test fails for some reason after downloading the zim file deleteZimIfExists() clickDownloadOnBottomNav() assertLibraryListDisplayed() - refreshList() + refreshOnlineList() waitForDataToLoad() stopDownloadIfAlreadyStarted() downloadZimFile() diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt index 948374a6b..942447404 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt @@ -27,7 +27,6 @@ import androidx.test.uiautomator.UiDevice import org.junit.Assert import org.junit.Before import org.junit.Test -import org.kiwix.libzim.Archive import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.core.NightModeConfig 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.testutils.TestUtils.closeSystemDialogs import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible +import org.kiwix.libzim.Archive import java.io.File import java.io.FileOutputStream import java.io.OutputStream @@ -53,6 +53,7 @@ class MimeTypeTest : BaseActivityTest() { putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false) putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false) putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false) + putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false) } activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply { moveToState(Lifecycle.State.RESUMED) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryTest.kt index 65f39e17a..8bab4bc16 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryTest.kt @@ -34,7 +34,6 @@ 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.search.SearchFragmentTest import org.kiwix.kiwixmobile.testutils.RetryRule import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs 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 val loadFileStream = - SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim") + LocalLibraryTest::class.java.classLoader.getResourceAsStream("testzim.zim") val zimFile = File( ContextCompat.getExternalFilesDirs(context, null)[0], diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/note/NoteFragmentTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/note/NoteFragmentTest.kt index 876713a9e..1c22c5f8b 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/note/NoteFragmentTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/note/NoteFragmentTest.kt @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile.note +import androidx.core.content.ContextCompat import androidx.core.content.edit import androidx.core.net.toUri import androidx.lifecycle.Lifecycle @@ -93,7 +94,10 @@ class NoteFragmentTest : BaseActivityTest() { val loadFileStream = 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() zimFile.createNewFile() loadFileStream.use { inputStream -> diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/LibkiwixBookmarkTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/LibkiwixBookmarkTest.kt index 2b57d2a0d..d86b3aaf0 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/LibkiwixBookmarkTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/page/bookmarks/LibkiwixBookmarkTest.kt @@ -60,6 +60,7 @@ class LibkiwixBookmarkTest : BaseActivityTest() { putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false) putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false) putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true) + putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false) } activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply { moveToState(Lifecycle.State.RESUMED)