diff --git a/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackingProtectionPanelInteractor.kt b/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackingProtectionPanelInteractor.kt index 9804e3d0b848..679898c888a6 100644 --- a/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackingProtectionPanelInteractor.kt +++ b/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackingProtectionPanelInteractor.kt @@ -5,6 +5,7 @@ package org.mozilla.fenix.trackingprotection import android.content.Context +import androidx.annotation.VisibleForTesting import androidx.fragment.app.Fragment import androidx.navigation.NavController import kotlinx.coroutines.CoroutineScope @@ -50,37 +51,57 @@ class TrackingProtectionPanelInteractor( override fun selectTrackingProtectionSettings() { openTrackingProtectionSettings.invoke() } + + /** + * Handles the navigation after checking for exceptions in tracking protection. + * This function is responsible for navigating to the QuickSettingsSheetDialogFragment + * after determining the state of tracking protection and cookie banners for the given tab. + * + * It first retrieves the cookie banner UI mode for the current tab. + * Then, on the main thread, it pops the back stack and navigates to the + * QuickSettingsSheetDialogFragment, passing along various details about the tab, + * site permissions, and tracking protection status. + * + * @param tab The current [SessionState] representing the active tab. + * @param containsException A boolean indicating whether the current site has an exception + * for tracking protection. + */ + @VisibleForTesting + internal fun handleNavigationAfterCheck(tab: SessionState, containsException: Boolean) { + ioScope.launch { + val cookieBannerUIMode = cookieBannersStorage.getCookieBannerUIMode( + context, + tab, + ) + withContext(Dispatchers.Main) { + fragment.runIfFragmentIsAttached { + navController().popBackStack() + val isTrackingProtectionEnabled = + tab.trackingProtection.enabled && !containsException + val directions = + BrowserFragmentDirections.actionGlobalQuickSettingsSheetDialogFragment( + sessionId = tab.id, + url = tab.content.url, + title = tab.content.title, + isLocalPdf = tab.content.url.isContentUrl(), + isSecured = tab.content.securityInfo.secure, + sitePermissions = sitePermissions, + gravity = gravity, + certificateName = tab.content.securityInfo.issuer, + permissionHighlights = tab.content.permissionHighlights, + isTrackingProtectionEnabled = isTrackingProtectionEnabled, + cookieBannerUIMode = cookieBannerUIMode, + ) + navController().navigate(directions) + } + } + } + } + override fun onBackPressed() { getCurrentTab()?.let { tab -> context.components.useCases.trackingProtectionUseCases.containsException(tab.id) { contains -> - ioScope.launch { - val cookieBannerUIMode = cookieBannersStorage.getCookieBannerUIMode( - context, - tab, - ) - withContext(Dispatchers.Main) { - fragment.runIfFragmentIsAttached { - navController().popBackStack() - val isTrackingProtectionEnabled = - tab.trackingProtection.enabled && !contains - val directions = - BrowserFragmentDirections.actionGlobalQuickSettingsSheetDialogFragment( - sessionId = tab.id, - url = tab.content.url, - title = tab.content.title, - isLocalPdf = tab.content.url.isContentUrl(), - isSecured = tab.content.securityInfo.secure, - sitePermissions = sitePermissions, - gravity = gravity, - certificateName = tab.content.securityInfo.issuer, - permissionHighlights = tab.content.permissionHighlights, - isTrackingProtectionEnabled = isTrackingProtectionEnabled, - cookieBannerUIMode = cookieBannerUIMode, - ) - navController().navigate(directions) - } - } - } + handleNavigationAfterCheck(tab, contains) } } } diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/trackingprotection/TrackingProtectionPanelInteractorTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/trackingprotection/TrackingProtectionPanelInteractorTest.kt index 21830daa3cfa..750a3d4d09fe 100644 --- a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/trackingprotection/TrackingProtectionPanelInteractorTest.kt +++ b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/trackingprotection/TrackingProtectionPanelInteractorTest.kt @@ -87,14 +87,6 @@ class TrackingProtectionPanelInteractorTest { every { fragment.context } returns context every { context.components.useCases.trackingProtectionUseCases } returns trackingProtectionUseCases every { context.components.appStore.state.isPrivateScreenLocked } returns true - - val onComplete = slot<(Boolean) -> Unit>() - every { - trackingProtectionUseCases.containsException.invoke( - "testID", - capture(onComplete), - ) - }.answers { onComplete.captured.invoke(true) } } @Test @@ -141,7 +133,7 @@ class TrackingProtectionPanelInteractorTest { every { context.settings().shouldUseCookieBannerPrivateMode } returns false val directionsSlot = slot() - interactor.onBackPressed() + interactor.handleNavigationAfterCheck(tab, true) coVerify { navController.popBackStack()