Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup tests #3645

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ interface StartDMAction {
* @param userId The user to start a DM with.
* @param actionState The state to update with the result of the action.
*/
suspend fun execute(userId: UserId, actionState: MutableState<AsyncAction<RoomId>>)
suspend fun execute(userId: UserId, actionState: MutableState<AsyncAction<Unit, RoomId>>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DefaultStartDMAction @Inject constructor(
private val matrixClient: MatrixClient,
private val analyticsService: AnalyticsService,
) : StartDMAction {
override suspend fun execute(userId: UserId, actionState: MutableState<AsyncAction<RoomId>>) {
override suspend fun execute(userId: UserId, actionState: MutableState<AsyncAction<Unit, RoomId>>) {
actionState.value = AsyncAction.Loading
when (val result = matrixClient.startDM(userId)) {
is StartDMResult.Success -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ConfigureRoomPresenter @Inject constructor(
}

val localCoroutineScope = rememberCoroutineScope()
val createRoomAction: MutableState<AsyncAction<RoomId>> = remember { mutableStateOf(AsyncAction.Uninitialized) }
val createRoomAction: MutableState<AsyncAction<Unit, RoomId>> = remember { mutableStateOf(AsyncAction.Uninitialized) }

fun createRoom(config: CreateRoomConfig) {
createRoomAction.value = AsyncAction.Uninitialized
Expand Down Expand Up @@ -123,7 +123,7 @@ class ConfigureRoomPresenter @Inject constructor(

private fun CoroutineScope.createRoom(
config: CreateRoomConfig,
createRoomAction: MutableState<AsyncAction<RoomId>>
createRoomAction: MutableState<AsyncAction<Unit, RoomId>>
) = launch {
suspend {
val avatarUrl = config.avatarUri?.let { uploadAvatar(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlinx.collections.immutable.ImmutableList
data class ConfigureRoomState(
val config: CreateRoomConfig,
val avatarActions: ImmutableList<AvatarAction>,
val createRoomAction: AsyncAction<RoomId>,
val createRoomAction: AsyncAction<Unit, RoomId>,
val cameraPermissionState: PermissionsState,
val eventSink: (ConfigureRoomEvents) -> Unit
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CreateRoomRootPresenter @Inject constructor(
val userListState = presenter.present()

val localCoroutineScope = rememberCoroutineScope()
val startDmActionState: MutableState<AsyncAction<RoomId>> = remember { mutableStateOf(AsyncAction.Uninitialized) }
val startDmActionState: MutableState<AsyncAction<Unit, RoomId>> = remember { mutableStateOf(AsyncAction.Uninitialized) }

fun handleEvents(event: CreateRoomRootEvents) {
when (event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ import io.element.android.libraries.matrix.api.core.RoomId
data class CreateRoomRootState(
val applicationName: String,
val userListState: UserListState,
val startDmAction: AsyncAction<RoomId>,
val startDmAction: AsyncAction<Unit, RoomId>,
val eventSink: (CreateRoomRootEvents) -> Unit,
)
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ open class CreateRoomRootStateProvider : PreviewParameterProvider<CreateRoomRoot
fun aCreateRoomRootState(
applicationName: String = "Element X Preview",
userListState: UserListState = aUserListState(),
startDmAction: AsyncAction<RoomId> = AsyncAction.Uninitialized,
startDmAction: AsyncAction<Unit, RoomId> = AsyncAction.Uninitialized,
eventSink: (CreateRoomRootEvents) -> Unit = {},
) = CreateRoomRootState(
applicationName = applicationName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DefaultStartDMActionTest {
givenFindDmResult(A_ROOM_ID)
}
val action = createStartDMAction(matrixClient)
val state = mutableStateOf<AsyncAction<RoomId>>(AsyncAction.Uninitialized)
val state = mutableStateOf<AsyncAction<Unit, RoomId>>(AsyncAction.Uninitialized)
action.execute(A_USER_ID, state)
assertThat(state.value).isEqualTo(AsyncAction.Success(A_ROOM_ID))
}
Expand All @@ -42,7 +42,7 @@ class DefaultStartDMActionTest {
}
val analyticsService = FakeAnalyticsService()
val action = createStartDMAction(matrixClient, analyticsService)
val state = mutableStateOf<AsyncAction<RoomId>>(AsyncAction.Uninitialized)
val state = mutableStateOf<AsyncAction<Unit, RoomId>>(AsyncAction.Uninitialized)
action.execute(A_USER_ID, state)
assertThat(state.value).isEqualTo(AsyncAction.Success(A_ROOM_ID))
assertThat(analyticsService.capturedEvents).containsExactly(CreatedRoom(isDM = true))
Expand All @@ -55,7 +55,7 @@ class DefaultStartDMActionTest {
givenCreateDmResult(Result.failure(A_THROWABLE))
}
val action = createStartDMAction(matrixClient)
val state = mutableStateOf<AsyncAction<RoomId>>(AsyncAction.Uninitialized)
val state = mutableStateOf<AsyncAction<Unit, RoomId>>(AsyncAction.Uninitialized)
action.execute(A_USER_ID, state)
assertThat(state.value).isEqualTo(AsyncAction.Failure(A_THROWABLE))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import io.element.android.libraries.matrix.test.A_ROOM_ID
import kotlinx.coroutines.delay

class FakeStartDMAction : StartDMAction {
private var executeResult: AsyncAction<RoomId> = AsyncAction.Success(A_ROOM_ID)
private var executeResult: AsyncAction<Unit, RoomId> = AsyncAction.Success(A_ROOM_ID)

fun givenExecuteResult(result: AsyncAction<RoomId>) {
fun givenExecuteResult(result: AsyncAction<Unit, RoomId>) {
executeResult = result
}

override suspend fun execute(userId: UserId, actionState: MutableState<AsyncAction<RoomId>>) {
override suspend fun execute(userId: UserId, actionState: MutableState<AsyncAction<Unit, RoomId>>) {
actionState.value = AsyncAction.Loading
delay(1)
actionState.value = executeResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AccountDeactivationPresenter @Inject constructor(
@Composable
override fun present(): AccountDeactivationState {
val localCoroutineScope = rememberCoroutineScope()
val action: MutableState<AsyncAction<Unit>> = remember {
val action: MutableState<AsyncAction<Unit, Unit>> = remember {
mutableStateOf(AsyncAction.Uninitialized)
}

Expand All @@ -51,7 +51,7 @@ class AccountDeactivationPresenter @Inject constructor(
action
)
} else {
action.value = AsyncAction.Confirming
action.value = AsyncAction.Confirming(Unit)
}
AccountDeactivationEvents.CloseDialogs -> {
action.value = AsyncAction.Uninitialized
Expand All @@ -72,7 +72,7 @@ class AccountDeactivationPresenter @Inject constructor(

private fun CoroutineScope.deactivateAccount(
formState: DeactivateFormState,
action: MutableState<AsyncAction<Unit>>,
action: MutableState<AsyncAction<Unit, Unit>>,
) = launch {
suspend {
matrixClient.deactivateAccount(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlinx.parcelize.Parcelize

data class AccountDeactivationState(
val deactivateFormState: DeactivateFormState,
val accountDeactivationAction: AsyncAction<Unit>,
val accountDeactivationAction: AsyncAction<Unit, Unit>,
val eventSink: (AccountDeactivationEvents) -> Unit,
) {
val submitEnabled: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class AccountDeactivationStateProvider : PreviewParameterProvider<AccountDe
),
anAccountDeactivationState(
deactivateFormState = filledForm,
accountDeactivationAction = AsyncAction.Confirming,
accountDeactivationAction = AsyncAction.Confirming(Unit),
),
anAccountDeactivationState(
deactivateFormState = filledForm,
Expand All @@ -43,7 +43,7 @@ internal fun aDeactivateFormState(

internal fun anAccountDeactivationState(
deactivateFormState: DeactivateFormState = aDeactivateFormState(),
accountDeactivationAction: AsyncAction<Unit> = AsyncAction.Uninitialized,
accountDeactivationAction: AsyncAction<Unit, Unit> = AsyncAction.Uninitialized,
eventSink: (AccountDeactivationEvents) -> Unit = {},
) = AccountDeactivationState(
deactivateFormState = deactivateFormState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import io.element.android.libraries.ui.strings.CommonStrings

@Composable
fun AccountDeactivationActionDialog(
state: AsyncAction<Unit>,
state: AsyncAction<Unit, Unit>,
onConfirmClick: () -> Unit,
onRetryClick: () -> Unit,
onDismissDialog: () -> Unit,
) {
when (state) {
AsyncAction.Uninitialized ->
Unit
AsyncAction.Confirming ->
is AsyncAction.Confirming ->
AccountDeactivationConfirmationDialog(
onSubmitClick = onConfirmClick,
onDismiss = onDismissDialog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AccountDeactivationPresenterTest {
skipItems(1)
initialState.eventSink(AccountDeactivationEvents.DeactivateAccount(isRetry = false))
val updatedState = awaitItem()
assertThat(updatedState.accountDeactivationAction).isEqualTo(AsyncAction.Confirming)
assertThat(updatedState.accountDeactivationAction).isEqualTo(AsyncAction.Confirming(Unit))
updatedState.eventSink(AccountDeactivationEvents.DeactivateAccount(isRetry = false))
val updatedState2 = awaitItem()
assertThat(updatedState2.accountDeactivationAction).isEqualTo(AsyncAction.Loading)
Expand Down Expand Up @@ -102,7 +102,7 @@ class AccountDeactivationPresenterTest {
skipItems(2)
initialState.eventSink(AccountDeactivationEvents.DeactivateAccount(isRetry = false))
val updatedState = awaitItem()
assertThat(updatedState.accountDeactivationAction).isEqualTo(AsyncAction.Confirming)
assertThat(updatedState.accountDeactivationAction).isEqualTo(AsyncAction.Confirming(Unit))
updatedState.eventSink(AccountDeactivationEvents.DeactivateAccount(isRetry = false))
val updatedState2 = awaitItem()
assertThat(updatedState2.accountDeactivationAction).isEqualTo(AsyncAction.Loading)
Expand Down Expand Up @@ -135,7 +135,7 @@ class AccountDeactivationPresenterTest {
skipItems(2)
initialState.eventSink(AccountDeactivationEvents.DeactivateAccount(isRetry = false))
val updatedState = awaitItem()
assertThat(updatedState.accountDeactivationAction).isEqualTo(AsyncAction.Confirming)
assertThat(updatedState.accountDeactivationAction).isEqualTo(AsyncAction.Confirming(Unit))
updatedState.eventSink(AccountDeactivationEvents.DeactivateAccount(isRetry = false))
val updatedState2 = awaitItem()
assertThat(updatedState2.accountDeactivationAction).isEqualTo(AsyncAction.Loading)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class AccountDeactivationViewTest {
deactivateFormState = aDeactivateFormState(
password = A_PASSWORD,
),
accountDeactivationAction = AsyncAction.Confirming,
accountDeactivationAction = AsyncAction.Confirming(Unit),
eventSink = eventsRecorder,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ package io.element.android.features.invite.api.response

import io.element.android.libraries.architecture.AsyncAction
import io.element.android.libraries.matrix.api.core.RoomId
import java.util.Optional

data class AcceptDeclineInviteState(
val invite: Optional<InviteData>,
val acceptAction: AsyncAction<RoomId>,
val declineAction: AsyncAction<RoomId>,
val acceptAction: AsyncAction<Unit, RoomId>,
val declineAction: AsyncAction<InviteData, RoomId>,
val eventSink: (AcceptDeclineInviteEvents) -> Unit,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,20 @@ package io.element.android.features.invite.api.response
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import io.element.android.libraries.architecture.AsyncAction
import io.element.android.libraries.matrix.api.core.RoomId
import java.util.Optional

open class AcceptDeclineInviteStateProvider : PreviewParameterProvider<AcceptDeclineInviteState> {
override val values: Sequence<AcceptDeclineInviteState>
get() = sequenceOf(
anAcceptDeclineInviteState(),
anAcceptDeclineInviteState(
invite = Optional.of(
InviteData(RoomId("!room:matrix.org"), isDm = true, roomName = "Alice"),
declineAction = AsyncAction.Confirming(
InviteData(RoomId("!room:matrix.org"), isDm = true, roomName = "Alice")
),
declineAction = AsyncAction.Confirming,
),
anAcceptDeclineInviteState(
invite = Optional.of(
InviteData(RoomId("!room:matrix.org"), isDm = false, roomName = "Some room"),
declineAction = AsyncAction.Confirming(
InviteData(RoomId("!room:matrix.org"), isDm = false, roomName = "Some room")
),
declineAction = AsyncAction.Confirming,
),
anAcceptDeclineInviteState(
acceptAction = AsyncAction.Failure(Throwable("Whoops")),
Expand All @@ -38,12 +35,10 @@ open class AcceptDeclineInviteStateProvider : PreviewParameterProvider<AcceptDec
}

fun anAcceptDeclineInviteState(
invite: Optional<InviteData> = Optional.empty(),
acceptAction: AsyncAction<RoomId> = AsyncAction.Uninitialized,
declineAction: AsyncAction<RoomId> = AsyncAction.Uninitialized,
acceptAction: AsyncAction<Unit, RoomId> = AsyncAction.Uninitialized,
declineAction: AsyncAction<InviteData, RoomId> = AsyncAction.Uninitialized,
eventSink: (AcceptDeclineInviteEvents) -> Unit = {}
) = AcceptDeclineInviteState(
invite = invite,
acceptAction = acceptAction,
declineAction = declineAction,
eventSink = eventSink,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ package io.element.android.features.invite.impl.response

import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import im.vector.app.features.analytics.plan.JoinedRoom
import io.element.android.features.invite.api.response.AcceptDeclineInviteEvents
import io.element.android.features.invite.api.response.AcceptDeclineInviteState
Expand All @@ -29,9 +27,7 @@ import io.element.android.libraries.matrix.api.room.join.JoinRoom
import io.element.android.libraries.push.api.notifications.NotificationCleaner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.util.Optional
import javax.inject.Inject
import kotlin.jvm.optionals.getOrNull

class AcceptDeclineInvitePresenter @Inject constructor(
private val client: MatrixClient,
Expand All @@ -41,37 +37,24 @@ class AcceptDeclineInvitePresenter @Inject constructor(
@Composable
override fun present(): AcceptDeclineInviteState {
val localCoroutineScope = rememberCoroutineScope()
val acceptedAction: MutableState<AsyncAction<RoomId>> = remember { mutableStateOf(AsyncAction.Uninitialized) }
val declinedAction: MutableState<AsyncAction<RoomId>> = remember { mutableStateOf(AsyncAction.Uninitialized) }
var currentInvite by remember {
mutableStateOf<Optional<InviteData>>(Optional.empty())
}
val acceptedAction: MutableState<AsyncAction<Unit, RoomId>> = remember { mutableStateOf(AsyncAction.Uninitialized) }
val declinedAction: MutableState<AsyncAction<InviteData, RoomId>> = remember { mutableStateOf(AsyncAction.Uninitialized) }

fun handleEvents(event: AcceptDeclineInviteEvents) {
when (event) {
is AcceptDeclineInviteEvents.AcceptInvite -> {
// currentInvite is used to render the decline confirmation dialog
// and to reuse the roomId when the user confirm the rejection of the invitation.
// Just set it to empty here.
currentInvite = Optional.empty()
localCoroutineScope.acceptInvite(event.invite.roomId, acceptedAction)
}

is AcceptDeclineInviteEvents.DeclineInvite -> {
currentInvite = Optional.of(event.invite)
declinedAction.value = AsyncAction.Confirming
declinedAction.value = AsyncAction.Confirming(event.invite)
}

is InternalAcceptDeclineInviteEvents.ConfirmDeclineInvite -> {
declinedAction.value = AsyncAction.Uninitialized
currentInvite.getOrNull()?.let {
localCoroutineScope.declineInvite(it.roomId, declinedAction)
}
currentInvite = Optional.empty()
localCoroutineScope.declineInvite(event.roomId, declinedAction)
}

is InternalAcceptDeclineInviteEvents.CancelDeclineInvite -> {
currentInvite = Optional.empty()
declinedAction.value = AsyncAction.Uninitialized
}

Expand All @@ -86,7 +69,6 @@ class AcceptDeclineInvitePresenter @Inject constructor(
}

return AcceptDeclineInviteState(
invite = currentInvite,
acceptAction = acceptedAction.value,
declineAction = declinedAction.value,
eventSink = ::handleEvents
Expand All @@ -95,7 +77,7 @@ class AcceptDeclineInvitePresenter @Inject constructor(

private fun CoroutineScope.acceptInvite(
roomId: RoomId,
acceptedAction: MutableState<AsyncAction<RoomId>>,
acceptedAction: MutableState<AsyncAction<Unit, RoomId>>,
) = launch {
acceptedAction.runUpdatingState {
joinRoom(
Expand All @@ -110,7 +92,7 @@ class AcceptDeclineInvitePresenter @Inject constructor(
}
}

private fun CoroutineScope.declineInvite(roomId: RoomId, declinedAction: MutableState<AsyncAction<RoomId>>) = launch {
private fun CoroutineScope.declineInvite(roomId: RoomId, declinedAction: MutableState<AsyncAction<InviteData, RoomId>>) = launch {
suspend {
client.getInvitedRoom(roomId)?.use {
it.declineInvite().getOrThrow()
Expand Down
Loading
Loading