Skip to content

Commit 776d935

Browse files
authored
Add more tests, particularly to the room list diffing (#1508)
* Add more tests to improve the covered area, particularly the room list diffing
1 parent 9e73d6d commit 776d935

File tree

5 files changed

+376
-21
lines changed

5 files changed

+376
-21
lines changed

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/AuthenticationException.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ import io.element.android.libraries.matrix.api.auth.AuthenticationException
2020
import org.matrix.rustcomponents.sdk.AuthenticationException as RustAuthenticationException
2121

2222
fun Throwable.mapAuthenticationException(): AuthenticationException {
23+
val message = this.message ?: "Unknown error"
2324
return when (this) {
24-
is RustAuthenticationException.ClientMissing -> AuthenticationException.ClientMissing(this.message!!)
25-
is RustAuthenticationException.Generic -> AuthenticationException.Generic(this.message!!)
26-
is RustAuthenticationException.InvalidServerName -> AuthenticationException.InvalidServerName(this.message!!)
27-
is RustAuthenticationException.SessionMissing -> AuthenticationException.SessionMissing(this.message!!)
28-
is RustAuthenticationException.SlidingSyncNotAvailable -> AuthenticationException.SlidingSyncNotAvailable(this.message!!)
29-
is RustAuthenticationException.OidcException -> AuthenticationException.OidcError("OidcException", message!!)
30-
is RustAuthenticationException.OidcMetadataInvalid -> AuthenticationException.OidcError("OidcMetadataInvalid", message!!)
31-
is RustAuthenticationException.OidcMetadataMissing -> AuthenticationException.OidcError("OidcMetadataMissing", message!!)
32-
is RustAuthenticationException.OidcNotSupported -> AuthenticationException.OidcError("OidcNotSupported", message!!)
33-
is RustAuthenticationException.OidcCancelled -> AuthenticationException.OidcError("OidcCancelled", message!!)
34-
is RustAuthenticationException.OidcCallbackUrlInvalid -> AuthenticationException.OidcError("OidcCallbackUrlInvalid", message!!)
35-
else -> AuthenticationException.Generic(this.message ?: "Unknown error")
25+
is RustAuthenticationException.ClientMissing -> AuthenticationException.ClientMissing(message)
26+
is RustAuthenticationException.Generic -> AuthenticationException.Generic(message)
27+
is RustAuthenticationException.InvalidServerName -> AuthenticationException.InvalidServerName(message)
28+
is RustAuthenticationException.SessionMissing -> AuthenticationException.SessionMissing(message)
29+
is RustAuthenticationException.SlidingSyncNotAvailable -> AuthenticationException.SlidingSyncNotAvailable(message)
30+
is RustAuthenticationException.OidcException -> AuthenticationException.OidcError("OidcException", message)
31+
is RustAuthenticationException.OidcMetadataInvalid -> AuthenticationException.OidcError("OidcMetadataInvalid", message)
32+
is RustAuthenticationException.OidcMetadataMissing -> AuthenticationException.OidcError("OidcMetadataMissing", message)
33+
is RustAuthenticationException.OidcNotSupported -> AuthenticationException.OidcError("OidcNotSupported", message)
34+
is RustAuthenticationException.OidcCancelled -> AuthenticationException.OidcError("OidcCancelled", message)
35+
is RustAuthenticationException.OidcCallbackUrlInvalid -> AuthenticationException.OidcError("OidcCallbackUrlInvalid", message)
36+
else -> AuthenticationException.Generic(message)
3637
}
3738
}

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/roomlist/RoomListExtensions.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import kotlinx.coroutines.channels.trySendBlocking
2323
import kotlinx.coroutines.flow.Flow
2424
import kotlinx.coroutines.flow.buffer
2525
import kotlinx.coroutines.flow.catch
26-
import org.matrix.rustcomponents.sdk.RoomList
2726
import org.matrix.rustcomponents.sdk.RoomListEntriesListener
2827
import org.matrix.rustcomponents.sdk.RoomListEntriesUpdate
2928
import org.matrix.rustcomponents.sdk.RoomListEntry
29+
import org.matrix.rustcomponents.sdk.RoomListInterface
3030
import org.matrix.rustcomponents.sdk.RoomListItem
3131
import org.matrix.rustcomponents.sdk.RoomListLoadingState
3232
import org.matrix.rustcomponents.sdk.RoomListLoadingStateListener
33-
import org.matrix.rustcomponents.sdk.RoomListService
33+
import org.matrix.rustcomponents.sdk.RoomListServiceInterface
3434
import org.matrix.rustcomponents.sdk.RoomListServiceState
3535
import org.matrix.rustcomponents.sdk.RoomListServiceStateListener
3636
import org.matrix.rustcomponents.sdk.RoomListServiceSyncIndicator
@@ -40,7 +40,7 @@ import timber.log.Timber
4040
private const val SYNC_INDICATOR_DELAY_BEFORE_SHOWING = 1000u
4141
private const val SYNC_INDICATOR_DELAY_BEFORE_HIDING = 0u
4242

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

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

79-
fun RoomListService.stateFlow(): Flow<RoomListServiceState> =
79+
fun RoomListServiceInterface.stateFlow(): Flow<RoomListServiceState> =
8080
mxCallbackFlow {
8181
val listener = object : RoomListServiceStateListener {
8282
override fun onUpdate(state: RoomListServiceState) {
@@ -88,7 +88,7 @@ fun RoomListService.stateFlow(): Flow<RoomListServiceState> =
8888
}
8989
}.buffer(Channel.UNLIMITED)
9090

91-
fun RoomListService.syncIndicator(): Flow<RoomListServiceSyncIndicator> =
91+
fun RoomListServiceInterface.syncIndicator(): Flow<RoomListServiceSyncIndicator> =
9292
mxCallbackFlow {
9393
val listener = object : RoomListServiceSyncIndicatorListener {
9494
override fun onUpdate(syncIndicator: RoomListServiceSyncIndicator) {
@@ -104,7 +104,7 @@ fun RoomListService.syncIndicator(): Flow<RoomListServiceSyncIndicator> =
104104
}
105105
}.buffer(Channel.UNLIMITED)
106106

107-
fun RoomListService.roomOrNull(roomId: String): RoomListItem? {
107+
fun RoomListServiceInterface.roomOrNull(roomId: String): RoomListItem? {
108108
return try {
109109
room(roomId)
110110
} catch (exception: Exception) {

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/roomlist/RoomSummaryListProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import kotlinx.coroutines.sync.withLock
2626
import kotlinx.coroutines.withContext
2727
import org.matrix.rustcomponents.sdk.RoomListEntriesUpdate
2828
import org.matrix.rustcomponents.sdk.RoomListEntry
29-
import org.matrix.rustcomponents.sdk.RoomListService
29+
import org.matrix.rustcomponents.sdk.RoomListServiceInterface
3030
import org.matrix.rustcomponents.sdk.use
3131
import timber.log.Timber
3232
import java.util.UUID
3333

3434
class RoomSummaryListProcessor(
3535
private val roomSummaries: MutableStateFlow<List<RoomSummary>>,
36-
private val roomListService: RoomListService,
36+
private val roomListService: RoomListServiceInterface,
3737
private val dispatcher: CoroutineDispatcher,
3838
private val roomSummaryDetailsFactory: RoomSummaryDetailsFactory = RoomSummaryDetailsFactory(),
3939
) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.element.android.libraries.matrix.impl.auth
18+
19+
import com.google.common.truth.ThrowableSubject
20+
import com.google.common.truth.Truth.assertThat
21+
import io.element.android.libraries.matrix.api.auth.AuthenticationException
22+
import org.junit.Test
23+
import org.matrix.rustcomponents.sdk.AuthenticationException as RustAuthenticationException
24+
25+
class AuthenticationExceptionMappingTests {
26+
27+
@Test
28+
fun `mapping an exception with no message returns 'Unknown error' message`() {
29+
val exception = Exception()
30+
val mappedException = exception.mapAuthenticationException()
31+
assertThat(mappedException.message).isEqualTo("Unknown error")
32+
}
33+
34+
@Test
35+
fun `mapping a generic exception returns a Generic AuthenticationException`() {
36+
val exception = Exception("Generic exception")
37+
val mappedException = exception.mapAuthenticationException()
38+
assertThat(mappedException).isException<AuthenticationException.Generic>("Generic exception")
39+
}
40+
41+
@Test
42+
fun `mapping specific exceptions map to their kotlin counterparts`() {
43+
assertThat(RustAuthenticationException.ClientMissing("Client missing").mapAuthenticationException())
44+
.isException<AuthenticationException.ClientMissing>("Client missing")
45+
46+
assertThat(RustAuthenticationException.Generic("Generic").mapAuthenticationException()).isException<AuthenticationException.Generic>("Generic")
47+
48+
assertThat(RustAuthenticationException.InvalidServerName("Invalid server name").mapAuthenticationException())
49+
.isException<AuthenticationException.InvalidServerName>("Invalid server name")
50+
51+
assertThat(RustAuthenticationException.SessionMissing("Session missing").mapAuthenticationException())
52+
.isException<AuthenticationException.SessionMissing>("Session missing")
53+
54+
assertThat(RustAuthenticationException.SlidingSyncNotAvailable("Sliding sync not available").mapAuthenticationException())
55+
.isException<AuthenticationException.SlidingSyncNotAvailable>("Sliding sync not available")
56+
}
57+
58+
@Test
59+
fun `mapping Oidc related exceptions creates an 'OidcError' with different types`() {
60+
assertIsOidcError(
61+
throwable = RustAuthenticationException.OidcException("Oidc exception"),
62+
type = "OidcException",
63+
message = "Oidc exception"
64+
)
65+
assertIsOidcError(
66+
throwable = RustAuthenticationException.OidcMetadataInvalid("Oidc metadata invalid"),
67+
type = "OidcMetadataInvalid",
68+
message = "Oidc metadata invalid"
69+
)
70+
assertIsOidcError(
71+
throwable = RustAuthenticationException.OidcMetadataMissing("Oidc metadata missing"),
72+
type = "OidcMetadataMissing",
73+
message = "Oidc metadata missing"
74+
)
75+
assertIsOidcError(
76+
throwable = RustAuthenticationException.OidcNotSupported("Oidc not supported"),
77+
type = "OidcNotSupported",
78+
message = "Oidc not supported"
79+
)
80+
assertIsOidcError(
81+
throwable = RustAuthenticationException.OidcCancelled("Oidc cancelled"),
82+
type = "OidcCancelled",
83+
message = "Oidc cancelled"
84+
)
85+
assertIsOidcError(
86+
throwable = RustAuthenticationException.OidcCallbackUrlInvalid("Oidc callback url invalid"),
87+
type = "OidcCallbackUrlInvalid",
88+
message = "Oidc callback url invalid"
89+
)
90+
}
91+
92+
private inline fun <reified T> ThrowableSubject.isException(message: String) {
93+
isInstanceOf(T::class.java)
94+
hasMessageThat().isEqualTo(message)
95+
}
96+
97+
private inline fun assertIsOidcError(throwable: Throwable, type: String, message: String) {
98+
val authenticationException = throwable.mapAuthenticationException()
99+
assertThat(authenticationException).isInstanceOf(AuthenticationException.OidcError::class.java)
100+
assertThat((authenticationException as? AuthenticationException.OidcError)?.type).isEqualTo(type)
101+
assertThat(authenticationException.message).isEqualTo(message)
102+
}
103+
104+
}

0 commit comments

Comments
 (0)