Migrate from Session.toTab to BrowserStore (#12221)
parent
9af167ba0a
commit
652af79e95
@ -0,0 +1,16 @@
|
||||
/* 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 org.mozilla.fenix.ext
|
||||
|
||||
import mozilla.components.browser.state.state.BrowserState
|
||||
import mozilla.components.browser.state.state.MediaState
|
||||
|
||||
fun BrowserState.getMediaStateForSession(sessionId: String): MediaState.State {
|
||||
return if (media.aggregate.activeTabId == sessionId) {
|
||||
media.aggregate.state
|
||||
} else {
|
||||
MediaState.State.NONE
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
/* 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 org.mozilla.fenix.ext
|
||||
|
||||
import android.content.Context
|
||||
import mozilla.components.browser.session.Session
|
||||
import mozilla.components.browser.state.selector.findTab
|
||||
import mozilla.components.browser.state.state.MediaState
|
||||
import mozilla.components.browser.state.store.BrowserStore
|
||||
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
|
||||
import org.mozilla.fenix.home.Tab
|
||||
|
||||
fun Session.toTab(context: Context, selected: Boolean? = null): Tab =
|
||||
this.toTab(
|
||||
context.components.core.store,
|
||||
context.components.publicSuffixList,
|
||||
selected
|
||||
)
|
||||
|
||||
fun Session.toTab(store: BrowserStore, publicSuffixList: PublicSuffixList, selected: Boolean? = null): Tab {
|
||||
val url = store.state.findTab(this.id)?.readerState?.activeUrl ?: this.url
|
||||
return Tab(
|
||||
sessionId = this.id,
|
||||
url = url,
|
||||
hostname = url.toShortUrl(publicSuffixList),
|
||||
title = this.title,
|
||||
selected = selected,
|
||||
icon = this.icon,
|
||||
mediaState = getMediaStateForSession(store, this)
|
||||
)
|
||||
}
|
||||
|
||||
private fun getMediaStateForSession(store: BrowserStore, session: Session): MediaState.State {
|
||||
// For now we are looking up the media state for this session in the BrowserStore. Eventually
|
||||
// we will migrate away from Session(Manager) and can use BrowserStore and BrowserState directly.
|
||||
return if (store.state.media.aggregate.activeTabId == session.id) {
|
||||
store.state.media.aggregate.state
|
||||
} else {
|
||||
MediaState.State.NONE
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/* 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 org.mozilla.fenix.ext
|
||||
|
||||
import mozilla.components.browser.state.state.BrowserState
|
||||
import mozilla.components.browser.state.state.MediaState
|
||||
import mozilla.components.browser.state.state.createTab
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
private const val SESSION_ID_MOZILLA = "0"
|
||||
private const val SESSION_ID_BCC = "1"
|
||||
private const val SESSION_ID_BAD = "not a real session id"
|
||||
|
||||
class BrowserStateTest {
|
||||
|
||||
private val sessionMozilla = createTab(url = "www.mozilla.org", id = SESSION_ID_MOZILLA)
|
||||
private val sessionBcc = createTab(url = "www.bcc.co.uk", id = SESSION_ID_BCC)
|
||||
|
||||
@Test
|
||||
fun `return media state if it matches tab id`() {
|
||||
val state = BrowserState(
|
||||
tabs = listOf(sessionBcc, sessionMozilla),
|
||||
media = MediaState(
|
||||
MediaState.Aggregate(
|
||||
state = MediaState.State.PLAYING,
|
||||
activeTabId = SESSION_ID_MOZILLA
|
||||
))
|
||||
)
|
||||
|
||||
assertEquals(MediaState.State.PLAYING, state.getMediaStateForSession(SESSION_ID_MOZILLA))
|
||||
assertEquals(MediaState.State.NONE, state.getMediaStateForSession(SESSION_ID_BCC))
|
||||
assertEquals(MediaState.State.NONE, state.getMediaStateForSession(SESSION_ID_BAD))
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
/* 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 org.mozilla.fenix.ext
|
||||
|
||||
import io.mockk.mockk
|
||||
import mozilla.components.browser.session.Session
|
||||
import mozilla.components.browser.state.state.BrowserState
|
||||
import mozilla.components.browser.state.state.ReaderState
|
||||
import mozilla.components.browser.state.state.createTab
|
||||
import mozilla.components.browser.state.store.BrowserStore
|
||||
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class SessionTest {
|
||||
|
||||
@Test
|
||||
fun `toTab uses active reader URL`() {
|
||||
val sessionWithoutReaderState = Session(id = "1", initialUrl = "https://example.com")
|
||||
val tabWithoutReaderState = createTab(url = sessionWithoutReaderState.url, id = sessionWithoutReaderState.id)
|
||||
|
||||
val sessionWithInactiveReaderState = Session(id = "2", initialUrl = "https://blog.mozilla.org")
|
||||
val tabWithInactiveReaderState = createTab(url = sessionWithInactiveReaderState.url, id = sessionWithInactiveReaderState.id,
|
||||
readerState = ReaderState(active = false, activeUrl = null)
|
||||
)
|
||||
|
||||
val sessionWithActiveReaderState = Session(id = "3", initialUrl = "moz-extension://123")
|
||||
val tabWithActiveReaderState = createTab(url = sessionWithActiveReaderState.url, id = sessionWithActiveReaderState.id,
|
||||
readerState = ReaderState(active = true, activeUrl = "https://blog.mozilla.org/123")
|
||||
)
|
||||
|
||||
val tabs = listOf(tabWithoutReaderState, tabWithInactiveReaderState, tabWithActiveReaderState)
|
||||
val store = BrowserStore(BrowserState(tabs))
|
||||
|
||||
val suffixList = mockk<PublicSuffixList>(relaxed = true)
|
||||
assertEquals(sessionWithoutReaderState.url, sessionWithoutReaderState.toTab(store, suffixList).url)
|
||||
assertEquals(sessionWithInactiveReaderState.url, sessionWithInactiveReaderState.toTab(store, suffixList).url)
|
||||
assertEquals("https://blog.mozilla.org/123", sessionWithActiveReaderState.toTab(store, suffixList).url)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue