diff --git a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt index 820cc2c0bc..d3525c2f23 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt @@ -458,9 +458,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit downloadFeature.onDownloadStopped = { downloadState, _, downloadJobStatus -> // If the download is just paused, don't show any in-app notification - if (downloadJobStatus == DownloadState.Status.COMPLETED || - downloadJobStatus == DownloadState.Status.FAILED - ) { + if (shouldShowCompletedDownloadDialog(downloadState, downloadJobStatus)) { saveDownloadDialogState( downloadState.sessionId, @@ -488,16 +486,13 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit onDismiss = { sharedViewModel.downloadDialogState.remove(downloadState.sessionId) } ) - // Don't show the dialog if we aren't in the tab that started the download - if (downloadState.sessionId == sessionManager.selectedSession?.id) { - dynamicDownloadDialog.show() - browserToolbarView.expand() - } + dynamicDownloadDialog.show() + browserToolbarView.expand() } } resumeDownloadDialogState( - sessionManager.selectedSession?.id, + getCurrentTab()?.id, store, view, context, toolbarHeight ) @@ -1152,7 +1147,8 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit } } - private fun getCurrentTab(): SessionState? { + @VisibleForTesting + internal fun getCurrentTab(): SessionState? { return requireComponents.core.store.state.findCustomTabOrSelectedTab(customTabSessionId) } @@ -1358,4 +1354,16 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit */ @VisibleForTesting internal fun getSwipeRefreshLayout() = swipeRefresh + + @VisibleForTesting + internal fun shouldShowCompletedDownloadDialog( + downloadState: DownloadState, + status: DownloadState.Status + ): Boolean { + + val isValidStatus = status in listOf(DownloadState.Status.COMPLETED, DownloadState.Status.FAILED) + val isSameTab = downloadState.sessionId == getCurrentTab()?.id ?: false + + return isValidStatus && isSameTab + } } diff --git a/app/src/test/java/org/mozilla/fenix/browser/BaseBrowserFragmentTest.kt b/app/src/test/java/org/mozilla/fenix/browser/BaseBrowserFragmentTest.kt index 64b8244329..d65605fbd2 100644 --- a/app/src/test/java/org/mozilla/fenix/browser/BaseBrowserFragmentTest.kt +++ b/app/src/test/java/org/mozilla/fenix/browser/BaseBrowserFragmentTest.kt @@ -12,8 +12,12 @@ import io.mockk.mockk import io.mockk.slot import io.mockk.spyk import io.mockk.verify +import junit.framework.TestCase.assertFalse +import junit.framework.TestCase.assertTrue import kotlinx.coroutines.ExperimentalCoroutinesApi import mozilla.components.browser.state.state.SessionState +import mozilla.components.browser.state.state.content.DownloadState +import mozilla.components.browser.state.state.createTab import mozilla.components.concept.engine.EngineView import mozilla.components.feature.contextmenu.ContextMenuCandidate import mozilla.components.feature.session.behavior.EngineViewBrowserToolbarBehavior @@ -111,6 +115,66 @@ class BaseBrowserFragmentTest { verify { (swipeRefreshLayout.layoutParams as CoordinatorLayout.LayoutParams).bottomMargin = 13 } } + + @Test + fun `WHEN status is equals to FAILED or COMPLETED and it is the same tab then shouldShowCompletedDownloadDialog will be true`() { + every { fragment.getCurrentTab() } returns createTab(id = "1", url = "") + + val download = DownloadState( + url = "", + sessionId = "1" + ) + + val status = DownloadState.Status.values() + .filter { it == DownloadState.Status.COMPLETED && it == DownloadState.Status.FAILED } + + status.forEach { + val result = + fragment.shouldShowCompletedDownloadDialog(download, it) + + assertTrue(result) + } + } + + @Test + fun `WHEN status is different from FAILED or COMPLETED then shouldShowCompletedDownloadDialog will be false`() { + every { fragment.getCurrentTab() } returns createTab(id = "1", url = "") + + val download = DownloadState( + url = "", + sessionId = "1" + ) + + val status = DownloadState.Status.values() + .filter { it != DownloadState.Status.COMPLETED && it != DownloadState.Status.FAILED } + + status.forEach { + val result = + fragment.shouldShowCompletedDownloadDialog(download, it) + + assertFalse(result) + } + } + + @Test + fun `WHEN the tab is different from the initial one then shouldShowCompletedDownloadDialog will be false`() { + every { fragment.getCurrentTab() } returns createTab(id = "1", url = "") + + val download = DownloadState( + url = "", + sessionId = "2" + ) + + val status = DownloadState.Status.values() + .filter { it != DownloadState.Status.COMPLETED && it != DownloadState.Status.FAILED } + + status.forEach { + val result = + fragment.shouldShowCompletedDownloadDialog(download, it) + + assertFalse(result) + } + } } @ExperimentalCoroutinesApi