tor-browser

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

commit 6321054b3c3c93af3825da18b33fc6ba646562fa
parent 442c00c57481e2175d7b5cedcab280624739b2c8
Author: mcarare <48995920+mcarare@users.noreply.github.com>
Date:   Wed, 19 Nov 2025 13:58:38 +0000

Bug 2000883 - Update GeckoWebNotificationDelegate to use an injected CoroutineDispatcher. r=android-reviewers,avirvara

This allows the tests to be updated to use `StandardTestDispatcher` and `runTest` instead of `MainCoroutineRule`.

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

Diffstat:
Mmobile/android/android-components/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/webnotifications/GeckoWebNotificationDelegate.kt | 10++++++++--
Mmobile/android/android-components/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/webnotifications/GeckoWebNotificationDelegateTest.kt | 20+++++++++-----------
2 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/mobile/android/android-components/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/webnotifications/GeckoWebNotificationDelegate.kt b/mobile/android/android-components/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/webnotifications/GeckoWebNotificationDelegate.kt @@ -4,7 +4,10 @@ package mozilla.components.browser.engine.gecko.webnotifications -import kotlinx.coroutines.MainScope +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch import mozilla.components.concept.engine.webnotifications.WebNotification import mozilla.components.concept.engine.webnotifications.WebNotificationAction @@ -14,12 +17,15 @@ import org.mozilla.geckoview.WebNotificationDelegate as GeckoViewWebNotification internal class GeckoWebNotificationDelegate( private val webNotificationDelegate: WebNotificationDelegate, + dispatcher: CoroutineDispatcher = Dispatchers.Main, ) : GeckoViewWebNotificationDelegate { + + private val scope = CoroutineScope(SupervisorJob() + dispatcher) override fun onShowNotification(webNotification: GeckoViewWebNotification) { val deferred = webNotificationDelegate.onShowNotification( webNotification.toWebNotification(), ) - MainScope().launch { + scope.launch { val succeeded = deferred.await() if (succeeded) { webNotification.show() diff --git a/mobile/android/android-components/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/webnotifications/GeckoWebNotificationDelegateTest.kt b/mobile/android/android-components/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/webnotifications/GeckoWebNotificationDelegateTest.kt @@ -5,18 +5,17 @@ package mozilla.components.browser.engine.gecko.webnotifications import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.runTest import mozilla.components.concept.engine.webnotifications.WebNotification import mozilla.components.concept.engine.webnotifications.WebNotificationDelegate import mozilla.components.support.test.any import mozilla.components.support.test.argumentCaptor import mozilla.components.support.test.mock -import mozilla.components.support.test.rule.MainCoroutineRule -import mozilla.components.support.test.rule.runTestOnMain import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Before -import org.junit.Rule import org.junit.Test import org.mockito.Mockito.verify import org.mockito.Mockito.`when` @@ -25,8 +24,7 @@ import org.mozilla.geckoview.WebNotificationAction as GeckoViewWebNotificationAc class GeckoWebNotificationDelegateTest { - @get:Rule - val coroutinesTestRule = MainCoroutineRule() + private val testDispatcher = StandardTestDispatcher() private val webNotificationDelegate: WebNotificationDelegate = mock() @@ -36,7 +34,7 @@ class GeckoWebNotificationDelegateTest { } @Test - fun `onShowNotification is forwarded to delegate`() = runTestOnMain { + fun `onShowNotification is forwarded to delegate`() = runTest(testDispatcher) { val geckoViewWebNotification: GeckoViewWebNotification = mockWebNotification( title = "title", tag = "tag", @@ -49,7 +47,7 @@ class GeckoWebNotificationDelegateTest { privateBrowsing = true, actions = arrayOf(GeckoViewWebNotificationAction("foo", "bar")), ) - val geckoWebNotificationDelegate = GeckoWebNotificationDelegate(webNotificationDelegate) + val geckoWebNotificationDelegate = GeckoWebNotificationDelegate(webNotificationDelegate, testDispatcher) val notificationCaptor = argumentCaptor<WebNotification>() geckoWebNotificationDelegate.onShowNotification(geckoViewWebNotification) @@ -76,7 +74,7 @@ class GeckoWebNotificationDelegateTest { } @Test - fun `onCloseNotification is forwarded to delegate`() { + fun `onCloseNotification is forwarded to delegate`() = runTest(testDispatcher) { val geckoViewWebNotification: GeckoViewWebNotification = mockWebNotification( title = "title", tag = "tag", @@ -89,7 +87,7 @@ class GeckoWebNotificationDelegateTest { privateBrowsing = false, actions = arrayOf(GeckoViewWebNotificationAction("foo", "bar")), ) - val geckoWebNotificationDelegate = GeckoWebNotificationDelegate(webNotificationDelegate) + val geckoWebNotificationDelegate = GeckoWebNotificationDelegate(webNotificationDelegate, testDispatcher) val notificationCaptor = argumentCaptor<WebNotification>() geckoWebNotificationDelegate.onCloseNotification(geckoViewWebNotification) @@ -110,7 +108,7 @@ class GeckoWebNotificationDelegateTest { } @Test - fun `notification without a source are from web extensions`() = runTestOnMain { + fun `notification without a source are from web extensions`() = runTest(testDispatcher) { val geckoViewWebNotification: GeckoViewWebNotification = mockWebNotification( title = "title", tag = "tag", @@ -123,7 +121,7 @@ class GeckoWebNotificationDelegateTest { privateBrowsing = true, actions = arrayOf(GeckoViewWebNotificationAction("foo", "bar")), ) - val geckoWebNotificationDelegate = GeckoWebNotificationDelegate(webNotificationDelegate) + val geckoWebNotificationDelegate = GeckoWebNotificationDelegate(webNotificationDelegate, testDispatcher) val notificationCaptor = argumentCaptor<WebNotification>() geckoWebNotificationDelegate.onShowNotification(geckoViewWebNotification)