mirror of
https://github.com/element-hq/element-android
synced 2024-11-27 11:59:12 +03:00
Adding unit tests for the get client info use case
This commit is contained in:
parent
9877e90df6
commit
ccc3ac628a
7 changed files with 128 additions and 19 deletions
|
@ -18,23 +18,24 @@ package im.vector.app.features.settings.devices.v2.details.extended
|
|||
|
||||
import MATRIX_CLIENT_INFO_KEY_PREFIX
|
||||
import im.vector.app.core.di.ActiveSessionHolder
|
||||
import org.matrix.android.sdk.api.session.events.model.toModel
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* This use case retrieves the current account data event containing extended client info.
|
||||
* This use case retrieves the current account data event containing extended client info
|
||||
* for a given deviceId.
|
||||
*/
|
||||
class GetMatrixClientInfoUseCase @Inject constructor(
|
||||
private val activeSessionHolder: ActiveSessionHolder,
|
||||
) {
|
||||
|
||||
// TODO add unit tests
|
||||
fun execute(): MatrixClientInfoContent? {
|
||||
fun execute(deviceId: String): MatrixClientInfoContent? {
|
||||
return activeSessionHolder
|
||||
.getSafeActiveSession()
|
||||
?.let { session ->
|
||||
val type = MATRIX_CLIENT_INFO_KEY_PREFIX + session.sessionParams.deviceId
|
||||
val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId
|
||||
val content = session.accountDataService().getUserAccountDataEvent(type)?.content
|
||||
MatrixClientInfoContent.fromJson(content)
|
||||
content.toModel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,13 +31,4 @@ data class MatrixClientInfoContent(
|
|||
// app url (optional, applicable only for web)
|
||||
@Json(name = "url")
|
||||
val url: String? = null,
|
||||
) {
|
||||
companion object {
|
||||
fun fromJson(obj: Any?): MatrixClientInfoContent? {
|
||||
return Moshi.Builder()
|
||||
.build()
|
||||
.adapter(MatrixClientInfoContent::class.java)
|
||||
.fromJsonValue(obj)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
|
@ -32,9 +32,12 @@ class SetMatrixClientInfoUseCase @Inject constructor(
|
|||
suspend fun execute(clientInfo: MatrixClientInfoContent): Result<Unit> = runCatching {
|
||||
activeSessionHolder.getSafeActiveSession()
|
||||
?.let { session ->
|
||||
val type = MATRIX_CLIENT_INFO_KEY_PREFIX + session.sessionParams.deviceId
|
||||
session.accountDataService()
|
||||
.updateUserAccountData(type, clientInfo.toContent())
|
||||
val deviceId = session.sessionParams.deviceId
|
||||
if (deviceId != null) {
|
||||
val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId
|
||||
session.accountDataService()
|
||||
.updateUserAccountData(type, clientInfo.toContent())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package im.vector.app.features.settings.devices.v2.details.extended
|
||||
|
||||
import im.vector.app.core.di.ActiveSessionHolder
|
||||
import im.vector.app.core.resources.AppNameProvider
|
||||
import im.vector.app.core.resources.BuildMeta
|
||||
import javax.inject.Inject
|
||||
|
@ -24,19 +25,22 @@ import javax.inject.Inject
|
|||
* This use case updates if needed the account data event containing extended client info.
|
||||
*/
|
||||
class UpdateMatrixClientInfoUseCase @Inject constructor(
|
||||
private val activeSessionHolder: ActiveSessionHolder,
|
||||
private val appNameProvider: AppNameProvider,
|
||||
private val buildMeta: BuildMeta,
|
||||
private val getMatrixClientInfoUseCase: GetMatrixClientInfoUseCase,
|
||||
private val setMatrixClientInfoUseCase: SetMatrixClientInfoUseCase,
|
||||
) {
|
||||
|
||||
// TODO call the use case after signin + on app startup
|
||||
// TODO add unit tests
|
||||
suspend fun execute(): Result<Unit> = runCatching {
|
||||
val clientInfo = MatrixClientInfoContent(
|
||||
name = appNameProvider.getAppName(),
|
||||
version = buildMeta.versionName
|
||||
)
|
||||
val storedClientInfo = getMatrixClientInfoUseCase.execute()
|
||||
val sessionId = activeSessionHolder.getActiveSession().sessionParams.deviceId
|
||||
val storedClientInfo = sessionId?.let { getMatrixClientInfoUseCase.execute(it) }
|
||||
if (clientInfo != storedClientInfo) {
|
||||
setMatrixClientInfoUseCase.execute(clientInfo)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* Copyright (c) 2022 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 im.vector.app.features.settings.devices.v2.details.extended
|
||||
|
||||
import MATRIX_CLIENT_INFO_KEY_PREFIX
|
||||
import im.vector.app.test.fakes.FakeActiveSessionHolder
|
||||
import org.amshove.kluent.shouldBe
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.junit.Test
|
||||
|
||||
private const val A_DEVICE_ID = "device-id"
|
||||
private const val A_CLIENT_NAME = "client-name"
|
||||
private const val A_CLIENT_VERSION = "client-version"
|
||||
private const val A_CLIENT_URL = "client-url"
|
||||
|
||||
class GetMatrixClientInfoUseCaseTest {
|
||||
|
||||
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
|
||||
|
||||
private val getMatrixClientInfoUseCase = GetMatrixClientInfoUseCase(
|
||||
activeSessionHolder = fakeActiveSessionHolder.instance
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `given a device id and existing content when getting the info then result should contain that info`() {
|
||||
// Given
|
||||
givenClientInfoContent(A_DEVICE_ID)
|
||||
val expectedClientInfo = MatrixClientInfoContent(
|
||||
name = A_CLIENT_NAME,
|
||||
version = A_CLIENT_VERSION,
|
||||
url = A_CLIENT_URL,
|
||||
)
|
||||
|
||||
// When
|
||||
val result = getMatrixClientInfoUseCase.execute(A_DEVICE_ID)
|
||||
|
||||
// Then
|
||||
result shouldBeEqualTo expectedClientInfo
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given no active session when getting the info then result should be null`() {
|
||||
// Given
|
||||
fakeActiveSessionHolder.givenGetSafeActiveSessionReturns(null)
|
||||
|
||||
// When
|
||||
val result = getMatrixClientInfoUseCase.execute(A_DEVICE_ID)
|
||||
|
||||
// Then
|
||||
result shouldBe null
|
||||
}
|
||||
|
||||
private fun givenClientInfoContent(deviceId: String) {
|
||||
val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId
|
||||
val content = mapOf(
|
||||
Pair("name", A_CLIENT_NAME),
|
||||
Pair("version", A_CLIENT_VERSION),
|
||||
Pair("url", A_CLIENT_URL),
|
||||
)
|
||||
fakeActiveSessionHolder.fakeSession
|
||||
.fakeSessionAccountDataService
|
||||
.givenGetUserAccountDataEventReturns(type, content)
|
||||
}
|
||||
}
|
|
@ -43,6 +43,7 @@ class FakeSession(
|
|||
val fakeRoomService: FakeRoomService = FakeRoomService(),
|
||||
val fakePushersService: FakePushersService = FakePushersService(),
|
||||
private val fakeEventService: FakeEventService = FakeEventService(),
|
||||
val fakeSessionAccountDataService: FakeSessionAccountDataService = FakeSessionAccountDataService(),
|
||||
) : Session by mockk(relaxed = true) {
|
||||
|
||||
init {
|
||||
|
@ -60,6 +61,7 @@ class FakeSession(
|
|||
override fun roomService() = fakeRoomService
|
||||
override fun eventService() = fakeEventService
|
||||
override fun pushersService() = fakePushersService
|
||||
override fun accountDataService() = fakeSessionAccountDataService
|
||||
|
||||
fun givenVectorStore(vectorSessionStore: VectorSessionStore) {
|
||||
coEvery {
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (c) 2022 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 im.vector.app.test.fakes
|
||||
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import org.matrix.android.sdk.api.session.accountdata.SessionAccountDataService
|
||||
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent
|
||||
import org.matrix.android.sdk.api.session.events.model.Content
|
||||
|
||||
class FakeSessionAccountDataService : SessionAccountDataService by mockk() {
|
||||
|
||||
fun givenGetUserAccountDataEventReturns(type: String, content: Content) {
|
||||
every { getUserAccountDataEvent(type) } returns UserAccountDataEvent(type, content)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue