Skip to content

Commit 1463f84

Browse files
committed
Fix tests
1 parent cca4c70 commit 1463f84

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

services/analytics/impl/src/main/kotlin/io/element/android/services/analytics/impl/DefaultAnalyticsService.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
1717
import im.vector.app.features.analytics.plan.SuperProperties
1818
import im.vector.app.features.analytics.plan.UserProperties
1919
import io.element.android.libraries.di.annotations.AppCoroutineScope
20+
import io.element.android.libraries.sessionstorage.api.SessionStore
2021
import io.element.android.libraries.sessionstorage.api.observer.SessionListener
2122
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
2223
import io.element.android.services.analytics.api.AnalyticsService
@@ -40,6 +41,7 @@ class DefaultAnalyticsService(
4041
@AppCoroutineScope
4142
private val coroutineScope: CoroutineScope,
4243
private val sessionObserver: SessionObserver,
44+
private val sessionStore: SessionStore,
4345
) : AnalyticsService, SessionListener {
4446
// Cache for the store values
4547
private val userConsent = AtomicBoolean(false)
@@ -84,8 +86,10 @@ class DefaultAnalyticsService(
8486
}
8587

8688
override suspend fun onSessionDeleted(userId: String) {
87-
// Delete the store
88-
// analyticsStore.reset()
89+
// Delete the store when the last session is deleted
90+
if (sessionStore.getAllSessions().isEmpty()) {
91+
analyticsStore.reset()
92+
}
8993
}
9094

9195
private fun observeUserConsent() {

services/analytics/impl/src/test/kotlin/io/element/android/services/analytics/impl/DefaultAnalyticsServiceTest.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import im.vector.app.features.analytics.plan.MobileScreen
1616
import im.vector.app.features.analytics.plan.PollEnd
1717
import im.vector.app.features.analytics.plan.SuperProperties
1818
import im.vector.app.features.analytics.plan.UserProperties
19+
import io.element.android.libraries.sessionstorage.api.SessionStore
1920
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
21+
import io.element.android.libraries.sessionstorage.test.InMemorySessionStore
22+
import io.element.android.libraries.sessionstorage.test.aSessionData
2023
import io.element.android.libraries.sessionstorage.test.observer.NoOpSessionObserver
2124
import io.element.android.services.analytics.impl.store.AnalyticsStore
2225
import io.element.android.services.analytics.impl.store.FakeAnalyticsStore
@@ -167,7 +170,7 @@ class DefaultAnalyticsServiceTest {
167170
}
168171

169172
@Test
170-
fun `when a session is deleted, the store is reset`() = runTest {
173+
fun `when the last session is deleted, the store is reset`() = runTest {
171174
val resetLambda = lambdaRecorder<Unit> { }
172175
val store = FakeAnalyticsStore(
173176
resetLambda = resetLambda,
@@ -180,6 +183,23 @@ class DefaultAnalyticsServiceTest {
180183
resetLambda.assertions().isCalledOnce()
181184
}
182185

186+
@Test
187+
fun `when a session is deleted, the store is not reset`() = runTest {
188+
val resetLambda = lambdaRecorder<Unit> { }
189+
val store = FakeAnalyticsStore(
190+
resetLambda = resetLambda,
191+
)
192+
val sut = createDefaultAnalyticsService(
193+
coroutineScope = backgroundScope,
194+
analyticsStore = store,
195+
sessionStore = InMemorySessionStore(
196+
initialList = listOf(aSessionData()),
197+
)
198+
)
199+
sut.onSessionDeleted("userId")
200+
resetLambda.assertions().isNeverCalled()
201+
}
202+
183203
@Test
184204
fun `when reset is invoked, the user consent is reset`() = runTest {
185205
val store = FakeAnalyticsStore(
@@ -272,11 +292,13 @@ class DefaultAnalyticsServiceTest {
272292
),
273293
analyticsStore: AnalyticsStore = FakeAnalyticsStore(),
274294
sessionObserver: SessionObserver = NoOpSessionObserver(),
295+
sessionStore: SessionStore = InMemorySessionStore(),
275296
) = DefaultAnalyticsService(
276297
analyticsProviders = analyticsProviders,
277298
analyticsStore = analyticsStore,
278299
coroutineScope = coroutineScope,
279300
sessionObserver = sessionObserver,
301+
sessionStore = sessionStore,
280302
).also {
281303
// Wait for the service to be ready
282304
delay(1)

0 commit comments

Comments
 (0)