For #15644: mock package inspection dependencies in GleanMetricsService.

The new robolectric version changed the behavior such that the app ID
that was returned for our app was `org.mozilla.fenix.debug` instead of
(I guess) `org.mozilla.fenix`. In general, relying on robolectric can be
fragile, such as this case, so it's better to mock. Also, this test
behavior should theoretically have varied between build flavors so
mocking prevents the tests from breaking across flavors.
upstream-sync
Michael Comella 4 years ago committed by Michael Comella
parent 2c7789b1a8
commit 690554b5c6

@ -723,7 +723,11 @@ private val Event.wrapper: EventWrapper<*>?
is Event.ChangedToDefaultBrowser -> null
}
class GleanMetricsService(private val context: Context) : MetricsService {
class GleanMetricsService(
private val context: Context,
private val browsersCache: BrowsersCache = BrowsersCache,
private val mozillaProductDetector: MozillaProductDetector = MozillaProductDetector
) : MetricsService {
override val type = MetricServiceType.Data
private val logger = Logger("GleanMetricsService")
@ -759,11 +763,11 @@ class GleanMetricsService(private val context: Context) : MetricsService {
internal fun setStartupMetrics() {
setPreferenceMetrics()
Metrics.apply {
defaultBrowser.set(BrowsersCache.all(context).isDefaultBrowser)
MozillaProductDetector.getMozillaBrowserDefault(context)?.also {
defaultBrowser.set(browsersCache.all(context).isDefaultBrowser)
mozillaProductDetector.getMozillaBrowserDefault(context)?.also {
defaultMozBrowser.set(it)
}
mozillaProducts.set(MozillaProductDetector.getInstalledMozillaProducts(context))
mozillaProducts.set(mozillaProductDetector.getInstalledMozillaProducts(context))
adjustCampaign.set(context.settings().adjustCampaignId)
adjustAdGroup.set(context.settings().adjustAdGroup)

@ -4,6 +4,9 @@
package org.mozilla.fenix.components.metrics
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import mozilla.components.service.glean.testing.GleanTestRule
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
@ -17,6 +20,7 @@ import org.mozilla.fenix.GleanMetrics.Events
import org.mozilla.fenix.GleanMetrics.Metrics
import org.mozilla.fenix.GleanMetrics.SearchDefaultEngine
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.utils.BrowsersCache
@RunWith(FenixRobolectricTestRunner::class)
class GleanMetricsServiceTest {
@ -25,20 +29,28 @@ class GleanMetricsServiceTest {
private lateinit var gleanService: GleanMetricsService
@MockK private lateinit var browsersCache: BrowsersCache
@MockK private lateinit var mozillaProductDetector: MozillaProductDetector
@Before
fun setup() {
gleanService = GleanMetricsService(testContext)
MockKAnnotations.init(this)
gleanService = GleanMetricsService(testContext, browsersCache, mozillaProductDetector)
}
@Test
fun `setStartupMetrics sets some base metrics`() {
// Set the metrics.
val expectedAppName = "org.mozilla.fenix"
every { browsersCache.all(any()).isDefaultBrowser } returns true
every { mozillaProductDetector.getMozillaBrowserDefault(any()) } returns expectedAppName
every { mozillaProductDetector.getInstalledMozillaProducts(any()) } returns listOf(expectedAppName)
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())
assertEquals(expectedAppName, Metrics.defaultMozBrowser.testGetValue())
assertEquals(listOf(expectedAppName), Metrics.mozillaProducts.testGetValue())
// Verify that search engine defaults are NOT set. This test does
// not mock most of the objects telemetry is collected from.

Loading…
Cancel
Save