LogoutEvents -> LogoutEvent

This commit is contained in:
Benoit Marty
2026-08-25 10:05:38 +02:00
parent 93efe3348f
commit 116897d64c
7 changed files with 36 additions and 36 deletions
@@ -8,7 +8,7 @@
package io.element.android.features.logout.impl package io.element.android.features.logout.impl
sealed interface LogoutEvents { sealed interface LogoutEvent {
data class Logout(val ignoreSdkError: Boolean) : LogoutEvents data class Logout(val ignoreSdkError: Boolean) : LogoutEvent
data object CloseDialogs : LogoutEvents data object CloseDialogs : LogoutEvent
} }
@@ -74,16 +74,16 @@ class LogoutPresenter(
} }
} }
fun handleEvent(event: LogoutEvents) { fun handleEvent(event: LogoutEvent) {
when (event) { when (event) {
is LogoutEvents.Logout -> { is LogoutEvent.Logout -> {
if (logoutAction.value.isConfirming() || event.ignoreSdkError) { if (logoutAction.value.isConfirming() || event.ignoreSdkError) {
localCoroutineScope.logout(logoutAction, event.ignoreSdkError) localCoroutineScope.logout(logoutAction, event.ignoreSdkError)
} else { } else {
logoutAction.value = AsyncAction.ConfirmingNoParams logoutAction.value = AsyncAction.ConfirmingNoParams
} }
} }
LogoutEvents.CloseDialogs -> { LogoutEvent.CloseDialogs -> {
logoutAction.value = AsyncAction.Uninitialized logoutAction.value = AsyncAction.Uninitialized
} }
} }
@@ -21,5 +21,5 @@ data class LogoutState(
val backupUploadState: BackupUploadState, val backupUploadState: BackupUploadState,
val waitingForALongTime: Boolean, val waitingForALongTime: Boolean,
val logoutAction: AsyncAction<Unit>, val logoutAction: AsyncAction<Unit>,
val eventSink: (LogoutEvents) -> Unit, val eventSink: (LogoutEvent) -> Unit,
) )
@@ -50,7 +50,7 @@ fun aLogoutState(
backupUploadState: BackupUploadState = BackupUploadState.Unknown, backupUploadState: BackupUploadState = BackupUploadState.Unknown,
waitingForALongTime: Boolean = false, waitingForALongTime: Boolean = false,
logoutAction: AsyncAction<Unit> = AsyncAction.Uninitialized, logoutAction: AsyncAction<Unit> = AsyncAction.Uninitialized,
eventSink: (LogoutEvents) -> Unit = {}, eventSink: (LogoutEvent) -> Unit = {},
) = LogoutState( ) = LogoutState(
isLastDevice = isLastDevice, isLastDevice = isLastDevice,
backupState = backupState, backupState = backupState,
@@ -61,7 +61,7 @@ fun LogoutView(
state = state, state = state,
onChangeRecoveryKeyClick = onChangeRecoveryKeyClick, onChangeRecoveryKeyClick = onChangeRecoveryKeyClick,
onLogoutClick = { onLogoutClick = {
eventSink(LogoutEvents.Logout(ignoreSdkError = false)) eventSink(LogoutEvent.Logout(ignoreSdkError = false))
} }
) )
}, },
@@ -72,13 +72,13 @@ fun LogoutView(
LogoutActionDialog( LogoutActionDialog(
state.logoutAction, state.logoutAction,
onConfirmClick = { onConfirmClick = {
eventSink(LogoutEvents.Logout(ignoreSdkError = false)) eventSink(LogoutEvent.Logout(ignoreSdkError = false))
}, },
onForceLogoutClick = { onForceLogoutClick = {
eventSink(LogoutEvents.Logout(ignoreSdkError = true)) eventSink(LogoutEvent.Logout(ignoreSdkError = true))
}, },
onDismissDialog = { onDismissDialog = {
eventSink(LogoutEvents.CloseDialogs) eventSink(LogoutEvent.CloseDialogs)
}, },
) )
} }
@@ -139,10 +139,10 @@ class LogoutPresenterTest {
presenter.present() presenter.present()
}.test { }.test {
val initialState = awaitFirstItem() val initialState = awaitFirstItem()
initialState.eventSink.invoke(LogoutEvents.Logout(ignoreSdkError = false)) initialState.eventSink.invoke(LogoutEvent.Logout(ignoreSdkError = false))
val confirmationState = awaitItem() val confirmationState = awaitItem()
assertThat(confirmationState.logoutAction).isEqualTo(AsyncAction.ConfirmingNoParams) assertThat(confirmationState.logoutAction).isEqualTo(AsyncAction.ConfirmingNoParams)
initialState.eventSink.invoke(LogoutEvents.CloseDialogs) initialState.eventSink.invoke(LogoutEvent.CloseDialogs)
val finalState = awaitItem() val finalState = awaitItem()
assertThat(finalState.logoutAction).isEqualTo(AsyncAction.Uninitialized) assertThat(finalState.logoutAction).isEqualTo(AsyncAction.Uninitialized)
} }
@@ -157,10 +157,10 @@ class LogoutPresenterTest {
presenter.present() presenter.present()
}.test { }.test {
val initialState = awaitFirstItem() val initialState = awaitFirstItem()
initialState.eventSink.invoke(LogoutEvents.Logout(ignoreSdkError = false)) initialState.eventSink.invoke(LogoutEvent.Logout(ignoreSdkError = false))
val confirmationState = awaitItem() val confirmationState = awaitItem()
assertThat(confirmationState.logoutAction).isEqualTo(AsyncAction.ConfirmingNoParams) assertThat(confirmationState.logoutAction).isEqualTo(AsyncAction.ConfirmingNoParams)
confirmationState.eventSink.invoke(LogoutEvents.Logout(ignoreSdkError = false)) confirmationState.eventSink.invoke(LogoutEvent.Logout(ignoreSdkError = false))
val loadingState = awaitItem() val loadingState = awaitItem()
assertThat(loadingState.logoutAction).isInstanceOf(AsyncAction.Loading::class.java) assertThat(loadingState.logoutAction).isInstanceOf(AsyncAction.Loading::class.java)
val successState = awaitItem() val successState = awaitItem()
@@ -184,15 +184,15 @@ class LogoutPresenterTest {
presenter.present() presenter.present()
}.test { }.test {
val initialState = awaitFirstItem() val initialState = awaitFirstItem()
initialState.eventSink.invoke(LogoutEvents.Logout(ignoreSdkError = false)) initialState.eventSink.invoke(LogoutEvent.Logout(ignoreSdkError = false))
val confirmationState = awaitItem() val confirmationState = awaitItem()
assertThat(confirmationState.logoutAction).isEqualTo(AsyncAction.ConfirmingNoParams) assertThat(confirmationState.logoutAction).isEqualTo(AsyncAction.ConfirmingNoParams)
confirmationState.eventSink.invoke(LogoutEvents.Logout(ignoreSdkError = false)) confirmationState.eventSink.invoke(LogoutEvent.Logout(ignoreSdkError = false))
val loadingState = awaitItem() val loadingState = awaitItem()
assertThat(loadingState.logoutAction).isInstanceOf(AsyncAction.Loading::class.java) assertThat(loadingState.logoutAction).isInstanceOf(AsyncAction.Loading::class.java)
val errorState = awaitItem() val errorState = awaitItem()
assertThat(errorState.logoutAction).isEqualTo(AsyncAction.Failure(AN_EXCEPTION)) assertThat(errorState.logoutAction).isEqualTo(AsyncAction.Failure(AN_EXCEPTION))
errorState.eventSink.invoke(LogoutEvents.CloseDialogs) errorState.eventSink.invoke(LogoutEvent.CloseDialogs)
val finalState = awaitItem() val finalState = awaitItem()
assertThat(finalState.logoutAction).isEqualTo(AsyncAction.Uninitialized) assertThat(finalState.logoutAction).isEqualTo(AsyncAction.Uninitialized)
} }
@@ -214,15 +214,15 @@ class LogoutPresenterTest {
presenter.present() presenter.present()
}.test { }.test {
val initialState = awaitFirstItem() val initialState = awaitFirstItem()
initialState.eventSink.invoke(LogoutEvents.Logout(ignoreSdkError = false)) initialState.eventSink.invoke(LogoutEvent.Logout(ignoreSdkError = false))
val confirmationState = awaitItem() val confirmationState = awaitItem()
assertThat(confirmationState.logoutAction).isEqualTo(AsyncAction.ConfirmingNoParams) assertThat(confirmationState.logoutAction).isEqualTo(AsyncAction.ConfirmingNoParams)
confirmationState.eventSink.invoke(LogoutEvents.Logout(ignoreSdkError = false)) confirmationState.eventSink.invoke(LogoutEvent.Logout(ignoreSdkError = false))
val loadingState = awaitItem() val loadingState = awaitItem()
assertThat(loadingState.logoutAction).isInstanceOf(AsyncAction.Loading::class.java) assertThat(loadingState.logoutAction).isInstanceOf(AsyncAction.Loading::class.java)
val errorState = awaitItem() val errorState = awaitItem()
assertThat(errorState.logoutAction).isEqualTo(AsyncAction.Failure(AN_EXCEPTION)) assertThat(errorState.logoutAction).isEqualTo(AsyncAction.Failure(AN_EXCEPTION))
errorState.eventSink.invoke(LogoutEvents.Logout(ignoreSdkError = true)) errorState.eventSink.invoke(LogoutEvent.Logout(ignoreSdkError = true))
val loadingState2 = awaitItem() val loadingState2 = awaitItem()
assertThat(loadingState2.logoutAction).isInstanceOf(AsyncAction.Loading::class.java) assertThat(loadingState2.logoutAction).isInstanceOf(AsyncAction.Loading::class.java)
val successState = awaitItem() val successState = awaitItem()
@@ -28,20 +28,20 @@ import org.junit.Test
class LogoutViewTest : RobolectricTest() { class LogoutViewTest : RobolectricTest() {
@Test @Test
fun `clicking on logout sends a LogoutEvents`() = runAndroidComposeUiTest { fun `clicking on logout sends a LogoutEvent`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<LogoutEvents>() val eventsRecorder = EventsRecorder<LogoutEvent>()
setLogoutView( setLogoutView(
aLogoutState( aLogoutState(
eventSink = eventsRecorder eventSink = eventsRecorder
), ),
) )
clickOn(CommonStrings.action_signout) clickOn(CommonStrings.action_signout)
eventsRecorder.assertSingle(LogoutEvents.Logout(false)) eventsRecorder.assertSingle(LogoutEvent.Logout(false))
} }
@Test @Test
fun `confirming logout sends a LogoutEvents`() = runAndroidComposeUiTest { fun `confirming logout sends a LogoutEvent`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<LogoutEvents>() val eventsRecorder = EventsRecorder<LogoutEvent>()
setLogoutView( setLogoutView(
aLogoutState( aLogoutState(
logoutAction = AsyncAction.ConfirmingNoParams, logoutAction = AsyncAction.ConfirmingNoParams,
@@ -49,12 +49,12 @@ class LogoutViewTest : RobolectricTest() {
), ),
) )
pressTag(TestTags.dialogPositive.value) pressTag(TestTags.dialogPositive.value)
eventsRecorder.assertSingle(LogoutEvents.Logout(false)) eventsRecorder.assertSingle(LogoutEvent.Logout(false))
} }
@Test @Test
fun `clicking on back invoke back callback`() = runAndroidComposeUiTest { fun `clicking on back invoke back callback`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<LogoutEvents>(expectEvents = false) val eventsRecorder = EventsRecorder<LogoutEvent>(expectEvents = false)
ensureCalledOnce { callback -> ensureCalledOnce { callback ->
setLogoutView( setLogoutView(
aLogoutState( aLogoutState(
@@ -67,8 +67,8 @@ class LogoutViewTest : RobolectricTest() {
} }
@Test @Test
fun `clicking on confirm after error sends a LogoutEvents`() = runAndroidComposeUiTest { fun `clicking on confirm after error sends a LogoutEvent`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<LogoutEvents>() val eventsRecorder = EventsRecorder<LogoutEvent>()
setLogoutView( setLogoutView(
aLogoutState( aLogoutState(
logoutAction = AsyncAction.Failure(Exception("Failed to logout")), logoutAction = AsyncAction.Failure(Exception("Failed to logout")),
@@ -76,12 +76,12 @@ class LogoutViewTest : RobolectricTest() {
), ),
) )
clickOn(CommonStrings.action_signout_anyway) clickOn(CommonStrings.action_signout_anyway)
eventsRecorder.assertSingle(LogoutEvents.Logout(true)) eventsRecorder.assertSingle(LogoutEvent.Logout(true))
} }
@Test @Test
fun `clicking on cancel after error sends a LogoutEvents`() = runAndroidComposeUiTest { fun `clicking on cancel after error sends a LogoutEvent`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<LogoutEvents>() val eventsRecorder = EventsRecorder<LogoutEvent>()
setLogoutView( setLogoutView(
aLogoutState( aLogoutState(
logoutAction = AsyncAction.Failure(Exception("Failed to logout")), logoutAction = AsyncAction.Failure(Exception("Failed to logout")),
@@ -89,12 +89,12 @@ class LogoutViewTest : RobolectricTest() {
), ),
) )
clickOn(CommonStrings.action_cancel) clickOn(CommonStrings.action_cancel)
eventsRecorder.assertSingle(LogoutEvents.CloseDialogs) eventsRecorder.assertSingle(LogoutEvent.CloseDialogs)
} }
@Test @Test
fun `last session setting button invoke onChangeRecoveryKeyClicked`() = runAndroidComposeUiTest { fun `last session setting button invoke onChangeRecoveryKeyClicked`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<LogoutEvents>(expectEvents = false) val eventsRecorder = EventsRecorder<LogoutEvent>(expectEvents = false)
ensureCalledOnce { callback -> ensureCalledOnce { callback ->
setLogoutView( setLogoutView(
aLogoutState( aLogoutState(