Adding test for stop live location share task

This commit is contained in:
Maxime NATUREL 2022-06-14 15:16:06 +02:00
parent af039371e1
commit d0b598463f
3 changed files with 142 additions and 1 deletions

View File

@ -35,7 +35,6 @@ internal interface StopLiveLocationShareTask : Task<StopLiveLocationShareTask.Pa
)
}
// TODO add unit tests
internal class DefaultStopLiveLocationShareTask @Inject constructor(
@UserId private val userId: String,
private val sendStateTask: SendStateTask,

View File

@ -0,0 +1,93 @@
/*
* 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.junit.After
import org.junit.Test
import org.matrix.android.sdk.api.query.QueryStringValue
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.internal.session.room.state.SendStateTask
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"
private const val AN_EVENT_ID = "event-id"
private const val A_TIMEOUT = 15_000L
private const val AN_EPOCH = 1655210176L
@ExperimentalCoroutinesApi
class DefaultStopLiveLocationShareTaskTest {
private val fakeSendStateTask = FakeSendStateTask()
private val fakeStateEventDataSource = FakeStateEventDataSource()
private val defaultStopLiveLocationShareTask = DefaultStopLiveLocationShareTask(
userId = A_USER_ID,
sendStateTask = fakeSendStateTask,
stateEventDataSource = fakeStateEventDataSource.instance
)
@After
fun tearDown() {
unmockkAll()
}
@Test
fun `given parameters when calling the task then it is correctly executed`() = runTest {
val params = StopLiveLocationShareTask.Params(roomId = A_ROOM_ID)
val currentStateEvent = Event(
stateKey = A_USER_ID,
content = MessageBeaconInfoContent(
timeout = A_TIMEOUT,
isLive = true,
unstableTimestampMillis = AN_EPOCH
).toContent()
)
fakeStateEventDataSource.givenGetStateEventReturns(currentStateEvent)
fakeSendStateTask.givenExecuteRetryReturns(AN_EVENT_ID)
defaultStopLiveLocationShareTask.execute(params)
val expectedBeaconContent = MessageBeaconInfoContent(
timeout = A_TIMEOUT,
isLive = false,
unstableTimestampMillis = AN_EPOCH
).toContent()
val expectedParams = SendStateTask.Params(
roomId = params.roomId,
stateKey = A_USER_ID,
eventType = EventType.STATE_ROOM_BEACON_INFO.first(),
body = expectedBeaconContent
)
fakeSendStateTask.verifyExecuteRetry(
params = expectedParams,
remainingRetry = 3
)
fakeStateEventDataSource.verifyGetStateEvent(
roomId = params.roomId,
eventType = EventType.STATE_ROOM_BEACON_INFO.first(),
stateKey = QueryStringValue.Equals(A_USER_ID)
)
}
}

View File

@ -0,0 +1,49 @@
/*
* 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.every
import io.mockk.mockk
import io.mockk.verify
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.internal.session.room.state.StateEventDataSource
internal class FakeStateEventDataSource {
val instance: StateEventDataSource = mockk()
fun givenGetStateEventReturns(event: Event) {
every {
instance.getStateEvent(
roomId = any(),
eventType = any(),
stateKey = any()
)
} returns event
}
fun verifyGetStateEvent(roomId: String, eventType: String, stateKey: QueryStringValue) {
verify {
instance.getStateEvent(
roomId = roomId,
eventType = eventType,
stateKey = stateKey
)
}
}
}