mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-03-18 04:08:44 +03:00
Using suspend modifier to make use case really testable
This commit is contained in:
parent
1475655f20
commit
3adc5ca430
5 changed files with 66 additions and 50 deletions
|
@ -110,7 +110,9 @@ class ActiveSessionHolder @Inject constructor(
|
|||
}
|
||||
?: sessionInitializer.tryInitialize(readCurrentSession = { activeSessionReference.get() }) { session ->
|
||||
setActiveSession(session)
|
||||
configureAndStartSessionUseCase.execute(session, startSyncing = startSync)
|
||||
runBlocking {
|
||||
configureAndStartSessionUseCase.execute(session, startSyncing = startSync)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor(
|
|||
private val updateMatrixClientInfoUseCase: UpdateMatrixClientInfoUseCase,
|
||||
) {
|
||||
|
||||
fun execute(session: Session, startSyncing: Boolean = true) {
|
||||
suspend fun execute(session: Session, startSyncing: Boolean = true) {
|
||||
Timber.i("Configure and start session for ${session.myUserId}. startSyncing: $startSyncing")
|
||||
session.open()
|
||||
session.filterService().setFilter(FilterService.FilterPreset.ElementFilter)
|
||||
|
|
|
@ -18,8 +18,6 @@ package im.vector.app.core.session.clientinfo
|
|||
|
||||
import im.vector.app.core.resources.AppNameProvider
|
||||
import im.vector.app.core.resources.BuildMeta
|
||||
import im.vector.app.features.session.coroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.matrix.android.sdk.api.session.Session
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
@ -34,25 +32,21 @@ class UpdateMatrixClientInfoUseCase @Inject constructor(
|
|||
private val setMatrixClientInfoUseCase: SetMatrixClientInfoUseCase,
|
||||
) {
|
||||
|
||||
fun execute(session: Session) {
|
||||
session.coroutineScope.launch {
|
||||
runCatching {
|
||||
val clientInfo = MatrixClientInfoContent(
|
||||
name = appNameProvider.getAppName(),
|
||||
version = buildMeta.versionName
|
||||
)
|
||||
val deviceId = session.sessionParams.deviceId.orEmpty()
|
||||
if (deviceId.isNotEmpty()) {
|
||||
val storedClientInfo = getMatrixClientInfoUseCase.execute(session, deviceId)
|
||||
Timber.d("storedClientInfo=$storedClientInfo, current client info=$clientInfo")
|
||||
if (clientInfo != storedClientInfo) {
|
||||
Timber.d("client info need to be updated")
|
||||
setMatrixClientInfoUseCase.execute(session, clientInfo)
|
||||
}
|
||||
} else {
|
||||
throw NoDeviceIdError()
|
||||
}
|
||||
suspend fun execute(session: Session) = runCatching {
|
||||
val clientInfo = MatrixClientInfoContent(
|
||||
name = appNameProvider.getAppName(),
|
||||
version = buildMeta.versionName
|
||||
)
|
||||
val deviceId = session.sessionParams.deviceId.orEmpty()
|
||||
if (deviceId.isNotEmpty()) {
|
||||
val storedClientInfo = getMatrixClientInfoUseCase.execute(session, deviceId)
|
||||
Timber.d("storedClientInfo=$storedClientInfo, current client info=$clientInfo")
|
||||
if (clientInfo != storedClientInfo) {
|
||||
Timber.d("client info need to be updated")
|
||||
return setMatrixClientInfoUseCase.execute(session, clientInfo)
|
||||
}
|
||||
} else {
|
||||
throw NoDeviceIdError()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@ import im.vector.app.core.session.clientinfo.UpdateMatrixClientInfoUseCase
|
|||
import im.vector.app.test.fakes.FakeContext
|
||||
import im.vector.app.test.fakes.FakeSession
|
||||
import im.vector.app.test.fakes.FakeWebRtcCallManager
|
||||
import io.mockk.coJustRun
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
|
@ -28,6 +30,7 @@ import io.mockk.mockkStatic
|
|||
import io.mockk.runs
|
||||
import io.mockk.unmockkAll
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
@ -56,11 +59,11 @@ class ConfigureAndStartSessionUseCaseTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `given a session and start sync needed when configuring and starting the session then it should be configured properly`() {
|
||||
fun `given a session and start sync needed when configuring and starting the session then it should be configured properly`() = runTest {
|
||||
// Given
|
||||
val fakeSession = givenASession()
|
||||
fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds()
|
||||
every { fakeUpdateMatrixClientInfoUseCase.execute(any()) } just runs
|
||||
coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) }
|
||||
|
||||
// When
|
||||
configureAndStartSessionUseCase.execute(fakeSession, startSyncing = true)
|
||||
|
@ -70,15 +73,15 @@ class ConfigureAndStartSessionUseCaseTest {
|
|||
fakeSession.fakeFilterService.verifySetFilter(FilterService.FilterPreset.ElementFilter)
|
||||
fakeSession.fakePushersService.verifyRefreshPushers()
|
||||
fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded()
|
||||
verify { fakeUpdateMatrixClientInfoUseCase.execute(fakeSession) }
|
||||
coVerify { fakeUpdateMatrixClientInfoUseCase.execute(fakeSession) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given a session and no start sync needed when configuring and starting the session then it should be configured properly`() {
|
||||
fun `given a session and no start sync needed when configuring and starting the session then it should be configured properly`() = runTest {
|
||||
// Given
|
||||
val fakeSession = givenASession()
|
||||
fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds()
|
||||
every { fakeUpdateMatrixClientInfoUseCase.execute(any()) } just runs
|
||||
coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) }
|
||||
|
||||
// When
|
||||
configureAndStartSessionUseCase.execute(fakeSession, startSyncing = false)
|
||||
|
@ -88,7 +91,7 @@ class ConfigureAndStartSessionUseCaseTest {
|
|||
fakeSession.fakeFilterService.verifySetFilter(FilterService.FilterPreset.ElementFilter)
|
||||
fakeSession.fakePushersService.verifyRefreshPushers()
|
||||
fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded()
|
||||
verify { fakeUpdateMatrixClientInfoUseCase.execute(fakeSession) }
|
||||
coVerify { fakeUpdateMatrixClientInfoUseCase.execute(fakeSession) }
|
||||
}
|
||||
|
||||
private fun givenASession(): FakeSession {
|
||||
|
|
|
@ -19,17 +19,14 @@ package im.vector.app.core.session.clientinfo
|
|||
import im.vector.app.core.resources.BuildMeta
|
||||
import im.vector.app.test.fakes.FakeAppNameProvider
|
||||
import im.vector.app.test.fakes.FakeSession
|
||||
import im.vector.app.test.testDispatcher
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.amshove.kluent.shouldBe
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.amshove.kluent.shouldBeInstanceOf
|
||||
import org.junit.Test
|
||||
|
||||
private const val AN_APP_NAME_1 = "app_name_1"
|
||||
|
@ -53,18 +50,8 @@ class UpdateMatrixClientInfoUseCaseTest {
|
|||
setMatrixClientInfoUseCase = setMatrixClientInfoUseCase,
|
||||
)
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
Dispatchers.resetMain()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given current client info is different than the stored one when trying to update then new client info is set`() = runTest(testDispatcher) {
|
||||
fun `given current client info is different than the stored one when trying to update then new client info is set`() = runTest {
|
||||
// Given
|
||||
givenCurrentAppName(AN_APP_NAME_1)
|
||||
givenCurrentVersionName(A_VERSION_NAME_1)
|
||||
|
@ -76,37 +63,63 @@ class UpdateMatrixClientInfoUseCaseTest {
|
|||
)
|
||||
|
||||
// When
|
||||
updateMatrixClientInfoUseCase.execute(fakeSession)
|
||||
val result = updateMatrixClientInfoUseCase.execute(fakeSession)
|
||||
|
||||
// Then
|
||||
result.isSuccess shouldBe true
|
||||
coVerify { setMatrixClientInfoUseCase.execute(fakeSession, match { it == expectedClientInfoToSet }) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given current client info is equal to the stored one when trying to update then nothing is done`() = runTest(testDispatcher) {
|
||||
fun `given error during set of new client info when trying to update then result is failure`() = runTest {
|
||||
// Given
|
||||
givenCurrentAppName(AN_APP_NAME_1)
|
||||
givenCurrentVersionName(A_VERSION_NAME_1)
|
||||
givenStoredClientInfo(AN_APP_NAME_2, A_VERSION_NAME_2)
|
||||
val error = Exception()
|
||||
givenSetClientInfoFails(error)
|
||||
val expectedClientInfoToSet = MatrixClientInfoContent(
|
||||
name = AN_APP_NAME_1,
|
||||
version = A_VERSION_NAME_1,
|
||||
)
|
||||
|
||||
// When
|
||||
val result = updateMatrixClientInfoUseCase.execute(fakeSession)
|
||||
|
||||
// Then
|
||||
result.isFailure shouldBe true
|
||||
result.exceptionOrNull() shouldBeEqualTo error
|
||||
coVerify { setMatrixClientInfoUseCase.execute(fakeSession, match { it == expectedClientInfoToSet }) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given current client info is equal to the stored one when trying to update then nothing is done`() = runTest {
|
||||
// Given
|
||||
givenCurrentAppName(AN_APP_NAME_1)
|
||||
givenCurrentVersionName(A_VERSION_NAME_1)
|
||||
givenStoredClientInfo(AN_APP_NAME_1, A_VERSION_NAME_1)
|
||||
|
||||
// When
|
||||
updateMatrixClientInfoUseCase.execute(fakeSession)
|
||||
val result = updateMatrixClientInfoUseCase.execute(fakeSession)
|
||||
|
||||
// Then
|
||||
result.isSuccess shouldBe true
|
||||
coVerify(inverse = true) { setMatrixClientInfoUseCase.execute(fakeSession, any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given no session id for current session when trying to update then nothing is done`() = runTest(testDispatcher) {
|
||||
fun `given no session id for current session when trying to update then nothing is done`() = runTest {
|
||||
// Given
|
||||
givenCurrentAppName(AN_APP_NAME_1)
|
||||
givenCurrentVersionName(A_VERSION_NAME_1)
|
||||
fakeSession.givenSessionId(null)
|
||||
|
||||
// When
|
||||
updateMatrixClientInfoUseCase.execute(fakeSession)
|
||||
val result = updateMatrixClientInfoUseCase.execute(fakeSession)
|
||||
|
||||
// Then
|
||||
result.isFailure shouldBe true
|
||||
result.exceptionOrNull() shouldBeInstanceOf NoDeviceIdError::class
|
||||
coVerify(inverse = true) { setMatrixClientInfoUseCase.execute(fakeSession, any()) }
|
||||
}
|
||||
|
||||
|
@ -129,4 +142,8 @@ class UpdateMatrixClientInfoUseCaseTest {
|
|||
private fun givenSetClientInfoSucceeds() {
|
||||
coEvery { setMatrixClientInfoUseCase.execute(any(), any()) } returns Result.success(Unit)
|
||||
}
|
||||
|
||||
private fun givenSetClientInfoFails(error: Throwable) {
|
||||
coEvery { setMatrixClientInfoUseCase.execute(any(), any()) } returns Result.failure(error)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue