forked from mozilla-mobile/android-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HistoryMetadata state, action, and reducer
Co-authored-by: Grisha Kruglov <[email protected]>
- Loading branch information
1 parent
a2da68d
commit b5080fd
Showing
6 changed files
with
88 additions
and
1 deletion.
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
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
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
34 changes: 34 additions & 0 deletions
34
...er/state/src/main/java/mozilla/components/browser/state/reducer/HistoryMetadataReducer.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,34 @@ | ||
/* 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.reducer | ||
|
||
import mozilla.components.browser.state.action.HistoryMetadataAction | ||
import mozilla.components.browser.state.state.BrowserState | ||
import mozilla.components.browser.state.state.TabSessionState | ||
import mozilla.components.concept.storage.HistoryMetadataKey | ||
|
||
internal object HistoryMetadataReducer { | ||
|
||
/** | ||
* [HistoryMetadataAction] reducer function for modifying [TabSessionState.historyMetadata]. | ||
*/ | ||
fun reduce(state: BrowserState, action: HistoryMetadataAction): BrowserState { | ||
return when (action) { | ||
is HistoryMetadataAction.SetHistoryMetadataKeyAction -> | ||
state.updateHistoryMetadataKey(action.tabId, action.historyMetadataKey) | ||
} | ||
} | ||
} | ||
|
||
private fun BrowserState.updateHistoryMetadataKey( | ||
tabId: String, | ||
key: HistoryMetadataKey | ||
): BrowserState { | ||
return copy( | ||
tabs = tabs.updateTabs(tabId) { current -> | ||
current.copy(historyMetadata = key) | ||
} ?: tabs | ||
) | ||
} |
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
33 changes: 33 additions & 0 deletions
33
.../state/src/test/java/mozilla/components/browser/state/action/HistoryMetadataActionTest.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,33 @@ | ||
/* 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.action | ||
|
||
import mozilla.components.browser.state.selector.findTab | ||
import mozilla.components.browser.state.state.BrowserState | ||
import mozilla.components.browser.state.state.createTab | ||
import mozilla.components.browser.state.store.BrowserStore | ||
import mozilla.components.concept.storage.HistoryMetadataKey | ||
import mozilla.components.support.test.ext.joinBlocking | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Test | ||
|
||
class HistoryMetadataActionTest { | ||
|
||
@Test | ||
fun `SetHistoryMetadataKeyAction - Associates tab with history metadata`() { | ||
val tab = createTab("https://www.mozilla.org") | ||
val store = BrowserStore(BrowserState(tabs = listOf(tab))) | ||
assertNull(store.state.findTab(tab.id)?.historyMetadata) | ||
|
||
val historyMetadata = HistoryMetadataKey( | ||
url = tab.content.url, | ||
referrerUrl = "https://firefox.com" | ||
) | ||
|
||
store.dispatch(HistoryMetadataAction.SetHistoryMetadataKeyAction(tab.id, historyMetadata)).joinBlocking() | ||
assertEquals(historyMetadata, store.state.findTab(tab.id)?.historyMetadata) | ||
} | ||
} |