mirror of
https://github.com/vector-im/element-android.git
synced 2024-11-15 01:35:07 +08:00
Updating the unit tests
This commit is contained in:
parent
c2c58f81d8
commit
324b3cc68c
@ -28,7 +28,6 @@ internal interface CheckIfExistingActiveLiveTask : Task<CheckIfExistingActiveLiv
|
||||
)
|
||||
}
|
||||
|
||||
// TODO unit tests
|
||||
internal class DefaultCheckIfExistingActiveLiveTask @Inject constructor(
|
||||
private val getActiveBeaconInfoForUserTask: GetActiveBeaconInfoForUserTask,
|
||||
) : CheckIfExistingActiveLiveTask {
|
||||
|
@ -41,7 +41,7 @@ internal class DefaultLocationSharingService @AssistedInject constructor(
|
||||
private val sendLiveLocationTask: SendLiveLocationTask,
|
||||
private val startLiveLocationShareTask: StartLiveLocationShareTask,
|
||||
private val stopLiveLocationShareTask: StopLiveLocationShareTask,
|
||||
private val stopCheckIfExistingActiveLiveTask: CheckIfExistingActiveLiveTask,
|
||||
private val checkIfExistingActiveLiveTask: CheckIfExistingActiveLiveTask,
|
||||
private val liveLocationShareAggregatedSummaryMapper: LiveLocationShareAggregatedSummaryMapper,
|
||||
) : LocationSharingService {
|
||||
|
||||
@ -75,7 +75,10 @@ internal class DefaultLocationSharingService @AssistedInject constructor(
|
||||
override suspend fun startLiveLocationShare(timeoutMillis: Long): UpdateLiveLocationShareResult {
|
||||
// Ensure to stop any active live before starting a new one
|
||||
if (checkIfExistingActiveLive()) {
|
||||
stopLiveLocationShare()
|
||||
val result = stopLiveLocationShare()
|
||||
if (result is UpdateLiveLocationShareResult.Failure) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
val params = StartLiveLocationShareTask.Params(
|
||||
roomId = roomId,
|
||||
@ -88,7 +91,7 @@ internal class DefaultLocationSharingService @AssistedInject constructor(
|
||||
val params = CheckIfExistingActiveLiveTask.Params(
|
||||
roomId = roomId
|
||||
)
|
||||
return stopCheckIfExistingActiveLiveTask.execute(params)
|
||||
return checkIfExistingActiveLiveTask.execute(params)
|
||||
}
|
||||
|
||||
override suspend fun stopLiveLocationShare(): UpdateLiveLocationShareResult {
|
||||
|
@ -33,7 +33,6 @@ internal interface GetActiveBeaconInfoForUserTask : Task<GetActiveBeaconInfoForU
|
||||
)
|
||||
}
|
||||
|
||||
// TODO unit tests
|
||||
internal class DefaultGetActiveBeaconInfoForUserTask @Inject constructor(
|
||||
@UserId private val userId: String,
|
||||
private val stateEventDataSource: StateEventDataSource,
|
||||
|
@ -32,7 +32,6 @@ internal interface StopLiveLocationShareTask : Task<StopLiveLocationShareTask.Pa
|
||||
)
|
||||
}
|
||||
|
||||
// TODO update unit tests
|
||||
internal class DefaultStopLiveLocationShareTask @Inject constructor(
|
||||
private val sendStateTask: SendStateTask,
|
||||
private val getActiveBeaconInfoForUserTask: GetActiveBeaconInfoForUserTask,
|
||||
|
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
|
||||
*
|
||||
* 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 org.matrix.android.sdk.internal.session.room.location
|
||||
|
||||
import io.mockk.unmockkAll
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.junit.After
|
||||
import org.junit.Test
|
||||
import org.matrix.android.sdk.api.session.events.model.Event
|
||||
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconInfoContent
|
||||
import org.matrix.android.sdk.test.fakes.FakeGetActiveBeaconInfoForUserTask
|
||||
|
||||
private const val A_USER_ID = "user-id"
|
||||
private const val A_ROOM_ID = "room-id"
|
||||
private const val A_TIMEOUT = 15_000L
|
||||
private const val AN_EPOCH = 1655210176L
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
class DefaultCheckIfExistingActiveLiveTaskTest {
|
||||
|
||||
private val fakeGetActiveBeaconInfoForUserTask = FakeGetActiveBeaconInfoForUserTask()
|
||||
|
||||
private val defaultCheckIfExistingActiveLiveTask = DefaultCheckIfExistingActiveLiveTask(
|
||||
getActiveBeaconInfoForUserTask = fakeGetActiveBeaconInfoForUserTask
|
||||
)
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
unmockkAll()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given parameters and existing active live event when calling the task then result is true`() = runTest {
|
||||
val params = CheckIfExistingActiveLiveTask.Params(
|
||||
roomId = A_ROOM_ID
|
||||
)
|
||||
val currentStateEvent = Event(
|
||||
stateKey = A_USER_ID,
|
||||
content = MessageBeaconInfoContent(
|
||||
timeout = A_TIMEOUT,
|
||||
isLive = true,
|
||||
unstableTimestampMillis = AN_EPOCH
|
||||
).toContent()
|
||||
)
|
||||
fakeGetActiveBeaconInfoForUserTask.givenExecuteReturns(currentStateEvent)
|
||||
|
||||
val result = defaultCheckIfExistingActiveLiveTask.execute(params)
|
||||
|
||||
result shouldBeEqualTo true
|
||||
val expectedGetActiveBeaconParams = GetActiveBeaconInfoForUserTask.Params(
|
||||
roomId = params.roomId
|
||||
)
|
||||
fakeGetActiveBeaconInfoForUserTask.verifyExecute(expectedGetActiveBeaconParams)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given parameters and no existing active live event when calling the task then result is false`() = runTest {
|
||||
val params = CheckIfExistingActiveLiveTask.Params(
|
||||
roomId = A_ROOM_ID
|
||||
)
|
||||
val inactiveEvents = listOf(
|
||||
// no event
|
||||
null,
|
||||
// null content
|
||||
Event(
|
||||
stateKey = A_USER_ID,
|
||||
content = null
|
||||
),
|
||||
// inactive live
|
||||
Event(
|
||||
stateKey = A_USER_ID,
|
||||
content = MessageBeaconInfoContent(
|
||||
timeout = A_TIMEOUT,
|
||||
isLive = false,
|
||||
unstableTimestampMillis = AN_EPOCH
|
||||
).toContent()
|
||||
)
|
||||
)
|
||||
|
||||
inactiveEvents.forEach { currentStateEvent ->
|
||||
fakeGetActiveBeaconInfoForUserTask.givenExecuteReturns(currentStateEvent)
|
||||
|
||||
val result = defaultCheckIfExistingActiveLiveTask.execute(params)
|
||||
|
||||
result shouldBeEqualTo false
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
|
||||
*
|
||||
* 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 org.matrix.android.sdk.internal.session.room.location
|
||||
|
||||
import io.mockk.unmockkAll
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.junit.After
|
||||
import org.junit.Test
|
||||
import org.matrix.android.sdk.api.session.events.model.Event
|
||||
import org.matrix.android.sdk.api.session.events.model.EventType
|
||||
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconInfoContent
|
||||
import org.matrix.android.sdk.test.fakes.FakeStateEventDataSource
|
||||
|
||||
private const val A_USER_ID = "user-id"
|
||||
private const val A_ROOM_ID = "room-id"
|
||||
private const val A_TIMEOUT = 15_000L
|
||||
private const val AN_EPOCH = 1655210176L
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
class DefaultGetActiveBeaconInfoForUserTaskTest {
|
||||
|
||||
private val fakeStateEventDataSource = FakeStateEventDataSource()
|
||||
|
||||
private val defaultGetActiveBeaconInfoForUserTask = DefaultGetActiveBeaconInfoForUserTask(
|
||||
userId = A_USER_ID,
|
||||
stateEventDataSource = fakeStateEventDataSource.instance
|
||||
)
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
unmockkAll()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given parameters and no error when calling the task then result is computed`() = runTest {
|
||||
val currentStateEvent = Event(
|
||||
stateKey = A_USER_ID,
|
||||
content = MessageBeaconInfoContent(
|
||||
timeout = A_TIMEOUT,
|
||||
isLive = true,
|
||||
unstableTimestampMillis = AN_EPOCH
|
||||
).toContent()
|
||||
)
|
||||
fakeStateEventDataSource.givenGetStateEventReturns(currentStateEvent)
|
||||
val params = GetActiveBeaconInfoForUserTask.Params(
|
||||
roomId = A_ROOM_ID
|
||||
)
|
||||
|
||||
val result = defaultGetActiveBeaconInfoForUserTask.execute(params)
|
||||
|
||||
result shouldBeEqualTo currentStateEvent
|
||||
fakeStateEventDataSource.verifyGetStateEvent(
|
||||
roomId = params.roomId,
|
||||
eventType = EventType.STATE_ROOM_BEACON_INFO.first(),
|
||||
stateKey = A_USER_ID
|
||||
)
|
||||
}
|
||||
}
|
@ -60,6 +60,7 @@ internal class DefaultLocationSharingServiceTest {
|
||||
private val sendLiveLocationTask = mockk<SendLiveLocationTask>()
|
||||
private val startLiveLocationShareTask = mockk<StartLiveLocationShareTask>()
|
||||
private val stopLiveLocationShareTask = mockk<StopLiveLocationShareTask>()
|
||||
private val checkIfExistingActiveLiveTask = mockk<CheckIfExistingActiveLiveTask>()
|
||||
private val fakeLiveLocationShareAggregatedSummaryMapper = mockk<LiveLocationShareAggregatedSummaryMapper>()
|
||||
|
||||
private val defaultLocationSharingService = DefaultLocationSharingService(
|
||||
@ -69,6 +70,7 @@ internal class DefaultLocationSharingServiceTest {
|
||||
sendLiveLocationTask = sendLiveLocationTask,
|
||||
startLiveLocationShareTask = startLiveLocationShareTask,
|
||||
stopLiveLocationShareTask = stopLiveLocationShareTask,
|
||||
checkIfExistingActiveLiveTask = checkIfExistingActiveLiveTask,
|
||||
liveLocationShareAggregatedSummaryMapper = fakeLiveLocationShareAggregatedSummaryMapper
|
||||
)
|
||||
|
||||
@ -130,17 +132,65 @@ internal class DefaultLocationSharingServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `live location share can be started with a given timeout`() = runTest {
|
||||
fun `given existing active live can be stopped when starting a live then the current live is stopped and the new live is started`() = runTest {
|
||||
coEvery { checkIfExistingActiveLiveTask.execute(any()) } returns true
|
||||
coEvery { stopLiveLocationShareTask.execute(any()) } returns UpdateLiveLocationShareResult.Success("stopped-event-id")
|
||||
coEvery { startLiveLocationShareTask.execute(any()) } returns UpdateLiveLocationShareResult.Success(AN_EVENT_ID)
|
||||
|
||||
val result = defaultLocationSharingService.startLiveLocationShare(A_TIMEOUT)
|
||||
|
||||
result shouldBeEqualTo UpdateLiveLocationShareResult.Success(AN_EVENT_ID)
|
||||
val expectedParams = StartLiveLocationShareTask.Params(
|
||||
val expectedCheckExistingParams = CheckIfExistingActiveLiveTask.Params(
|
||||
roomId = A_ROOM_ID
|
||||
)
|
||||
coVerify { checkIfExistingActiveLiveTask.execute(expectedCheckExistingParams) }
|
||||
val expectedStopParams = StopLiveLocationShareTask.Params(
|
||||
roomId = A_ROOM_ID
|
||||
)
|
||||
coVerify { stopLiveLocationShareTask.execute(expectedStopParams) }
|
||||
val expectedStartParams = StartLiveLocationShareTask.Params(
|
||||
roomId = A_ROOM_ID,
|
||||
timeoutMillis = A_TIMEOUT
|
||||
)
|
||||
coVerify { startLiveLocationShareTask.execute(expectedParams) }
|
||||
coVerify { startLiveLocationShareTask.execute(expectedStartParams) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given existing active live cannot be stopped when starting a live then the result is failure`() = runTest {
|
||||
coEvery { checkIfExistingActiveLiveTask.execute(any()) } returns true
|
||||
val error = Throwable()
|
||||
coEvery { stopLiveLocationShareTask.execute(any()) } returns UpdateLiveLocationShareResult.Failure(error)
|
||||
|
||||
val result = defaultLocationSharingService.startLiveLocationShare(A_TIMEOUT)
|
||||
|
||||
result shouldBeEqualTo UpdateLiveLocationShareResult.Failure(error)
|
||||
val expectedCheckExistingParams = CheckIfExistingActiveLiveTask.Params(
|
||||
roomId = A_ROOM_ID
|
||||
)
|
||||
coVerify { checkIfExistingActiveLiveTask.execute(expectedCheckExistingParams) }
|
||||
val expectedStopParams = StopLiveLocationShareTask.Params(
|
||||
roomId = A_ROOM_ID
|
||||
)
|
||||
coVerify { stopLiveLocationShareTask.execute(expectedStopParams) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given no existing active live when starting a live then the new live is started`() = runTest {
|
||||
coEvery { checkIfExistingActiveLiveTask.execute(any()) } returns false
|
||||
coEvery { startLiveLocationShareTask.execute(any()) } returns UpdateLiveLocationShareResult.Success(AN_EVENT_ID)
|
||||
|
||||
val result = defaultLocationSharingService.startLiveLocationShare(A_TIMEOUT)
|
||||
|
||||
result shouldBeEqualTo UpdateLiveLocationShareResult.Success(AN_EVENT_ID)
|
||||
val expectedCheckExistingParams = CheckIfExistingActiveLiveTask.Params(
|
||||
roomId = A_ROOM_ID
|
||||
)
|
||||
coVerify { checkIfExistingActiveLiveTask.execute(expectedCheckExistingParams) }
|
||||
val expectedStartParams = StartLiveLocationShareTask.Params(
|
||||
roomId = A_ROOM_ID,
|
||||
timeoutMillis = A_TIMEOUT
|
||||
)
|
||||
coVerify { startLiveLocationShareTask.execute(expectedStartParams) }
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -27,11 +27,10 @@ import org.matrix.android.sdk.api.session.events.model.Event
|
||||
import org.matrix.android.sdk.api.session.events.model.EventType
|
||||
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||
import org.matrix.android.sdk.api.session.room.location.UpdateLiveLocationShareResult
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageAudioContent
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconInfoContent
|
||||
import org.matrix.android.sdk.internal.session.room.state.SendStateTask
|
||||
import org.matrix.android.sdk.test.fakes.FakeGetActiveBeaconInfoForUserTask
|
||||
import org.matrix.android.sdk.test.fakes.FakeSendStateTask
|
||||
import org.matrix.android.sdk.test.fakes.FakeStateEventDataSource
|
||||
|
||||
private const val A_USER_ID = "user-id"
|
||||
private const val A_ROOM_ID = "room-id"
|
||||
@ -43,12 +42,11 @@ private const val AN_EPOCH = 1655210176L
|
||||
class DefaultStopLiveLocationShareTaskTest {
|
||||
|
||||
private val fakeSendStateTask = FakeSendStateTask()
|
||||
private val fakeStateEventDataSource = FakeStateEventDataSource()
|
||||
private val fakeGetActiveBeaconInfoForUserTask = FakeGetActiveBeaconInfoForUserTask()
|
||||
|
||||
private val defaultStopLiveLocationShareTask = DefaultStopLiveLocationShareTask(
|
||||
userId = A_USER_ID,
|
||||
sendStateTask = fakeSendStateTask,
|
||||
stateEventDataSource = fakeStateEventDataSource.instance
|
||||
getActiveBeaconInfoForUserTask = fakeGetActiveBeaconInfoForUserTask
|
||||
)
|
||||
|
||||
@After
|
||||
@ -67,7 +65,7 @@ class DefaultStopLiveLocationShareTaskTest {
|
||||
unstableTimestampMillis = AN_EPOCH
|
||||
).toContent()
|
||||
)
|
||||
fakeStateEventDataSource.givenGetStateEventReturns(currentStateEvent)
|
||||
fakeGetActiveBeaconInfoForUserTask.givenExecuteReturns(currentStateEvent)
|
||||
fakeSendStateTask.givenExecuteRetryReturns(AN_EVENT_ID)
|
||||
|
||||
val result = defaultStopLiveLocationShareTask.execute(params)
|
||||
@ -78,20 +76,21 @@ class DefaultStopLiveLocationShareTaskTest {
|
||||
isLive = false,
|
||||
unstableTimestampMillis = AN_EPOCH
|
||||
).toContent()
|
||||
val expectedParams = SendStateTask.Params(
|
||||
val expectedSendParams = SendStateTask.Params(
|
||||
roomId = params.roomId,
|
||||
stateKey = A_USER_ID,
|
||||
eventType = EventType.STATE_ROOM_BEACON_INFO.first(),
|
||||
body = expectedBeaconContent
|
||||
)
|
||||
fakeSendStateTask.verifyExecuteRetry(
|
||||
params = expectedParams,
|
||||
params = expectedSendParams,
|
||||
remainingRetry = 3
|
||||
)
|
||||
fakeStateEventDataSource.verifyGetStateEvent(
|
||||
roomId = params.roomId,
|
||||
eventType = EventType.STATE_ROOM_BEACON_INFO.first(),
|
||||
stateKey = A_USER_ID
|
||||
val expectedGetBeaconParams = GetActiveBeaconInfoForUserTask.Params(
|
||||
roomId = params.roomId
|
||||
)
|
||||
fakeGetActiveBeaconInfoForUserTask.verifyExecute(
|
||||
expectedGetBeaconParams
|
||||
)
|
||||
}
|
||||
|
||||
@ -109,18 +108,15 @@ class DefaultStopLiveLocationShareTaskTest {
|
||||
unstableTimestampMillis = AN_EPOCH
|
||||
).toContent()
|
||||
),
|
||||
// incorrect content
|
||||
// null content
|
||||
Event(
|
||||
stateKey = A_USER_ID,
|
||||
content = MessageAudioContent(
|
||||
msgType = "",
|
||||
body = ""
|
||||
).toContent()
|
||||
content = null
|
||||
)
|
||||
)
|
||||
|
||||
incorrectCurrentStateEvents.forEach { currentStateEvent ->
|
||||
fakeStateEventDataSource.givenGetStateEventReturns(currentStateEvent)
|
||||
fakeGetActiveBeaconInfoForUserTask.givenExecuteReturns(currentStateEvent)
|
||||
fakeSendStateTask.givenExecuteRetryReturns(AN_EVENT_ID)
|
||||
val params = StopLiveLocationShareTask.Params(roomId = A_ROOM_ID)
|
||||
|
||||
@ -141,7 +137,7 @@ class DefaultStopLiveLocationShareTaskTest {
|
||||
unstableTimestampMillis = AN_EPOCH
|
||||
).toContent()
|
||||
)
|
||||
fakeStateEventDataSource.givenGetStateEventReturns(currentStateEvent)
|
||||
fakeGetActiveBeaconInfoForUserTask.givenExecuteReturns(currentStateEvent)
|
||||
fakeSendStateTask.givenExecuteRetryReturns("")
|
||||
|
||||
val result = defaultStopLiveLocationShareTask.execute(params)
|
||||
@ -160,7 +156,7 @@ class DefaultStopLiveLocationShareTaskTest {
|
||||
unstableTimestampMillis = AN_EPOCH
|
||||
).toContent()
|
||||
)
|
||||
fakeStateEventDataSource.givenGetStateEventReturns(currentStateEvent)
|
||||
fakeGetActiveBeaconInfoForUserTask.givenExecuteReturns(currentStateEvent)
|
||||
val error = Throwable()
|
||||
fakeSendStateTask.givenExecuteRetryThrows(error)
|
||||
|
||||
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
|
||||
*
|
||||
* 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 org.matrix.android.sdk.test.fakes
|
||||
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.mockk
|
||||
import org.matrix.android.sdk.api.session.events.model.Event
|
||||
import org.matrix.android.sdk.internal.session.room.location.GetActiveBeaconInfoForUserTask
|
||||
|
||||
internal class FakeGetActiveBeaconInfoForUserTask : GetActiveBeaconInfoForUserTask by mockk() {
|
||||
|
||||
fun givenExecuteReturns(event: Event?) {
|
||||
coEvery { execute(any()) } returns event
|
||||
}
|
||||
|
||||
fun verifyExecute(params: GetActiveBeaconInfoForUserTask.Params) {
|
||||
coVerify { execute(params) }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user