|
|
@ -24,7 +24,6 @@ import io.element.android.services.analyticsproviders.api.AnalyticsProvider |
|
|
|
import io.element.android.services.analyticsproviders.test.FakeAnalyticsProvider |
|
|
|
import io.element.android.services.analyticsproviders.test.FakeAnalyticsProvider |
|
|
|
import io.element.android.tests.testutils.lambda.lambdaRecorder |
|
|
|
import io.element.android.tests.testutils.lambda.lambdaRecorder |
|
|
|
import io.element.android.tests.testutils.lambda.value |
|
|
|
import io.element.android.tests.testutils.lambda.value |
|
|
|
import io.element.android.tests.testutils.runCancellableScopeTest |
|
|
|
|
|
|
|
import kotlinx.coroutines.CompletableDeferred |
|
|
|
import kotlinx.coroutines.CompletableDeferred |
|
|
|
import kotlinx.coroutines.CoroutineScope |
|
|
|
import kotlinx.coroutines.CoroutineScope |
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi |
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi |
|
|
@ -37,13 +36,13 @@ import org.junit.Test |
|
|
|
|
|
|
|
|
|
|
|
class DefaultAnalyticsServiceTest { |
|
|
|
class DefaultAnalyticsServiceTest { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `getAvailableAnalyticsProviders return the set of provider`() = runCancellableScopeTest { |
|
|
|
fun `getAvailableAnalyticsProviders return the set of provider`() = runTest { |
|
|
|
val providers = setOf( |
|
|
|
val providers = setOf( |
|
|
|
FakeAnalyticsProvider(name = "provider1", stopLambda = { }), |
|
|
|
FakeAnalyticsProvider(name = "provider1", stopLambda = { }), |
|
|
|
FakeAnalyticsProvider(name = "provider2", stopLambda = { }), |
|
|
|
FakeAnalyticsProvider(name = "provider2", stopLambda = { }), |
|
|
|
) |
|
|
|
) |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
analyticsProviders = providers |
|
|
|
analyticsProviders = providers |
|
|
|
) |
|
|
|
) |
|
|
|
val result = sut.getAvailableAnalyticsProviders() |
|
|
|
val result = sut.getAvailableAnalyticsProviders() |
|
|
@ -51,17 +50,17 @@ class DefaultAnalyticsServiceTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `when consent is not provided, capture is no op`() = runCancellableScopeTest { |
|
|
|
fun `when consent is not provided, capture is no op`() = runTest { |
|
|
|
val sut = createDefaultAnalyticsService(it) |
|
|
|
val sut = createDefaultAnalyticsService(backgroundScope) |
|
|
|
sut.capture(anEvent) |
|
|
|
sut.capture(anEvent) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `when consent is provided, capture is sent to the AnalyticsProvider`() = runCancellableScopeTest { |
|
|
|
fun `when consent is provided, capture is sent to the AnalyticsProvider`() = runTest { |
|
|
|
val initLambda = lambdaRecorder<Unit> { } |
|
|
|
val initLambda = lambdaRecorder<Unit> { } |
|
|
|
val captureLambda = lambdaRecorder<VectorAnalyticsEvent, Unit> { _ -> } |
|
|
|
val captureLambda = lambdaRecorder<VectorAnalyticsEvent, Unit> { _ -> } |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
analyticsStore = FakeAnalyticsStore(defaultUserConsent = true), |
|
|
|
analyticsStore = FakeAnalyticsStore(defaultUserConsent = true), |
|
|
|
analyticsProviders = setOf( |
|
|
|
analyticsProviders = setOf( |
|
|
|
FakeAnalyticsProvider( |
|
|
|
FakeAnalyticsProvider( |
|
|
@ -76,17 +75,17 @@ class DefaultAnalyticsServiceTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `when consent is not provided, screen is no op`() = runCancellableScopeTest { |
|
|
|
fun `when consent is not provided, screen is no op`() = runTest { |
|
|
|
val sut = createDefaultAnalyticsService(it) |
|
|
|
val sut = createDefaultAnalyticsService(backgroundScope) |
|
|
|
sut.screen(aScreen) |
|
|
|
sut.screen(aScreen) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `when consent is provided, screen is sent to the AnalyticsProvider`() = runCancellableScopeTest { |
|
|
|
fun `when consent is provided, screen is sent to the AnalyticsProvider`() = runTest { |
|
|
|
val initLambda = lambdaRecorder<Unit> { } |
|
|
|
val initLambda = lambdaRecorder<Unit> { } |
|
|
|
val screenLambda = lambdaRecorder<VectorAnalyticsScreen, Unit> { _ -> } |
|
|
|
val screenLambda = lambdaRecorder<VectorAnalyticsScreen, Unit> { _ -> } |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
analyticsStore = FakeAnalyticsStore(defaultUserConsent = true), |
|
|
|
analyticsStore = FakeAnalyticsStore(defaultUserConsent = true), |
|
|
|
analyticsProviders = setOf( |
|
|
|
analyticsProviders = setOf( |
|
|
|
FakeAnalyticsProvider( |
|
|
|
FakeAnalyticsProvider( |
|
|
@ -101,17 +100,17 @@ class DefaultAnalyticsServiceTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `when consent is not provided, trackError is no op`() = runCancellableScopeTest { |
|
|
|
fun `when consent is not provided, trackError is no op`() = runTest { |
|
|
|
val sut = createDefaultAnalyticsService(it) |
|
|
|
val sut = createDefaultAnalyticsService(backgroundScope) |
|
|
|
sut.trackError(anError) |
|
|
|
sut.trackError(anError) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `when consent is provided, trackError is sent to the AnalyticsProvider`() = runCancellableScopeTest { |
|
|
|
fun `when consent is provided, trackError is sent to the AnalyticsProvider`() = runTest { |
|
|
|
val initLambda = lambdaRecorder<Unit> { } |
|
|
|
val initLambda = lambdaRecorder<Unit> { } |
|
|
|
val trackErrorLambda = lambdaRecorder<Throwable, Unit> { _ -> } |
|
|
|
val trackErrorLambda = lambdaRecorder<Throwable, Unit> { _ -> } |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
analyticsStore = FakeAnalyticsStore(defaultUserConsent = true), |
|
|
|
analyticsStore = FakeAnalyticsStore(defaultUserConsent = true), |
|
|
|
analyticsProviders = setOf( |
|
|
|
analyticsProviders = setOf( |
|
|
|
FakeAnalyticsProvider( |
|
|
|
FakeAnalyticsProvider( |
|
|
@ -126,10 +125,10 @@ class DefaultAnalyticsServiceTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `setUserConsent is sent to the store`() = runCancellableScopeTest { |
|
|
|
fun `setUserConsent is sent to the store`() = runTest { |
|
|
|
val store = FakeAnalyticsStore() |
|
|
|
val store = FakeAnalyticsStore() |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
analyticsStore = store, |
|
|
|
analyticsStore = store, |
|
|
|
) |
|
|
|
) |
|
|
|
assertThat(store.userConsentFlow.first()).isFalse() |
|
|
|
assertThat(store.userConsentFlow.first()).isFalse() |
|
|
@ -140,10 +139,10 @@ class DefaultAnalyticsServiceTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `setAnalyticsId is sent to the store`() = runCancellableScopeTest { |
|
|
|
fun `setAnalyticsId is sent to the store`() = runTest { |
|
|
|
val store = FakeAnalyticsStore() |
|
|
|
val store = FakeAnalyticsStore() |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
analyticsStore = store, |
|
|
|
analyticsStore = store, |
|
|
|
) |
|
|
|
) |
|
|
|
assertThat(store.analyticsIdFlow.first()).isEqualTo("") |
|
|
|
assertThat(store.analyticsIdFlow.first()).isEqualTo("") |
|
|
@ -154,10 +153,10 @@ class DefaultAnalyticsServiceTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `setDidAskUserConsent is sent to the store`() = runCancellableScopeTest { |
|
|
|
fun `setDidAskUserConsent is sent to the store`() = runTest { |
|
|
|
val store = FakeAnalyticsStore() |
|
|
|
val store = FakeAnalyticsStore() |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
analyticsStore = store, |
|
|
|
analyticsStore = store, |
|
|
|
) |
|
|
|
) |
|
|
|
assertThat(store.didAskUserConsentFlow.first()).isFalse() |
|
|
|
assertThat(store.didAskUserConsentFlow.first()).isFalse() |
|
|
@ -168,13 +167,13 @@ class DefaultAnalyticsServiceTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `when a session is deleted, the store is reset`() = runCancellableScopeTest { |
|
|
|
fun `when a session is deleted, the store is reset`() = runTest { |
|
|
|
val resetLambda = lambdaRecorder<Unit> { } |
|
|
|
val resetLambda = lambdaRecorder<Unit> { } |
|
|
|
val store = FakeAnalyticsStore( |
|
|
|
val store = FakeAnalyticsStore( |
|
|
|
resetLambda = resetLambda, |
|
|
|
resetLambda = resetLambda, |
|
|
|
) |
|
|
|
) |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
analyticsStore = store, |
|
|
|
analyticsStore = store, |
|
|
|
) |
|
|
|
) |
|
|
|
sut.onSessionDeleted("userId") |
|
|
|
sut.onSessionDeleted("userId") |
|
|
@ -182,12 +181,12 @@ class DefaultAnalyticsServiceTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `when reset is invoked, the user consent is reset`() = runCancellableScopeTest { |
|
|
|
fun `when reset is invoked, the user consent is reset`() = runTest { |
|
|
|
val store = FakeAnalyticsStore( |
|
|
|
val store = FakeAnalyticsStore( |
|
|
|
defaultDidAskUserConsent = true, |
|
|
|
defaultDidAskUserConsent = true, |
|
|
|
) |
|
|
|
) |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
analyticsStore = store, |
|
|
|
analyticsStore = store, |
|
|
|
) |
|
|
|
) |
|
|
|
assertThat(store.didAskUserConsentFlow.first()).isTrue() |
|
|
|
assertThat(store.didAskUserConsentFlow.first()).isTrue() |
|
|
@ -196,9 +195,9 @@ class DefaultAnalyticsServiceTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `when a session is added, nothing happen`() = runCancellableScopeTest { |
|
|
|
fun `when a session is added, nothing happen`() = runTest { |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
) |
|
|
|
) |
|
|
|
sut.onSessionCreated("userId") |
|
|
|
sut.onSessionCreated("userId") |
|
|
|
} |
|
|
|
} |
|
|
@ -231,10 +230,10 @@ class DefaultAnalyticsServiceTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `when consent is provided, updateUserProperties is sent to the provider`() = runCancellableScopeTest { |
|
|
|
fun `when consent is provided, updateUserProperties is sent to the provider`() = runTest { |
|
|
|
val updateUserPropertiesLambda = lambdaRecorder<UserProperties, Unit> { _ -> } |
|
|
|
val updateUserPropertiesLambda = lambdaRecorder<UserProperties, Unit> { _ -> } |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
analyticsProviders = setOf( |
|
|
|
analyticsProviders = setOf( |
|
|
|
FakeAnalyticsProvider( |
|
|
|
FakeAnalyticsProvider( |
|
|
|
initLambda = { }, |
|
|
|
initLambda = { }, |
|
|
@ -248,10 +247,10 @@ class DefaultAnalyticsServiceTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun `when super properties are updated, updateSuperProperties is sent to the provider`() = runCancellableScopeTest { |
|
|
|
fun `when super properties are updated, updateSuperProperties is sent to the provider`() = runTest { |
|
|
|
val updateSuperPropertiesLambda = lambdaRecorder<SuperProperties, Unit> { _ -> } |
|
|
|
val updateSuperPropertiesLambda = lambdaRecorder<SuperProperties, Unit> { _ -> } |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
val sut = createDefaultAnalyticsService( |
|
|
|
coroutineScope = it, |
|
|
|
coroutineScope = backgroundScope, |
|
|
|
analyticsProviders = setOf( |
|
|
|
analyticsProviders = setOf( |
|
|
|
FakeAnalyticsProvider( |
|
|
|
FakeAnalyticsProvider( |
|
|
|
initLambda = { }, |
|
|
|
initLambda = { }, |
|
|
|