Adding unit tests for ConfigureAndStartSessionUseCase

This commit is contained in:
Maxime NATUREL 2022-10-03 16:44:20 +02:00
parent 34e37ea608
commit e465969e5e
5 changed files with 178 additions and 1 deletions

View file

@ -32,7 +32,6 @@ class ConfigureAndStartSessionUseCase @Inject constructor(
private val updateMatrixClientInfoUseCase: UpdateMatrixClientInfoUseCase, private val updateMatrixClientInfoUseCase: UpdateMatrixClientInfoUseCase,
) { ) {
// TODO add unit tests
fun execute(session: Session, startSyncing: Boolean = true) { fun execute(session: Session, startSyncing: Boolean = true) {
Timber.i("Configure and start session for ${session.myUserId}. startSyncing: $startSyncing") Timber.i("Configure and start session for ${session.myUserId}. startSyncing: $startSyncing")
session.open() session.open()

View file

@ -0,0 +1,102 @@
/*
* 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.core.session
import im.vector.app.core.extensions.startSyncing
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.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.runs
import io.mockk.unmockkAll
import io.mockk.verify
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.matrix.android.sdk.api.session.sync.FilterService
class ConfigureAndStartSessionUseCaseTest {
private val fakeContext = FakeContext()
private val fakeWebRtcCallManager = FakeWebRtcCallManager()
private val fakeUpdateMatrixClientInfoUseCase = mockk<UpdateMatrixClientInfoUseCase>()
private val configureAndStartSessionUseCase = ConfigureAndStartSessionUseCase(
context = fakeContext.instance,
webRtcCallManager = fakeWebRtcCallManager.instance,
updateMatrixClientInfoUseCase = fakeUpdateMatrixClientInfoUseCase,
)
@Before
fun setup() {
mockkStatic("im.vector.app.core.extensions.SessionKt")
}
@After
fun tearDown() {
unmockkAll()
}
@Test
fun `given a session and start sync needed when configuring and starting the session then it should be configured properly`() {
// Given
val fakeSession = givenASession()
fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds()
every { fakeUpdateMatrixClientInfoUseCase.execute(any()) } just runs
// When
configureAndStartSessionUseCase.execute(fakeSession, startSyncing = true)
// Then
verify { fakeSession.startSyncing(fakeContext.instance) }
fakeSession.fakeFilterService.verifySetFilter(FilterService.FilterPreset.ElementFilter)
fakeSession.fakePushersService.verifyRefreshPushers()
fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded()
verify { 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`() {
// Given
val fakeSession = givenASession()
fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds()
every { fakeUpdateMatrixClientInfoUseCase.execute(any()) } just runs
// When
configureAndStartSessionUseCase.execute(fakeSession, startSyncing = false)
// Then
verify(inverse = true) { fakeSession.startSyncing(fakeContext.instance) }
fakeSession.fakeFilterService.verifySetFilter(FilterService.FilterPreset.ElementFilter)
fakeSession.fakePushersService.verifyRefreshPushers()
fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded()
verify { fakeUpdateMatrixClientInfoUseCase.execute(fakeSession) }
}
private fun givenASession(): FakeSession {
val fakeSession = FakeSession()
every { fakeSession.open() } just runs
fakeSession.fakeFilterService.givenSetFilterSucceeds()
every { fakeSession.startSyncing(any()) } just runs
fakeSession.fakePushersService.givenRefreshPushersSucceeds()
return fakeSession
}
}

View file

@ -0,0 +1,35 @@
/*
* 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.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.verify
import org.matrix.android.sdk.api.session.sync.FilterService
class FakeFilterService : FilterService by mockk() {
fun givenSetFilterSucceeds() {
every { setFilter(any()) } just runs
}
fun verifySetFilter(filterPreset: FilterService.FilterPreset) {
verify { setFilter(filterPreset) }
}
}

View file

@ -44,6 +44,8 @@ class FakeSession(
val fakePushersService: FakePushersService = FakePushersService(), val fakePushersService: FakePushersService = FakePushersService(),
private val fakeEventService: FakeEventService = FakeEventService(), private val fakeEventService: FakeEventService = FakeEventService(),
val fakeSessionAccountDataService: FakeSessionAccountDataService = FakeSessionAccountDataService(), val fakeSessionAccountDataService: FakeSessionAccountDataService = FakeSessionAccountDataService(),
val fakeFilterService: FakeFilterService = FakeFilterService(),
val fakePushersService: FakePushersService = FakePushersService(),
) : Session by mockk(relaxed = true) { ) : Session by mockk(relaxed = true) {
init { init {
@ -62,6 +64,8 @@ class FakeSession(
override fun eventService() = fakeEventService override fun eventService() = fakeEventService
override fun pushersService() = fakePushersService override fun pushersService() = fakePushersService
override fun accountDataService() = fakeSessionAccountDataService override fun accountDataService() = fakeSessionAccountDataService
override fun filterService() = fakeFilterService
override fun pushersService() = fakePushersService
fun givenVectorStore(vectorSessionStore: VectorSessionStore) { fun givenVectorStore(vectorSessionStore: VectorSessionStore) {
coEvery { coEvery {

View file

@ -0,0 +1,37 @@
/*
* 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 im.vector.app.features.call.webrtc.WebRtcCallManager
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.verify
class FakeWebRtcCallManager {
val instance = mockk<WebRtcCallManager>()
fun givenCheckForProtocolsSupportIfNeededSucceeds() {
every { instance.checkForProtocolsSupportIfNeeded() } just runs
}
fun verifyCheckForProtocolsSupportIfNeeded() {
verify { instance.checkForProtocolsSupportIfNeeded() }
}
}