fix tests and linting

This commit is contained in:
Charlie Humphreys 2022-11-23 12:47:12 -05:00 committed by mergify[bot]
parent 41f7698bfa
commit e400a8ed8d
4 changed files with 48 additions and 13 deletions

View File

@ -175,7 +175,7 @@ class HomeActivityIntentTestRule internal constructor(
}
override fun getActivityIntent(): Intent {
return if(this::intent.isInitialized) {
return if (this::intent.isInitialized) {
this.intent
} else {
super.getActivityIntent()

View File

@ -3,14 +3,21 @@ package org.mozilla.fenix.ui
import android.content.Intent
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import mozilla.components.concept.sync.*
import io.mockk.mockk
import mozilla.components.concept.sync.AuthType
import okhttp3.mockwebserver.MockWebServer
import org.junit.*
import org.junit.After
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mozilla.fenix.components.TelemetryAccountObserver
import org.mozilla.fenix.helpers.*
import org.mozilla.fenix.helpers.AndroidAssetDispatcher
import org.mozilla.fenix.helpers.Experimentation
import org.mozilla.fenix.helpers.HomeActivityIntentTestRule
import org.mozilla.fenix.helpers.RetryTestRule
import org.mozilla.fenix.helpers.TestHelper.appContext
import org.mozilla.fenix.ui.robots.homeScreen
import io.mockk.mockk
class NimbusEventTest {
private lateinit var mDevice: UiDevice
@ -18,9 +25,11 @@ class NimbusEventTest {
@get:Rule
val homeActivityTestRule = HomeActivityIntentTestRule.withDefaultSettingsOverrides()
.withIntent(Intent().apply {
action = Intent.ACTION_VIEW
})
.withIntent(
Intent().apply {
action = Intent.ACTION_VIEW
},
)
@Rule
@JvmField
@ -46,7 +55,7 @@ class NimbusEventTest {
homeScreen { }.dismissOnboarding()
Experimentation.withHelper {
Assert.assertTrue(evalJexl("'app_opened'|eventSum('Days', 28, 0) > 0"))
assertTrue(evalJexl("'app_opened'|eventSum('Days', 28, 0) > 0"))
}
}
@ -56,7 +65,7 @@ class NimbusEventTest {
observer.onAuthenticated(mockk(), AuthType.Signin)
Experimentation.withHelper {
Assert.assertTrue(evalJexl("'sync_auth_sign_in'|eventSum('Days', 28, 0) > 0"))
assertTrue(evalJexl("'sync_auth_sign_in'|eventSum('Days', 28, 0) > 0"))
}
}
}

View File

@ -126,7 +126,7 @@ class BackgroundServices(
}
private val telemetryAccountObserver = TelemetryAccountObserver(
context
context,
)
val accountAbnormalities = AccountAbnormalities(context, crashReporter, strictMode)
@ -218,7 +218,7 @@ private class AccountManagerReadyObserver(
@VisibleForTesting(otherwise = PRIVATE)
internal class TelemetryAccountObserver(
private val context: Context
private val context: Context,
) : AccountObserver {
override fun onAuthenticated(account: OAuthAccount, authType: AuthType) {
context.settings().signedInFxaAccount = true

View File

@ -4,6 +4,7 @@
package org.mozilla.fenix.components
import android.content.Context
import io.mockk.MockKAnnotations
import io.mockk.Runs
import io.mockk.confirmVerified
@ -15,6 +16,7 @@ import io.mockk.verify
import mozilla.components.concept.sync.AccountObserver
import mozilla.components.concept.sync.AuthType
import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.service.nimbus.NimbusApi
import mozilla.components.support.base.observer.ObserverRegistry
import mozilla.components.support.test.robolectric.testContext
import mozilla.telemetry.glean.testing.GleanTestRule
@ -24,6 +26,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.GleanMetrics.SyncAuth
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.utils.Settings
@ -34,9 +37,15 @@ class BackgroundServicesTest {
@get:Rule
val gleanTestRule = GleanTestRule(testContext)
@MockK
private lateinit var context: Context
@MockK
private lateinit var settings: Settings
@MockK
private lateinit var nimbus: NimbusApi
private lateinit var observer: TelemetryAccountObserver
private lateinit var registry: ObserverRegistry<AccountObserver>
@ -45,7 +54,15 @@ class BackgroundServicesTest {
MockKAnnotations.init(this)
every { settings.signedInFxaAccount = any() } just Runs
observer = TelemetryAccountObserver(settings)
val mockComponents: Components = mockk()
every { mockComponents.settings } returns settings
every { mockComponents.analytics } returns mockk {
every { experiments } returns nimbus
}
every { context.components } returns mockComponents
every { nimbus.recordEvent(any()) } returns Unit
observer = TelemetryAccountObserver(context)
registry = ObserverRegistry<AccountObserver>().apply { register(observer) }
}
@ -123,4 +140,13 @@ class BackgroundServicesTest {
verify { settings.signedInFxaAccount = false }
confirmVerified(settings)
}
@Test
fun `telemetry account observer records nimbus event for logins`() {
observer.onAuthenticated(mockk(), AuthType.Signin)
verify {
nimbus.recordEvent("sync_auth_sign_in")
}
confirmVerified(nimbus)
}
}