tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit f9560c4992638472c499e196c02efcd83a414abe
parent 0e94c2e11a1729f52a6e381f7904df40be7be224
Author: mcarare <48995920+mcarare@users.noreply.github.com>
Date:   Thu,  2 Oct 2025 17:19:35 +0000

Bug 1991559 - Refactor AppState related tests to use the AppStoreReducer directly. r=android-reviewers,jonalmeida

This migrates unit tests for actions and reducers related to `AppState` to call the `AppStoreReducer.reduce` function directly, instead of using a full `AppStore` instance to dispatch actions.

This change simplifies the tests by removing the need for an actual store, middlewares, and asynchronous dispatching with `joinBlocking`. The tests now verify the state transformations in a purely functional and synchronous way.

Differential Revision: https://phabricator.services.mozilla.com/D266989

Diffstat:
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/AppActionTest.kt | 15++++++---------
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/AppStoreReducerTest.kt | 121++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/FindInPageStateReducerTest.kt | 20+++++++++-----------
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/ReaderViewStateReducerTest.kt | 27++++++++++++---------------
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/ShortcutStateReducerTest.kt | 10+++++-----
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/SnackbarStateReducerTest.kt | 26+++++++++++---------------
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/TabStripActionTest.kt | 14++++++--------
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/qrscanner/QrScannerActionTest.kt | 45++++++++++++++++++++++-----------------------
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/search/SearchStateReducerTest.kt | 53+++++++++++++++++++++++++----------------------------
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/share/ShareActionReducerTest.kt | 40++++++++++++++++++++++------------------
10 files changed, 179 insertions(+), 192 deletions(-)

diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/AppActionTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/AppActionTest.kt @@ -4,24 +4,21 @@ package org.mozilla.fenix.components.appstate -import mozilla.components.support.test.ext.joinBlocking -import mozilla.components.support.test.middleware.CaptureActionsMiddleware import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test -import org.mozilla.fenix.components.AppStore class AppActionTest { - private val capture = CaptureActionsMiddleware<AppState, AppAction>() - private val appStore = AppStore(middlewares = listOf(capture)) - @Test fun `WHEN UpdateInactiveExpanded is dispatched THEN update inactiveTabsExpanded`() { - assertFalse(appStore.state.inactiveTabsExpanded) + val initialState = AppState() + + assertFalse(initialState.inactiveTabsExpanded) - appStore.dispatch(AppAction.UpdateInactiveExpanded(true)).joinBlocking() + val finalState = + AppStoreReducer.reduce(initialState, AppAction.UpdateInactiveExpanded(true)) - assertTrue(appStore.state.inactiveTabsExpanded) + assertTrue(finalState.inactiveTabsExpanded) } } diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/AppStoreReducerTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/AppStoreReducerTest.kt @@ -10,13 +10,11 @@ import mozilla.components.browser.state.state.content.DownloadState import mozilla.components.concept.storage.BookmarkNode import mozilla.components.concept.storage.BookmarkNodeType import mozilla.components.lib.crash.Crash.NativeCodeCrash -import mozilla.components.support.test.ext.joinBlocking import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test -import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.AppAction.AddNonFatalCrash import org.mozilla.fenix.components.appstate.AppAction.RemoveAllNonFatalCrashes import org.mozilla.fenix.components.appstate.AppAction.RemoveNonFatalCrash @@ -91,7 +89,7 @@ class AppStoreReducerTest { assertFalse(initialState.searchState.isSearchActive) - var updatedState = AppStoreReducer.reduce( + val updatedState = AppStoreReducer.reduce( initialState, AppAction.SearchAction.SearchStarted( tabId = "test", @@ -143,22 +141,23 @@ class AppStoreReducerTest { @Test fun `WHEN translation started action is dispatched THEN snackbar state is updated`() { - val appStore = AppStore() + val initialState = AppState() val sessionId = "sessionId" - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.TranslationsAction.TranslationStarted(sessionId = sessionId), - ).joinBlocking() + ) assertEquals( SnackbarState.TranslationInProgress(sessionId = sessionId), - appStore.state.snackbarState, + finalState.snackbarState, ) } @Test fun `WHEN bookmark added action is dispatched THEN snackbar state is updated`() { - val appStore = AppStore() + val initialState = AppState() val guidToEdit = "guidToEdit" val parentNode = BookmarkNode( type = BookmarkNodeType.FOLDER, @@ -172,149 +171,149 @@ class AppStoreReducerTest { children = listOf(), ) - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.BookmarkAction.BookmarkAdded( guidToEdit = guidToEdit, parentNode = parentNode, source = MetricsUtils.BookmarkAction.Source.TEST, ), ) - .joinBlocking() assertEquals( SnackbarState.BookmarkAdded( guidToEdit = guidToEdit, parentNode = parentNode, ), - appStore.state.snackbarState, + finalState.snackbarState, ) } @Test fun `WHEN bookmark deleted action is dispatched THEN snackbar state is updated`() { - val appStore = AppStore() + val initialState = AppState() val bookmarkTitle = "test" - appStore.dispatch(AppAction.BookmarkAction.BookmarkDeleted(title = bookmarkTitle)) - .joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, AppAction.BookmarkAction.BookmarkDeleted(title = bookmarkTitle)) assertEquals( SnackbarState.BookmarkDeleted(title = bookmarkTitle), - appStore.state.snackbarState, + finalState.snackbarState, ) } @Test fun `WHEN delete and quit selected action is dispatched THEN snackbar state is updated`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.DeleteAndQuitStarted, - ).joinBlocking() + ) assertEquals( SnackbarState.DeletingBrowserDataInProgress, - appStore.state.snackbarState, + finalState.snackbarState, ) } @Test fun `WHEN open in firefox started action is dispatched THEN open in firefox requested is true`() { - val appStore = AppStore() - assertFalse(appStore.state.openInFirefoxRequested) + val initialState = AppState() + assertFalse(initialState.openInFirefoxRequested) - appStore.dispatch(AppAction.OpenInFirefoxStarted) - .joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, AppAction.OpenInFirefoxStarted) - assertTrue(appStore.state.openInFirefoxRequested) + assertTrue(finalState.openInFirefoxRequested) } @Test fun `WHEN open in firefox finished action is dispatched THEN open in firefox requested is false`() { - val appStore = AppStore( - initialState = AppState( - openInFirefoxRequested = true, - ), + val initialState = AppState( + openInFirefoxRequested = true, ) - assertTrue(appStore.state.openInFirefoxRequested) + assertTrue(initialState.openInFirefoxRequested) - appStore.dispatch(AppAction.OpenInFirefoxFinished) - .joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, AppAction.OpenInFirefoxFinished) - assertFalse(appStore.state.openInFirefoxRequested) + assertFalse(finalState.openInFirefoxRequested) } @Test fun `WHEN UserAccountAuthenticated action is dispatched THEN snackbar state is updated`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.UserAccountAuthenticated, - ).joinBlocking() + ) assertEquals( SnackbarState.UserAccountAuthenticated, - appStore.state.snackbarState, + finalState.snackbarState, ) } @Test fun `WHEN the current tab is closed THEN show a snackbar`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.CurrentTabClosed(true), - ).joinBlocking() + ) assertEquals( SnackbarState.CurrentTabClosed(true), - appStore.state.snackbarState, + finalState.snackbarState, ) } @Test fun `WHEN the current tab's URL has been copied THEN show a snackbar`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch(AppAction.URLCopiedToClipboard).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, AppAction.URLCopiedToClipboard) assertEquals( SnackbarState.URLCopiedToClipboard, - appStore.state.snackbarState, + finalState.snackbarState, ) } @Test fun `WHEN download in progress action is dispatched THEN snackbar state is updated`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.DownloadAction.DownloadInProgress("id"), - ).joinBlocking() + ) assertEquals( SnackbarState.DownloadInProgress("id"), - appStore.state.snackbarState, + finalState.snackbarState, ) } @Test fun `WHEN download failed action is dispatched THEN snackbar state is updated`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.DownloadAction.DownloadFailed("fileName"), - ).joinBlocking() + ) assertEquals( SnackbarState.DownloadFailed("fileName"), - appStore.state.snackbarState, + finalState.snackbarState, ) } @Test fun `WHEN download completed action is dispatched THEN snackbar state is updated`() { - val appStore = AppStore() + val initialState = AppState() val downloadState = DownloadState( id = "1", url = "url", @@ -328,24 +327,25 @@ class AppStoreReducerTest { createdTime = 33, etag = "etag", ) - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.DownloadAction.DownloadCompleted( downloadState, ), - ).joinBlocking() + ) assertEquals( SnackbarState.DownloadCompleted( downloadState, ), - appStore.state.snackbarState, + finalState.snackbarState, ) - assertTrue(appStore.state.supportedMenuNotifications.contains(SupportedMenuNotifications.Downloads)) + assertTrue(finalState.supportedMenuNotifications.contains(SupportedMenuNotifications.Downloads)) } @Test fun `WHEN can not open file action is dispatched THEN snackbar state is updated`() { - val appStore = AppStore() + val initialState = AppState() val downloadState = DownloadState( id = "1", @@ -361,17 +361,18 @@ class AppStoreReducerTest { etag = "etag", ) - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.DownloadAction.CannotOpenFile( downloadState, ), - ).joinBlocking() + ) assertEquals( SnackbarState.CannotOpenFileError( downloadState, ), - appStore.state.snackbarState, + finalState.snackbarState, ) } } diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/FindInPageStateReducerTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/FindInPageStateReducerTest.kt @@ -4,39 +4,37 @@ package org.mozilla.fenix.components.appstate -import mozilla.components.support.test.ext.joinBlocking import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test -import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.AppAction.FindInPageAction class FindInPageStateReducerTest { @Test fun `WHEN find in page started action is dispatched THEN state is updated`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch(FindInPageAction.FindInPageStarted).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, FindInPageAction.FindInPageStarted) - assertTrue(appStore.state.showFindInPage) + assertTrue(finalState.showFindInPage) } @Test fun `WHEN find in page dismissed action is dispatched THEN state is updated`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch(FindInPageAction.FindInPageDismissed).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, FindInPageAction.FindInPageDismissed) - assertFalse(appStore.state.showFindInPage) + assertFalse(finalState.showFindInPage) } @Test fun `WHEN find in page shown action is dispatched THEN state is updated`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch(FindInPageAction.FindInPageShown).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, FindInPageAction.FindInPageShown) - assertFalse(appStore.state.showFindInPage) + assertFalse(finalState.showFindInPage) } } diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/ReaderViewStateReducerTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/ReaderViewStateReducerTest.kt @@ -4,40 +4,37 @@ package org.mozilla.fenix.components.appstate -import mozilla.components.support.test.ext.joinBlocking import org.junit.Assert.assertEquals import org.junit.Test -import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.AppAction.ReaderViewAction import org.mozilla.fenix.components.appstate.readerview.ReaderViewState class ReaderViewStateReducerTest { - @Test fun `WHEN reader view started action is dispatched THEN reader view state is updated`() { - val appStore = AppStore() - appStore.dispatch(ReaderViewAction.ReaderViewStarted).joinBlocking() - assertEquals(ReaderViewState.Active, appStore.state.readerViewState) + val initialState = AppState() + val finalState = AppStoreReducer.reduce(initialState, ReaderViewAction.ReaderViewStarted) + assertEquals(ReaderViewState.Active, finalState.readerViewState) } @Test fun `WHEN reader view dismissed action is dispatched THEN reader view state is updated`() { - val appStore = AppStore() - appStore.dispatch(ReaderViewAction.ReaderViewDismissed).joinBlocking() - assertEquals(ReaderViewState.Dismiss, appStore.state.readerViewState) + val initialState = AppState() + val finalState = AppStoreReducer.reduce(initialState, ReaderViewAction.ReaderViewDismissed) + assertEquals(ReaderViewState.Dismiss, finalState.readerViewState) } @Test fun `WHEN reader view controls shown action is dispatched THEN reader view state is updated`() { - val appStore = AppStore() - appStore.dispatch(ReaderViewAction.ReaderViewControlsShown).joinBlocking() - assertEquals(ReaderViewState.ShowControls, appStore.state.readerViewState) + val initialState = AppState() + val finalState = AppStoreReducer.reduce(initialState, ReaderViewAction.ReaderViewControlsShown) + assertEquals(ReaderViewState.ShowControls, finalState.readerViewState) } @Test fun `WHEN reader view reset action is dispatched THEN reader view state is updated`() { - val appStore = AppStore() - appStore.dispatch(ReaderViewAction.Reset).joinBlocking() - assertEquals(ReaderViewState.None, appStore.state.readerViewState) + val initialState = AppState() + val finalState = AppStoreReducer.reduce(initialState, ReaderViewAction.Reset) + assertEquals(ReaderViewState.None, finalState.readerViewState) } } diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/ShortcutStateReducerTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/ShortcutStateReducerTest.kt @@ -4,20 +4,20 @@ package org.mozilla.fenix.components.appstate -import mozilla.components.support.test.ext.joinBlocking import org.junit.Assert.assertEquals import org.junit.Test -import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.snackbar.SnackbarState class ShortcutStateReducerTest { @Test fun `WHEN shortcut added action is dispatched THEN state is updated`() { - val appStore = AppStore() + val initialState = AppState() + assertEquals(SnackbarState.None(), initialState.snackbarState) - appStore.dispatch(AppAction.ShortcutAction.ShortcutAdded).joinBlocking() + val finalState = + AppStoreReducer.reduce(initialState, AppAction.ShortcutAction.ShortcutAdded) - assertEquals(SnackbarState.ShortcutAdded, appStore.state.snackbarState) + assertEquals(SnackbarState.ShortcutAdded, finalState.snackbarState) } } diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/SnackbarStateReducerTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/SnackbarStateReducerTest.kt @@ -4,43 +4,39 @@ package org.mozilla.fenix.components.appstate -import mozilla.components.support.test.ext.joinBlocking import org.junit.Assert.assertTrue import org.junit.Test -import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.AppAction.SnackbarAction import org.mozilla.fenix.components.appstate.snackbar.SnackbarState.DeletingBrowserDataInProgress import org.mozilla.fenix.components.appstate.snackbar.SnackbarState.Dismiss import org.mozilla.fenix.components.appstate.snackbar.SnackbarState.None class SnackbarStateReducerTest { - private val appStore = AppStore( - initialState = AppState( - snackbarState = DeletingBrowserDataInProgress, - ), + private val initialState = AppState( + snackbarState = DeletingBrowserDataInProgress, ) @Test fun `WHEN snackbar dismissed action is dispatched THEN state is updated`() { - appStore.dispatch(SnackbarAction.SnackbarDismissed).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, SnackbarAction.SnackbarDismissed) - assertTrue(appStore.state.snackbarState is Dismiss) - assertTrue((appStore.state.snackbarState as Dismiss).previous == DeletingBrowserDataInProgress) + assertTrue(finalState.snackbarState is Dismiss) + assertTrue((finalState.snackbarState as Dismiss).previous == DeletingBrowserDataInProgress) } @Test fun `WHEN snackbar shown action is dispatched THEN state is updated`() { - appStore.dispatch(SnackbarAction.SnackbarShown).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, SnackbarAction.SnackbarShown) - assertTrue(appStore.state.snackbarState is None) - assertTrue((appStore.state.snackbarState as None).previous == DeletingBrowserDataInProgress) + assertTrue(finalState.snackbarState is None) + assertTrue((finalState.snackbarState as None).previous == DeletingBrowserDataInProgress) } @Test fun `WHEN reset action is dispatched THEN state is updated`() { - appStore.dispatch(SnackbarAction.Reset).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, SnackbarAction.Reset) - assertTrue(appStore.state.snackbarState is None) - assertTrue((appStore.state.snackbarState as None).previous == DeletingBrowserDataInProgress) + assertTrue(finalState.snackbarState is None) + assertTrue((finalState.snackbarState as None).previous == DeletingBrowserDataInProgress) } } diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/TabStripActionTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/TabStripActionTest.kt @@ -4,32 +4,30 @@ package org.mozilla.fenix.components.appstate -import mozilla.components.support.test.ext.joinBlocking import org.junit.Assert.assertEquals import org.junit.Test -import org.mozilla.fenix.components.AppStore class TabStripActionTest { @Test fun `WHEN the last remaining tab that was closed was private THEN state should reflect that`() { - val store = AppStore(initialState = AppState()) + val initialState = AppState() - store.dispatch(AppAction.TabStripAction.UpdateLastTabClosed(true)).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, AppAction.TabStripAction.UpdateLastTabClosed(true)) val expected = AppState(wasLastTabClosedPrivate = true) - assertEquals(expected, store.state) + assertEquals(expected, finalState) } @Test fun `WHEN the last remaining tab that was closed was not private THEN state should reflect that`() { - val store = AppStore(initialState = AppState()) + val initialState = AppState() - store.dispatch(AppAction.TabStripAction.UpdateLastTabClosed(false)).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, AppAction.TabStripAction.UpdateLastTabClosed(false)) val expected = AppState(wasLastTabClosedPrivate = false) - assertEquals(expected, store.state) + assertEquals(expected, finalState) } } diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/qrscanner/QrScannerActionTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/qrscanner/QrScannerActionTest.kt @@ -4,21 +4,20 @@ package org.mozilla.fenix.components.appstate.qrscanner -import mozilla.components.support.test.ext.joinBlocking import org.junit.Assert.assertEquals import org.junit.Test -import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.AppAction import org.mozilla.fenix.components.appstate.AppState +import org.mozilla.fenix.components.appstate.AppStoreReducer import org.mozilla.fenix.components.appstate.qrScanner.QrScannerState class QrScannerActionTest { @Test fun `WHEN the QrScanner is requested THEN state should reflect that`() { - val store = AppStore(initialState = AppState()) + val initialState = AppState() - store.dispatch(AppAction.QrScannerAction.QrScannerRequested).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, AppAction.QrScannerAction.QrScannerRequested) val expectedState = AppState( qrScannerState = QrScannerState( @@ -28,14 +27,14 @@ class QrScannerActionTest { ), ) - assertEquals(expectedState, store.state) + assertEquals(expectedState, finalState) } @Test fun `WHEN the QrScanner request is consumed THEN the state should reflect that`() { - val store = AppStore(initialState = AppState()) + var state = AppState() - store.dispatch(AppAction.QrScannerAction.QrScannerRequested).joinBlocking() + state = AppStoreReducer.reduce(state, AppAction.QrScannerAction.QrScannerRequested) var expectedState = AppState( qrScannerState = QrScannerState( @@ -45,9 +44,9 @@ class QrScannerActionTest { ), ) - assertEquals(expectedState, store.state) + assertEquals(expectedState, state) - store.dispatch(AppAction.QrScannerAction.QrScannerRequestConsumed).joinBlocking() + state = AppStoreReducer.reduce(state, AppAction.QrScannerAction.QrScannerRequestConsumed) expectedState = AppState( qrScannerState = QrScannerState( @@ -57,14 +56,14 @@ class QrScannerActionTest { ), ) - assertEquals(expectedState, store.state) + assertEquals(expectedState, state) } @Test fun `WHEN the QrScanner Input is ready THEN the state should reflect that`() { - val store = AppStore(initialState = AppState()) + val initialState = AppState() - store.dispatch(AppAction.QrScannerAction.QrScannerInputAvailable("test")).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, AppAction.QrScannerAction.QrScannerInputAvailable("test")) val expectedState = AppState( qrScannerState = QrScannerState( @@ -74,14 +73,14 @@ class QrScannerActionTest { ), ) - assertEquals(expectedState, store.state) + assertEquals(expectedState, finalState) } @Test fun `WHEN the QrScanner Input is consumed THEN the state should reflect that`() { - val store = AppStore(initialState = AppState()) + var state = AppState() - store.dispatch(AppAction.QrScannerAction.QrScannerInputAvailable("test")).joinBlocking() + state = AppStoreReducer.reduce(state, AppAction.QrScannerAction.QrScannerInputAvailable("test")) var expectedState = AppState( qrScannerState = QrScannerState( @@ -91,20 +90,20 @@ class QrScannerActionTest { ), ) - assertEquals(expectedState, store.state) + assertEquals(expectedState, state) - store.dispatch(AppAction.QrScannerAction.QrScannerInputConsumed).joinBlocking() + state = AppStoreReducer.reduce(state, AppAction.QrScannerAction.QrScannerInputConsumed) expectedState = AppState(qrScannerState = QrScannerState.DEFAULT) - assertEquals(expectedState, store.state) + assertEquals(expectedState, state) } @Test fun `WHEN the QrScanner is dismissed THEN the state should reflect that`() { - val store = AppStore(initialState = AppState()) + var state = AppState() - store.dispatch(AppAction.QrScannerAction.QrScannerRequested).joinBlocking() + state = AppStoreReducer.reduce(state, AppAction.QrScannerAction.QrScannerRequested) var expectedState = AppState( qrScannerState = QrScannerState( @@ -114,12 +113,12 @@ class QrScannerActionTest { ), ) - assertEquals(expectedState, store.state) + assertEquals(expectedState, state) - store.dispatch(AppAction.QrScannerAction.QrScannerDismissed).joinBlocking() + state = AppStoreReducer.reduce(state, AppAction.QrScannerAction.QrScannerDismissed) expectedState = AppState(qrScannerState = QrScannerState.DEFAULT) - assertEquals(expectedState, store.state) + assertEquals(expectedState, state) } } diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/search/SearchStateReducerTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/appstate/search/SearchStateReducerTest.kt @@ -6,70 +6,67 @@ package org.mozilla.fenix.components.appstate.search import io.mockk.mockk import mozilla.components.browser.state.search.SearchEngine -import mozilla.components.support.test.ext.joinBlocking import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Test -import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.AppAction.SearchAction.SearchEnded import org.mozilla.fenix.components.appstate.AppAction.SearchAction.SearchEngineSelected import org.mozilla.fenix.components.appstate.AppAction.SearchAction.SearchStarted import org.mozilla.fenix.components.appstate.AppState +import org.mozilla.fenix.components.appstate.AppStoreReducer import org.mozilla.fenix.components.metrics.MetricsUtils class SearchStateReducerTest { @Test fun `GIVEN no parameters provided WHEN the search is started THEN update the app state with default search related values`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch(SearchStarted()).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, SearchStarted()) - assertEquals(true, appStore.state.searchState.isSearchActive) - assertNull(appStore.state.searchState.selectedSearchEngine) - assertNull(appStore.state.searchState.sourceTabId) - assertEquals(MetricsUtils.Source.NONE, appStore.state.searchState.searchAccessPoint) + assertEquals(true, finalState.searchState.isSearchActive) + assertNull(finalState.searchState.selectedSearchEngine) + assertNull(finalState.searchState.sourceTabId) + assertEquals(MetricsUtils.Source.NONE, finalState.searchState.searchAccessPoint) } @Test fun `GIVEN search details provided WHEN the search is started THEN update the app state with the provided details`() { val sourceTabId = "test" val source = MetricsUtils.Source.SUGGESTION - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch(SearchStarted(sourceTabId, source)).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, SearchStarted(sourceTabId, source)) - assertEquals(true, appStore.state.searchState.isSearchActive) - assertNull(appStore.state.searchState.selectedSearchEngine) - assertEquals(sourceTabId, appStore.state.searchState.sourceTabId) - assertEquals(source, appStore.state.searchState.searchAccessPoint) + assertEquals(true, finalState.searchState.isSearchActive) + assertNull(finalState.searchState.selectedSearchEngine) + assertEquals(sourceTabId, finalState.searchState.sourceTabId) + assertEquals(source, finalState.searchState.searchAccessPoint) } @Test fun `WHEN the search is ended THEN reset the search state in the app state`() { - val appStore = AppStore( - AppState( - searchState = SearchState( - isSearchActive = true, - selectedSearchEngine = mockk(), - sourceTabId = "test", - searchAccessPoint = MetricsUtils.Source.ACTION, - ), + val initialState = AppState( + searchState = SearchState( + isSearchActive = true, + selectedSearchEngine = mockk(), + sourceTabId = "test", + searchAccessPoint = MetricsUtils.Source.ACTION, ), ) - appStore.dispatch(SearchEnded).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, SearchEnded) - assertEquals(SearchState.EMPTY, appStore.state.searchState) + assertEquals(SearchState.EMPTY, finalState.searchState) } @Test fun `WHEN a new search engine is selected THEN add it in the app state`() { val searchEngine: SearchEngine = mockk() - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch(SearchEngineSelected(searchEngine, true)).joinBlocking() + val finalState = AppStoreReducer.reduce(initialState, SearchEngineSelected(searchEngine, true)) - assertEquals(searchEngine, appStore.state.searchState.selectedSearchEngine?.searchEngine) - assertEquals(true, appStore.state.searchState.selectedSearchEngine?.isUserSelected) + assertEquals(searchEngine, finalState.searchState.selectedSearchEngine?.searchEngine) + assertEquals(true, finalState.searchState.selectedSearchEngine?.isUserSelected) } } diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/share/ShareActionReducerTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/share/ShareActionReducerTest.kt @@ -2,25 +2,26 @@ package org.mozilla.fenix.share import io.mockk.mockk import mozilla.components.concept.sync.TabData -import mozilla.components.support.test.ext.joinBlocking import org.junit.Assert.assertEquals import org.junit.Test -import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.AppAction +import org.mozilla.fenix.components.appstate.AppState +import org.mozilla.fenix.components.appstate.AppStoreReducer import org.mozilla.fenix.components.appstate.snackbar.SnackbarState class ShareActionReducerTest { @Test fun `WHEN ShareToAppFailed action is dispatched THEN snackbar state is updated`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.ShareAction.ShareToAppFailed, - ).joinBlocking() + ) assertEquals( SnackbarState.ShareToAppFailed, - appStore.state.snackbarState, + finalState.snackbarState, ) } @@ -28,15 +29,16 @@ class ShareActionReducerTest { fun `WHEN SharedTabsSuccessfully action is dispatched THEN snackbar state is updated`() { val destination = listOf("a") val tabs = listOf(mockk<TabData>(), mockk<TabData>()) - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.ShareAction.SharedTabsSuccessfully(destination, tabs), - ).joinBlocking() + ) assertEquals( SnackbarState.SharedTabsSuccessfully(destination, tabs), - appStore.state.snackbarState, + finalState.snackbarState, ) } @@ -44,29 +46,31 @@ class ShareActionReducerTest { fun `WHEN ShareTabsFailed action is dispatched THEN snackbar state is updated`() { val destination = listOf("a") val tabs = listOf(mockk<TabData>(), mockk<TabData>()) - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.ShareAction.ShareTabsFailed(destination, tabs), - ).joinBlocking() + ) assertEquals( SnackbarState.ShareTabsFailed(destination, tabs), - appStore.state.snackbarState, + finalState.snackbarState, ) } @Test fun `WHEN CopyLinkToClipboard action is dispatched THEN snackbar state is updated`() { - val appStore = AppStore() + val initialState = AppState() - appStore.dispatch( + val finalState = AppStoreReducer.reduce( + initialState, AppAction.ShareAction.CopyLinkToClipboard, - ).joinBlocking() + ) assertEquals( SnackbarState.CopyLinkToClipboard, - appStore.state.snackbarState, + finalState.snackbarState, ) } }