mirror of
https://github.com/fork-maintainers/iceraven-browser
synced 2024-11-03 23:15:31 +00:00
[fenix] For https://github.com/mozilla-mobile/fenix/issues/12769: Refactored Background Service Test into multiple test cases.
This commit is contained in:
parent
854065ea5a
commit
f2ad00d86e
@ -4,8 +4,10 @@
|
|||||||
|
|
||||||
package org.mozilla.fenix.components
|
package org.mozilla.fenix.components
|
||||||
|
|
||||||
|
import io.mockk.Called
|
||||||
import io.mockk.MockKAnnotations
|
import io.mockk.MockKAnnotations
|
||||||
import io.mockk.Runs
|
import io.mockk.Runs
|
||||||
|
import io.mockk.confirmVerified
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.impl.annotations.MockK
|
import io.mockk.impl.annotations.MockK
|
||||||
import io.mockk.just
|
import io.mockk.just
|
||||||
@ -23,8 +25,11 @@ import org.mozilla.fenix.utils.Settings
|
|||||||
|
|
||||||
class BackgroundServicesTest {
|
class BackgroundServicesTest {
|
||||||
|
|
||||||
@MockK private lateinit var metrics: MetricController
|
@MockK
|
||||||
@MockK private lateinit var settings: Settings
|
private lateinit var metrics: MetricController
|
||||||
|
|
||||||
|
@MockK
|
||||||
|
private lateinit var settings: Settings
|
||||||
|
|
||||||
private lateinit var observer: TelemetryAccountObserver
|
private lateinit var observer: TelemetryAccountObserver
|
||||||
private lateinit var registry: ObserverRegistry<AccountObserver>
|
private lateinit var registry: ObserverRegistry<AccountObserver>
|
||||||
@ -35,101 +40,95 @@ class BackgroundServicesTest {
|
|||||||
every { metrics.track(any()) } just Runs
|
every { metrics.track(any()) } just Runs
|
||||||
every { settings.fxaSignedIn = any() } just Runs
|
every { settings.fxaSignedIn = any() } just Runs
|
||||||
|
|
||||||
observer = TelemetryAccountObserver(mockk(relaxed = true), metrics)
|
observer = TelemetryAccountObserver(settings, metrics)
|
||||||
registry = ObserverRegistry<AccountObserver>().apply { register(observer) }
|
registry = ObserverRegistry<AccountObserver>().apply { register(observer) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `telemetry account observer`() {
|
fun `telemetry account observer tracks sign in event`() {
|
||||||
val account = mockk<OAuthAccount>()
|
val account = mockk<OAuthAccount>()
|
||||||
|
|
||||||
// Sign-in
|
|
||||||
registry.notifyObservers { onAuthenticated(account, AuthType.Signin) }
|
registry.notifyObservers { onAuthenticated(account, AuthType.Signin) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignIn)) }
|
verify { metrics.track(Event.SyncAuthSignIn) }
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthSignUp)) }
|
verify { settings.fxaSignedIn = true }
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthPaired)) }
|
confirmVerified(metrics, settings)
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthFromShared)) }
|
}
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthRecovered)) }
|
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthOtherExternal)) }
|
@Test
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthSignOut)) }
|
fun `telemetry account observer tracks sign up event`() {
|
||||||
|
val account = mockk<OAuthAccount>()
|
||||||
|
|
||||||
// Sign-up
|
|
||||||
registry.notifyObservers { onAuthenticated(account, AuthType.Signup) }
|
registry.notifyObservers { onAuthenticated(account, AuthType.Signup) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignIn)) }
|
verify { metrics.track(Event.SyncAuthSignUp) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignUp)) }
|
verify { settings.fxaSignedIn = true }
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthPaired)) }
|
confirmVerified(metrics, settings)
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthFromShared)) }
|
}
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthRecovered)) }
|
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthOtherExternal)) }
|
@Test
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthSignOut)) }
|
fun `telemetry account observer tracks pairing event`() {
|
||||||
|
val account = mockk<OAuthAccount>()
|
||||||
|
|
||||||
// Pairing
|
|
||||||
registry.notifyObservers { onAuthenticated(account, AuthType.Pairing) }
|
registry.notifyObservers { onAuthenticated(account, AuthType.Pairing) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignIn)) }
|
verify { metrics.track(Event.SyncAuthPaired) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignUp)) }
|
verify { settings.fxaSignedIn = true }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthPaired)) }
|
confirmVerified(metrics, settings)
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthFromShared)) }
|
}
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthRecovered)) }
|
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthOtherExternal)) }
|
@Test
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthSignOut)) }
|
fun `telemetry account observer tracks shared event`() {
|
||||||
|
val account = mockk<OAuthAccount>()
|
||||||
|
|
||||||
// Auto-login/shared account
|
|
||||||
registry.notifyObservers { onAuthenticated(account, AuthType.Shared) }
|
registry.notifyObservers { onAuthenticated(account, AuthType.Shared) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignIn)) }
|
verify { metrics.track(Event.SyncAuthFromShared) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignUp)) }
|
verify { settings.fxaSignedIn = true }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthPaired)) }
|
confirmVerified(metrics, settings)
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthFromShared)) }
|
}
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthRecovered)) }
|
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthOtherExternal)) }
|
@Test
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthSignOut)) }
|
fun `telemetry account observer tracks recovered event`() {
|
||||||
|
val account = mockk<OAuthAccount>()
|
||||||
|
|
||||||
// Internally recovered
|
|
||||||
registry.notifyObservers { onAuthenticated(account, AuthType.Recovered) }
|
registry.notifyObservers { onAuthenticated(account, AuthType.Recovered) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignIn)) }
|
verify { metrics.track(Event.SyncAuthRecovered) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignUp)) }
|
verify { settings.fxaSignedIn = true }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthPaired)) }
|
confirmVerified(metrics, settings)
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthFromShared)) }
|
}
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthRecovered)) }
|
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthOtherExternal)) }
|
@Test
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthSignOut)) }
|
fun `telemetry account observer tracks external creation event with null action`() {
|
||||||
|
val account = mockk<OAuthAccount>()
|
||||||
|
|
||||||
// Other external
|
|
||||||
registry.notifyObservers { onAuthenticated(account, AuthType.OtherExternal(null)) }
|
registry.notifyObservers { onAuthenticated(account, AuthType.OtherExternal(null)) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignIn)) }
|
verify { metrics.track(Event.SyncAuthOtherExternal) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignUp)) }
|
verify { settings.fxaSignedIn = true }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthPaired)) }
|
confirmVerified(metrics, settings)
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthFromShared)) }
|
}
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthRecovered)) }
|
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthOtherExternal)) }
|
@Test
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthSignOut)) }
|
fun `telemetry account observer tracks external creation event with some action`() {
|
||||||
|
val account = mockk<OAuthAccount>()
|
||||||
|
|
||||||
registry.notifyObservers { onAuthenticated(account, AuthType.OtherExternal("someAction")) }
|
registry.notifyObservers { onAuthenticated(account, AuthType.OtherExternal("someAction")) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignIn)) }
|
verify { metrics.track(Event.SyncAuthOtherExternal) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignUp)) }
|
verify { settings.fxaSignedIn = true }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthPaired)) }
|
confirmVerified(metrics, settings)
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthFromShared)) }
|
}
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthRecovered)) }
|
|
||||||
verify(exactly = 2) { metrics.track(eq(Event.SyncAuthOtherExternal)) }
|
@Test
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthSignOut)) }
|
fun `telemetry account observer does not track existing account`() {
|
||||||
|
val account = mockk<OAuthAccount>()
|
||||||
|
|
||||||
// NB: 'Existing' auth type isn't expected to record any auth telemetry.
|
|
||||||
registry.notifyObservers { onAuthenticated(account, AuthType.Existing) }
|
registry.notifyObservers { onAuthenticated(account, AuthType.Existing) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignIn)) }
|
verify { metrics wasNot Called }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignUp)) }
|
verify { settings.fxaSignedIn = true }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthPaired)) }
|
confirmVerified(metrics, settings)
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthFromShared)) }
|
}
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthRecovered)) }
|
|
||||||
verify(exactly = 2) { metrics.track(eq(Event.SyncAuthOtherExternal)) }
|
|
||||||
verify(exactly = 0) { metrics.track(eq(Event.SyncAuthSignOut)) }
|
|
||||||
|
|
||||||
// Logout
|
@Test
|
||||||
|
fun `telemetry account observer tracks sign out event`() {
|
||||||
registry.notifyObservers { onLoggedOut() }
|
registry.notifyObservers { onLoggedOut() }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignIn)) }
|
verify { metrics.track(Event.SyncAuthSignOut) }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignUp)) }
|
verify { settings.fxaSignedIn = false }
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthPaired)) }
|
confirmVerified(metrics, settings)
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthFromShared)) }
|
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthRecovered)) }
|
|
||||||
verify(exactly = 2) { metrics.track(eq(Event.SyncAuthOtherExternal)) }
|
|
||||||
verify(exactly = 1) { metrics.track(eq(Event.SyncAuthSignOut)) }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user