Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Issue 7170: Make ThumbnailsUseCases optional in TabsFeature and TabsT…
Browse files Browse the repository at this point in the history
…rayPresenter
  • Loading branch information
gabrielluong committed May 29, 2020
1 parent 521d633 commit d5b9b3f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ class TabsFeature(
tabsTray: TabsTray,
private val store: BrowserStore,
tabsUseCases: TabsUseCases,
thumbnailsUseCases: ThumbnailsUseCases,
private val defaultTabsFilter: (TabSessionState) -> Boolean = { true },
closeTabsTray: () -> Unit
closeTabsTray: () -> Unit,
thumbnailsUseCases: ThumbnailsUseCases? = null
) : LifecycleAwareFeature {
@VisibleForTesting
internal var presenter = TabsTrayPresenter(
tabsTray,
store,
thumbnailsUseCases,
defaultTabsFilter,
closeTabsTray
closeTabsTray,
thumbnailsUseCases
)

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import mozilla.components.support.ktx.kotlinx.coroutines.flow.ifChanged
class TabsTrayPresenter(
private val tabsTray: TabsTray,
private val store: BrowserStore,
private val thumbnailsUseCases: ThumbnailsUseCases,
internal var tabsFilter: (TabSessionState) -> Boolean,
private val closeTabsTray: () -> Unit
private val closeTabsTray: () -> Unit,
private val thumbnailsUseCases: ThumbnailsUseCases? = null
) {
private var tabs: Tabs? = null
private var scope: CoroutineScope? = null
Expand All @@ -46,10 +46,14 @@ class TabsTrayPresenter(
private suspend fun collect(flow: Flow<BrowserState>) {
flow.map { it.toTabs(tabsFilter) }
.map { tabs ->
// Load the tab thumbnail from the memory or disk caches.
tabs.copy(list = tabs.list.map { tab ->
tab.copy(thumbnail = thumbnailsUseCases.loadThumbnail(tab.id))
})
if (thumbnailsUseCases != null) {
// Load the tab thumbnail from the memory or disk caches.
tabs.copy(list = tabs.list.map { tab ->
tab.copy(thumbnail = thumbnailsUseCases.loadThumbnail(tab.id))
})
} else {
tabs
}
}
.ifChanged()
.collect { tabs ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class TabsFeatureTest {
val filter: (TabSessionState) -> Boolean = { false }
val sessionManager = SessionManager(engine = mock())
val useCases = TabsUseCases(sessionManager)
val tabsFeature = spy(TabsFeature(mock(), store, useCases, mock(), filter, mock()))
val tabsFeature = spy(TabsFeature(mock(), store, useCases, filter, mock()))
val presenter: TabsTrayPresenter = mock()
val interactor: TabsTrayInteractor = mock()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ 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.browser.thumbnails.ThumbnailsUseCases
import mozilla.components.browser.thumbnails.storage.ThumbnailStorage
import mozilla.components.concept.tabstray.Tabs
import mozilla.components.concept.tabstray.TabsTray
import mozilla.components.feature.tabs.ext.toTabs
import mozilla.components.support.test.any
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
Expand Down Expand Up @@ -61,16 +58,15 @@ class TabsTrayPresenterTest {
val store = BrowserStore(
BrowserState(
tabs = listOf(
createTab("https://www.mozilla.org", id = "a", thumbnail = mock()),
createTab("https://getpocket.com", id = "b", thumbnail = mock())
createTab("https://www.mozilla.org", id = "a"),
createTab("https://getpocket.com", id = "b")
),
selectedTabId = "a"
)
)

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

verifyNoMoreInteractions(tabsTray)

Expand All @@ -93,16 +89,15 @@ class TabsTrayPresenterTest {
val store = BrowserStore(
BrowserState(
tabs = listOf(
createTab("https://www.mozilla.org", id = "a", thumbnail = mock()),
createTab("https://getpocket.com", id = "b", thumbnail = mock())
createTab("https://www.mozilla.org", id = "a"),
createTab("https://getpocket.com", id = "b")
),
selectedTabId = "a"
)
)

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

presenter.start()

Expand All @@ -112,7 +107,7 @@ class TabsTrayPresenterTest {

store.dispatch(
TabListAction.AddTabAction(
createTab("https://developer.mozilla.org/", thumbnail = mock())
createTab("https://developer.mozilla.org/")
)
).joinBlocking()

Expand All @@ -128,16 +123,15 @@ class TabsTrayPresenterTest {
val store = BrowserStore(
BrowserState(
tabs = listOf(
createTab("https://www.mozilla.org", id = "a", thumbnail = mock()),
createTab("https://getpocket.com", id = "b", thumbnail = mock())
createTab("https://www.mozilla.org", id = "a"),
createTab("https://getpocket.com", id = "b")
),
selectedTabId = "a"
)
)

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

presenter.start()

Expand Down Expand Up @@ -165,16 +159,15 @@ class TabsTrayPresenterTest {
val store = BrowserStore(
BrowserState(
tabs = listOf(
createTab("https://www.mozilla.org", id = "a", thumbnail = mock()),
createTab("https://getpocket.com", id = "b", thumbnail = mock())
createTab("https://www.mozilla.org", id = "a"),
createTab("https://getpocket.com", id = "b")
),
selectedTabId = "a"
)
)

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

presenter.start()

Expand All @@ -197,19 +190,18 @@ class TabsTrayPresenterTest {
val store = BrowserStore(
BrowserState(
tabs = listOf(
createTab("https://www.mozilla.org", id = "a", thumbnail = mock()),
createTab("https://getpocket.com", id = "b", thumbnail = mock()),
createTab("https://developer.mozilla.org", id = "c", thumbnail = mock()),
createTab("https://www.firefox.com", id = "d", thumbnail = mock()),
createTab("https://www.google.com", id = "e", thumbnail = mock())
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 thumbnailsUseCases = ThumbnailsUseCases(store, ThumbnailStorage(testContext))
val presenter = TabsTrayPresenter(tabsTray, store, thumbnailsUseCases, { true }, mock())
val presenter = TabsTrayPresenter(tabsTray, store, { true }, mock())

presenter.start()
testDispatcher.advanceUntilIdle()
Expand All @@ -232,29 +224,25 @@ class TabsTrayPresenterTest {
val store = BrowserStore(
BrowserState(
tabs = listOf(
createTab("https://www.mozilla.org", id = "a", thumbnail = mock()),
createTab("https://getpocket.com", id = "b", thumbnail = mock()),
createTab("https://developer.mozilla.org", id = "c", thumbnail = mock()),
createTab("https://www.firefox.com", id = "d", thumbnail = mock()),
createTab("https://www.google.com", id = "e", thumbnail = mock())
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 thumbnailsUseCases = ThumbnailsUseCases(store, ThumbnailStorage(testContext))
val presenter = TabsTrayPresenter(tabsTray, store, thumbnailsUseCases, { true }, mock())
val presenter = TabsTrayPresenter(tabsTray, store, { true }, mock())

presenter.start()
testDispatcher.advanceUntilIdle()

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 All @@ -268,11 +256,11 @@ class TabsTrayPresenterTest {
val store = BrowserStore(
BrowserState(
tabs = listOf(
createTab("https://www.mozilla.org", id = "a", thumbnail = mock()),
createTab("https://getpocket.com", id = "b", thumbnail = mock()),
createTab("https://developer.mozilla.org", id = "c", thumbnail = mock()),
createTab("https://www.firefox.com", id = "d", thumbnail = mock()),
createTab("https://www.google.com", id = "e", thumbnail = mock())
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 @@ -281,13 +269,7 @@ class TabsTrayPresenterTest {
var closed = false

val tabsTray: MockedTabsTray = spy(MockedTabsTray())
val thumbnailsUseCases = ThumbnailsUseCases(store, ThumbnailStorage(testContext))
val presenter = TabsTrayPresenter(
tabsTray,
store,
thumbnailsUseCases,
tabsFilter = { true },
closeTabsTray = { closed = true })
val presenter = TabsTrayPresenter(tabsTray, store, tabsFilter = { true }, closeTabsTray = { closed = true })

presenter.start()
testDispatcher.advanceUntilIdle()
Expand All @@ -307,8 +289,8 @@ class TabsTrayPresenterTest {
val store = BrowserStore(
BrowserState(
tabs = listOf(
createTab("https://www.mozilla.org", id = "a", thumbnail = mock()),
createTab("https://getpocket.com", id = "b", thumbnail = mock())
createTab("https://www.mozilla.org", id = "a"),
createTab("https://getpocket.com", id = "b")
),
selectedTabId = "a"
)
Expand All @@ -317,13 +299,7 @@ class TabsTrayPresenterTest {
var closed = false

val tabsTray: MockedTabsTray = spy(MockedTabsTray())
val thumbnailsUseCases = ThumbnailsUseCases(store, ThumbnailStorage(testContext))
val presenter = TabsTrayPresenter(
tabsTray,
store,
thumbnailsUseCases,
tabsFilter = { true },
closeTabsTray = { closed = true })
val presenter = TabsTrayPresenter(tabsTray, store, tabsFilter = { true }, closeTabsTray = { closed = true })

presenter.start()
testDispatcher.advanceUntilIdle()
Expand All @@ -348,16 +324,15 @@ class TabsTrayPresenterTest {
val store = BrowserStore(
BrowserState(
tabs = listOf(
createTab("https://www.mozilla.org", id = "a", thumbnail = mock()),
createTab("https://getpocket.com", id = "b", thumbnail = mock())
createTab("https://www.mozilla.org", id = "a"),
createTab("https://getpocket.com", id = "b")
),
selectedTabId = "a"
)
)

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

presenter.start()
testDispatcher.advanceUntilIdle()
Expand All @@ -379,17 +354,15 @@ class TabsTrayPresenterTest {
val store = BrowserStore(
BrowserState(
tabs = listOf(
createTab("https://www.mozilla.org", id = "a", thumbnail = mock()),
createTab("https://getpocket.com", id = "b", private = true, thumbnail = mock())
createTab("https://www.mozilla.org", id = "a"),
createTab("https://getpocket.com", id = "b", private = true)
),
selectedTabId = "a"
)
)

val tabsTray: MockedTabsTray = spy(MockedTabsTray())
val thumbnailsUseCases = ThumbnailsUseCases(store, ThumbnailStorage(testContext))
val presenter =
TabsTrayPresenter(tabsTray, store, thumbnailsUseCases, { it.content.private }, mock())
val presenter = TabsTrayPresenter(tabsTray, store, { it.content.private }, mock())

presenter.start()
testDispatcher.advanceUntilIdle()
Expand All @@ -408,8 +381,7 @@ class TabsTrayPresenterTest {

var invoked = false
val tabsTray: MockedTabsTray = spy(MockedTabsTray())
val presenter =
TabsTrayPresenter(tabsTray, store, mock(), { it.content.private }, { invoked = true })
val presenter = TabsTrayPresenter(tabsTray, store, { it.content.private }, { invoked = true })

presenter.start()
testDispatcher.advanceUntilIdle()
Expand Down Expand Up @@ -447,8 +419,7 @@ private class MockedTabsTray : TabsTray {

override fun notifyAtLeastOneObserver(block: TabsTray.Observer.() -> Unit) {}

override fun <R> wrapConsumers(block: TabsTray.Observer.(R) -> Boolean): List<(R) -> Boolean> =
emptyList()
override fun <R> wrapConsumers(block: TabsTray.Observer.(R) -> Boolean): List<(R) -> Boolean> = emptyList()

override fun isObserved(): Boolean = false

Expand Down
4 changes: 2 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ permalink: /changelog/
* Added support for [onbeforeunload prompt](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload)

* **feature-tabs**
* ⚠️ **This is a breaking change**: Added a dependency on `ThumbnailsUseCases` to `TabsFeature` and `TabsTrayPresenter`
for loading a tab's thumbnail.
* Added an optional `ThumbnailsUseCases` to `TabsFeature` and `TabsTrayPresenter` for loading a
tab's thumbnail.

* **browser-thumbnails**
* Adds `LoadThumbnailUseCase` in `ThumbnailsUseCases` for loading the thumbnail of a tab.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class TabsTrayFragment : Fragment(), UserInteractionHandler {
tabsTray,
components.store,
components.tabsUseCases,
components.thumbnailsUseCases,
closeTabsTray = ::closeTabsTray
closeTabsTray = ::closeTabsTray,
thumbnailsUseCases = components.thumbnailsUseCases
).also { lifecycle.addObserver(it) }
}

Expand Down

0 comments on commit d5b9b3f

Please sign in to comment.