diff --git a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt index 504ed830df..be40fd907a 100644 --- a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt @@ -32,7 +32,6 @@ class ConfigureAndStartSessionUseCase @Inject constructor( private val updateMatrixClientInfoUseCase: UpdateMatrixClientInfoUseCase, ) { - // TODO add unit tests fun execute(session: Session, startSyncing: Boolean = true) { Timber.i("Configure and start session for ${session.myUserId}. startSyncing: $startSyncing") session.open() diff --git a/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt new file mode 100644 index 0000000000..1b070a97be --- /dev/null +++ b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt @@ -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() + + 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 + } +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeFilterService.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeFilterService.kt new file mode 100644 index 0000000000..4332368127 --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeFilterService.kt @@ -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) } + } +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt index f4b17ea750..ef2104ed8a 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt @@ -44,6 +44,8 @@ class FakeSession( val fakePushersService: FakePushersService = FakePushersService(), private val fakeEventService: FakeEventService = FakeEventService(), val fakeSessionAccountDataService: FakeSessionAccountDataService = FakeSessionAccountDataService(), + val fakeFilterService: FakeFilterService = FakeFilterService(), + val fakePushersService: FakePushersService = FakePushersService(), ) : Session by mockk(relaxed = true) { init { @@ -62,6 +64,8 @@ class FakeSession( override fun eventService() = fakeEventService override fun pushersService() = fakePushersService override fun accountDataService() = fakeSessionAccountDataService + override fun filterService() = fakeFilterService + override fun pushersService() = fakePushersService fun givenVectorStore(vectorSessionStore: VectorSessionStore) { coEvery { diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeWebRtcCallManager.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeWebRtcCallManager.kt new file mode 100644 index 0000000000..b3664bafa1 --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeWebRtcCallManager.kt @@ -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() + + fun givenCheckForProtocolsSupportIfNeededSucceeds() { + every { instance.checkForProtocolsSupportIfNeeded() } just runs + } + + fun verifyCheckForProtocolsSupportIfNeeded() { + verify { instance.checkForProtocolsSupportIfNeeded() } + } +}