2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-19 09:25:34 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/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.
This commit is contained in:
Michael Comella 2020-10-14 15:28:20 -07:00 committed by Michael Comella
parent 69a126cbb6
commit 910b7f19aa
2 changed files with 24 additions and 8 deletions

View File

@ -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)

View File

@ -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.