Commit 5a5cf9cd authored by Christian Sadilek's avatar Christian Sadilek Committed by Sebastian Kaspari
Browse files

Refactor BrowserToolbarController to use browser store

parent ec543976
......@@ -281,14 +281,14 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit
)
val browserToolbarController = DefaultBrowserToolbarController(
store = store,
tabsUseCases = requireComponents.useCases.tabsUseCases,
activity = activity,
navController = findNavController(),
metrics = requireComponents.analytics.metrics,
readerModeController = readerMenuController,
sessionManager = requireComponents.core.sessionManager,
engineView = engineView,
homeViewModel = homeViewModel,
customTabSession = customTabSessionId?.let { sessionManager.findSessionById(it) },
customTabSessionId = customTabSessionId,
onTabCounterClicked = {
thumbnailsFeature.get()?.requestScreenshot()
findNavController().nav(
......
......@@ -5,11 +5,14 @@
package org.mozilla.fenix.components.toolbar
import androidx.navigation.NavController
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import mozilla.components.browser.state.action.ContentAction
import mozilla.components.browser.state.selector.findCustomTabOrSelectedTab
import mozilla.components.browser.state.selector.getNormalOrPrivateTabs
import mozilla.components.browser.state.selector.selectedTab
import mozilla.components.browser.state.state.SessionState
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.EngineView
import mozilla.components.feature.tabs.TabsUseCases
import mozilla.components.support.ktx.kotlin.isUrl
import mozilla.components.ui.tabcounter.TabCounterMenu
import org.mozilla.fenix.HomeActivity
......@@ -22,7 +25,6 @@ import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.sessionsOfType
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.home.HomeScreenViewModel
......@@ -41,20 +43,20 @@ interface BrowserToolbarController {
class DefaultBrowserToolbarController(
private val store: BrowserStore,
private val tabsUseCases: TabsUseCases,
private val activity: HomeActivity,
private val navController: NavController,
private val metrics: MetricController,
private val readerModeController: ReaderModeController,
private val sessionManager: SessionManager,
private val engineView: EngineView,
private val homeViewModel: HomeScreenViewModel,
private val customTabSession: Session?,
private val customTabSessionId: String?,
private val onTabCounterClicked: () -> Unit,
private val onCloseTab: (Session) -> Unit
private val onCloseTab: (SessionState) -> Unit
) : BrowserToolbarController {
private val currentSession
get() = customTabSession ?: sessionManager.selectedSession
get() = store.state.findCustomTabOrSelectedTab(customTabSessionId)
override fun handleToolbarPaste(text: String) {
navController.nav(
......@@ -112,18 +114,16 @@ class DefaultBrowserToolbarController(
metrics.track(
Event.TabCounterMenuItemTapped(Event.TabCounterMenuItemTapped.Item.CLOSE_TAB)
)
sessionManager.selectedSession?.let {
store.state.selectedTab?.let {
// When closing the last tab we must show the undo snackbar in the home fragment
if (sessionManager.sessionsOfType(it.private).count() == 1) {
if (store.state.getNormalOrPrivateTabs(it.content.private).count() == 1) {
homeViewModel.sessionToDelete = it.id
navController.navigate(
BrowserFragmentDirections.actionGlobalHome()
)
} else {
onCloseTab.invoke(it)
// The removeTab use case does not currently select a parent session, so
// we are using sessionManager.remove
sessionManager.remove(it, selectParentIfExists = true)
tabsUseCases.removeTab(it.id, selectParentIfExists = true)
}
}
}
......
......@@ -15,17 +15,19 @@ import io.mockk.just
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import mozilla.components.browser.state.action.BrowserAction
import mozilla.components.browser.state.action.ContentAction
import mozilla.components.browser.state.action.TabListAction
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.SessionState
import mozilla.components.browser.state.state.createTab
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.EngineView
import mozilla.components.feature.search.SearchUseCases
import mozilla.components.feature.session.SessionUseCases
import mozilla.components.feature.tabs.TabsUseCases
import mozilla.components.feature.top.sites.TopSitesUseCases
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.libstate.ext.waitUntilIdle
import mozilla.components.support.test.middleware.CaptureActionsMiddleware
import mozilla.components.ui.tabcounter.TabCounterMenu
......@@ -61,17 +63,11 @@ class DefaultBrowserToolbarControllerTest {
private lateinit var onTabCounterClicked: () -> Unit
@RelaxedMockK
private lateinit var onCloseTab: (Session) -> Unit
@RelaxedMockK
private lateinit var sessionManager: SessionManager
private lateinit var onCloseTab: (SessionState) -> Unit
@MockK(relaxUnitFun = true)
private lateinit var engineView: EngineView
@MockK
private lateinit var currentSession: Session
@RelaxedMockK
private lateinit var metrics: MetricController
......@@ -81,6 +77,9 @@ class DefaultBrowserToolbarControllerTest {
@RelaxedMockK
private lateinit var sessionUseCases: SessionUseCases
@RelaxedMockK
private lateinit var tabsUseCases: TabsUseCases
@RelaxedMockK
private lateinit var browserAnimator: BrowserAnimator
......@@ -103,12 +102,9 @@ class DefaultBrowserToolbarControllerTest {
every { activity.components.useCases.sessionUseCases } returns sessionUseCases
every { activity.components.useCases.searchUseCases } returns searchUseCases
every { activity.components.useCases.topSitesUseCase } returns topSitesUseCase
every { sessionManager.selectedSession } returns currentSession
every { navController.currentDestination } returns mockk {
every { id } returns R.id.browserFragment
}
every { currentSession.id } returns "1"
every { currentSession.private } returns false
val onComplete = slot<() -> Unit>()
every { browserAnimator.captureEngineViewAndDrawStatically(capture(onComplete)) } answers { onComplete.captured.invoke() }
......@@ -248,14 +244,6 @@ class DefaultBrowserToolbarControllerTest {
@Test
fun handleToolbarCloseTabPressWithLastPrivateSession() {
val item = TabCounterMenu.Item.CloseTab
val sessions = listOf(
mockk<Session> {
every { private } returns true
}
)
every { currentSession.private } returns true
every { sessionManager.sessions } returns sessions
val controller = createController()
controller.handleTabCounterItemInteraction(item)
......@@ -269,11 +257,13 @@ class DefaultBrowserToolbarControllerTest {
fun handleToolbarCloseTabPress() {
val item = TabCounterMenu.Item.CloseTab
every { sessionManager.sessions } returns emptyList()
val testTab = createTab("https://www.firefox.com")
store.dispatch(TabListAction.AddTabAction(testTab)).joinBlocking()
store.dispatch(TabListAction.SelectTabAction(testTab.id)).joinBlocking()
val controller = createController()
controller.handleTabCounterItemInteraction(item)
verify { sessionManager.remove(currentSession, selectParentIfExists = true) }
verify { tabsUseCases.removeTab(testTab.id, selectParentIfExists = true) }
}
@Test
......@@ -324,17 +314,17 @@ class DefaultBrowserToolbarControllerTest {
private fun createController(
activity: HomeActivity = this.activity,
customTabSession: Session? = null
customTabSessionId: String? = null
) = DefaultBrowserToolbarController(
store = store,
tabsUseCases = tabsUseCases,
activity = activity,
navController = navController,
metrics = metrics,
engineView = engineView,
homeViewModel = homeViewModel,
customTabSession = customTabSession,
customTabSessionId = customTabSessionId,
readerModeController = readerModeController,
sessionManager = sessionManager,
onTabCounterClicked = onTabCounterClicked,
onCloseTab = onCloseTab
)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment