This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[components] Closes mozilla-mobile/android-components#12190: Create a…
… middleware that allows to change tabs priority.
- Loading branch information
1 parent
be7c336
commit 4a5c1b0
Showing
6 changed files
with
234 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
80 changes: 80 additions & 0 deletions
80
...ava/mozilla/components/browser/state/engine/middleware/SessionPrioritizationMiddleware.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,80 @@ | ||
/* 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.engine.middleware | ||
|
||
import androidx.annotation.VisibleForTesting | ||
import mozilla.components.browser.state.action.BrowserAction | ||
import mozilla.components.browser.state.action.EngineAction | ||
import mozilla.components.browser.state.action.TabListAction | ||
import mozilla.components.browser.state.selector.findTab | ||
import mozilla.components.browser.state.state.BrowserState | ||
import mozilla.components.concept.engine.EngineSession | ||
import mozilla.components.concept.engine.EngineSession.SessionPriority.DEFAULT | ||
import mozilla.components.concept.engine.EngineSession.SessionPriority.HIGH | ||
import mozilla.components.lib.state.Middleware | ||
import mozilla.components.lib.state.MiddlewareContext | ||
import mozilla.components.support.base.log.logger.Logger | ||
|
||
/** | ||
* [Middleware] implementation responsible for updating the priority of the selected [EngineSession] | ||
* to [HIGH] and the rest to [DEFAULT]. | ||
*/ | ||
class SessionPrioritizationMiddleware : Middleware<BrowserState, BrowserAction> { | ||
private val logger = Logger("SessionPrioritizationMiddleware") | ||
|
||
@VisibleForTesting | ||
internal var previousHighestPriorityTabId = "" | ||
|
||
override fun invoke( | ||
context: MiddlewareContext<BrowserState, BrowserAction>, | ||
next: (BrowserAction) -> Unit, | ||
action: BrowserAction | ||
) { | ||
|
||
when (action) { | ||
is EngineAction.UnlinkEngineSessionAction -> { | ||
val activeTab = context.state.findTab(action.tabId) | ||
activeTab?.engineState?.engineSession?.updateSessionPriority(DEFAULT) | ||
logger.info("Update the tab ${activeTab?.id} priority to ${DEFAULT.name}") | ||
} | ||
else -> { | ||
// no-op | ||
} | ||
} | ||
|
||
next(action) | ||
|
||
when (action) { | ||
is TabListAction, | ||
is EngineAction.LinkEngineSessionAction -> { | ||
val state = context.state | ||
if (previousHighestPriorityTabId != state.selectedTabId) { | ||
updatePriorityIfNeeded(state) | ||
} | ||
} | ||
else -> { | ||
// no-op | ||
} | ||
} | ||
} | ||
|
||
private fun updatePriorityIfNeeded(state: BrowserState) { | ||
val currentSelectedTab = state.selectedTabId?.let { state.findTab(it) } | ||
val previousSelectedTab = state.findTab(previousHighestPriorityTabId) | ||
val currentEngineSession: EngineSession? = currentSelectedTab?.engineState?.engineSession | ||
|
||
// We need to make sure, we alter the previousHighestPriorityTabId, after the session is linked. | ||
// So we update the priority on the engine session, as we could get actions where the tab | ||
// is selected but not linked yet, causing out sync issues, | ||
// when previousHighestPriorityTabId didn't call updateSessionPriority() | ||
if (currentEngineSession != null) { | ||
previousSelectedTab?.engineState?.engineSession?.updateSessionPriority(DEFAULT) | ||
currentEngineSession.updateSessionPriority(HIGH) | ||
logger.info("Update the currentSelectedTab ${currentSelectedTab.id} priority to ${HIGH.name}") | ||
logger.info("Update the previousSelectedTab ${previousSelectedTab?.id} priority to ${DEFAULT.name}") | ||
previousHighestPriorityTabId = currentSelectedTab.id | ||
} | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...mozilla/components/browser/state/engine/middleware/SessionPrioritizationMiddlewareTest.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,118 @@ | ||
/* 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.engine.middleware | ||
|
||
import mozilla.components.browser.state.action.EngineAction | ||
import mozilla.components.browser.state.action.TabListAction | ||
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.engine.EngineSession | ||
import mozilla.components.concept.engine.EngineSession.SessionPriority.DEFAULT | ||
import mozilla.components.concept.engine.EngineSession.SessionPriority.HIGH | ||
import mozilla.components.support.test.ext.joinBlocking | ||
import mozilla.components.support.test.mock | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import org.mockito.Mockito.verify | ||
|
||
class SessionPrioritizationMiddlewareTest { | ||
|
||
@Test | ||
fun `GIVEN a linked session WHEN UnlinkEngineSessionAction THEN set the DEFAULT priority to the unlinked tab`() { | ||
val middleware = SessionPrioritizationMiddleware() | ||
val store = BrowserStore( | ||
initialState = BrowserState( | ||
tabs = listOf( | ||
createTab("https://www.mozilla.org", id = "1"), | ||
) | ||
), | ||
middleware = listOf(middleware) | ||
) | ||
val engineSession1: EngineSession = mock() | ||
|
||
store.dispatch(EngineAction.LinkEngineSessionAction("1", engineSession1)).joinBlocking() | ||
store.dispatch(EngineAction.UnlinkEngineSessionAction("1")).joinBlocking() | ||
|
||
verify(engineSession1).updateSessionPriority(DEFAULT) | ||
assertEquals("", middleware.previousHighestPriorityTabId) | ||
} | ||
|
||
@Test | ||
fun `GIVEN a previous selected tab WHEN LinkEngineSessionAction THEN update the selected linked tab priority to HIGH`() { | ||
val middleware = SessionPrioritizationMiddleware() | ||
val store = BrowserStore( | ||
initialState = BrowserState( | ||
tabs = listOf( | ||
createTab("https://www.mozilla.org", id = "1"), | ||
) | ||
), | ||
middleware = listOf(middleware) | ||
) | ||
val engineSession1: EngineSession = mock() | ||
|
||
store.dispatch(TabListAction.SelectTabAction("1")).joinBlocking() | ||
|
||
assertEquals("", middleware.previousHighestPriorityTabId) | ||
|
||
store.dispatch(EngineAction.LinkEngineSessionAction("1", engineSession1)).joinBlocking() | ||
|
||
assertEquals("1", middleware.previousHighestPriorityTabId) | ||
verify(engineSession1).updateSessionPriority(HIGH) | ||
} | ||
|
||
@Test | ||
fun `GIVEN a previous selected tab with priority DEFAULT WHEN selecting and linking a new tab THEN update the previous tab to DEFAULT and the new one to HIGH`() { | ||
val middleware = SessionPrioritizationMiddleware() | ||
val store = BrowserStore( | ||
initialState = BrowserState( | ||
tabs = listOf( | ||
createTab("https://www.mozilla.org", id = "1"), | ||
createTab("https://www.firefox.com", id = "2") | ||
) | ||
), | ||
middleware = listOf(middleware) | ||
) | ||
val engineSession1: EngineSession = mock() | ||
val engineSession2: EngineSession = mock() | ||
|
||
store.dispatch(TabListAction.SelectTabAction("1")).joinBlocking() | ||
|
||
assertEquals("", middleware.previousHighestPriorityTabId) | ||
|
||
store.dispatch(EngineAction.LinkEngineSessionAction("1", engineSession1)).joinBlocking() | ||
|
||
assertEquals("1", middleware.previousHighestPriorityTabId) | ||
verify(engineSession1).updateSessionPriority(HIGH) | ||
|
||
store.dispatch(TabListAction.SelectTabAction("2")).joinBlocking() | ||
|
||
assertEquals("1", middleware.previousHighestPriorityTabId) | ||
|
||
store.dispatch(EngineAction.LinkEngineSessionAction("2", engineSession2)).joinBlocking() | ||
|
||
assertEquals("2", middleware.previousHighestPriorityTabId) | ||
verify(engineSession1).updateSessionPriority(DEFAULT) | ||
verify(engineSession2).updateSessionPriority(HIGH) | ||
} | ||
|
||
@Test | ||
fun `GIVEN no linked tab WHEN SelectTabAction THEN no changes in priority show happened`() { | ||
val middleware = SessionPrioritizationMiddleware() | ||
val store = BrowserStore( | ||
initialState = BrowserState( | ||
tabs = listOf( | ||
createTab("https://www.mozilla.org", id = "1"), | ||
createTab("https://www.firefox.com", id = "2") | ||
) | ||
), | ||
middleware = listOf(middleware) | ||
) | ||
|
||
store.dispatch(TabListAction.SelectTabAction("1")).joinBlocking() | ||
|
||
assertEquals("", middleware.previousHighestPriorityTabId) | ||
} | ||
} |
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