commit 2eb454d460817962a5d0ae07dd145339861dc890
parent b9ec4cc31e29713ddf3979b5ac11af1f96618a0c
Author: mcarare <48995920+mcarare@users.noreply.github.com>
Date: Mon, 29 Dec 2025 15:22:10 +0000
Bug 2007344 - Update coroutine testing in StartupTypeTelemetryTest r=android-reviewers,rebecatudor273
Migrate `StartupTypeTelemetryTest` from `MainCoroutineRule` and `runTestOnMain` to standard `runTest` and `StandardTestDispatcher`.
Differential Revision: https://phabricator.services.mozilla.com/D277416
Diffstat:
1 file changed, 10 insertions(+), 15 deletions(-)
diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/perf/StartupTypeTelemetryTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/perf/StartupTypeTelemetryTest.kt
@@ -12,12 +12,10 @@ import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.test.advanceUntilIdle
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.runTest
import mozilla.components.support.ktx.kotlin.crossProduct
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.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
@@ -43,11 +41,10 @@ private val activityClass = HomeActivity::class.java
class StartupTypeTelemetryTest {
@get:Rule
- val coroutinesTestRule = MainCoroutineRule()
-
- @get:Rule
val gleanTestRule = FenixGleanTestRule(testContext)
+ private val testDispatcher = StandardTestDispatcher()
+
private lateinit var telemetry: StartupTypeTelemetry
private lateinit var callbacks: StartupTypeTelemetry.StartupTypeLifecycleObserver
@@ -70,9 +67,8 @@ class StartupTypeTelemetryTest {
verify { lifecycle.addObserver(any()) }
}
- @OptIn(ExperimentalCoroutinesApi::class) // advanceUntilIdle
@Test
- fun `GIVEN all possible path and state combinations WHEN record telemetry THEN the labels are incremented the appropriate number of times`() = runTestOnMain {
+ fun `GIVEN all possible path and state combinations WHEN record telemetry THEN the labels are incremented the appropriate number of times`() = runTest(testDispatcher) {
val allPossibleInputArgs = StartupState.entries.crossProduct(
StartupPath.entries,
) { state, path ->
@@ -83,8 +79,8 @@ class StartupTypeTelemetryTest {
every { stateProvider.getStartupStateForStartedActivity(activityClass) } returns state
every { pathProvider.startupPathForActivity } returns path
- telemetry.record(coroutinesTestRule.testDispatcher)
- advanceUntilIdle()
+ telemetry.record(testDispatcher)
+ testDispatcher.scheduler.advanceUntilIdle()
}
validTelemetryLabels.forEach { label ->
@@ -97,14 +93,13 @@ class StartupTypeTelemetryTest {
assertNull(PerfStartup.startupType["__other__"].testGetValue())
}
- @OptIn(ExperimentalCoroutinesApi::class) // advanceUntilIdle
@Test
- fun `WHEN record is called THEN telemetry is recorded with the appropriate label`() = runTestOnMain {
+ fun `WHEN record is called THEN telemetry is recorded with the appropriate label`() = runTest(testDispatcher) {
every { stateProvider.getStartupStateForStartedActivity(activityClass) } returns StartupState.COLD
every { pathProvider.startupPathForActivity } returns StartupPath.MAIN
- telemetry.record(coroutinesTestRule.testDispatcher)
- advanceUntilIdle()
+ telemetry.record(testDispatcher)
+ testDispatcher.scheduler.advanceUntilIdle()
assertEquals(1, PerfStartup.startupType["cold_main"].testGetValue())
}