Skip to content

Commit 9fecd17

Browse files
MozLandocsadilek
andcommitted
4358: Closes mozilla-mobile#4348: Empty toolbar after switching from custom tab to browser r=pocmo a=csadilek Pretty much exactly as described in mozilla-mobile#4348 (comment). We need to update our store to move the tab from the custom tab list to the regular tab list. I decided to re-use our existing events (Remove, Add) instead of introducing a new one, as it's technically two different reducers having to handle it. I've verified that this fixes the issues in Fenix (and r-b). Co-authored-by: Christian Sadilek <[email protected]>
2 parents b7bf724 + 0b0b921 commit 9fecd17

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

components/browser/session/src/main/java/mozilla/components/browser/session/Session.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import mozilla.components.browser.session.engine.request.LoadRequestMetadata
1010
import mozilla.components.browser.session.engine.request.LoadRequestOption
1111
import mozilla.components.browser.session.ext.syncDispatch
1212
import mozilla.components.browser.session.ext.toSecurityInfoState
13+
import mozilla.components.browser.session.ext.toTabSessionState
1314
import mozilla.components.browser.state.action.ContentAction.ConsumeDownloadAction
1415
import mozilla.components.browser.state.action.ContentAction.ConsumeHitResultAction
1516
import mozilla.components.browser.state.action.ContentAction.RemoveIconAction
@@ -24,6 +25,8 @@ import mozilla.components.browser.state.action.ContentAction.UpdateSecurityInfoA
2425
import mozilla.components.browser.state.action.ContentAction.UpdateThumbnailAction
2526
import mozilla.components.browser.state.action.ContentAction.UpdateTitleAction
2627
import mozilla.components.browser.state.action.ContentAction.UpdateUrlAction
28+
import mozilla.components.browser.state.action.CustomTabListAction.RemoveCustomTabAction
29+
import mozilla.components.browser.state.action.TabListAction.AddTabAction
2730
import mozilla.components.browser.state.action.TrackingProtectionAction
2831
import mozilla.components.browser.state.state.CustomTabConfig
2932
import mozilla.components.browser.state.store.BrowserStore
@@ -263,8 +266,16 @@ class Session(
263266
/**
264267
* Configuration data in case this session is used for a Custom Tab.
265268
*/
266-
var customTabConfig: CustomTabConfig? by Delegates.observable<CustomTabConfig?>(null) { _, _, new ->
269+
var customTabConfig: CustomTabConfig? by Delegates.observable<CustomTabConfig?>(null) { _, old, new ->
267270
notifyObservers { onCustomTabConfigChanged(this@Session, new) }
271+
272+
// The custom tab config is set to null when we're migrating custom
273+
// tabs to regular tabs, so we have to dispatch the corresponding
274+
// browser actions to keep the store in sync.
275+
if (old != new && new == null) {
276+
store?.syncDispatch(RemoveCustomTabAction(id))
277+
store?.syncDispatch(AddTabAction(toTabSessionState()))
278+
}
268279
}
269280

270281
/**

components/browser/session/src/test/java/mozilla/components/browser/session/SessionManagerMigrationTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,34 @@ class SessionManagerMigrationTest {
7070
assertEquals("https://www.mozilla.org", tab.content.url)
7171
}
7272

73+
@Test
74+
fun `Migrating a custom tab session`() {
75+
val store = BrowserStore()
76+
77+
val sessionManager = SessionManager(engine = mock(), store = store)
78+
79+
val customTabSession = Session("https://www.mozilla.org")
80+
customTabSession.customTabConfig = mock()
81+
sessionManager.add(customTabSession)
82+
83+
assertEquals(0, sessionManager.sessions.size)
84+
assertEquals(1, sessionManager.all.size)
85+
assertEquals(0, store.state.tabs.size)
86+
assertEquals(1, store.state.customTabs.size)
87+
88+
val customTab = store.state.customTabs[0]
89+
assertEquals("https://www.mozilla.org", customTab.content.url)
90+
91+
customTabSession.customTabConfig = null
92+
assertEquals(1, sessionManager.sessions.size)
93+
assertEquals(1, sessionManager.all.size)
94+
assertEquals(1, store.state.tabs.size)
95+
assertEquals(0, store.state.customTabs.size)
96+
97+
val tab = store.state.tabs[0]
98+
assertEquals("https://www.mozilla.org", tab.content.url)
99+
}
100+
73101
@Test
74102
fun `Remove session`() {
75103
val store = BrowserStore()

components/browser/session/src/test/java/mozilla/components/browser/session/SessionTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import mozilla.components.browser.session.Session.Source
1313
import mozilla.components.browser.session.engine.request.LoadRequestMetadata
1414
import mozilla.components.browser.session.engine.request.LoadRequestOption
1515
import mozilla.components.browser.session.ext.toSecurityInfoState
16+
import mozilla.components.browser.session.ext.toTabSessionState
1617
import mozilla.components.browser.state.action.ContentAction
18+
import mozilla.components.browser.state.action.CustomTabListAction
19+
import mozilla.components.browser.state.action.TabListAction
1720
import mozilla.components.browser.state.state.CustomTabConfig
1821
import mozilla.components.browser.state.store.BrowserStore
1922
import mozilla.components.concept.engine.HitResult
@@ -305,6 +308,24 @@ class SessionTest {
305308
assertEquals("id", config!!.id)
306309
}
307310

311+
@Test
312+
fun `action is dispatched when custom tab config is set`() {
313+
val store: BrowserStore = mock()
314+
`when`(store.dispatch(any())).thenReturn(mock())
315+
316+
val session = Session("https://www.mozilla.org")
317+
session.store = store
318+
session.customTabConfig = mock()
319+
320+
verify(store, never()).dispatch(CustomTabListAction.RemoveCustomTabAction(session.id))
321+
verify(store, never()).dispatch(TabListAction.AddTabAction(session.toTabSessionState()))
322+
323+
session.customTabConfig = null
324+
verify(store).dispatch(CustomTabListAction.RemoveCustomTabAction(session.id))
325+
verify(store).dispatch(TabListAction.AddTabAction(session.toTabSessionState()))
326+
verifyNoMoreInteractions(store)
327+
}
328+
308329
@Test
309330
fun `observer is notified when web app manifest is set`() {
310331
val manifest = WebAppManifest(

0 commit comments

Comments
 (0)