Introduce JsonProvider.
It will ensure that classes are using the correct Json instances in the unit tests.
This commit is contained in:
parent
fa8ddba1f5
commit
df48ed5a2d
21 changed files with 103 additions and 52 deletions
|
|
@ -9,18 +9,18 @@ package io.element.android.features.call.impl.utils
|
|||
|
||||
import dev.zacsweers.metro.Inject
|
||||
import io.element.android.features.call.impl.data.WidgetMessage
|
||||
import io.element.android.libraries.androidutils.json.JsonProvider
|
||||
import io.element.android.libraries.core.extensions.runCatchingExceptions
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
@Inject
|
||||
class WidgetMessageSerializer(
|
||||
private val json: Json,
|
||||
private val json: JsonProvider,
|
||||
) {
|
||||
fun deserialize(message: String): Result<WidgetMessage> {
|
||||
return runCatchingExceptions { json.decodeFromString(WidgetMessage.serializer(), message) }
|
||||
return runCatchingExceptions { json().decodeFromString(WidgetMessage.serializer(), message) }
|
||||
}
|
||||
|
||||
fun serialize(message: WidgetMessage): String {
|
||||
return json.encodeToString(WidgetMessage.serializer(), message)
|
||||
return json().encodeToString(WidgetMessage.serializer(), message)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import io.element.android.features.call.impl.utils.WidgetMessageSerializer
|
|||
import io.element.android.features.call.utils.FakeActiveCallManager
|
||||
import io.element.android.features.call.utils.FakeCallWidgetProvider
|
||||
import io.element.android.features.call.utils.FakeWidgetMessageInterceptor
|
||||
import io.element.android.libraries.androidutils.json.DefaultJsonProvider
|
||||
import io.element.android.libraries.architecture.AsyncData
|
||||
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
|
||||
import io.element.android.libraries.matrix.api.sync.SyncState
|
||||
|
|
@ -47,7 +48,6 @@ import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
|||
import kotlinx.coroutines.test.advanceTimeBy
|
||||
import kotlinx.coroutines.test.runCurrent
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
|
@ -412,7 +412,7 @@ class CallScreenPresenterTest {
|
|||
languageTagProvider = FakeLanguageTagProvider("en-US"),
|
||||
appForegroundStateService = appForegroundStateService,
|
||||
appCoroutineScope = backgroundScope,
|
||||
widgetMessageSerializer = WidgetMessageSerializer(Json { ignoreUnknownKeys = true }),
|
||||
widgetMessageSerializer = WidgetMessageSerializer(DefaultJsonProvider()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@ import dev.zacsweers.metro.AppScope
|
|||
import dev.zacsweers.metro.ContributesBinding
|
||||
import dev.zacsweers.metro.Inject
|
||||
import io.element.android.features.login.impl.accountprovider.AccountProviderDataSource
|
||||
import io.element.android.libraries.androidutils.json.JsonProvider
|
||||
import io.element.android.libraries.matrix.api.auth.external.ExternalSession
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
interface MessageParser {
|
||||
/**
|
||||
|
|
@ -26,10 +26,10 @@ interface MessageParser {
|
|||
@Inject
|
||||
class DefaultMessageParser(
|
||||
private val accountProviderDataSource: AccountProviderDataSource,
|
||||
private val json: Json,
|
||||
private val json: JsonProvider,
|
||||
) : MessageParser {
|
||||
override fun parse(message: String): ExternalSession {
|
||||
val response = json.decodeFromString(MobileRegistrationResponse.serializer(), message)
|
||||
val response = json().decodeFromString(MobileRegistrationResponse.serializer(), message)
|
||||
val userId = response.userId ?: error("No user ID in response")
|
||||
val homeServer = response.homeServer ?: accountProviderDataSource.flow.value.url
|
||||
val accessToken = response.accessToken ?: error("No access token in response")
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@ import com.google.common.truth.Truth.assertThat
|
|||
import io.element.android.appconfig.AuthenticationConfig
|
||||
import io.element.android.features.enterprise.test.FakeEnterpriseService
|
||||
import io.element.android.features.login.impl.accountprovider.AccountProviderDataSource
|
||||
import io.element.android.libraries.androidutils.json.DefaultJsonProvider
|
||||
import io.element.android.libraries.matrix.api.auth.external.ExternalSession
|
||||
import kotlinx.serialization.SerializationException
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Test
|
||||
|
||||
|
|
@ -70,7 +70,7 @@ class DefaultMessageParserTest {
|
|||
private fun createDefaultMessageParser(): DefaultMessageParser {
|
||||
return DefaultMessageParser(
|
||||
accountProviderDataSource = AccountProviderDataSource(FakeEnterpriseService()),
|
||||
json = Json { ignoreUnknownKeys = true },
|
||||
json = DefaultJsonProvider(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue