From f9c13388261760f72bbca2cc544c2533f24be9da Mon Sep 17 00:00:00 2001 From: Mugurell Date: Tue, 22 Jun 2021 20:02:05 +0300 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/20046 - Show the last accessed tab in Jump back in --- .../home/recenttabs/RecentTabsListFeature.kt | 14 +++--- .../fenix/home/RecentTabsListFeatureTest.kt | 44 ++++++++++++++++--- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/home/recenttabs/RecentTabsListFeature.kt b/app/src/main/java/org/mozilla/fenix/home/recenttabs/RecentTabsListFeature.kt index 1a87010981..96b40f46b2 100644 --- a/app/src/main/java/org/mozilla/fenix/home/recenttabs/RecentTabsListFeature.kt +++ b/app/src/main/java/org/mozilla/fenix/home/recenttabs/RecentTabsListFeature.kt @@ -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(browserStore) { override suspend fun onState(flow: Flow) { 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() diff --git a/app/src/test/java/org/mozilla/fenix/home/RecentTabsListFeatureTest.kt b/app/src/test/java/org/mozilla/fenix/home/RecentTabsListFeatureTest.kt index bf00d3f2df..c077aeced4 100644 --- a/app/src/test/java/org/mozilla/fenix/home/RecentTabsListFeatureTest.kt +++ b/app/src/test/java/org/mozilla/fenix/home/RecentTabsListFeatureTest.kt @@ -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