tor-browser

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

commit c9c85c0f361181e85d7a3556be4afdd2d8d131c4
parent b77ea8700fc2dda9e12eb4ae9b3034d9d2a716e7
Author: Ted Campbell <tcampbell@mozilla.com>
Date:   Wed,  7 Jan 2026 06:25:57 +0000

Bug 2005839 - Part 1: Simplify FenixRobolectricTestApplication overrides r=android-reviewers,jonalmeida

Instead of providing fine-grained overrides that make it harder to reason about
startup of FenixApplication, use a single `initializeFenixProcess` method that
Robolectric can replace. Also cleanup some stale overrides we aren't using and
update comments with more context.

This technically removes the `PerfStartup.applicationOnCreate` metric from the
unit test environment, but we neither use nor test it.

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

Diffstat:
Mmobile/android/fenix/app/src/main/java/org/mozilla/fenix/FenixApplication.kt | 24++++++++++--------------
Mmobile/android/fenix/app/src/test/java/org/mozilla/fenix/helpers/FenixRobolectricTestApplication.kt | 22+++++++++-------------
2 files changed, 19 insertions(+), 27 deletions(-)

diff --git a/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -13,7 +13,6 @@ import android.os.Build.VERSION.SDK_INT import android.os.StrictMode import android.os.SystemClock import android.util.Log.INFO -import androidx.annotation.OpenForTesting import androidx.annotation.VisibleForTesting import androidx.appcompat.app.AppCompatDelegate import androidx.core.app.NotificationManagerCompat @@ -147,13 +146,15 @@ open class FenixApplication : LocaleAwareApplication(), Provider { override fun onCreate() { super.onCreate() - initialize() + initializeFenixProcess() } /** - * Initializes Fenix and all required subsystems such as Nimbus, Glean and Gecko. + * Process-level initialization for Fenix and its services. Sets up required native subsystems + * such as Nimbus, Glean and Gecko. Note that Robolectric tests override this with an empty + * implementation that skips this initialization. */ - fun initialize() { + protected open fun initializeFenixProcess() { // We measure ourselves to avoid a call into Glean before its loaded. val start = SystemClock.elapsedRealtimeNanos() @@ -190,8 +191,7 @@ open class FenixApplication : LocaleAwareApplication(), Provider { } @OptIn(DelicateCoroutinesApi::class) // GlobalScope usage - @VisibleForTesting - protected open fun initializeGlean() { + private fun initializeGlean() { val settings = settings() // We delay the Glean initialization until, we have user consent (After onboarding). // If onboarding is disabled (when in local builds), continue to initialize Glean. @@ -207,8 +207,7 @@ open class FenixApplication : LocaleAwareApplication(), Provider { } @SuppressLint("NewApi") - @VisibleForTesting - protected open fun setupInAllProcesses() { + private fun setupInAllProcesses() { // See Bug 1969818: Crash reporting requires updates to be compatible with // isolated content process. if (!android.os.Process.isIsolated()) { @@ -218,8 +217,7 @@ open class FenixApplication : LocaleAwareApplication(), Provider { Log.addSink(FenixLogSink(logsDebug = Config.channel.isDebug, AndroidLogSink())) } - @VisibleForTesting - protected open fun setupInMainProcessOnly() { + private fun setupInMainProcessOnly() { // ⚠️ DO NOT ADD ANYTHING ABOVE THIS LINE. // Especially references to the engine/BrowserStore which can alter the app initialization. // See: https://github.com/mozilla-mobile/fenix/issues/26320 @@ -532,7 +530,7 @@ open class FenixApplication : LocaleAwareApplication(), Provider { } } - protected open fun initializeNimbus() { + private fun initializeNimbus() { beginSetupMegazord() // This lazily constructs the Nimbus object… @@ -995,9 +993,7 @@ open class FenixApplication : LocaleAwareApplication(), Provider { reportHomeScreenMetrics(settings) } - @VisibleForTesting - @OpenForTesting - internal open fun setAutofillMetrics() { + private fun setAutofillMetrics() { @OptIn(DelicateCoroutinesApi::class) GlobalScope.launch(IO) { try { diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/helpers/FenixRobolectricTestApplication.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/helpers/FenixRobolectricTestApplication.kt @@ -10,9 +10,14 @@ import org.mozilla.fenix.R import org.mozilla.fenix.components.Components /** - * An override of our application for use in Robolectric-based unit tests. We're forced to override - * because our standard application fails to initialize in Robolectric with exceptions like: - * "Crash handler service must run in a separate process". + * An override of our application for use in Robolectric-based unit tests. This bypasses standard + * process initialization of native code (Glean, Nimbus, Gecko, etc) as well mocking `components` + * which is the primary service repository for our Kotlin code used by Fenix. Unit tests should + * avoid relying global subsystems being available to them. + * + * Note: Robolectric runs on a host machine JVM, so if you want native code your packages must + * include binaries for host platform. For example the [glean-native-forUnitTests] package + * is used for some unit tests, but we don't provide this in the default Application. */ class FenixRobolectricTestApplication : FenixApplication() { @@ -22,16 +27,7 @@ class FenixRobolectricTestApplication : FenixApplication() { } override val components = mockk<Components>() - - override fun initializeNimbus() = Unit - - override fun initializeGlean() = Unit - - override fun setupInAllProcesses() = Unit - - override fun setupInMainProcessOnly() = Unit - - override fun downloadWallpapers() = Unit + override fun initializeFenixProcess() = Unit private fun setApplicationTheme() { // According to the Robolectric devs, the application context will not have the <application>'s