Skip to content

Commit baf191a

Browse files
csadilekmergify[bot]
authored andcommitted
RemoveTabUseCase: Support selecting parent tab
1 parent df1cf70 commit baf191a

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

components/feature/tabs/src/main/java/mozilla/components/feature/tabs/TabsUseCases.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ class TabsUseCases(
8080
*/
8181
operator fun invoke(sessionId: String)
8282

83+
/**
84+
* Removes the session with the provided ID. This method
85+
* has no effect if the session doesn't exist.
86+
*
87+
* @param sessionId The ID of the session to remove.
88+
* @param selectParentIfExists Whether or not to select the parent tab
89+
* of the removed tab if a parent exists. Note that the default implementation
90+
* of this method will ignore [selectParentIfExists] and never select a parent.
91+
* This is a temporary workaround to prevent additional API breakage for
92+
* subtypes other than [DefaultRemoveTabUseCase]. The default implementation
93+
* should be removed together with invoke(Session).
94+
*/
95+
operator fun invoke(sessionId: String, selectParentIfExists: Boolean) = invoke(sessionId)
96+
8397
/**
8498
* Removes the provided session.
8599
*
@@ -109,6 +123,21 @@ class TabsUseCases(
109123
}
110124
}
111125

126+
/**
127+
* Removes the session with the provided ID. This method
128+
* has no effect if the session doesn't exist.
129+
*
130+
* @param sessionId The ID of the session to remove.
131+
* @param selectParentIfExists Whether or not to select the parent tab
132+
* of the removed tab if a parent exists.
133+
*/
134+
override operator fun invoke(sessionId: String, selectParentIfExists: Boolean) {
135+
val session = sessionManager.findSessionById(sessionId)
136+
if (session != null) {
137+
sessionManager.remove(session, selectParentIfExists)
138+
}
139+
}
140+
112141
/**
113142
* Removes the provided session.
114143
*

components/feature/tabs/src/test/java/mozilla/components/feature/tabs/TabsUseCasesTest.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import mozilla.components.concept.engine.EngineSession.LoadUrlFlags
2121
import mozilla.components.support.test.argumentCaptor
2222
import mozilla.components.support.test.mock
2323
import mozilla.components.support.test.rule.MainCoroutineRule
24+
import mozilla.components.support.test.whenever
2425
import org.junit.Assert.assertEquals
2526
import org.junit.Assert.assertFalse
2627
import org.junit.Assert.assertNotEquals
@@ -58,19 +59,29 @@ class TabsUseCasesTest {
5859
val session = Session("A")
5960
useCases.removeTab(session)
6061

61-
verify(sessionManager).remove(session)
62+
verify(sessionManager).remove(session, false)
6263
}
6364

6465
@Test
6566
fun `RemoveTabUseCase - session can be removed by ID`() {
66-
val sessionManager = spy(SessionManager(mock()))
67+
val sessionManager: SessionManager = mock()
68+
val session = Session(id = "test", initialUrl = "http://mozilla.org")
69+
whenever(sessionManager.findSessionById(session.id)).thenReturn(session)
70+
6771
val useCases = TabsUseCases(BrowserStore(), sessionManager)
72+
useCases.removeTab(session.id)
73+
verify(sessionManager).remove(session, false)
74+
}
6875

76+
@Test
77+
fun `RemoveTabUseCase - remove by ID and select parent if it exists`() {
78+
val sessionManager: SessionManager = mock()
6979
val session = Session(id = "test", initialUrl = "http://mozilla.org")
70-
sessionManager.remove(session)
71-
useCases.removeTab(session.id)
80+
whenever(sessionManager.findSessionById(session.id)).thenReturn(session)
7281

73-
verify(sessionManager).remove(session)
82+
val useCases = TabsUseCases(BrowserStore(), sessionManager)
83+
useCases.removeTab(session.id, selectParentIfExists = true)
84+
verify(sessionManager).remove(session, true)
7485
}
7586

7687
@Test

0 commit comments

Comments
 (0)