tor-browser

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

commit edb367016c2c7128c3c9166fe19e28849a130b9b
parent f5edda047abe2757d89bb0b14651cb1a7de2c082
Author: mcarare <48995920+mcarare@users.noreply.github.com>
Date:   Wed, 19 Nov 2025 15:07:49 +0000

Bug 2000863 - Refactor AutoSaveTest to use StandardTestDispatcher r=android-reviewers,avirvara

This patch replaces the usage of `MainCoroutineRule` and `runTestOnMain` with `StandardTestDispatcher` and `runTest`.

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

Diffstat:
Mmobile/android/android-components/components/browser/session-storage/src/test/java/mozilla/components/browser/session/storage/AutoSaveTest.kt | 51+++++++++++++++++++++++++--------------------------
1 file changed, 25 insertions(+), 26 deletions(-)

diff --git a/mobile/android/android-components/components/browser/session-storage/src/test/java/mozilla/components/browser/session/storage/AutoSaveTest.kt b/mobile/android/android-components/components/browser/session-storage/src/test/java/mozilla/components/browser/session/storage/AutoSaveTest.kt @@ -8,7 +8,10 @@ import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.runTest import mozilla.components.browser.state.action.ContentAction import mozilla.components.browser.state.action.TabListAction import mozilla.components.browser.state.state.BrowserState @@ -19,12 +22,9 @@ import mozilla.components.support.test.any import mozilla.components.support.test.eq import mozilla.components.support.test.mock import mozilla.components.support.test.robolectric.testContext -import mozilla.components.support.test.rule.MainCoroutineRule -import mozilla.components.support.test.rule.runTestOnMain import org.junit.Assert.assertNotSame import org.junit.Assert.assertNull import org.junit.Assert.assertSame -import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mockito.doReturn @@ -39,14 +39,13 @@ import java.util.concurrent.TimeUnit @RunWith(AndroidJUnit4::class) class AutoSaveTest { - @get:Rule - val coroutinesTestRule = MainCoroutineRule() - private val dispatcher = coroutinesTestRule.testDispatcher - private val scope = coroutinesTestRule.scope + + private val testDispatcher = StandardTestDispatcher() + private val scope = CoroutineScope(testDispatcher) @Test fun `AutoSave - when going to background`() { - runTestOnMain { + runTest(testDispatcher) { // Keep the "owner" in scope to avoid it getting garbage collected and therefore lifecycle events // not getting propagated (See #1428). val owner = mock(LifecycleOwner::class.java) @@ -81,7 +80,7 @@ class AutoSaveTest { @Test fun `AutoSave - when tab gets added`() { - runTestOnMain { + runTest(testDispatcher) { val state = BrowserState() val store = BrowserStore(state) @@ -93,7 +92,7 @@ class AutoSaveTest { minimumIntervalMs = 0, ).whenSessionsChange(scope) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() assertNull(autoSave.saveJob) verify(sessionStorage, never()).save(any()) @@ -104,7 +103,7 @@ class AutoSaveTest { ), ) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() autoSave.saveJob?.join() @@ -114,7 +113,7 @@ class AutoSaveTest { @Test fun `AutoSave - when tab gets removed`() { - runTestOnMain { + runTest(testDispatcher) { val sessionStorage: SessionStorage = mock() val store = BrowserStore( @@ -133,14 +132,14 @@ class AutoSaveTest { minimumIntervalMs = 0, ).whenSessionsChange(scope) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() assertNull(autoSave.saveJob) verify(sessionStorage, never()).save(any()) store.dispatch(TabListAction.RemoveTabAction("mozilla")) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() autoSave.saveJob?.join() @@ -150,7 +149,7 @@ class AutoSaveTest { @Test fun `AutoSave - when all tabs get removed`() { - runTestOnMain { + runTest(testDispatcher) { val store = BrowserStore( BrowserState( tabs = listOf( @@ -169,14 +168,14 @@ class AutoSaveTest { minimumIntervalMs = 0, ).whenSessionsChange(scope) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() assertNull(autoSave.saveJob) verify(sessionStorage, never()).save(any()) store.dispatch(TabListAction.RemoveAllNormalTabsAction) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() autoSave.saveJob?.join() @@ -186,7 +185,7 @@ class AutoSaveTest { @Test fun `AutoSave - when no tabs are left`() { - runTestOnMain { + runTest(testDispatcher) { val store = BrowserStore( BrowserState( tabs = listOf(createTab("https://www.firefox.com", id = "firefox")), @@ -202,13 +201,13 @@ class AutoSaveTest { minimumIntervalMs = 0, ).whenSessionsChange(scope) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() assertNull(autoSave.saveJob) verify(sessionStorage, never()).save(any()) store.dispatch(TabListAction.RemoveTabAction("firefox")) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() autoSave.saveJob?.join() @@ -218,7 +217,7 @@ class AutoSaveTest { @Test fun `AutoSave - when tab gets selected`() { - runTestOnMain { + runTest(testDispatcher) { val store = BrowserStore( BrowserState( tabs = listOf( @@ -237,14 +236,14 @@ class AutoSaveTest { minimumIntervalMs = 0, ).whenSessionsChange(scope) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() assertNull(autoSave.saveJob) verify(sessionStorage, never()).save(any()) store.dispatch(TabListAction.SelectTabAction("mozilla")) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() autoSave.saveJob?.join() @@ -254,7 +253,7 @@ class AutoSaveTest { @Test fun `AutoSave - when tab loading state changes`() { - runTestOnMain { + runTest(testDispatcher) { val sessionStorage: SessionStorage = mock() val store = BrowserStore( @@ -279,7 +278,7 @@ class AutoSaveTest { ), ) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() assertNull(autoSave.saveJob) verify(sessionStorage, never()).save(any()) @@ -291,7 +290,7 @@ class AutoSaveTest { ), ) - dispatcher.scheduler.advanceUntilIdle() + testDispatcher.scheduler.advanceUntilIdle() autoSave.saveJob?.join()