Skip to content

Add more tests, particularly to the room list diffing #1508

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ sealed class AuthenticationException(message: String) : Exception(message) {
class SessionMissing(message: String) : AuthenticationException(message)
class Generic(message: String) : AuthenticationException(message)
data class OidcError(val type: String, override val message: String) : AuthenticationException(message)

override fun equals(other: Any?): Boolean {
return when {
this === other -> true
other !is AuthenticationException -> false
else -> this::class.java == other::class.java && message == other.message
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ import io.element.android.libraries.matrix.api.auth.AuthenticationException
import org.matrix.rustcomponents.sdk.AuthenticationException as RustAuthenticationException

fun Throwable.mapAuthenticationException(): AuthenticationException {
val message = this.message ?: "Unknown error"
return when (this) {
is RustAuthenticationException.ClientMissing -> AuthenticationException.ClientMissing(this.message!!)
is RustAuthenticationException.Generic -> AuthenticationException.Generic(this.message!!)
is RustAuthenticationException.InvalidServerName -> AuthenticationException.InvalidServerName(this.message!!)
is RustAuthenticationException.SessionMissing -> AuthenticationException.SessionMissing(this.message!!)
is RustAuthenticationException.SlidingSyncNotAvailable -> AuthenticationException.SlidingSyncNotAvailable(this.message!!)
is RustAuthenticationException.OidcException -> AuthenticationException.OidcError("OidcException", message!!)
is RustAuthenticationException.OidcMetadataInvalid -> AuthenticationException.OidcError("OidcMetadataInvalid", message!!)
is RustAuthenticationException.OidcMetadataMissing -> AuthenticationException.OidcError("OidcMetadataMissing", message!!)
is RustAuthenticationException.OidcNotSupported -> AuthenticationException.OidcError("OidcNotSupported", message!!)
is RustAuthenticationException.OidcCancelled -> AuthenticationException.OidcError("OidcCancelled", message!!)
is RustAuthenticationException.OidcCallbackUrlInvalid -> AuthenticationException.OidcError("OidcCallbackUrlInvalid", message!!)
else -> AuthenticationException.Generic(this.message ?: "Unknown error")
is RustAuthenticationException.ClientMissing -> AuthenticationException.ClientMissing(message)
is RustAuthenticationException.Generic -> AuthenticationException.Generic(message)
is RustAuthenticationException.InvalidServerName -> AuthenticationException.InvalidServerName(message)
is RustAuthenticationException.SessionMissing -> AuthenticationException.SessionMissing(message)
is RustAuthenticationException.SlidingSyncNotAvailable -> AuthenticationException.SlidingSyncNotAvailable(message)
is RustAuthenticationException.OidcException -> AuthenticationException.OidcError("OidcException", message)
is RustAuthenticationException.OidcMetadataInvalid -> AuthenticationException.OidcError("OidcMetadataInvalid", message)
is RustAuthenticationException.OidcMetadataMissing -> AuthenticationException.OidcError("OidcMetadataMissing", message)
is RustAuthenticationException.OidcNotSupported -> AuthenticationException.OidcError("OidcNotSupported", message)
is RustAuthenticationException.OidcCancelled -> AuthenticationException.OidcError("OidcCancelled", message)
is RustAuthenticationException.OidcCallbackUrlInvalid -> AuthenticationException.OidcError("OidcCallbackUrlInvalid", message)
else -> AuthenticationException.Generic(message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.catch
import org.matrix.rustcomponents.sdk.RoomList
import org.matrix.rustcomponents.sdk.RoomListEntriesListener
import org.matrix.rustcomponents.sdk.RoomListEntriesUpdate
import org.matrix.rustcomponents.sdk.RoomListEntry
import org.matrix.rustcomponents.sdk.RoomListInterface
import org.matrix.rustcomponents.sdk.RoomListItem
import org.matrix.rustcomponents.sdk.RoomListLoadingState
import org.matrix.rustcomponents.sdk.RoomListLoadingStateListener
import org.matrix.rustcomponents.sdk.RoomListService
import org.matrix.rustcomponents.sdk.RoomListServiceInterface
import org.matrix.rustcomponents.sdk.RoomListServiceState
import org.matrix.rustcomponents.sdk.RoomListServiceStateListener
import org.matrix.rustcomponents.sdk.RoomListServiceSyncIndicator
Expand All @@ -40,7 +40,7 @@ import timber.log.Timber
private const val SYNC_INDICATOR_DELAY_BEFORE_SHOWING = 1000u
private const val SYNC_INDICATOR_DELAY_BEFORE_HIDING = 0u

fun RoomList.loadingStateFlow(): Flow<RoomListLoadingState> =
fun RoomListInterface.loadingStateFlow(): Flow<RoomListLoadingState> =
mxCallbackFlow {
val listener = object : RoomListLoadingStateListener {
override fun onUpdate(state: RoomListLoadingState) {
Expand All @@ -58,7 +58,7 @@ fun RoomList.loadingStateFlow(): Flow<RoomListLoadingState> =
Timber.d(it, "loadingStateFlow() failed")
}.buffer(Channel.UNLIMITED)

fun RoomList.entriesFlow(onInitialList: suspend (List<RoomListEntry>) -> Unit): Flow<List<RoomListEntriesUpdate>> =
fun RoomListInterface.entriesFlow(onInitialList: suspend (List<RoomListEntry>) -> Unit): Flow<List<RoomListEntriesUpdate>> =
mxCallbackFlow {
val listener = object : RoomListEntriesListener {
override fun onUpdate(roomEntriesUpdate: List<RoomListEntriesUpdate>) {
Expand All @@ -76,7 +76,7 @@ fun RoomList.entriesFlow(onInitialList: suspend (List<RoomListEntry>) -> Unit):
Timber.d(it, "entriesFlow() failed")
}.buffer(Channel.UNLIMITED)

fun RoomListService.stateFlow(): Flow<RoomListServiceState> =
fun RoomListServiceInterface.stateFlow(): Flow<RoomListServiceState> =
mxCallbackFlow {
val listener = object : RoomListServiceStateListener {
override fun onUpdate(state: RoomListServiceState) {
Expand All @@ -88,7 +88,7 @@ fun RoomListService.stateFlow(): Flow<RoomListServiceState> =
}
}.buffer(Channel.UNLIMITED)

fun RoomListService.syncIndicator(): Flow<RoomListServiceSyncIndicator> =
fun RoomListServiceInterface.syncIndicator(): Flow<RoomListServiceSyncIndicator> =
mxCallbackFlow {
val listener = object : RoomListServiceSyncIndicatorListener {
override fun onUpdate(syncIndicator: RoomListServiceSyncIndicator) {
Expand All @@ -104,7 +104,7 @@ fun RoomListService.syncIndicator(): Flow<RoomListServiceSyncIndicator> =
}
}.buffer(Channel.UNLIMITED)

fun RoomListService.roomOrNull(roomId: String): RoomListItem? {
fun RoomListServiceInterface.roomOrNull(roomId: String): RoomListItem? {
return try {
room(roomId)
} catch (exception: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import org.matrix.rustcomponents.sdk.RoomListEntriesUpdate
import org.matrix.rustcomponents.sdk.RoomListEntry
import org.matrix.rustcomponents.sdk.RoomListService
import org.matrix.rustcomponents.sdk.RoomListServiceInterface
import org.matrix.rustcomponents.sdk.use
import timber.log.Timber
import java.util.UUID

class RoomSummaryListProcessor(
private val roomSummaries: MutableStateFlow<List<RoomSummary>>,
private val roomListService: RoomListService,
private val roomListService: RoomListServiceInterface,
private val dispatcher: CoroutineDispatcher,
private val roomSummaryDetailsFactory: RoomSummaryDetailsFactory = RoomSummaryDetailsFactory(),
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.element.android.libraries.matrix.impl.auth

import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.matrix.api.auth.AuthenticationException
import org.junit.Test
import org.matrix.rustcomponents.sdk.AuthenticationException as RustAuthenticationException

class AuthenticationExceptionMappingTests {

@Test
fun `mapping an exception with no message returns 'Unknown error' message`() {
val exception = Exception()
val mappedException = exception.mapAuthenticationException()
assertThat(mappedException.message).isEqualTo("Unknown error")
}

@Test
fun `mapping a generic exception returns a Generic AuthenticationException`() {
val exception = Exception("Generic exception")
val mappedException = exception.mapAuthenticationException()
assertThat(mappedException).isEqualTo(AuthenticationException.Generic("Generic exception"))
}

@Test
fun `mapping specific exceptions map to their kotlin counterparts`() {
assertThat(RustAuthenticationException.ClientMissing("Client missing").mapAuthenticationException())
.isEqualTo(AuthenticationException.ClientMissing("Client missing"))

assertThat(RustAuthenticationException.Generic("Generic").mapAuthenticationException()).isEqualTo(AuthenticationException.Generic("Generic"))

assertThat(RustAuthenticationException.InvalidServerName("Invalid server name").mapAuthenticationException())
.isEqualTo(AuthenticationException.InvalidServerName("Invalid server name"))

assertThat(RustAuthenticationException.SessionMissing("Session missing").mapAuthenticationException())
.isEqualTo(AuthenticationException.SessionMissing("Session missing"))

assertThat(RustAuthenticationException.SlidingSyncNotAvailable("Sliding sync not available").mapAuthenticationException())
.isEqualTo(AuthenticationException.SlidingSyncNotAvailable("Sliding sync not available"))
}

@Test
fun `mapping Oidc related exceptions creates an 'OidcError' with different types`() {
assertThat(RustAuthenticationException.OidcException("Oidc exception").mapAuthenticationException())
.isEqualTo(AuthenticationException.OidcError("OidcException", "Oidc exception"))

assertThat(RustAuthenticationException.OidcMetadataInvalid("Oidc metadata invalid").mapAuthenticationException())
.isEqualTo(AuthenticationException.OidcError("OidcMetadataInvalid", "Oidc metadata invalid"))

assertThat(RustAuthenticationException.OidcMetadataMissing("Oidc metadata missing").mapAuthenticationException())
.isEqualTo(AuthenticationException.OidcError("OidcMetadataMissing", "Oidc metadata missing"))

assertThat(RustAuthenticationException.OidcNotSupported("Oidc not supported").mapAuthenticationException())
.isEqualTo(AuthenticationException.OidcError("OidcNotSupported", "Oidc not supported"))

assertThat(RustAuthenticationException.OidcCancelled("Oidc cancelled").mapAuthenticationException())
.isEqualTo(AuthenticationException.OidcError("OidcCancelled", "Oidc cancelled"))

assertThat(RustAuthenticationException.OidcCallbackUrlInvalid("Oidc callback url invalid").mapAuthenticationException())
.isEqualTo(AuthenticationException.OidcError("OidcCallbackUrlInvalid", "Oidc callback url invalid"))
}

}
Loading