From f99e37a01705c36037b1b3caa89829ba83ccf1ab Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Mon, 11 Aug 2025 22:51:18 +0530 Subject: [PATCH] Improved the background download test. --- .../kiwixmobile/download/DownloadTest.kt | 87 ++++++++++--------- 1 file changed, 48 insertions(+), 39 deletions(-) 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 3e7847496..d32dc8e61 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt @@ -242,46 +242,55 @@ class DownloadTest : BaseActivityTest() { @Test fun downloadZIMFileInBackground() { - activityScenario.onActivity { - kiwixMainActivity = it - it.navigate(KiwixDestination.Library.route) - } - // delete all the ZIM files showing in the LocalLibrary - // screen to properly test the scenario. - library { - refreshList(composeTestRule) - waitUntilZimFilesRefreshing(composeTestRule) - deleteZimIfExists(composeTestRule) - } - downloadRobot { - clickDownloadOnBottomNav(composeTestRule) - waitForDataToLoad(composeTestRule = composeTestRule) - stopDownloadIfAlreadyStarted(composeTestRule) - searchZappingSauvageFile(composeTestRule) - downloadZimFile(composeTestRule) - assertDownloadStart(composeTestRule) - } - // press the home button so that application goes into background - InstrumentationRegistry.getInstrumentation().uiAutomation.performGlobalAction( - AccessibilityService.GLOBAL_ACTION_HOME - ) - // wait for 2 minutes to download the ZIM file in background. - composeTestRule.waitUntilTimeout(TWO_MINUTES_IN_MILLISECONDS.toLong()) - // relaunch the application. - val context = ApplicationProvider.getApplicationContext() - val intent = context.packageManager.getLaunchIntentForPackage(context.packageName) - intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) - context.startActivity(intent) - InstrumentationRegistry.getInstrumentation().waitForIdleSync() - activityScenario.onActivity { - kiwixMainActivity = it - it.navigate(KiwixDestination.Library.route) - } - library { - refreshList(composeTestRule) - waitUntilZimFilesRefreshing(composeTestRule) + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) { + activityScenario.onActivity { + kiwixMainActivity = it + it.navigate(KiwixDestination.Library.route) + } + // delete all the ZIM files showing in the LocalLibrary + // screen to properly test the scenario. + library { + refreshList(composeTestRule) + waitUntilZimFilesRefreshing(composeTestRule) + deleteZimIfExists(composeTestRule) + } downloadRobot { - checkIfZimFileDownloaded(composeTestRule) + clickDownloadOnBottomNav(composeTestRule) + waitForDataToLoad(composeTestRule = composeTestRule) + stopDownloadIfAlreadyStarted(composeTestRule) + searchZappingSauvageFile(composeTestRule) + downloadZimFile(composeTestRule) + assertDownloadStart(composeTestRule) + } + // press the home button so that application goes into background + InstrumentationRegistry.getInstrumentation().uiAutomation.performGlobalAction( + AccessibilityService.GLOBAL_ACTION_HOME + ) + // wait for 2 minutes to download the ZIM file in background. + composeTestRule.waitUntilTimeout(TWO_MINUTES_IN_MILLISECONDS.toLong()) + // relaunch the application. + val context = ApplicationProvider.getApplicationContext() + val intent = context.packageManager.getLaunchIntentForPackage(context.packageName) + intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) + context.startActivity(intent) + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + activityScenario.onActivity { + kiwixMainActivity = it + it.navigate(KiwixDestination.Library.route) + } + library { + refreshList(composeTestRule) + waitUntilZimFilesRefreshing(composeTestRule) + downloadRobot { + runCatching { + checkIfZimFileDownloaded(composeTestRule) + }.onFailure { + // if currently downloading check. + clickDownloadOnBottomNav(composeTestRule) + waitForDataToLoad(composeTestRule = composeTestRule) + stopDownloadIfAlreadyStarted(composeTestRule) + } + } } } }