From f057d041c17870f84f1b4a29231ef6835d8bbb76 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 19 Sep 2024 19:55:27 +0200 Subject: [PATCH] Add test on RustNotificationSettingsService --- .../factories/RoomNotificationSettings.kt | 19 +++++++++ .../fakes/FakeRustNotificationSettings.kt | 13 +++++- .../RustNotificationSettingsServiceTest.kt | 42 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/factories/RoomNotificationSettings.kt create mode 100644 libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsServiceTest.kt diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/factories/RoomNotificationSettings.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/factories/RoomNotificationSettings.kt new file mode 100644 index 0000000000..6324b28811 --- /dev/null +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/factories/RoomNotificationSettings.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2024 New Vector Ltd. + * + * SPDX-License-Identifier: AGPL-3.0-only + * Please see LICENSE in the repository root for full details. + */ + +package io.element.android.libraries.matrix.impl.fixtures.factories + +import org.matrix.rustcomponents.sdk.RoomNotificationMode +import org.matrix.rustcomponents.sdk.RoomNotificationSettings + +fun aRustRoomNotificationSettings( + mode: RoomNotificationMode = RoomNotificationMode.ALL_MESSAGES, + isDefault: Boolean = true, +) = RoomNotificationSettings( + mode = mode, + isDefault = isDefault +) diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustNotificationSettings.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustNotificationSettings.kt index a889530cb2..4b0acbfc08 100644 --- a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustNotificationSettings.kt +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustNotificationSettings.kt @@ -7,14 +7,25 @@ package io.element.android.libraries.matrix.impl.fixtures.fakes +import io.element.android.libraries.matrix.impl.fixtures.factories.aRustRoomNotificationSettings import org.matrix.rustcomponents.sdk.NoPointer import org.matrix.rustcomponents.sdk.NotificationSettings import org.matrix.rustcomponents.sdk.NotificationSettingsDelegate +import org.matrix.rustcomponents.sdk.RoomNotificationSettings -class FakeRustNotificationSettings : NotificationSettings(NoPointer) { +class FakeRustNotificationSettings( + private val roomNotificationSettings: RoomNotificationSettings = aRustRoomNotificationSettings(), +) : NotificationSettings(NoPointer) { private var delegate: NotificationSettingsDelegate? = null override fun setDelegate(delegate: NotificationSettingsDelegate?) { this.delegate = delegate } + + override suspend fun getRoomNotificationSettings( + roomId: String, + isEncrypted: Boolean, + isOneToOne: Boolean, + ): RoomNotificationSettings = roomNotificationSettings } + diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsServiceTest.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsServiceTest.kt new file mode 100644 index 0000000000..01b2c1f3ab --- /dev/null +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsServiceTest.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2024 New Vector Ltd. + * + * SPDX-License-Identifier: AGPL-3.0-only + * Please see LICENSE in the repository root for full details. + */ + +package io.element.android.libraries.matrix.impl.notificationsettings + +import com.google.common.truth.Truth.assertThat +import io.element.android.libraries.matrix.api.room.RoomNotificationMode +import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeRustClient +import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeRustNotificationSettings +import io.element.android.libraries.matrix.test.A_ROOM_ID +import io.element.android.tests.testutils.testCoroutineDispatchers +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.runTest +import org.junit.Test +import org.matrix.rustcomponents.sdk.NotificationSettings + +class RustNotificationSettingsServiceTest { + @Test + fun test() = runTest { + val sut = createRustNotificationSettingsService() + val result = sut.getRoomNotificationSettings( + roomId = A_ROOM_ID, + isEncrypted = true, + isOneToOne = true, + ).getOrNull()!! + assertThat(result.mode).isEqualTo(RoomNotificationMode.ALL_MESSAGES) + assertThat(result.isDefault).isTrue() + } + + private fun TestScope.createRustNotificationSettingsService( + notificationSettings: NotificationSettings = FakeRustNotificationSettings(), + ) = RustNotificationSettingsService( + client = FakeRustClient( + notificationSettings = notificationSettings, + ), + dispatchers = testCoroutineDispatchers(), + ) +}