Ensure in test that rooms are sorted.

This commit is contained in:
Benoit Marty 2025-11-19 09:59:22 +01:00
parent a64fe57ec6
commit 88f0111137
2 changed files with 85 additions and 7 deletions

View file

@ -13,6 +13,8 @@ import io.element.android.features.preferences.impl.notifications.edit.EditDefau
import io.element.android.features.preferences.impl.notifications.edit.EditDefaultNotificationSettingStateEvents import io.element.android.features.preferences.impl.notifications.edit.EditDefaultNotificationSettingStateEvents
import io.element.android.libraries.matrix.api.room.RoomNotificationMode import io.element.android.libraries.matrix.api.room.RoomNotificationMode
import io.element.android.libraries.matrix.test.AN_EXCEPTION import io.element.android.libraries.matrix.test.AN_EXCEPTION
import io.element.android.libraries.matrix.test.A_ROOM_ID
import io.element.android.libraries.matrix.test.A_ROOM_ID_2
import io.element.android.libraries.matrix.test.notificationsettings.FakeNotificationSettingsService import io.element.android.libraries.matrix.test.notificationsettings.FakeNotificationSettingsService
import io.element.android.libraries.matrix.test.room.aRoomSummary import io.element.android.libraries.matrix.test.room.aRoomSummary
import io.element.android.libraries.matrix.test.roomlist.FakeRoomListService import io.element.android.libraries.matrix.test.roomlist.FakeRoomListService
@ -25,12 +27,15 @@ import org.junit.Test
class EditDefaultNotificationSettingsPresenterTest { class EditDefaultNotificationSettingsPresenterTest {
@Test @Test
fun `present - ensures initial state is correct`() = runTest { fun `present - ensures initial state is correct`() = runTest {
val notificationSettingsService = FakeNotificationSettingsService() val notificationSettingsService = FakeNotificationSettingsService(
getRoomsWithUserDefinedRulesResult = { Result.success(emptyList()) },
)
val presenter = createEditDefaultNotificationSettingPresenter(notificationSettingsService) val presenter = createEditDefaultNotificationSettingPresenter(notificationSettingsService)
presenter.test { presenter.test {
val initialState = awaitItem() val initialState = awaitItem()
assertThat(initialState.mode).isNull() assertThat(initialState.mode).isNull()
assertThat(initialState.isOneToOne).isFalse() assertThat(initialState.isOneToOne).isFalse()
assertThat(initialState.roomsWithUserDefinedMode).isEmpty()
val loadedState = consumeItemsUntilPredicate { val loadedState = consumeItemsUntilPredicate {
it.mode == RoomNotificationMode.MENTIONS_AND_KEYWORDS_ONLY it.mode == RoomNotificationMode.MENTIONS_AND_KEYWORDS_ONLY
@ -45,7 +50,8 @@ class EditDefaultNotificationSettingsPresenterTest {
fun `present - ensure list of rooms with user defined mode`() = runTest { fun `present - ensure list of rooms with user defined mode`() = runTest {
val notificationSettingsService = FakeNotificationSettingsService( val notificationSettingsService = FakeNotificationSettingsService(
initialRoomMode = RoomNotificationMode.ALL_MESSAGES, initialRoomMode = RoomNotificationMode.ALL_MESSAGES,
initialRoomModeIsDefault = false initialRoomModeIsDefault = false,
getRoomsWithUserDefinedRulesResult = { Result.success(listOf(A_ROOM_ID.value)) },
) )
val roomListService = FakeRoomListService() val roomListService = FakeRoomListService()
val presenter = createEditDefaultNotificationSettingPresenter(notificationSettingsService, roomListService) val presenter = createEditDefaultNotificationSettingPresenter(notificationSettingsService, roomListService)
@ -58,9 +64,77 @@ class EditDefaultNotificationSettingsPresenterTest {
} }
} }
@Test
fun `present - ensure list of rooms is sorted`() = runTest {
val notificationSettingsService = FakeNotificationSettingsService(
initialRoomMode = RoomNotificationMode.MENTIONS_AND_KEYWORDS_ONLY,
initialRoomModeIsDefault = false,
getRoomsWithUserDefinedRulesResult = { Result.success(listOf(A_ROOM_ID.value, A_ROOM_ID_2.value)) },
)
val roomListService = FakeRoomListService()
val presenter = createEditDefaultNotificationSettingPresenter(notificationSettingsService, roomListService)
presenter.test {
roomListService.postAllRooms(
listOf(
aRoomSummary(
roomId = A_ROOM_ID,
name = "Z",
userDefinedNotificationMode = RoomNotificationMode.MENTIONS_AND_KEYWORDS_ONLY,
),
aRoomSummary(
roomId = A_ROOM_ID_2,
name = "A",
userDefinedNotificationMode = RoomNotificationMode.MENTIONS_AND_KEYWORDS_ONLY,
),
),
)
val loadedState = consumeItemsUntilPredicate { state ->
state.roomsWithUserDefinedMode.any { it.notificationMode == RoomNotificationMode.MENTIONS_AND_KEYWORDS_ONLY }
}.last()
assertThat(loadedState.roomsWithUserDefinedMode[0].name).isEqualTo("A")
assertThat(loadedState.roomsWithUserDefinedMode[1].name).isEqualTo("Z")
}
}
@Test
fun `present - ensure list of rooms is sorted, with name null`() = runTest {
val notificationSettingsService = FakeNotificationSettingsService(
initialRoomMode = RoomNotificationMode.MUTE,
initialRoomModeIsDefault = false,
getRoomsWithUserDefinedRulesResult = { Result.success(listOf(A_ROOM_ID.value, A_ROOM_ID_2.value)) },
)
val roomListService = FakeRoomListService()
val presenter = createEditDefaultNotificationSettingPresenter(notificationSettingsService, roomListService)
presenter.test {
roomListService.postAllRooms(
listOf(
aRoomSummary(
roomId = A_ROOM_ID,
name = "Z",
userDefinedNotificationMode = RoomNotificationMode.MUTE,
),
aRoomSummary(
roomId = A_ROOM_ID_2,
name = null,
userDefinedNotificationMode = RoomNotificationMode.MUTE,
),
),
)
val loadedState = consumeItemsUntilPredicate { state ->
state.roomsWithUserDefinedMode.any { it.notificationMode == RoomNotificationMode.MUTE }
}.last()
assertThat(loadedState.roomsWithUserDefinedMode[0].name).isNull()
assertThat(loadedState.roomsWithUserDefinedMode[1].name).isEqualTo("Z")
}
}
@Test @Test
fun `present - edit default notification setting`() = runTest { fun `present - edit default notification setting`() = runTest {
val presenter = createEditDefaultNotificationSettingPresenter() val presenter = createEditDefaultNotificationSettingPresenter(
notificationSettingsService = FakeNotificationSettingsService(
getRoomsWithUserDefinedRulesResult = { Result.success(emptyList()) },
),
)
presenter.test { presenter.test {
awaitItem().eventSink(EditDefaultNotificationSettingStateEvents.SetNotificationMode(RoomNotificationMode.ALL_MESSAGES)) awaitItem().eventSink(EditDefaultNotificationSettingStateEvents.SetNotificationMode(RoomNotificationMode.ALL_MESSAGES))
val loadedState = consumeItemsUntilPredicate { val loadedState = consumeItemsUntilPredicate {
@ -72,7 +146,9 @@ class EditDefaultNotificationSettingsPresenterTest {
@Test @Test
fun `present - edit default notification setting failed`() = runTest { fun `present - edit default notification setting failed`() = runTest {
val notificationSettingsService = FakeNotificationSettingsService() val notificationSettingsService = FakeNotificationSettingsService(
getRoomsWithUserDefinedRulesResult = { Result.success(emptyList()) },
)
val presenter = createEditDefaultNotificationSettingPresenter(notificationSettingsService) val presenter = createEditDefaultNotificationSettingPresenter(notificationSettingsService)
notificationSettingsService.givenSetDefaultNotificationModeError(AN_EXCEPTION) notificationSettingsService.givenSetDefaultNotificationModeError(AN_EXCEPTION)
presenter.test { presenter.test {
@ -91,7 +167,9 @@ class EditDefaultNotificationSettingsPresenterTest {
@Test @Test
fun `present - display mentions only warning if homeserver does not support it`() = runTest { fun `present - display mentions only warning if homeserver does not support it`() = runTest {
val notificationSettingsService = FakeNotificationSettingsService().apply { val notificationSettingsService = FakeNotificationSettingsService(
getRoomsWithUserDefinedRulesResult = { Result.success(emptyList()) },
).apply {
givenCanHomeServerPushEncryptedEventsToDeviceResult(Result.success(false)) givenCanHomeServerPushEncryptedEventsToDeviceResult(Result.success(false))
} }
val presenter = createEditDefaultNotificationSettingPresenter(notificationSettingsService) val presenter = createEditDefaultNotificationSettingPresenter(notificationSettingsService)

View file

@ -12,7 +12,6 @@ import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.notificationsettings.NotificationSettingsService import io.element.android.libraries.matrix.api.notificationsettings.NotificationSettingsService
import io.element.android.libraries.matrix.api.room.RoomNotificationMode import io.element.android.libraries.matrix.api.room.RoomNotificationMode
import io.element.android.libraries.matrix.api.room.RoomNotificationSettings import io.element.android.libraries.matrix.api.room.RoomNotificationSettings
import io.element.android.libraries.matrix.test.A_ROOM_ID
import io.element.android.libraries.matrix.test.A_ROOM_NOTIFICATION_MODE import io.element.android.libraries.matrix.test.A_ROOM_NOTIFICATION_MODE
import io.element.android.tests.testutils.lambda.lambdaError import io.element.android.tests.testutils.lambda.lambdaError
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@ -26,6 +25,7 @@ class FakeNotificationSettingsService(
initialOneToOneDefaultMode: RoomNotificationMode = RoomNotificationMode.ALL_MESSAGES, initialOneToOneDefaultMode: RoomNotificationMode = RoomNotificationMode.ALL_MESSAGES,
initialEncryptedOneToOneDefaultMode: RoomNotificationMode = RoomNotificationMode.ALL_MESSAGES, initialEncryptedOneToOneDefaultMode: RoomNotificationMode = RoomNotificationMode.ALL_MESSAGES,
private val getRawPushRulesResult: () -> Result<String> = { lambdaError() }, private val getRawPushRulesResult: () -> Result<String> = { lambdaError() },
private val getRoomsWithUserDefinedRulesResult: () -> Result<List<String>> = { lambdaError() },
) : NotificationSettingsService { ) : NotificationSettingsService {
private val notificationSettingsStateFlow = MutableStateFlow(Unit) private val notificationSettingsStateFlow = MutableStateFlow(Unit)
private var defaultGroupRoomNotificationMode: RoomNotificationMode = initialGroupDefaultMode private var defaultGroupRoomNotificationMode: RoomNotificationMode = initialGroupDefaultMode
@ -155,7 +155,7 @@ class FakeNotificationSettingsService(
} }
override suspend fun getRoomsWithUserDefinedRules(): Result<List<String>> { override suspend fun getRoomsWithUserDefinedRules(): Result<List<String>> {
return Result.success(if (roomNotificationModeIsDefault) listOf() else listOf(A_ROOM_ID.value)) return getRoomsWithUserDefinedRulesResult()
} }
override suspend fun canHomeServerPushEncryptedEventsToDevice(): Result<Boolean> { override suspend fun canHomeServerPushEncryptedEventsToDevice(): Result<Boolean> {