mirror of
https://github.com/vector-im/element-android.git
synced 2024-11-15 01:35:07 +08:00
Adding unit tests for the new use case
This commit is contained in:
parent
68f106efac
commit
0c3310dd16
@ -26,7 +26,6 @@ import org.matrix.android.sdk.api.util.Optional
|
||||
import org.matrix.android.sdk.api.util.toOptional
|
||||
import javax.inject.Inject
|
||||
|
||||
// TODO add unit tests
|
||||
class GetDeviceFullInfoUseCase @Inject constructor(
|
||||
private val activeSessionHolder: ActiveSessionHolder,
|
||||
) {
|
||||
|
@ -18,7 +18,7 @@ package im.vector.app.features.location.live
|
||||
|
||||
import im.vector.app.test.fakes.FakeFlowLiveDataConversions
|
||||
import im.vector.app.test.fakes.FakeSession
|
||||
import im.vector.app.test.fakes.givenAsFlowReturns
|
||||
import im.vector.app.test.fakes.givenAsFlow
|
||||
import io.mockk.unmockkAll
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.test.runTest
|
||||
@ -28,7 +28,6 @@ import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconLocationDataContent
|
||||
import org.matrix.android.sdk.api.util.Optional
|
||||
|
||||
private const val A_ROOM_ID = "room_id"
|
||||
private const val AN_EVENT_ID = "event_id"
|
||||
@ -64,7 +63,7 @@ class GetLiveLocationShareSummaryUseCaseTest {
|
||||
.getRoom(A_ROOM_ID)
|
||||
.locationSharingService()
|
||||
.givenLiveLocationShareSummaryReturns(AN_EVENT_ID, summary)
|
||||
.givenAsFlowReturns(Optional(summary))
|
||||
.givenAsFlow()
|
||||
|
||||
val result = getLiveLocationShareSummaryUseCase.execute(A_ROOM_ID, AN_EVENT_ID).first()
|
||||
|
||||
@ -77,7 +76,7 @@ class GetLiveLocationShareSummaryUseCaseTest {
|
||||
.getRoom(A_ROOM_ID)
|
||||
.locationSharingService()
|
||||
.givenLiveLocationShareSummaryReturns(AN_EVENT_ID, null)
|
||||
.givenAsFlowReturns(Optional(null))
|
||||
.givenAsFlow()
|
||||
|
||||
val result = getLiveLocationShareSummaryUseCase.execute(A_ROOM_ID, AN_EVENT_ID).first()
|
||||
|
||||
|
@ -19,7 +19,7 @@ package im.vector.app.features.location.live.map
|
||||
import im.vector.app.features.location.LocationData
|
||||
import im.vector.app.test.fakes.FakeFlowLiveDataConversions
|
||||
import im.vector.app.test.fakes.FakeSession
|
||||
import im.vector.app.test.fakes.givenAsFlowReturns
|
||||
import im.vector.app.test.fakes.givenAsFlow
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.mockk
|
||||
import io.mockk.unmockkAll
|
||||
@ -81,7 +81,7 @@ class GetListOfUserLiveLocationUseCaseTest {
|
||||
.getRoom(A_ROOM_ID)
|
||||
.locationSharingService()
|
||||
.givenRunningLiveLocationShareSummariesReturns(summaries)
|
||||
.givenAsFlowReturns(summaries)
|
||||
.givenAsFlow()
|
||||
|
||||
val viewState1 = UserLiveLocationViewState(
|
||||
matrixItem = MatrixItem.UserItem(id = "@userId1:matrix.org", displayName = "User 1", avatarUrl = ""),
|
||||
|
@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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.overview
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.asFlow
|
||||
import im.vector.app.features.settings.devices.DeviceFullInfo
|
||||
import im.vector.app.test.fakes.FakeActiveSessionHolder
|
||||
import im.vector.app.test.fakes.FakeFlowLiveDataConversions
|
||||
import im.vector.app.test.fakes.givenAsFlow
|
||||
import io.mockk.unmockkAll
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo
|
||||
import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo
|
||||
import org.matrix.android.sdk.api.util.Optional
|
||||
|
||||
private const val A_DEVICE_ID = "device-id"
|
||||
|
||||
class GetDeviceFullInfoUseCaseTest {
|
||||
|
||||
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
|
||||
private val fakeFlowLiveDataConversions = FakeFlowLiveDataConversions()
|
||||
|
||||
private val getDeviceFullInfoUseCase = GetDeviceFullInfoUseCase(
|
||||
activeSessionHolder = fakeActiveSessionHolder.instance
|
||||
)
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
fakeFlowLiveDataConversions.setup()
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
unmockkAll()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given an active session and info for device when getting device info then the result is correct`() = runTest {
|
||||
val deviceInfo = DeviceInfo()
|
||||
fakeActiveSessionHolder.fakeSession.fakeCryptoService.myDevicesInfoWithIdLiveData = MutableLiveData(Optional(deviceInfo))
|
||||
fakeActiveSessionHolder.fakeSession.fakeCryptoService.myDevicesInfoWithIdLiveData.givenAsFlow()
|
||||
val cryptoDeviceInfo = CryptoDeviceInfo(deviceId = A_DEVICE_ID, userId = "")
|
||||
fakeActiveSessionHolder.fakeSession.fakeCryptoService.cryptoDeviceInfoWithIdLiveData = MutableLiveData(Optional(cryptoDeviceInfo))
|
||||
fakeActiveSessionHolder.fakeSession.fakeCryptoService.cryptoDeviceInfoWithIdLiveData.givenAsFlow()
|
||||
|
||||
val deviceFullInfo = getDeviceFullInfoUseCase.execute(A_DEVICE_ID).firstOrNull()
|
||||
|
||||
deviceFullInfo shouldBeEqualTo Optional(DeviceFullInfo(deviceInfo = deviceInfo, cryptoDeviceInfo = cryptoDeviceInfo))
|
||||
verify { fakeActiveSessionHolder.instance.getSafeActiveSession() }
|
||||
verify { fakeActiveSessionHolder.fakeSession.fakeCryptoService.getMyDevicesInfoLive(A_DEVICE_ID).asFlow() }
|
||||
verify { fakeActiveSessionHolder.fakeSession.fakeCryptoService.getLiveCryptoDeviceInfoWithId(A_DEVICE_ID).asFlow() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given an active session and no info for device when getting device info then the result is null`() = runTest {
|
||||
fakeActiveSessionHolder.fakeSession.fakeCryptoService.myDevicesInfoWithIdLiveData = MutableLiveData(Optional(null))
|
||||
fakeActiveSessionHolder.fakeSession.fakeCryptoService.myDevicesInfoWithIdLiveData.givenAsFlow()
|
||||
fakeActiveSessionHolder.fakeSession.fakeCryptoService.cryptoDeviceInfoWithIdLiveData = MutableLiveData(Optional(null))
|
||||
fakeActiveSessionHolder.fakeSession.fakeCryptoService.cryptoDeviceInfoWithIdLiveData.givenAsFlow()
|
||||
|
||||
val deviceFullInfo = getDeviceFullInfoUseCase.execute(A_DEVICE_ID).firstOrNull()
|
||||
|
||||
deviceFullInfo shouldBeEqualTo Optional(null)
|
||||
verify { fakeActiveSessionHolder.instance.getSafeActiveSession() }
|
||||
verify { fakeActiveSessionHolder.fakeSession.fakeCryptoService.getMyDevicesInfoLive(A_DEVICE_ID).asFlow() }
|
||||
verify { fakeActiveSessionHolder.fakeSession.fakeCryptoService.getLiveCryptoDeviceInfoWithId(A_DEVICE_ID).asFlow() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given no active session when getting device info then the result is empty`() = runTest {
|
||||
fakeActiveSessionHolder.givenGetSafeActiveSessionReturns(null)
|
||||
|
||||
val deviceFullInfo = getDeviceFullInfoUseCase.execute(A_DEVICE_ID).firstOrNull()
|
||||
|
||||
deviceFullInfo shouldBeEqualTo null
|
||||
verify { fakeActiveSessionHolder.instance.getSafeActiveSession() }
|
||||
}
|
||||
}
|
@ -19,7 +19,7 @@ package im.vector.app.test
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
|
||||
|
||||
private val testDispatcher = UnconfinedTestDispatcher()
|
||||
internal val testDispatcher = UnconfinedTestDispatcher()
|
||||
|
||||
internal val testCoroutineDispatchers = MatrixCoroutineDispatchers(
|
||||
io = testDispatcher,
|
||||
|
@ -33,4 +33,8 @@ class FakeActiveSessionHolder(
|
||||
fun expectSetsActiveSession(session: Session) {
|
||||
justRun { instance.setActiveSession(session) }
|
||||
}
|
||||
|
||||
fun givenGetSafeActiveSessionReturns(session: Session?) {
|
||||
every { instance.getSafeActiveSession() } returns session
|
||||
}
|
||||
}
|
||||
|
@ -20,11 +20,15 @@ import androidx.lifecycle.MutableLiveData
|
||||
import io.mockk.mockk
|
||||
import org.matrix.android.sdk.api.session.crypto.CryptoService
|
||||
import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo
|
||||
import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo
|
||||
import org.matrix.android.sdk.api.util.Optional
|
||||
|
||||
class FakeCryptoService : CryptoService by mockk() {
|
||||
|
||||
var roomKeysExport = ByteArray(size = 1)
|
||||
var cryptoDeviceInfos = mutableMapOf<String, CryptoDeviceInfo>()
|
||||
var cryptoDeviceInfoWithIdLiveData: MutableLiveData<Optional<CryptoDeviceInfo>> = MutableLiveData()
|
||||
var myDevicesInfoWithIdLiveData: MutableLiveData<Optional<DeviceInfo>> = MutableLiveData()
|
||||
|
||||
override suspend fun exportRoomKeys(password: String) = roomKeysExport
|
||||
|
||||
@ -35,4 +39,8 @@ class FakeCryptoService : CryptoService by mockk() {
|
||||
override fun getLiveCryptoDeviceInfo(userIds: List<String>) = MutableLiveData(
|
||||
cryptoDeviceInfos.filterKeys { userIds.contains(it) }.values.toList()
|
||||
)
|
||||
|
||||
override fun getLiveCryptoDeviceInfoWithId(deviceId: String) = cryptoDeviceInfoWithIdLiveData
|
||||
|
||||
override fun getMyDevicesInfoLive(deviceId: String) = myDevicesInfoWithIdLiveData
|
||||
}
|
||||
|
@ -28,6 +28,6 @@ class FakeFlowLiveDataConversions {
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> LiveData<T>.givenAsFlowReturns(value: T) {
|
||||
every { asFlow() } returns flowOf(value)
|
||||
fun <T> LiveData<T>.givenAsFlow() {
|
||||
every { asFlow() } returns flowOf(value!!)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user