Skip to content

Commit

Permalink
For mozilla-mobile#6907 - Adds test for TabSessionState
Browse files Browse the repository at this point in the history
  • Loading branch information
boek committed May 12, 2020
1 parent f14626b commit 7e38659
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,20 @@ class TabsTrayPresenter(
private val closeTabsTray: () -> Unit
) {
private var tabs: Tabs? = null
private var tabScope: CoroutineScope? = null
private var scope: CoroutineScope? = null

fun start() {
tabScope = store.flowScoped { flow -> collect(flow) }
scope = store.flowScoped { flow -> collect(flow) }
}

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

private suspend fun collect(flow: Flow<BrowserState>) {
flow.map { state ->
val tabs = state.toTabs(tabsFilter)
tabs
}
flow.map { it.toTabs(tabsFilter) }
.ifChanged()
.collect { tabs ->

// Do not invoke the callback on start if this is the initial state.
if (tabs.list.isEmpty() && this.tabs != null) {
closeTabsTray.invoke()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.feature.tabs.ext


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.engine.media.Media
import org.junit.Test
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull

class TabSessionStateTest {
@Test
fun `mediaState gets set correctly`() {
var browserState = 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",
media = MediaState(MediaState.Aggregate(activeTabId = "a", state = MediaState.State.PLAYING))
)

var tabs = browserState.toTabs()
assertEquals(Media.State.PLAYING, tabs.list[0].mediaState)

browserState = browserState.copy(
media = MediaState(MediaState.Aggregate(activeTabId = "b", state = MediaState.State.PAUSED))
)
tabs = browserState.toTabs()
assertNull(tabs.list[0].mediaState)
assertEquals(Media.State.PAUSED, tabs.list[1].mediaState)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,15 @@ class TabsTrayPresenterTest {
@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"
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"
)
)

Expand All @@ -242,7 +242,7 @@ class TabsTrayPresenterTest {

store.dispatch(
MediaAction.UpdateMediaAggregateAction(
store.state.media.aggregate.copy(activeTabId = "a", state = MediaState.State.PLAYING)
store.state.media.aggregate.copy(activeTabId = "a", state = MediaState.State.PLAYING)
)
).joinBlocking()

Expand Down

0 comments on commit 7e38659

Please sign in to comment.