This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #10335: Add TargetTab helper for observing specific tabs.
- Loading branch information
Showing
4 changed files
with
417 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
...ate/src/androidTest/java/mozilla/components/browser/state/helper/OnDeviceTargetTabTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* 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.browser.state.helper | ||
|
||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import kotlinx.coroutines.runBlocking | ||
import mozilla.components.browser.state.action.BrowserAction | ||
import mozilla.components.browser.state.action.CustomTabListAction | ||
import mozilla.components.browser.state.action.TabListAction | ||
import mozilla.components.browser.state.state.BrowserState | ||
import mozilla.components.browser.state.state.createCustomTab | ||
import mozilla.components.browser.state.state.createTab | ||
import mozilla.components.browser.state.store.BrowserStore | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
/** | ||
* On-device tests for [TargetTab]. | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class OnDeviceTargetTabTest { | ||
@get:Rule | ||
val rule = createComposeRule() | ||
|
||
@Test | ||
fun observingSelectedTab() { | ||
val store = BrowserStore() | ||
|
||
val target = TargetTab.Selected | ||
var observedTabId: String? = null | ||
|
||
rule.setContent { | ||
val state = target.observeAsStateFrom( | ||
store = store, | ||
observe = { tab -> tab?.id } | ||
) | ||
observedTabId = state.value?.id | ||
} | ||
|
||
assertNull(observedTabId) | ||
|
||
store.dispatchBlockingOnIdle( | ||
TabListAction.AddTabAction(createTab("https://www.mozilla.org", id = "mozilla")) | ||
) | ||
|
||
rule.runOnIdle { | ||
assertEquals("mozilla", observedTabId) | ||
} | ||
|
||
store.dispatchBlockingOnIdle( | ||
TabListAction.AddTabAction(createTab("https://example.org", id = "example")) | ||
) | ||
|
||
rule.runOnIdle { | ||
assertEquals("mozilla", observedTabId) | ||
} | ||
|
||
store.dispatchBlockingOnIdle( | ||
TabListAction.SelectTabAction("example") | ||
) | ||
|
||
rule.runOnIdle { | ||
assertEquals("example", observedTabId) | ||
} | ||
|
||
store.dispatchBlockingOnIdle( | ||
TabListAction.RemoveTabAction("example") | ||
) | ||
|
||
rule.runOnIdle { | ||
assertEquals("mozilla", observedTabId) | ||
} | ||
|
||
store.dispatchBlockingOnIdle(TabListAction.RemoveAllTabsAction) | ||
|
||
rule.runOnIdle { | ||
assertNull(observedTabId) | ||
} | ||
} | ||
|
||
@Test | ||
fun observingPinnedTab() { | ||
val store = BrowserStore( | ||
initialState = BrowserState( | ||
tabs = listOf( | ||
createTab("https://www.mozilla.org", id = "mozilla"), | ||
createTab("https://www.example.org", id = "example") | ||
), | ||
selectedTabId = "mozilla" | ||
) | ||
) | ||
|
||
val target = TargetTab.Pinned("mozilla") | ||
var observedTabId: String? = null | ||
|
||
rule.setContent { | ||
val state = target.observeAsStateFrom( | ||
store = store, | ||
observe = { tab -> tab?.id } | ||
) | ||
observedTabId = state.value?.id | ||
} | ||
|
||
assertEquals("mozilla", observedTabId) | ||
|
||
store.dispatchBlockingOnIdle(TabListAction.SelectTabAction("example")) | ||
|
||
rule.runOnIdle { | ||
assertEquals("mozilla", observedTabId) | ||
} | ||
|
||
store.dispatchBlockingOnIdle(TabListAction.RemoveTabAction("mozilla")) | ||
|
||
rule.runOnIdle { | ||
assertNull(observedTabId) | ||
} | ||
} | ||
|
||
@Test | ||
fun observingCustomTab() { | ||
val store = BrowserStore( | ||
initialState = BrowserState( | ||
tabs = listOf( | ||
createTab("https://www.mozilla.org", id = "mozilla"), | ||
createTab("https://www.example.org", id = "example") | ||
), | ||
customTabs = listOf( | ||
createCustomTab("https://www.reddit.com/r/firefox/", id = "reddit") | ||
), | ||
selectedTabId = "mozilla" | ||
) | ||
) | ||
|
||
val target = TargetTab.Custom("reddit") | ||
|
||
var observedTabId: String? = null | ||
|
||
rule.setContent { | ||
val state = target.observeAsStateFrom( | ||
store = store, | ||
observe = { tab -> tab?.id } | ||
) | ||
observedTabId = state.value?.id | ||
} | ||
|
||
assertEquals("reddit", observedTabId) | ||
|
||
store.dispatchBlockingOnIdle(TabListAction.SelectTabAction("example")) | ||
|
||
rule.runOnIdle { | ||
assertEquals("reddit", observedTabId) | ||
} | ||
|
||
store.dispatchBlockingOnIdle(TabListAction.RemoveTabAction("mozilla")) | ||
|
||
rule.runOnIdle { | ||
assertEquals("reddit", observedTabId) | ||
} | ||
|
||
store.dispatchBlockingOnIdle(CustomTabListAction.RemoveCustomTabAction("reddit")) | ||
|
||
rule.runOnIdle { | ||
assertNull(observedTabId) | ||
} | ||
} | ||
|
||
private fun BrowserStore.dispatchBlockingOnIdle(action: BrowserAction) { | ||
rule.runOnIdle { | ||
val job = dispatch(action) | ||
runBlocking { job.join() } | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
components/browser/state/src/main/java/mozilla/components/browser/state/helper/TargetTab.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* 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.browser.state.helper | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.State | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleOwner | ||
import mozilla.components.browser.state.selector.findCustomTab | ||
import mozilla.components.browser.state.selector.findTab | ||
import mozilla.components.browser.state.selector.selectedTab | ||
import mozilla.components.browser.state.state.BrowserState | ||
import mozilla.components.browser.state.state.SessionState | ||
import mozilla.components.browser.state.store.BrowserStore | ||
import mozilla.components.lib.state.Store | ||
import mozilla.components.lib.state.ext.observeAsState | ||
|
||
/** | ||
* Helper for allowing a component consumer to specify which tab a component should target (e.g. | ||
* the selected tab, a specific pinned tab or a custom tab). Additional helper methods make it | ||
* easier to lookup the current state of the tab or observe changes. | ||
*/ | ||
sealed class TargetTab { | ||
/** | ||
* Looks up this target in the given [BrowserStore] and returns the matching [SessionState] if | ||
* available. Otherwise returns `null`. | ||
*/ | ||
fun lookupIn(store: BrowserStore): SessionState? = lookupIn(store.state) | ||
|
||
/** | ||
* Looks up this target in the given [BrowserState] and returns the matching [SessionState] if | ||
* available. Otherwise returns `null`. | ||
*/ | ||
abstract fun lookupIn(state: BrowserState): SessionState? | ||
|
||
/** | ||
* Observes this target and represents the mapped state (using [map]) via [State]. | ||
* | ||
* Everytime the [Store] state changes and the result of the [observe] function changes for this | ||
* state, the returned [State] will be updated causing recomposition of every [State.value] usage. | ||
* | ||
* The [Store] observer will automatically be removed when this composable disposes or the current | ||
* [LifecycleOwner] moves to the [Lifecycle.State.DESTROYED] state. | ||
*/ | ||
@Composable | ||
fun <R> observeAsStateFrom( | ||
store: BrowserStore, | ||
observe: (SessionState?) -> R | ||
): State<SessionState?> { | ||
return store.observeAsState( | ||
map = { state -> lookupIn(state) }, | ||
observe = { state -> observe(lookupIn(state)) } | ||
) | ||
} | ||
|
||
/** | ||
* Targets the selected tab. | ||
*/ | ||
object Selected : TargetTab() { | ||
override fun lookupIn(state: BrowserState): SessionState? { | ||
return state.selectedTab | ||
} | ||
} | ||
|
||
/** | ||
* Targets a specific tab by its [tabId]. | ||
*/ | ||
class Pinned(val tabId: String) : TargetTab() { | ||
override fun lookupIn(state: BrowserState): SessionState? { | ||
return state.findTab(tabId) | ||
} | ||
} | ||
|
||
/** | ||
* Targets a specific custom tab by its [customTabId]. | ||
*/ | ||
class Custom(val customTabId: String) : TargetTab() { | ||
override fun lookupIn(state: BrowserState): SessionState? { | ||
return state.findCustomTab(customTabId) | ||
} | ||
} | ||
} |
Oops, something went wrong.