Bug 1971665 - Avoid mocking lambda functions in mockk tests. r=android-reviewers,jonalmeida a=dmeehan

Differential Revision: https://phabricator.services.mozilla.com/D253578
This commit is contained in:
mcarare
2025-06-13 06:08:02 +00:00
committed by dmeehan@mozilla.com
parent cea6d2c3b0
commit fe50381f10
2 changed files with 50 additions and 37 deletions

View File

@@ -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)
}
}
}

View File

@@ -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<NavDirections>()
interactor.onBackPressed()
interactor.handleNavigationAfterCheck(tab, true)
coVerify {
navController.popBackStack()