Skip to content

Commit

Permalink
For mozilla-mobile#6907 - Dispatches tabsTray update when MediaState …
Browse files Browse the repository at this point in the history
…changes.
  • Loading branch information
boek committed May 12, 2020
1 parent 99e1445 commit a1f7d61
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ class TabsTrayPresenter(
private val closeTabsTray: () -> Unit
) {
private var tabs: Tabs? = null
private var scope: CoroutineScope? = null
private var mediaScope: CoroutineScope? = null
private var tabScope: CoroutineScope? = null

fun start() {
scope = store.flowScoped { flow -> collect(flow) }
tabScope = store.flowScoped { flow -> collect(flow) }
mediaScope = store.flowScoped {
it
.ifChanged { state -> state.media.aggregate }
.collect { this.tabs?.also(tabsTray::updateTabs) }
}
}

fun stop() {
scope?.cancel()
tabScope?.cancel()
mediaScope?.cancel()
}

private suspend fun collect(flow: Flow<BrowserState>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import mozilla.components.browser.state.action.MediaAction
import mozilla.components.browser.state.action.TabListAction
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.MediaState
import mozilla.components.browser.state.state.createTab
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.tabstray.Tabs
Expand Down Expand Up @@ -216,6 +218,33 @@ class TabsTrayPresenterTest {
verify(tabsTray).onTabsChanged(3, 1)
}

@Test
fun `tabs tray will get updated if mediaState changes`() {
val store = BrowserStore(
BrowserState(
tabs = listOf(
createTab("https://www.mozilla.org", id = "a"),
createTab("https://getpocket.com", id = "b"),
createTab("https://developer.mozilla.org", id = "c"),
createTab("https://www.firefox.com", id = "d"),
createTab("https://www.google.com", id = "e")
),
selectedTabId = "a"
)
)

val tabsTray: MockedTabsTray = spy(MockedTabsTray())
val presenter = TabsTrayPresenter(tabsTray, store, { true }, mock())

presenter.start()
testDispatcher.advanceUntilIdle()

store.dispatch(MediaAction.UpdateMediaAggregateAction(store.state.media.aggregate.copy(state = MediaState.State.PLAYING))).joinBlocking()
testDispatcher.advanceUntilIdle()

verify(tabsTray, times(3)).updateTabs(store.state.toTabs())
}

@Test
fun `presenter will close tabs tray when all sessions get removed`() {
val store = BrowserStore(
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ permalink: /changelog/
* [Gecko](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Gecko.kt)
* [Configuration](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Config.kt)

* **feature-tabs**
* Fixed issue [#6907](https://github.com/mozilla-mobile/android-components/issues/6907). Dispatches tabsTray update when MediaState changes.

* **feature-session**
* ⚠️ **This is a breaking change**: Added optional `crashReporting` param to [PictureInPictureFeature] so we can record caught exceptions.

Expand Down

0 comments on commit a1f7d61

Please sign in to comment.