mirror of
https://github.com/fork-maintainers/iceraven-browser
synced 2024-11-19 09:25:34 +00:00
[fenix] Add basic test coverage for Glean metrics in Fenix
This introduces test coverage, using the Glean SDK testing API, for the metrics that are set at startup by Fenix in the GleanMetricsService. This additional adds a basic test for the translation of the `app_opened` event.
This commit is contained in:
parent
c43b478447
commit
819e8d4ebb
@ -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.
|
||||
|
@ -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"])
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user