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 5ad9682c4..93fb0f904 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadRobot.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadRobot.kt @@ -26,6 +26,7 @@ import androidx.compose.ui.test.junit4.ComposeContentTestRule import androidx.compose.ui.test.onAllNodesWithTag import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.performClick +import androidx.compose.ui.test.performTextClearance import androidx.compose.ui.test.performTextInput import applyWithViewHierarchyPrinting import org.kiwix.kiwixmobile.BaseRobot @@ -57,6 +58,7 @@ fun downloadRobot(func: DownloadRobot.() -> Unit) = DownloadRobot().applyWithViewHierarchyPrinting(func) class DownloadRobot : BaseRobot() { + private val searchZIMFileTitle = "Zapping Sauvage" fun clickLibraryOnBottomNav(composeTestRule: ComposeContentTestRule) { composeTestRule.apply { waitUntilTimeout() @@ -135,6 +137,27 @@ class DownloadRobot : BaseRobot() { } } + fun searchZappingSauvageFile(composeTestRule: ComposeContentTestRule) { + testFlakyView({ + composeTestRule.apply { + waitForIdle() // let the compose settle. + runCatching { + // if searchView already opened do nothing. + onNodeWithTag(ONLINE_LIBRARY_SEARCH_VIEW_CLOSE_BUTTON_TESTING_TAG).assertExists() + }.onFailure { + // if searchView is not opened then open it. + onNodeWithTag(SEARCH_ICON_TESTING_TAG).performClick() + } + waitForIdle() + onNodeWithTag(ONLINE_LIBRARY_SEARCH_VIEW_TESTING_TAG).apply { + performTextClearance() + performTextInput(searchZIMFileTitle) + } + waitUntilTimeout(TestUtils.TEST_PAUSE_MS_FOR_DOWNLOAD_TEST.toLong()) + } + }) + } + private fun refreshOnlineList(composeTestRule: ComposeContentTestRule) { composeTestRule.refresh() } 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 66a2b96e4..3e7847496 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt @@ -17,6 +17,9 @@ */ package org.kiwix.kiwixmobile.download +import android.accessibilityservice.AccessibilityService +import android.content.Context +import android.content.Intent import android.os.Build import android.util.Log import androidx.compose.ui.test.junit4.createComposeRule @@ -24,6 +27,7 @@ import androidx.core.content.edit import androidx.lifecycle.Lifecycle import androidx.preference.PreferenceManager import androidx.test.core.app.ActivityScenario +import androidx.test.core.app.ApplicationProvider import androidx.test.espresso.IdlingPolicies import androidx.test.espresso.IdlingRegistry import androidx.test.espresso.accessibility.AccessibilityChecks @@ -59,10 +63,13 @@ 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 org.kiwix.kiwixmobile.testutils.TestUtils.waitUntilTimeout import org.kiwix.kiwixmobile.ui.KiwixDestination import org.kiwix.kiwixmobile.utils.KiwixIdlingResource.Companion.getInstance import java.util.concurrent.TimeUnit +const val TWO_MINUTES_IN_MILLISECONDS = 2 * 60 * 1000 + @LargeTest class DownloadTest : BaseActivityTest() { @Rule(order = RETRY_RULE_ORDER) @@ -233,6 +240,52 @@ 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) + downloadRobot { + checkIfZimFileDownloaded(composeTestRule) + } + } + } + @After fun finish() { IdlingRegistry.getInstance().unregister(getInstance()) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryFragment.kt index 5d187a0bf..221d3a1df 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryFragment.kt @@ -144,8 +144,7 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal swipeRefreshItem = Pair(false, true), scanningProgressItem = Pair(false, ZERO), noFilesViewItem = Triple("", "", false), - actionMenuItems = listOf(), - bottomNavigationHeight = ZERO + actionMenuItems = listOf() ) ) @@ -187,10 +186,7 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal setContent { val lazyListState = rememberLazyListState() LaunchedEffect(Unit) { - updateLibraryScreenState( - bottomNavigationHeight = getBottomNavigationHeight(), - actionMenuItems = actionMenuItems() - ) + updateLibraryScreenState(actionMenuItems = actionMenuItems()) } LocalLibraryScreen( listState = lazyListState, @@ -266,8 +262,7 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal swipeRefreshItem: Pair? = null, scanningProgressItem: Pair? = null, noFilesViewItem: Triple? = null, - actionMenuItems: List? = null, - bottomNavigationHeight: Int? = null + actionMenuItems: List? = null ) { libraryScreenState.value = libraryScreenState.value.copy( fileSelectListState = fileSelectListState ?: libraryScreenState.value.fileSelectListState, @@ -276,9 +271,7 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal scanningProgressItem = scanningProgressItem ?: libraryScreenState.value.scanningProgressItem, noFilesViewItem = noFilesViewItem ?: libraryScreenState.value.noFilesViewItem, - actionMenuItems = actionMenuItems ?: libraryScreenState.value.actionMenuItems, - bottomNavigationHeight = bottomNavigationHeight - ?: libraryScreenState.value.bottomNavigationHeight + actionMenuItems = actionMenuItems ?: libraryScreenState.value.actionMenuItems ) } @@ -363,12 +356,6 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal } } - // private fun getBottomNavigationView() = - // requireActivity().findViewById(R.id.bottom_nav_view) - - private fun getBottomNavigationHeight() = ZERO - // getBottomNavigationView().measuredHeight - private fun filePickerButtonClick() { if (!requireActivity().isManageExternalStoragePermissionGranted(sharedPreferenceUtil)) { showManageExternalStoragePermissionDialog() diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryScreenState.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryScreenState.kt index f79ecc3ba..e5f7e4d66 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryScreenState.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryScreenState.kt @@ -65,9 +65,5 @@ data class LocalLibraryScreenState( /** * Represents a list of action menu items available in the screen's top app bar. */ - val actionMenuItems: List, - /** - * Stores the height of the bottom navigation bar in pixels. - */ - val bottomNavigationHeight: Int + val actionMenuItems: List ) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineBookItem.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineBookItem.kt index 10523668c..6f5782796 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineBookItem.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineBookItem.kt @@ -206,7 +206,9 @@ private fun BookSizeAndDateRow(item: BookItem) { ) { BookSize( Byte(item.book.size).humanReadable, - modifier = Modifier.weight(1f).testTag(ONLINE_BOOK_SIZE_TEXT_TESTING_TAG) + modifier = Modifier + .weight(1f) + .testTag(ONLINE_BOOK_SIZE_TEXT_TESTING_TAG) ) BookDate(item.book.date) } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineLibraryFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineLibraryFragment.kt index c1309e130..d31b5f4ae 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineLibraryFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineLibraryFragment.kt @@ -58,7 +58,6 @@ import org.kiwix.kiwixmobile.core.base.BaseFragment import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions import org.kiwix.kiwixmobile.core.data.remote.KiwixService.Companion.ITEMS_PER_PAGE import org.kiwix.kiwixmobile.core.downloader.Downloader -import org.kiwix.kiwixmobile.core.downloader.downloadManager.ZERO import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.hasNotificationPermission import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.isManageExternalStoragePermissionGranted import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.navigate @@ -129,7 +128,6 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions { isRefreshing = false, scanningProgressItem = Pair(false, ""), noContentViewItem = Pair("", false), - bottomNavigationHeight = ZERO, onBookItemClick = { onBookItemClick(it) }, availableSpaceCalculator = availableSpaceCalculator, onRefresh = { refreshFragment(true) }, @@ -209,12 +207,6 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions { composeView = it } - // private fun getBottomNavigationView() = - // requireActivity().findViewById(org.kiwix.kiwixmobile.R.id.bottom_nav_view) - - private fun getBottomNavigationHeight() = ZERO - // getBottomNavigationView().measuredHeight - private fun onPauseResumeButtonClick(item: LibraryListItem.LibraryDownloadItem) { context?.let { context -> if (isNotConnected) { @@ -251,7 +243,6 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions { LaunchedEffect(Unit) { onlineLibraryScreenState.value.update { copy( - bottomNavigationHeight = getBottomNavigationHeight(), isSearchActive = isSearchActive, scanningProgressItem = false to getString(R.string.reaching_remote_library) ) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineLibraryScreenState.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineLibraryScreenState.kt index d30e848ed..7ea84a844 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineLibraryScreenState.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/OnlineLibraryScreenState.kt @@ -30,10 +30,6 @@ data class OnlineLibraryScreenState( * Manages the online library list state. */ val onlineLibraryList: List?, - /** - * Stores the height of the bottom navigation bar in pixels. - */ - val bottomNavigationHeight: Int, /** * Controls the visibility and behavior of the "Pull to refresh" animation. * diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt index fa1242b4d..f2a7f77b0 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt @@ -60,7 +60,6 @@ import org.kiwix.kiwixmobile.core.reader.ZimReaderSource import org.kiwix.kiwixmobile.core.utils.ExternalLinkOpener import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower import org.kiwix.kiwixmobile.core.utils.dialog.RateDialogHandler -import org.kiwix.kiwixmobile.core.utils.files.Log import javax.inject.Inject import kotlin.system.exitProcess @@ -351,9 +350,7 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider { } private fun activeFragments(): MutableList = - supportFragmentManager.fragments.also { - Log.d("Fragments", "Found fragments: ${supportFragmentManager.fragments}") - } + supportFragmentManager.fragments fun navigate(action: NavDirections) { navController.currentDestination?.getAction(action.actionId)?.run {