for #11830 created class containing the logic for sending AllStartup telemetry logic
lint check renamed the intentReceived telemetry to appOpenedAllSource added comments removed unused code moved lifecycle process to AppAllSourceStartTelemetry moved tracking event out of init function lint fix moved appAllStartTelemetry to components added bit more info about the metrics added the onReceivedIntent metric back minor fix change discriptions based on the comments frm MR wrote test cases for AppAllSourceStartTelemetry.kt lint fix test case to mock application going background post rebase: post rebase: fixed nit from comments fixed nit from comments fixed nit from comments lint fix lint fixreleases/v80.0.0
parent
6a618aa318
commit
537d95c04d
@ -0,0 +1,59 @@
|
||||
/* 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,95 @@
|
||||
/* 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)) }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue