diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt index 170eac6ab..c86978247 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt @@ -426,30 +426,38 @@ class GleanMetricsService(private val context: Context) : MetricsService { Glean.registerPings(Pings) Glean.initialize(context, Configuration(channel = BuildConfig.BUILD_TYPE)) - Metrics.apply { - defaultBrowser.set(Browsers.all(context).isDefaultBrowser) - MozillaProductDetector.getMozillaBrowserDefault(context)?.also { - defaultMozBrowser.set(it) - } - mozillaProducts.set(MozillaProductDetector.getInstalledMozillaProducts(context)) - } - - SearchDefaultEngine.apply { - val defaultEngine = context - .components - .search - .searchEngineManager - .defaultSearchEngine ?: return@apply - - code.set(defaultEngine.identifier) - name.set(defaultEngine.name) - submissionUrl.set(defaultEngine.buildSearchUrl("")) - } - - activationPing.checkAndSend() + setStartupMetrics() } } + /** + * Sets some basic Glean metrics required by Fenix. + * This is a separate function to simplify testing. + */ + internal fun setStartupMetrics() { + Metrics.apply { + defaultBrowser.set(Browsers.all(context).isDefaultBrowser) + MozillaProductDetector.getMozillaBrowserDefault(context)?.also { + defaultMozBrowser.set(it) + } + mozillaProducts.set(MozillaProductDetector.getInstalledMozillaProducts(context)) + } + + SearchDefaultEngine.apply { + val defaultEngine = context + .components + .search + .searchEngineManager + .defaultSearchEngine ?: return@apply + + code.set(defaultEngine.identifier) + name.set(defaultEngine.name) + submissionUrl.set(defaultEngine.buildSearchUrl("")) + } + + activationPing.checkAndSend() + } + override fun stop() { /* * We cannot stop until we're done starting. diff --git a/app/src/test/java/org/mozilla/fenix/components/metrics/GleanMetricsServiceTest.kt b/app/src/test/java/org/mozilla/fenix/components/metrics/GleanMetricsServiceTest.kt new file mode 100644 index 000000000..646b85766 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/components/metrics/GleanMetricsServiceTest.kt @@ -0,0 +1,74 @@ +/* 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 kotlinx.coroutines.ObsoleteCoroutinesApi +import mozilla.components.service.glean.testing.GleanTestRule +import mozilla.components.support.test.robolectric.testContext +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.mozilla.fenix.GleanMetrics.Events +import org.mozilla.fenix.GleanMetrics.Metrics +import org.mozilla.fenix.GleanMetrics.SearchDefaultEngine +import org.mozilla.fenix.TestApplication +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@ObsoleteCoroutinesApi +@RunWith(RobolectricTestRunner::class) +@Config(application = TestApplication::class) +class GleanMetricsServiceTest { + @get:Rule + val gleanRule = GleanTestRule(testContext) + + @Test + fun `setStartupMetrics sets some base metrics`() { + val gleanService = GleanMetricsService(testContext) + + // Set the metrics. + gleanService.setStartupMetrics() + + // Verify that browser defaults metrics are set. + assertEquals(true, Metrics.defaultBrowser.testGetValue()) + assertEquals(true, Metrics.defaultMozBrowser.testHasValue()) + assertEquals(listOf("org.mozilla.fenix"), Metrics.mozillaProducts.testGetValue()) + + // Verify that search engine defaults are NOT set. This test does + // not mock most of the objects telemetry is collected from. + assertFalse(SearchDefaultEngine.code.testHasValue()) + assertFalse(SearchDefaultEngine.name.testHasValue()) + assertFalse(SearchDefaultEngine.submissionUrl.testHasValue()) + } + + @Test + fun `the app_opened event is correctly recorded`() { + val gleanService = GleanMetricsService(testContext) + + // Build the event wrapper used by Fenix. + val event = Event.OpenedApp(Event.OpenedApp.Source.APP_ICON) + + // Feed the wrapped event in the Glean service. + gleanService.track(event) + + // Use the testing API to verify that it's correctly recorded. + assertTrue(Events.appOpened.testHasValue()) + + // Get all the recorded events. We only expect 1 to be recorded. + val events = Events.appOpened.testGetValue() + assertEquals(1, events.size) + + // Verify that we get the expected content out. + assertEquals("events", events[0].category) + assertEquals("app_opened", events[0].name) + + // We only expect 1 extra key. + assertEquals(1, events[0].extra!!.size) + assertEquals("APP_ICON", events[0].extra!!["source"]) + } +}