Commit 45dba0c3 authored by MozLando's avatar MozLando
Browse files

Merge #6769



6769: Closes #6538: Add navigation state to browser state r=pocmo a=csadilek

Need this for my reader mode refactor :). 
Co-authored-by: default avatarChristian Sadilek <christian.sadilek@gmail.com>
parents a30e917d 54946ae6
......@@ -18,6 +18,8 @@ import mozilla.components.browser.state.action.ContentAction.AddFindResultAction
import mozilla.components.browser.state.action.ContentAction.ClearFindResultsAction
import mozilla.components.browser.state.action.ContentAction.ConsumeHitResultAction
import mozilla.components.browser.state.action.ContentAction.FullScreenChangedAction
import mozilla.components.browser.state.action.ContentAction.UpdateBackNavigationStateAction
import mozilla.components.browser.state.action.ContentAction.UpdateForwardNavigationStateAction
import mozilla.components.browser.state.action.ContentAction.RemoveThumbnailAction
import mozilla.components.browser.state.action.ContentAction.UpdateHitResultAction
import mozilla.components.browser.state.action.ContentAction.UpdateLoadingStateAction
......@@ -232,6 +234,7 @@ class Session(
*/
var canGoBack: Boolean by Delegates.observable(false) { _, old, new ->
notifyObservers(old, new) { onNavigationStateChanged(this@Session, new, canGoForward) }
store?.syncDispatch(UpdateBackNavigationStateAction(id, canGoBack))
}
/**
......@@ -239,6 +242,7 @@ class Session(
*/
var canGoForward: Boolean by Delegates.observable(false) { _, old, new ->
notifyObservers(old, new) { onNavigationStateChanged(this@Session, canGoBack, new) }
store?.syncDispatch(UpdateForwardNavigationStateAction(id, canGoForward))
}
/**
......
......@@ -932,4 +932,36 @@ class SessionTest {
assertFalse(parentSession.hasParentSession)
assertTrue(session.hasParentSession)
}
@Test
fun `action is dispatched when back navigation state changes`() {
val store: BrowserStore = mock()
`when`(store.dispatch(any())).thenReturn(mock())
val session = Session("https://www.mozilla.org")
session.store = store
session.canGoBack = true
verify(store).dispatch(ContentAction.UpdateBackNavigationStateAction(session.id, true))
session.canGoBack = false
verify(store).dispatch(ContentAction.UpdateBackNavigationStateAction(session.id, false))
verifyNoMoreInteractions(store)
}
@Test
fun `action is dispatched when forward navigation state changes`() {
val store: BrowserStore = mock()
`when`(store.dispatch(any())).thenReturn(mock())
val session = Session("https://www.mozilla.org")
session.store = store
session.canGoForward = true
verify(store).dispatch(ContentAction.UpdateForwardNavigationStateAction(session.id, true))
session.canGoForward = false
verify(store).dispatch(ContentAction.UpdateForwardNavigationStateAction(session.id, false))
verifyNoMoreInteractions(store)
}
}
......@@ -259,6 +259,16 @@ sealed class ContentAction : BrowserAction() {
* @property layoutInDisplayCutoutMode value of defined in https://developer.android.com/reference/android/view/WindowManager.LayoutParams#layoutInDisplayCutoutMode
*/
data class ViewportFitChangedAction(val sessionId: String, val layoutInDisplayCutoutMode: Int) : ContentAction()
/**
* Updates the [ContentState] of the given [sessionId] to indicate whether or not a back navigation is possible.
*/
data class UpdateBackNavigationStateAction(val sessionId: String, val canGoBack: Boolean) : ContentAction()
/**
* Updates the [ContentState] of the given [sessionId] to indicate whether or not a forward navigation is possible.
*/
data class UpdateForwardNavigationStateAction(val sessionId: String, val canGoForward: Boolean) : ContentAction()
}
/**
......
......@@ -105,6 +105,12 @@ internal object ContentStateReducer {
is ContentAction.ViewportFitChangedAction -> updateContentState(state, action.sessionId) {
it.copy(layoutInDisplayCutoutMode = action.layoutInDisplayCutoutMode)
}
is ContentAction.UpdateBackNavigationStateAction -> updateContentState(state, action.sessionId) {
it.copy(canGoBack = action.canGoBack)
}
is ContentAction.UpdateForwardNavigationStateAction -> updateContentState(state, action.sessionId) {
it.copy(canGoForward = action.canGoForward)
}
}
}
}
......
......@@ -35,6 +35,8 @@ import mozilla.components.concept.engine.window.WindowRequest
* @property searchRequest the last received [SearchRequest]
* @property fullScreen true if the page is full screen, false if not.
* @property layoutInDisplayCutoutMode the display layout cutout mode state.
* @property canGoBack whether or not there's an history item to navigate back to.
* @property canGoForward whether or not there's an history item to navigate forward to.
*/
data class ContentState(
val url: String,
......@@ -53,5 +55,7 @@ data class ContentState(
val windowRequest: WindowRequest? = null,
val searchRequest: SearchRequest? = null,
val fullScreen: Boolean = false,
val layoutInDisplayCutoutMode: Int = 0
val layoutInDisplayCutoutMode: Int = 0,
val canGoBack: Boolean = false,
val canGoForward: Boolean = false
)
......@@ -192,7 +192,7 @@ class ContentActionTest {
}
@Test
fun `UpdateSecurityInfo updates searchInfo`() {
fun `UpdateSecurityInfo updates securityInfo`() {
val newSecurityInfo = SecurityInfoState(true, "mozilla.org", "The Mozilla Team")
assertNotEquals(newSecurityInfo, tab.content.securityInfo)
......@@ -527,4 +527,36 @@ class ContentActionTest {
assertNull(tab.content.windowRequest)
}
@Test
fun `UpdateBackNavigationStateAction updates canGoBack`() {
assertFalse(tab.content.canGoBack)
assertFalse(otherTab.content.canGoBack)
store.dispatch(ContentAction.UpdateBackNavigationStateAction(tab.id, true)).joinBlocking()
assertTrue(tab.content.canGoBack)
assertFalse(otherTab.content.canGoBack)
store.dispatch(ContentAction.UpdateBackNavigationStateAction(tab.id, false)).joinBlocking()
assertFalse(tab.content.canGoBack)
assertFalse(otherTab.content.canGoBack)
}
@Test
fun `UpdateForwardNavigationStateAction updates canGoForward`() {
assertFalse(tab.content.canGoForward)
assertFalse(otherTab.content.canGoForward)
store.dispatch(ContentAction.UpdateForwardNavigationStateAction(tab.id, true)).joinBlocking()
assertTrue(tab.content.canGoForward)
assertFalse(otherTab.content.canGoForward)
store.dispatch(ContentAction.UpdateForwardNavigationStateAction(tab.id, false)).joinBlocking()
assertFalse(tab.content.canGoForward)
assertFalse(otherTab.content.canGoForward)
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment