tor-browser

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

commit f9d8702e26624ab46a35bf6561a7c8143c6f246a
parent 56701017d151b57915e49439bea7332c9fc73bbc
Author: mcarare <48995920+mcarare@users.noreply.github.com>
Date:   Mon, 29 Dec 2025 15:21:59 +0000

Bug 2006938 - Update coroutine testing in RecentVisitsFeatureTest. r=android-reviewers,rebecatudor273

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

Diffstat:
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/home/recentvisits/RecentVisitsFeatureTest.kt | 70++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 32 insertions(+), 38 deletions(-)

diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/home/recentvisits/RecentVisitsFeatureTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/home/recentvisits/RecentVisitsFeatureTest.kt @@ -10,8 +10,10 @@ import io.mockk.every import io.mockk.mockk import io.mockk.slot import io.mockk.spyk +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.runTest import mozilla.components.browser.storage.sync.PlacesHistoryStorage import mozilla.components.concept.storage.DocumentType import mozilla.components.concept.storage.HistoryHighlight @@ -20,12 +22,9 @@ import mozilla.components.concept.storage.HistoryMetadata import mozilla.components.concept.storage.HistoryMetadataKey import mozilla.components.concept.storage.HistoryMetadataStorage import mozilla.components.support.test.middleware.CaptureActionsMiddleware -import mozilla.components.support.test.rule.MainCoroutineRule -import mozilla.components.support.test.rule.runTestOnMain import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Before -import org.junit.Rule import org.junit.Test import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.AppAction @@ -39,27 +38,23 @@ import kotlin.random.Random class RecentVisitsFeatureTest { - private lateinit var historyHightlightsStorage: PlacesHistoryStorage + private lateinit var historyHighlightsStorage: PlacesHistoryStorage private lateinit var historyMetadataStorage: HistoryMetadataStorage private val middleware = CaptureActionsMiddleware<AppState, AppAction>() private val appStore = AppStore(middlewares = listOf(middleware)) - - @get:Rule - val coroutinesTestRule = MainCoroutineRule() - private val testDispatcher = coroutinesTestRule.testDispatcher - private val scope = coroutinesTestRule.scope + private val testDispatcher = StandardTestDispatcher() @Before fun setup() { - historyHightlightsStorage = mockk(relaxed = true) + historyHighlightsStorage = mockk(relaxed = true) historyMetadataStorage = mockk(relaxed = true) Settings.searchGroupMinimumSites = 1 } @Test fun `GIVEN no recent visits WHEN feature starts THEN fetch history metadata and highlights then notify store`() = - runTestOnMain { + runTest(testDispatcher) { val historyEntry = HistoryMetadata( key = HistoryMetadataKey("http://www.mozilla.com", "mozilla", null), title = "mozilla", @@ -80,11 +75,11 @@ class RecentVisitsFeatureTest { historyEntry, ) } - coEvery { historyHightlightsStorage.getHistoryHighlights(any(), any()) }.coAnswers { + coEvery { historyHighlightsStorage.getHistoryHighlights(any(), any()) }.coAnswers { listOf(highlightEntry) } - startRecentVisitsFeature() + startRecentVisitsFeature(scope = this) middleware.assertLastAction(AppAction.RecentHistoryChange::class) { assertEquals(listOf(recentHistoryGroup, recentHistoryHighlight), it.recentHistory) @@ -93,14 +88,14 @@ class RecentVisitsFeatureTest { @Test fun `WHEN asking for history highlights THEN use a specific query`() { - runTestOnMain { + runTest(testDispatcher) { val highlightWeights = slot<HistoryHighlightWeights>() val highlightsAskedForNumber = slot<Int>() - startRecentVisitsFeature() + startRecentVisitsFeature(scope = this) coVerify { - historyHightlightsStorage.getHistoryHighlights( + historyHighlightsStorage.getHistoryHighlights( capture(highlightWeights), capture(highlightsAskedForNumber), ) @@ -114,7 +109,7 @@ class RecentVisitsFeatureTest { @Test fun `GIVEN groups containing history metadata items with the same url WHEN they are added to store THEN entries are deduped`() = - runTestOnMain { + runTest(testDispatcher) { val historyEntry1 = HistoryMetadata( key = HistoryMetadataKey("http://www.mozilla.com", "mozilla", null), title = "mozilla", @@ -165,7 +160,7 @@ class RecentVisitsFeatureTest { ) } - startRecentVisitsFeature() + startRecentVisitsFeature(scope = this) middleware.assertLastAction(AppAction.RecentHistoryChange::class) { assertEquals(listOf(expectedHistoryGroup), it.recentHistory) @@ -174,7 +169,7 @@ class RecentVisitsFeatureTest { @Test fun `GIVEN different groups containing history metadata items with the same url WHEN they are added to store THEN entries are not deduped`() = - runTestOnMain { + runTest(testDispatcher) { val now = System.currentTimeMillis() val historyEntry1 = HistoryMetadata( key = HistoryMetadataKey("http://www.mozilla.com", "mozilla", null), @@ -224,7 +219,7 @@ class RecentVisitsFeatureTest { ) } - startRecentVisitsFeature() + startRecentVisitsFeature(scope = this) middleware.assertLastAction(AppAction.RecentHistoryChange::class) { assertEquals(listOf(expectedHistoryGroup1, expectedHistoryGroup2), it.recentHistory) @@ -233,7 +228,7 @@ class RecentVisitsFeatureTest { @Test fun `GIVEN history groups WHEN they are added to store THEN they are sorted descending by last updated timestamp`() = - runTestOnMain { + runTest(testDispatcher) { val now = System.currentTimeMillis() val historyEntry1 = HistoryMetadata( key = HistoryMetadataKey("http://www.mozilla.com", "mozilla", null), @@ -283,7 +278,7 @@ class RecentVisitsFeatureTest { ) } - startRecentVisitsFeature() + startRecentVisitsFeature(scope = this) middleware.assertLastAction(AppAction.RecentHistoryChange::class) { assertEquals(listOf(expectedHistoryGroup2, expectedHistoryGroup1), it.recentHistory) @@ -292,7 +287,7 @@ class RecentVisitsFeatureTest { @Test fun `GIVEN multiple groups exist but no highlights WHEN they are added to store THEN only MAX_RESULTS_TOTAL are sent`() = - runTestOnMain { + runTest(testDispatcher) { val visitsFromSearch = getSearchFromHistoryMetadataItems(10) val expectedRecentHistoryGroups = visitsFromSearch // Expect to only have the last accessed 9 groups. @@ -300,7 +295,7 @@ class RecentVisitsFeatureTest { .toIndividualRecentHistoryGroups() coEvery { historyMetadataStorage.getHistoryMetadataSince(any()) }.coAnswers { visitsFromSearch } - startRecentVisitsFeature() + startRecentVisitsFeature(scope = this) middleware.assertLastAction(AppAction.RecentHistoryChange::class) { assertEquals( @@ -313,15 +308,15 @@ class RecentVisitsFeatureTest { @Test fun `GIVEN multiple highlights exist but no history groups WHEN they are added to store THEN only MAX_RESULTS_TOTAL are sent`() = - runTestOnMain { + runTest(testDispatcher) { val highlights = getHistoryHighlightsItems(10) val expectedRecentHighlights = highlights // Expect to only have 9 highlights .subList(0, 9) .toRecentHistoryHighlights() - coEvery { historyHightlightsStorage.getHistoryHighlights(any(), any()) }.coAnswers { highlights } + coEvery { historyHighlightsStorage.getHistoryHighlights(any(), any()) }.coAnswers { highlights } - startRecentVisitsFeature() + startRecentVisitsFeature(scope = this) middleware.assertLastAction(AppAction.RecentHistoryChange::class) { assertEquals( @@ -333,7 +328,7 @@ class RecentVisitsFeatureTest { @Test fun `GIVEN multiple history highlights and history groups WHEN they are added to store THEN only last accessed are added`() = - runTestOnMain { + runTest(testDispatcher) { val visitsFromSearch = getSearchFromHistoryMetadataItems(10) val directVisits = getDirectVisitsHistoryMetadataItems(10) val expectedRecentHistoryGroups = visitsFromSearch @@ -345,11 +340,11 @@ class RecentVisitsFeatureTest { listOf(it.first, it.second) }.take(9) coEvery { historyMetadataStorage.getHistoryMetadataSince(any()) }.coAnswers { visitsFromSearch + directVisits } - coEvery { historyHightlightsStorage.getHistoryHighlights(any(), any()) }.coAnswers { + coEvery { historyHighlightsStorage.getHistoryHighlights(any(), any()) }.coAnswers { directVisits.toHistoryHighlights() } - startRecentVisitsFeature() + startRecentVisitsFeature(scope = this) middleware.assertLastAction(AppAction.RecentHistoryChange::class) { assertEquals(expectedItems, it.recentHistory) @@ -357,7 +352,7 @@ class RecentVisitsFeatureTest { } @Test - fun `GIVEN history highlights exist as history metadata WHEN they are added to store THEN don't add highlight dupes`() { + fun `GIVEN history highlights exist as history metadata WHEN they are added to store THEN don't add highlight dupes`() = runTest(testDispatcher) { // To know if a highlight appears in a search group each visit's url should be checked. val visitsFromSearch = getSearchFromHistoryMetadataItems(10) val directDistinctVisits = getDirectVisitsHistoryMetadataItems(10).takeLast(2) @@ -378,11 +373,11 @@ class RecentVisitsFeatureTest { coEvery { historyMetadataStorage.getHistoryMetadataSince(any()) }.coAnswers { visitsFromSearch + directDistinctVisits + directDupeVisits } - coEvery { historyHightlightsStorage.getHistoryHighlights(any(), any()) }.coAnswers { + coEvery { historyHighlightsStorage.getHistoryHighlights(any(), any()) }.coAnswers { directDistinctVisits.toHistoryHighlights() + directDupeVisits.toHistoryHighlights() } - startRecentVisitsFeature() + startRecentVisitsFeature(scope = this) middleware.assertLastAction(AppAction.RecentHistoryChange::class) { assertEquals(expectedItems, it.recentHistory) @@ -646,11 +641,11 @@ class RecentVisitsFeatureTest { } @OptIn(ExperimentalCoroutinesApi::class) // advanceUntilIdle - private fun startRecentVisitsFeature() { + private fun startRecentVisitsFeature(scope: CoroutineScope) { val feature = RecentVisitsFeature( appStore, historyMetadataStorage, - lazy { historyHightlightsStorage }, + lazy { historyHighlightsStorage }, scope, testDispatcher, ) @@ -658,8 +653,7 @@ class RecentVisitsFeatureTest { assertEquals(emptyList<RecentHistoryGroup>(), appStore.state.recentHistory) feature.start() - - scope.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() coVerify { historyMetadataStorage.getHistoryMetadataSince(any())