Skip to content

Commit

Permalink
For mozilla-mobile#18836: add getStartupStateForStartedActivity method.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcomella committed Apr 14, 2021
1 parent f39b2a9 commit 57c3e07
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/src/main/java/org/mozilla/fenix/perf/StartupStateProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<out Activity>): 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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {

Expand Down Expand Up @@ -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.
//
Expand Down

0 comments on commit 57c3e07

Please sign in to comment.