Merge branch 'develop' into bma/fixLeak

This commit is contained in:
Benoit Marty
2026-09-16 09:42:36 +02:00
committed by GitHub
184 changed files with 940 additions and 750 deletions
@@ -57,7 +57,6 @@ import org.junit.Test
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@Suppress("LargeClass")
class DefaultPinnedMessagesBannerFormatterTest : RobolectricTest() {
private lateinit var context: Context
private lateinit var fakeMatrixClient: FakeMatrixClient
@@ -58,7 +58,6 @@ import org.junit.Test
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@Suppress("LargeClass")
class DefaultRoomLatestEventFormatterTest : RobolectricTest() {
private lateinit var context: Context
private lateinit var fakeMatrixClient: FakeMatrixClient
@@ -21,7 +21,6 @@ import org.junit.Test
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@Suppress("LargeClass")
class RtcNotificationContentFormatterTest : RobolectricTest() {
private lateinit var context: Context
private lateinit var fakeMatrixClient: FakeMatrixClient
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.libraries.matrix.api.accountprovider
import androidx.compose.runtime.Immutable
@Immutable
sealed interface AccountProvider {
/**
* An account provider that was input by the user.
*/
data class Generic(
val serverName: String,
) : AccountProvider
/**
* An account provider that is provided by the app.
*/
data class Managed(
val serverName: String,
val baseUrl: String,
val canUseServerName: Boolean,
) : AccountProvider
fun serverNameOrBaseUrl(): String {
return when (this) {
is Generic -> serverName
is Managed -> if (canUseServerName) serverName else baseUrl
}.sanitized()
}
/**
* Return the server name, to be used in the UI.
*/
fun friendlyServerName(): String {
return when (this) {
is Generic -> serverName
is Managed -> serverName
}.sanitized()
}
/**
* The user may reach an allowed account provider by either of its identifiers: the server name it is known by
* (what the user types, e.g. `element.io`) or the base url its client API lives at (e.g. `https://element.ems.host`).
*/
fun matches(other: AccountProvider): Boolean {
return serverNameOrBaseUrl() == other.serverNameOrBaseUrl()
}
/**
* Sanitizes the string with the following rules:
* - Trim any whitespace.
* - Lowercase the string.
* - Removes a https scheme (treating it as implicit).
* - Remove any trailing slashes.
*/
private fun String.sanitized(): String {
return trim()
.lowercase()
// Intentionally continue to show http:// as an indicator (and for history).
.removePrefix("https://")
.removeSuffix("/")
}
}
val matrixOrgAccountProvider = AccountProvider.Generic(
serverName = "matrix.org",
)
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.libraries.matrix.api.accountprovider
import com.google.common.truth.Truth.assertThat
import org.junit.Test
class AccountProviderTest {
@Test
fun `serverNameOrBaseUrl of a generic account provider is the server name the user input`() {
assertThat(AccountProvider.Generic("matrix.org").serverNameOrBaseUrl()).isEqualTo("matrix.org")
}
@Test
fun `serverNameOrBaseUrl of a managed account provider is its server name when it can be used`() {
val accountProvider = AccountProvider.Managed(
serverName = "element.io",
baseUrl = "https://element.ems.host",
canUseServerName = true,
)
assertThat(accountProvider.serverNameOrBaseUrl()).isEqualTo("element.io")
}
@Test
fun `serverNameOrBaseUrl of a managed account provider is its base url when the server name cannot be used`() {
val accountProvider = AccountProvider.Managed(
serverName = "element.io",
baseUrl = "https://element.ems.host",
canUseServerName = false,
)
assertThat(accountProvider.serverNameOrBaseUrl()).isEqualTo("element.ems.host")
}
@Test
fun `matrixOrgAccountProvider is known by its server name`() {
assertThat(matrixOrgAccountProvider.serverNameOrBaseUrl()).isEqualTo("matrix.org")
}
@Test
fun `serverNameOrBaseUrl trims, lowercases and drops the implicit https scheme and a trailing slash`() {
assertThat(AccountProvider.Generic(" MATRIX.org ").serverNameOrBaseUrl()).isEqualTo("matrix.org")
assertThat(AccountProvider.Generic("https://matrix.org").serverNameOrBaseUrl()).isEqualTo("matrix.org")
assertThat(AccountProvider.Generic("https://matrix.org/").serverNameOrBaseUrl()).isEqualTo("matrix.org")
}
@Test
fun `serverNameOrBaseUrl keeps an explicit http scheme as an indicator`() {
assertThat(AccountProvider.Generic("http://localhost:8080").serverNameOrBaseUrl()).isEqualTo("http://localhost:8080")
}
@Test
fun `matches ignores the scheme, the case and a trailing slash`() {
val typed = AccountProvider.Generic("Element.io/")
assertThat(typed.matches(AccountProvider.Generic("https://element.io"))).isTrue()
assertThat(
typed.matches(
AccountProvider.Managed(serverName = "element.io", baseUrl = "https://element.ems.host", canUseServerName = true)
)
).isTrue()
}
@Test
fun `matches a managed provider that cannot use its server name only through its base url`() {
val managed = AccountProvider.Managed(
serverName = "element.io",
baseUrl = "https://element.ems.host",
canUseServerName = false,
)
assertThat(AccountProvider.Generic("element.ems.host").matches(managed)).isTrue()
assertThat(AccountProvider.Generic("element.io").matches(managed)).isFalse()
}
@Test
fun `matches is false for a different account provider`() {
val typed = AccountProvider.Generic("element.io")
assertThat(typed.matches(AccountProvider.Generic("matrix.org"))).isFalse()
// A managed provider that can use its server name is not reached through its base url.
assertThat(
typed.matches(
AccountProvider.Managed(serverName = "other.io", baseUrl = "https://element.io", canUseServerName = true)
)
).isFalse()
}
}
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.libraries.matrix.test.accountprovider
import io.element.android.libraries.matrix.api.accountprovider.AccountProvider
import io.element.android.libraries.matrix.test.AN_ACCOUNT_PROVIDER
import io.element.android.libraries.matrix.test.AN_ACCOUNT_PROVIDER_URL
fun anAccountProviderGeneric(
serverName: String = AN_ACCOUNT_PROVIDER,
) = AccountProvider.Generic(
serverName = serverName,
)
fun anAccountProviderManaged(
baseUrl: String = AN_ACCOUNT_PROVIDER_URL,
serverName: String = AN_ACCOUNT_PROVIDER,
canUseServerName: Boolean = true,
) = AccountProvider.Managed(
serverName = serverName,
baseUrl = baseUrl,
canUseServerName = canUseServerName,
)
@@ -54,7 +54,6 @@ import kotlinx.coroutines.test.runTest
import org.junit.Test
import kotlin.time.Duration.Companion.minutes
@Suppress("LargeClass")
class InReplyToMetadataKtTest : RobolectricTest() {
@Test
fun `any message content`() = runTest {
@@ -62,7 +62,6 @@ import kotlinx.collections.immutable.toImmutableList
import org.junit.Test
import kotlin.time.Duration.Companion.seconds
@Suppress("LargeClass")
class EventItemFactoryTest {
@Test
fun `create check all null cases`() {
@@ -64,7 +64,6 @@ private val TESTED_MEDIA_INFO = anApkMediaInfo(
senderId = A_USER_ID,
)
@Suppress("LargeClass")
class MediaViewerPresenterTest {
@get:Rule
val warmUpRule = WarmUpRule()
@@ -69,7 +69,6 @@ import org.junit.Test
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@Suppress("LargeClass")
class DefaultNotifiableEventResolverTest : RobolectricTest() {
@Test
fun `resolve event no session`() = runTest {
@@ -52,7 +52,6 @@ import kotlin.time.Duration.Companion.milliseconds
private const val A_PUSHER_INFO = "info"
@Suppress("LargeClass")
class DefaultPushHandlerTest {
@Test
fun `check handleInvalid behavior`() = runTest {