[fenix] For https://github.com/mozilla-mobile/fenix/issues/20046 - Show the last accessed tab in Jump back in

pull/600/head
Mugurell 3 years ago committed by mergify[bot]
parent 11354992b5
commit f9c1338826

@ -8,6 +8,8 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import mozilla.components.browser.state.selector.normalTabs
import mozilla.components.browser.state.selector.selectedNormalTab
import mozilla.components.browser.state.selector.selectedTab
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.store.BrowserStore
@ -22,17 +24,19 @@ import org.mozilla.fenix.home.HomeFragmentStore
*/
@OptIn(ExperimentalCoroutinesApi::class)
class RecentTabsListFeature(
browserStore: BrowserStore,
private val browserStore: BrowserStore,
private val homeStore: HomeFragmentStore
) : AbstractBinding<BrowserState>(browserStore) {
override suspend fun onState(flow: Flow<BrowserState>) {
flow.map { it.selectedTab }
.ifAnyChanged { arrayOf(it?.id, it?.content?.title, it?.content?.icon) }
.collect { selectedTab ->
// Attempt to get the selected normal tab since here may not be a selected tab or
// the selected tab may be a private tab.
val recentTabsList = if (selectedTab != null && selectedTab.content.private.not()) {
.collect { _ ->
// Attempt to get the selected normal tab or the last accessed normal tab
// if there is no selected tab or the selected tab is a private one.
val selectedTab = browserStore.state.selectedNormalTab
?: browserStore.state.normalTabs.maxByOrNull { it.lastAccess }
val recentTabsList = if (selectedTab != null) {
listOf(selectedTab)
} else {
emptyList()

@ -49,7 +49,7 @@ class RecentTabsListFeatureTest {
}
@Test
fun `GIVEN no selected tab WHEN the feature starts THEN dispatch an empty list`() {
fun `GIVEN no selected or last active tab WHEN the feature starts THEN dispatch an empty list`() {
val browserStore = BrowserStore()
val homeStore = HomeFragmentStore()
val feature = RecentTabsListFeature(
@ -64,6 +64,28 @@ class RecentTabsListFeatureTest {
assertEquals(0, homeStore.state.recentTabs.size)
}
@Test
fun `GIVEN no selected but last active tab available WHEN the feature starts THEN dispatch the last active tab as a recent tab list`() {
val tab = createTab(
url = "https://www.mozilla.org",
id = "1"
)
val tabs = listOf(tab)
val browserStore = BrowserStore(
BrowserState(tabs = tabs)
)
val feature = RecentTabsListFeature(
browserStore = browserStore,
homeStore = homeStore
)
feature.start()
homeStore.waitUntilIdle()
assertEquals(1, homeStore.state.recentTabs.size)
}
@Test
fun `GIVEN a selected tab WHEN the feature starts THEN dispatch the selected tab as a recent tab list`() {
val tab = createTab(
@ -128,16 +150,22 @@ class RecentTabsListFeatureTest {
@Test
fun `WHEN the browser state selects a private tab THEN dispatch an empty list`() {
val normalTab = createTab(
val selectedNormalTab = createTab(
url = "https://www.mozilla.org",
id = "1"
id = "1",
lastAccess = 0
)
val lastAccessedNormalTab = createTab(
url = "https://www.mozilla.org",
id = "2",
lastAccess = 1
)
val privateTab = createTab(
url = "https://www.firefox.com",
id = "2",
id = "3",
private = true
)
val tabs = listOf(normalTab, privateTab)
val tabs = listOf(selectedNormalTab, lastAccessedNormalTab, privateTab)
val browserStore = BrowserStore(
BrowserState(
tabs = tabs,
@ -154,13 +182,15 @@ class RecentTabsListFeatureTest {
homeStore.waitUntilIdle()
assertEquals(1, homeStore.state.recentTabs.size)
assertEquals(normalTab, homeStore.state.recentTabs[0])
assertEquals(selectedNormalTab, homeStore.state.recentTabs[0])
browserStore.dispatch(TabListAction.SelectTabAction(privateTab.id)).joinBlocking()
homeStore.waitUntilIdle()
assertEquals(0, homeStore.state.recentTabs.size)
// If the selected tab is a private tab the feature should show the last accessed normal tab.
assertEquals(1, homeStore.state.recentTabs.size)
assertEquals(lastAccessedNormalTab, homeStore.state.recentTabs[0])
}
@Test

Loading…
Cancel
Save