pull/35/head
parent
3713b5a63a
commit
316b70940f
@ -1,59 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.components.metrics
|
||||
|
||||
import android.content.Intent
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import androidx.annotation.VisibleForTesting.PRIVATE
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleObserver
|
||||
import androidx.lifecycle.OnLifecycleEvent
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
import mozilla.components.support.utils.SafeIntent
|
||||
|
||||
/**
|
||||
* Tracks how the application was opened through [Event.AppOpenedAllSourceStartup].
|
||||
* We only considered to be "opened" if it received an intent and the app was in the background.
|
||||
*/
|
||||
class AppAllSourceStartTelemetry(private val metrics: MetricController) : LifecycleObserver {
|
||||
|
||||
// default value is true to capture the first launch of the application
|
||||
private var wasApplicationInBackground = true
|
||||
|
||||
init {
|
||||
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
|
||||
}
|
||||
|
||||
fun receivedIntentInExternalAppBrowserActivity(safeIntent: SafeIntent) {
|
||||
setAppOpenedAllSourceFromIntent(safeIntent, true)
|
||||
}
|
||||
|
||||
fun receivedIntentInHomeActivity(safeIntent: SafeIntent) {
|
||||
setAppOpenedAllSourceFromIntent(safeIntent, false)
|
||||
}
|
||||
|
||||
private fun setAppOpenedAllSourceFromIntent(intent: SafeIntent, isExternalAppBrowserActivity: Boolean) {
|
||||
if (!wasApplicationInBackground) {
|
||||
return
|
||||
}
|
||||
|
||||
val source = when {
|
||||
isExternalAppBrowserActivity -> Event.AppOpenedAllSourceStartup.Source.CUSTOM_TAB
|
||||
intent.isLauncherIntent -> Event.AppOpenedAllSourceStartup.Source.APP_ICON
|
||||
intent.action == Intent.ACTION_VIEW -> Event.AppOpenedAllSourceStartup.Source.LINK
|
||||
else -> Event.AppOpenedAllSourceStartup.Source.UNKNOWN
|
||||
}
|
||||
|
||||
metrics.track(Event.AppOpenedAllSourceStartup(source))
|
||||
|
||||
wasApplicationInBackground = false
|
||||
}
|
||||
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
|
||||
@VisibleForTesting(otherwise = PRIVATE)
|
||||
fun onApplicationOnStop() {
|
||||
wasApplicationInBackground = true
|
||||
}
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.components.metrics
|
||||
|
||||
import android.content.Intent
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleObserver
|
||||
import androidx.lifecycle.OnLifecycleEvent
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
import mozilla.components.support.utils.SafeIntent
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Source
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Source.APP_ICON
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Source.CUSTOM_TAB
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Source.LINK
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Source.UNKNOWN
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Type
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Type.ERROR
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Type.HOT
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Type.COLD
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Type.WARM
|
||||
|
||||
/**
|
||||
* Tracks application startup source, type, and whether or not activity has savedInstance to restore
|
||||
* the activity from. Sample metric = [source = COLD, type = APP_ICON, hasSavedInstance = false]
|
||||
* The basic idea is to collect these metrics from different phases of startup through
|
||||
* [AppAllStartup] and finally report them on Activity's onResume() function.
|
||||
*/
|
||||
@Suppress("TooManyFunctions")
|
||||
class AppStartupTelemetry(private val metrics: MetricController) : LifecycleObserver {
|
||||
|
||||
init {
|
||||
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
|
||||
}
|
||||
|
||||
private var isMetricRecordedSinceAppWasForegrounded = false
|
||||
private var wasAppCreateCalledBeforeActivityCreate = false
|
||||
|
||||
private var onCreateData: AppAllStartup? = null
|
||||
private var onRestartData: Pair<Type, Boolean?>? = null
|
||||
private var onNewIntentData: Source? = null
|
||||
|
||||
fun onFenixApplicationOnCreate() {
|
||||
wasAppCreateCalledBeforeActivityCreate = true
|
||||
}
|
||||
|
||||
fun onHomeActivityOnCreate(safeIntent: SafeIntent, hasSavedInstanceState: Boolean) {
|
||||
setOnCreateData(safeIntent, hasSavedInstanceState, false)
|
||||
}
|
||||
|
||||
fun onExternalAppBrowserOnCreate(safeIntent: SafeIntent, hasSavedInstanceState: Boolean) {
|
||||
setOnCreateData(safeIntent, hasSavedInstanceState, true)
|
||||
}
|
||||
|
||||
fun onHomeActivityOnRestart() {
|
||||
// we are not setting [Source] in this method since source is derived from an intent.
|
||||
// therefore source gets set in onNewIntent().
|
||||
onRestartData = Pair(HOT, null)
|
||||
}
|
||||
|
||||
fun onHomeActivityOnNewIntent(safeIntent: SafeIntent) {
|
||||
// we are only setting [Source] in this method since source is derived from an intent].
|
||||
// other metric fields are set in onRestart()
|
||||
onNewIntentData = getStartupSourceFromIntent(safeIntent, false)
|
||||
}
|
||||
|
||||
private fun setOnCreateData(
|
||||
safeIntent: SafeIntent,
|
||||
hasSavedInstanceState: Boolean,
|
||||
isExternalAppBrowserActivity: Boolean
|
||||
) {
|
||||
onCreateData = AppAllStartup(
|
||||
getStartupSourceFromIntent(safeIntent, isExternalAppBrowserActivity),
|
||||
getAppStartupType(),
|
||||
hasSavedInstanceState
|
||||
)
|
||||
wasAppCreateCalledBeforeActivityCreate = false
|
||||
}
|
||||
|
||||
private fun getAppStartupType(): Type {
|
||||
return if (wasAppCreateCalledBeforeActivityCreate) COLD else WARM
|
||||
}
|
||||
|
||||
private fun getStartupSourceFromIntent(
|
||||
intent: SafeIntent,
|
||||
isExternalAppBrowserActivity: Boolean
|
||||
): Source {
|
||||
return when {
|
||||
// since the intent action is same (ACTION_VIEW) for both CUSTOM_TAB and LINK.
|
||||
// we have to make sure that we are checking for CUSTOM_TAB condition first as this
|
||||
// check does not rely on intent action
|
||||
isExternalAppBrowserActivity -> CUSTOM_TAB
|
||||
intent.isLauncherIntent -> APP_ICON
|
||||
intent.action == Intent.ACTION_VIEW -> LINK
|
||||
// one of the unknown case is app switcher, where we go to the recent tasks to launch
|
||||
// Fenix.
|
||||
else -> UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The reason we record metric on resume is because we need to wait for onNewIntent(), and
|
||||
* we are not guaranteed that onNewIntent() will be called before or after onStart() / onRestart().
|
||||
* However we are guaranteed onResume() will be called after onNewIntent() and onStart(). Source:
|
||||
* https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent)
|
||||
*/
|
||||
fun onHomeActivityOnResume() {
|
||||
recordMetric()
|
||||
}
|
||||
|
||||
private fun recordMetric() {
|
||||
if (!isMetricRecordedSinceAppWasForegrounded) {
|
||||
val appAllStartup: AppAllStartup = if (onCreateData != null) {
|
||||
onCreateData!!
|
||||
} else {
|
||||
mergeOnRestartAndOnNewIntentIntoStartup()
|
||||
}
|
||||
metrics.track(appAllStartup)
|
||||
isMetricRecordedSinceAppWasForegrounded = true
|
||||
}
|
||||
// we don't want any weird previous states to persist on our next metric record.
|
||||
onCreateData = null
|
||||
onNewIntentData = null
|
||||
onRestartData = null
|
||||
}
|
||||
|
||||
private fun mergeOnRestartAndOnNewIntentIntoStartup(): AppAllStartup {
|
||||
return AppAllStartup(
|
||||
onNewIntentData ?: UNKNOWN,
|
||||
onRestartData?.first ?: ERROR,
|
||||
onRestartData?.second
|
||||
)
|
||||
}
|
||||
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
fun onApplicationOnStop() {
|
||||
// application was backgrounded, we need to record the new metric type if
|
||||
// application was to come to foreground again.
|
||||
// Therefore we set the isMetricRecorded flag to false.
|
||||
isMetricRecordedSinceAppWasForegrounded = false
|
||||
}
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.components.metrics
|
||||
|
||||
import android.content.Intent
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.every
|
||||
import io.mockk.impl.annotations.RelaxedMockK
|
||||
import io.mockk.verify
|
||||
import mozilla.components.support.utils.toSafeIntent
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
class AppAllSourceStartTelemetryTest {
|
||||
|
||||
@RelaxedMockK
|
||||
private lateinit var metricController: MetricController
|
||||
|
||||
@RelaxedMockK
|
||||
private lateinit var intent: Intent
|
||||
|
||||
private lateinit var appAllSourceStartTelemetry: AppAllSourceStartTelemetry
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
MockKAnnotations.init(this)
|
||||
appAllSourceStartTelemetry = AppAllSourceStartTelemetry(metricController)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN a main launcher intent is received in HomeActivity THEN an app start metric is recorded from app_icon`() {
|
||||
|
||||
every { intent.action } returns Intent.ACTION_MAIN
|
||||
every { intent.categories.contains(Intent.CATEGORY_LAUNCHER) } returns true
|
||||
|
||||
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
|
||||
|
||||
val validSource = Event.AppOpenedAllSourceStartup.Source.APP_ICON
|
||||
verify(exactly = 1) { metricController.track(Event.AppOpenedAllSourceStartup(validSource)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN a VIEW intent is received in HomeActivity THEN an app start metric is recorded from link`() {
|
||||
every { intent.action } returns Intent.ACTION_VIEW
|
||||
|
||||
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
|
||||
|
||||
val validSource = Event.AppOpenedAllSourceStartup.Source.LINK
|
||||
verify(exactly = 1) { metricController.track(Event.AppOpenedAllSourceStartup(validSource)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN a intent is received in ExternalAppBrowserActivity THEN an app start metric is recorded from custom_tab`() {
|
||||
val intent = Intent()
|
||||
|
||||
appAllSourceStartTelemetry.receivedIntentInExternalAppBrowserActivity(intent.toSafeIntent())
|
||||
|
||||
val validSource = Event.AppOpenedAllSourceStartup.Source.CUSTOM_TAB
|
||||
verify(exactly = 1) { metricController.track(Event.AppOpenedAllSourceStartup(validSource)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN an app is in the foreground WHEN an intent is received THEN no startup metric is recorded`() {
|
||||
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
|
||||
|
||||
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
|
||||
|
||||
verify(exactly = 1) { metricController.track(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN application goes in background and comes foreground, THEN an app start metric is recorded`() {
|
||||
// first startup
|
||||
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
|
||||
|
||||
// mock application going in the background
|
||||
appAllSourceStartTelemetry.onApplicationOnStop()
|
||||
|
||||
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
|
||||
|
||||
verify(exactly = 2) { metricController.track(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN an intent received in HomeActivity is not launcher or does not have VIEW action, THEN an app start is recorded from unknown`() {
|
||||
every { intent.action } returns Intent.ACTION_MAIN
|
||||
|
||||
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
|
||||
|
||||
val validSource = Event.AppOpenedAllSourceStartup.Source.UNKNOWN
|
||||
verify(exactly = 1) { metricController.track(Event.AppOpenedAllSourceStartup(validSource)) }
|
||||
}
|
||||
}
|
@ -0,0 +1,266 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.components.metrics
|
||||
|
||||
import android.content.Intent
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.clearMocks
|
||||
import io.mockk.every
|
||||
import io.mockk.impl.annotations.MockK
|
||||
import io.mockk.verify
|
||||
import mozilla.components.support.utils.toSafeIntent
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.mozilla.fenix.GleanMetrics.Events
|
||||
import org.mozilla.fenix.GleanMetrics.Events.appOpenedAllStartupKeys.hasSavedInstanceState
|
||||
import org.mozilla.fenix.GleanMetrics.Events.appOpenedAllStartupKeys.source
|
||||
import org.mozilla.fenix.GleanMetrics.Events.appOpenedAllStartupKeys.type
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Source
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Source.APP_ICON
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Source.UNKNOWN
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Source.CUSTOM_TAB
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Source.LINK
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Type.COLD
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Type.WARM
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Type.HOT
|
||||
import org.mozilla.fenix.components.metrics.Event.AppAllStartup.Type.ERROR
|
||||
|
||||
class AppStartupTelemetryTest {
|
||||
|
||||
@MockK
|
||||
private lateinit var metricController: MetricController
|
||||
@MockK
|
||||
private lateinit var intent: Intent
|
||||
|
||||
private lateinit var appStartupTelemetry: AppStartupTelemetry
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
MockKAnnotations.init(this)
|
||||
appStartupTelemetry = AppStartupTelemetry(metricController)
|
||||
every { metricController.track(any()) } returns Unit
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN application is launch for the first time through application icon THEN records the correct values`() {
|
||||
setupIntentMock(APP_ICON)
|
||||
|
||||
appStartupTelemetry.onFenixApplicationOnCreate()
|
||||
appStartupTelemetry.onHomeActivityOnCreate(intent.toSafeIntent(), false)
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(APP_ICON, COLD, false)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN application is launch for the first time through a url link THEN records the correct values`() {
|
||||
setupIntentMock(LINK)
|
||||
|
||||
appStartupTelemetry.onFenixApplicationOnCreate()
|
||||
appStartupTelemetry.onHomeActivityOnCreate(intent.toSafeIntent(), false)
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(LINK, COLD, false)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN application is launch for the first time through an custom tab THEN records the correct values`() {
|
||||
setupIntentMock(CUSTOM_TAB)
|
||||
|
||||
appStartupTelemetry.onFenixApplicationOnCreate()
|
||||
appStartupTelemetry.onExternalAppBrowserOnCreate(intent.toSafeIntent(), false)
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(CUSTOM_TAB, COLD, false)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN, application exists and is backgrounded, WHEN application is launched again through app icon and HomeActivity is recreated THEN records the correct values`() {
|
||||
setupIntentMock(APP_ICON)
|
||||
launchApplicationAndPutApplicationInBackground()
|
||||
|
||||
appStartupTelemetry.onHomeActivityOnCreate(intent.toSafeIntent(), false)
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(APP_ICON, WARM, false)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN, application exists and is backgrounded, WHEN application is launched again through url link and HomeActivity is recreated THEN records the correct values`() {
|
||||
setupIntentMock(LINK)
|
||||
launchApplicationAndPutApplicationInBackground()
|
||||
|
||||
appStartupTelemetry.onHomeActivityOnCreate(intent.toSafeIntent(), false)
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(LINK, WARM, false)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN, application exists and is backgrounded, WHEN application is launched again through custom tab and ExternalAppBrowserActivity is recreated THEN records the correct values`() {
|
||||
setupIntentMock(CUSTOM_TAB)
|
||||
launchApplicationAndPutApplicationInBackground()
|
||||
|
||||
appStartupTelemetry.onExternalAppBrowserOnCreate(intent.toSafeIntent(), false)
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(CUSTOM_TAB, WARM, false)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN, application exists and is backgrounded, WHEN application is launched again through app icon and HomeActivity is restarted THEN records the correct values`() {
|
||||
setupIntentMock(APP_ICON)
|
||||
launchApplicationAndPutApplicationInBackground()
|
||||
|
||||
appStartupTelemetry.onHomeActivityOnRestart()
|
||||
appStartupTelemetry.onHomeActivityOnNewIntent(intent.toSafeIntent())
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(APP_ICON, HOT)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN, application exists and is backgrounded, WHEN application is launched again through url link and HomeActivity is restarted THEN records the correct values`() {
|
||||
setupIntentMock(LINK)
|
||||
launchApplicationAndPutApplicationInBackground()
|
||||
|
||||
appStartupTelemetry.onHomeActivityOnRestart()
|
||||
appStartupTelemetry.onHomeActivityOnNewIntent(intent.toSafeIntent())
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(LINK, HOT)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN application is launched and onResume() is called twice THEN metric is reported only once`() {
|
||||
setupIntentMock(LINK)
|
||||
appStartupTelemetry.onExternalAppBrowserOnCreate(intent.toSafeIntent(), false)
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
verify(exactly = 1) { metricController.track(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN application is in background WHEN application is launched again through unknown source and HomeActivity exists THEN records the correct values`() {
|
||||
setupIntentMock(UNKNOWN)
|
||||
launchApplicationAndPutApplicationInBackground()
|
||||
|
||||
appStartupTelemetry.onHomeActivityOnCreate(intent.toSafeIntent(), false)
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(UNKNOWN, WARM, false)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN application exists and is backgrounded WHEN application started again through app icon but HomeActivity is recreated from savedInstanceState THEN records the correct values`() {
|
||||
setupIntentMock(APP_ICON)
|
||||
launchApplicationAndPutApplicationInBackground()
|
||||
|
||||
appStartupTelemetry.onHomeActivityOnCreate(intent.toSafeIntent(), true)
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(APP_ICON, WARM, true)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
private fun launchApplicationAndPutApplicationInBackground() {
|
||||
appStartupTelemetry.onFenixApplicationOnCreate()
|
||||
appStartupTelemetry.onHomeActivityOnCreate(intent.toSafeIntent(), false)
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
// have to clear the mock function calls so it doesnt interfere with tests
|
||||
clearMocks(metricController, answers = false)
|
||||
|
||||
appStartupTelemetry.onApplicationOnStop()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN application is in background WHEN application is launched again HomeActivity only calls onResume THEN records the correct values`() {
|
||||
setupIntentMock(UNKNOWN)
|
||||
launchApplicationAndPutApplicationInBackground()
|
||||
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(UNKNOWN, ERROR)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN application is in background WHEN application is launched again HomeActivity calls onRestart but not onNewIntent THEN records the correct values`() {
|
||||
setupIntentMock(APP_ICON)
|
||||
launchApplicationAndPutApplicationInBackground()
|
||||
|
||||
appStartupTelemetry.onHomeActivityOnRestart()
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(UNKNOWN, HOT)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN application is in background WHEN application is launched again and HomeActivity calls onNewIntent but not onRestart THEN records the correct values`() {
|
||||
setupIntentMock(APP_ICON)
|
||||
launchApplicationAndPutApplicationInBackground()
|
||||
|
||||
appStartupTelemetry.onHomeActivityOnNewIntent(intent.toSafeIntent())
|
||||
appStartupTelemetry.onHomeActivityOnResume()
|
||||
|
||||
val validMetric = AppAllStartup(APP_ICON, ERROR)
|
||||
verify(exactly = 1) { metricController.track(validMetric) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN AppAllStartup does not have savedInstanceState THEN do not return savedInstanceState`() {
|
||||
val expectedExtra: Map<Events.appOpenedAllStartupKeys, String>? = hashMapOf(
|
||||
source to APP_ICON.toString(),
|
||||
type to HOT.toString())
|
||||
|
||||
val appAllStartup = AppAllStartup(APP_ICON, HOT)
|
||||
|
||||
assertTrue(appAllStartup.extras!! == expectedExtra)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN AppAllStartup have savedInstanceState THEN return savedInstanceState `() {
|
||||
val expectedExtra: Map<Events.appOpenedAllStartupKeys, String>? = hashMapOf(
|
||||
source to APP_ICON.toString(),
|
||||
type to COLD.toString(),
|
||||
hasSavedInstanceState to true.toString())
|
||||
|
||||
val appAllStartup = AppAllStartup(APP_ICON, COLD, true)
|
||||
|
||||
assertTrue(appAllStartup.extras!! == expectedExtra)
|
||||
}
|
||||
|
||||
private fun setupIntentMock(source: Source) {
|
||||
when (source) {
|
||||
APP_ICON -> {
|
||||
every { intent.action } returns Intent.ACTION_MAIN
|
||||
every { intent.categories } returns setOf(Intent.CATEGORY_LAUNCHER)
|
||||
}
|
||||
LINK, CUSTOM_TAB -> {
|
||||
every { intent.action } returns Intent.ACTION_VIEW
|
||||
every { intent.categories } returns emptySet()
|
||||
}
|
||||
UNKNOWN -> {
|
||||
every { intent.action } returns Intent.ACTION_MAIN
|
||||
every { intent.categories } returns emptySet()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue