diff --git a/app/src/main/java/org/mozilla/fenix/perf/StartupStateProvider.kt b/app/src/main/java/org/mozilla/fenix/perf/StartupStateProvider.kt index c033e9e25..e891b6b4a 100644 --- a/app/src/main/java/org/mozilla/fenix/perf/StartupStateProvider.kt +++ b/app/src/main/java/org/mozilla/fenix/perf/StartupStateProvider.kt @@ -25,6 +25,36 @@ class StartupStateProvider( private val startReasonProvider: AppStartReasonProvider ) { + /** + * The restoration state of the application upon this most recent start up. See the + * [Fenix perf glossary](https://wiki.mozilla.org/index.php?title=Performance/Fenix/Glossary) + * for specific definitions. + */ + enum class StartupState { + COLD, WARM, HOT, + + /** + * A start up state where we weren't able to bucket it into the other categories. + * This includes, but is not limited to: + * - if the activity this is called from is not currently started + * - if the currently started activity is not the first started activity + */ + UNKNOWN; + } + + /** + * Returns the [StartupState] for the currently started activity. Note: the state will be + * [StartupState.UNKNOWN] if the currently started activity is not the first started activity. + * + * This method must be called after the foreground Activity is STARTED. + */ + fun getStartupStateForStartedActivity(activityClass: Class): StartupState = when { + isColdStartForStartedActivity(activityClass) -> StartupState.COLD + isWarmStartForStartedActivity(activityClass) -> StartupState.WARM + isHotStartForStartedActivity(activityClass) -> StartupState.HOT + else -> StartupState.UNKNOWN + } + /** * Returns true if the current startup state is COLD and the currently started activity is the * first started activity (i.e. we can use it for performance measurements). diff --git a/app/src/test/java/org/mozilla/fenix/perf/StartupStateProviderTest.kt b/app/src/test/java/org/mozilla/fenix/perf/StartupStateProviderTest.kt index 347846c35..3f4b0a909 100644 --- a/app/src/test/java/org/mozilla/fenix/perf/StartupStateProviderTest.kt +++ b/app/src/test/java/org/mozilla/fenix/perf/StartupStateProviderTest.kt @@ -7,6 +7,7 @@ package org.mozilla.fenix.perf import io.mockk.MockKAnnotations import io.mockk.every import io.mockk.impl.annotations.MockK +import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Before @@ -15,6 +16,7 @@ import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.IntentReceiverActivity import org.mozilla.fenix.perf.AppStartReasonProvider.StartReason import org.mozilla.fenix.perf.StartupActivityLog.LogEntry +import org.mozilla.fenix.perf.StartupStateProvider.StartupState class StartupStateProviderTest { @@ -237,6 +239,41 @@ class StartupStateProviderTest { assertFalse(provider.isHotStartForStartedActivity(homeActivityClass)) } + @Test + fun `GIVEN the app started for an activity WHEN it is a cold start THEN get startup state is cold`() { + forEachColdStartEntries { index -> + assertEquals("$index", StartupState.COLD, provider.getStartupStateForStartedActivity(homeActivityClass)) + } + } + + @Test + fun `WHEN it is a warm start THEN get startup state is warm`() { + forEachWarmStartEntries { index -> + assertEquals("$index", StartupState.WARM, provider.getStartupStateForStartedActivity(homeActivityClass)) + } + } + + @Test + fun `WHEN it is a hot start THEN get startup state is hot`() { + forEachHotStartEntries { index -> + assertEquals("$index", StartupState.HOT, provider.getStartupStateForStartedActivity(homeActivityClass)) + } + } + + @Test + fun `WHEN two activities are started THEN get startup state is unknown`() { + logEntries.addAll(listOf( + LogEntry.ActivityCreated(irActivityClass), + LogEntry.ActivityStarted(irActivityClass), + LogEntry.AppStarted, + LogEntry.ActivityCreated(homeActivityClass), + LogEntry.ActivityStarted(homeActivityClass), + LogEntry.ActivityStopped(irActivityClass) + )) + + assertEquals(StartupState.UNKNOWN, provider.getStartupStateForStartedActivity(homeActivityClass)) + } + private fun forEachColdStartEntries(block: (index: Int) -> Unit) { // These entries mimic observed behavior. //