Use TestScope.backgroundScope

This commit is contained in:
Benoit Marty 2025-10-27 10:58:28 +01:00 committed by Benoit Marty
parent fbecf8d34f
commit 31ff72b319
2 changed files with 3 additions and 20 deletions

View file

@ -7,7 +7,6 @@
package io.element.android.libraries.push.impl.notifications package io.element.android.libraries.push.impl.notifications
import androidx.annotation.VisibleForTesting
import dev.zacsweers.metro.AppScope import dev.zacsweers.metro.AppScope
import dev.zacsweers.metro.ContributesBinding import dev.zacsweers.metro.ContributesBinding
import dev.zacsweers.metro.SingleIn import dev.zacsweers.metro.SingleIn
@ -31,7 +30,6 @@ import io.element.android.services.appnavstate.api.AppNavigationStateService
import io.element.android.services.appnavstate.api.NavigationState import io.element.android.services.appnavstate.api.NavigationState
import io.element.android.services.appnavstate.api.currentSessionId import io.element.android.services.appnavstate.api.currentSessionId
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
@ -54,25 +52,17 @@ class DefaultNotificationDrawerManager(
private val imageLoaderHolder: ImageLoaderHolder, private val imageLoaderHolder: ImageLoaderHolder,
private val activeNotificationsProvider: ActiveNotificationsProvider, private val activeNotificationsProvider: ActiveNotificationsProvider,
) : NotificationCleaner { ) : NotificationCleaner {
private var appNavigationStateObserver: Job? = null
// TODO EAx add a setting per user for this // TODO EAx add a setting per user for this
private var useCompleteNotificationFormat = true private var useCompleteNotificationFormat = true
init { init {
// Observe application state // Observe application state
appNavigationStateObserver = coroutineScope.launch { coroutineScope.launch {
appNavigationStateService.appNavigationState appNavigationStateService.appNavigationState
.collect { onAppNavigationStateChange(it.navigationState) } .collect { onAppNavigationStateChange(it.navigationState) }
} }
} }
// For test only
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun destroy() {
appNavigationStateObserver?.cancel()
}
private var currentAppNavigationState: NavigationState? = null private var currentAppNavigationState: NavigationState? = null
private fun onAppNavigationStateChange(navigationState: NavigationState) { private fun onAppNavigationStateChange(navigationState: NavigationState) {

View file

@ -56,7 +56,6 @@ class DefaultNotificationDrawerManagerTest {
fun `clearAllEvents should have no effect when queue is empty`() = runTest { fun `clearAllEvents should have no effect when queue is empty`() = runTest {
val defaultNotificationDrawerManager = createDefaultNotificationDrawerManager() val defaultNotificationDrawerManager = createDefaultNotificationDrawerManager()
defaultNotificationDrawerManager.clearAllEvents(A_SESSION_ID) defaultNotificationDrawerManager.clearAllEvents(A_SESSION_ID)
defaultNotificationDrawerManager.destroy()
} }
@Test @Test
@ -88,7 +87,6 @@ class DefaultNotificationDrawerManagerTest {
defaultNotificationDrawerManager.onNotifiableEventReceived(aNotifiableMessageEvent()) defaultNotificationDrawerManager.onNotifiableEventReceived(aNotifiableMessageEvent())
// Add the same Event again (will be ignored) // Add the same Event again (will be ignored)
defaultNotificationDrawerManager.onNotifiableEventReceived(aNotifiableMessageEvent()) defaultNotificationDrawerManager.onNotifiableEventReceived(aNotifiableMessageEvent())
defaultNotificationDrawerManager.destroy()
} }
@Test @Test
@ -101,7 +99,7 @@ class DefaultNotificationDrawerManagerTest {
) )
) )
val appNavigationStateService = FakeAppNavigationStateService(appNavigationState = appNavigationStateFlow) val appNavigationStateService = FakeAppNavigationStateService(appNavigationState = appNavigationStateFlow)
val defaultNotificationDrawerManager = createDefaultNotificationDrawerManager( createDefaultNotificationDrawerManager(
appNavigationStateService = appNavigationStateService appNavigationStateService = appNavigationStateService
) )
appNavigationStateFlow.emit(AppNavigationState(aNavigationState(), isInForeground = true)) appNavigationStateFlow.emit(AppNavigationState(aNavigationState(), isInForeground = true))
@ -117,7 +115,6 @@ class DefaultNotificationDrawerManagerTest {
// Like a user sign out // Like a user sign out
appNavigationStateFlow.emit(AppNavigationState(aNavigationState(), isInForeground = true)) appNavigationStateFlow.emit(AppNavigationState(aNavigationState(), isInForeground = true))
runCurrent() runCurrent()
defaultNotificationDrawerManager.destroy()
} }
@Test @Test
@ -172,8 +169,6 @@ class DefaultNotificationDrawerManagerTest {
any(), any(),
), ),
) )
defaultNotificationDrawerManager.destroy()
} }
@Test @Test
@ -205,8 +200,6 @@ class DefaultNotificationDrawerManagerTest {
listOf(value(null), value(roomMessageId)), listOf(value(null), value(roomMessageId)),
listOf(value(null), value(summaryId)), listOf(value(null), value(summaryId)),
) )
defaultNotificationDrawerManager.destroy()
} }
private fun TestScope.createDefaultNotificationDrawerManager( private fun TestScope.createDefaultNotificationDrawerManager(
@ -234,7 +227,7 @@ class DefaultNotificationDrawerManagerTest {
sessionStore = sessionStore, sessionStore = sessionStore,
), ),
appNavigationStateService = appNavigationStateService, appNavigationStateService = appNavigationStateService,
coroutineScope = this, coroutineScope = backgroundScope,
matrixClientProvider = matrixClientProvider, matrixClientProvider = matrixClientProvider,
imageLoaderHolder = FakeImageLoaderHolder(), imageLoaderHolder = FakeImageLoaderHolder(),
activeNotificationsProvider = activeNotificationsProvider, activeNotificationsProvider = activeNotificationsProvider,