Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Bug 1876567 - Fix Middleware Issue with Processing Translations States
Browse files Browse the repository at this point in the history
This patch corrects an issue where `isTranslateProcessing` and
`isRestoreProcessing` states were not updating due to not sending along
pre-process actions in `EngineDelegateMiddleware`.
  • Loading branch information
ohall-m committed Jan 25, 2024
1 parent aa64798 commit 34c1552
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ internal class EngineDelegateMiddleware(
is EngineAction.ExitFullScreenModeAction -> exitFullScreen(context.store, action)
is EngineAction.SaveToPdfAction -> saveToPdf(context.store, action)
is EngineAction.PrintContentAction -> printContent(context.store, action)
is TranslationsAction.TranslateAction -> translate(context.store, action)
is TranslationsAction.TranslateRestoreAction -> translateRestoreOriginal(context.store, action)
is TranslationsAction.TranslateAction -> translate(context.store, next, action)
is TranslationsAction.TranslateRestoreAction -> translateRestoreOriginal(context.store, next, action)
is EngineAction.ClearDataAction -> clearData(context.store, action)
is EngineAction.PurgeHistoryAction -> purgeHistory(context.state)
else -> next(action)
Expand Down Expand Up @@ -156,16 +156,20 @@ internal class EngineDelegateMiddleware(

private fun translate(
store: Store<BrowserState, BrowserAction>,
next: (BrowserAction) -> Unit,
action: TranslationsAction.TranslateAction,
) = scope.launch {
next(action)
getEngineSessionOrDispatch(store, action)
?.requestTranslate(action.fromLanguage, action.toLanguage, action.options)
}

private fun translateRestoreOriginal(
store: Store<BrowserState, BrowserAction>,
next: (BrowserAction) -> Unit,
action: TranslationsAction.TranslateRestoreAction,
) = scope.launch {
next(action)
getEngineSessionOrDispatch(store, action)
?.requestTranslationRestore()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
package mozilla.components.browser.state.engine.middleware

import mozilla.components.browser.state.action.EngineAction
import mozilla.components.browser.state.action.TranslationsAction
import mozilla.components.browser.state.engine.EngineMiddleware
import mozilla.components.browser.state.selector.findTab
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.EngineState
import mozilla.components.browser.state.state.createCustomTab
import mozilla.components.browser.state.state.createTab
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSession
import mozilla.components.support.test.any
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.libstate.ext.waitUntilIdle
import mozilla.components.support.test.mock
Expand Down Expand Up @@ -742,4 +745,67 @@ class EngineDelegateMiddlewareTest {
verify(engineSession1).purgeHistory()
verify(engineSession2).purgeHistory()
}

@Test
fun `TranslateAction correctly sets progress state AND begins a translation`() {
val tab = createTab("https://www.mozilla.org")
val engineSession: EngineSession = mock()
val engine: Engine = mock()
doReturn(engineSession).`when`(engine).createSession()
val store = BrowserStore(
middleware = EngineMiddleware.create(
engine = engine,
scope = scope,
),
initialState = BrowserState(
tabs = listOf(tab),
),
)

assertEquals(false, store.state.findTab(tab.id)?.translationsState?.isTranslateProcessing)

store.dispatch(
TranslationsAction.TranslateAction(
tabId = tab.id,
fromLanguage = "es",
toLanguage = "en",
options = null,
),
).joinBlocking()

dispatcher.scheduler.advanceUntilIdle()
store.waitUntilIdle()

verify(engineSession).requestTranslate(any(), any(), any())
assertEquals(true, store.state.findTab(tab.id)?.translationsState?.isTranslateProcessing)
}

@Test
fun `TranslateRestoreAction correctly sets progress state AND begins a restore`() {
val tab = createTab("https://www.mozilla.org")
val engineSession: EngineSession = mock()
val engine: Engine = mock()
doReturn(engineSession).`when`(engine).createSession()
val store = BrowserStore(
middleware = EngineMiddleware.create(
engine = engine,
scope = scope,
),
initialState = BrowserState(
tabs = listOf(tab),
),
)

assertEquals(false, store.state.findTab(tab.id)?.translationsState?.isRestoreProcessing)

store.dispatch(
TranslationsAction.TranslateRestoreAction(tabId = tab.id),
).joinBlocking()

dispatcher.scheduler.advanceUntilIdle()
store.waitUntilIdle()

verify(engineSession).requestTranslationRestore()
assertEquals(true, store.state.findTab(tab.id)?.translationsState?.isRestoreProcessing)
}
}

0 comments on commit 34c1552

Please sign in to comment.