Rewrites HeartRateListenerTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-09-18 19:59:05 -07:00
parent 8acb13ee37
commit a9abfbe3cf

View file

@ -3,14 +3,13 @@ package info.nightscout.androidaps.heartrate
import android.content.Context import android.content.Context
import android.hardware.Sensor import android.hardware.Sensor
import android.hardware.SensorManager import android.hardware.SensorManager
import com.google.common.truth.Truth.assertThat
import info.nightscout.rx.AapsSchedulers import info.nightscout.rx.AapsSchedulers
import info.nightscout.rx.logging.AAPSLoggerTest import info.nightscout.rx.logging.AAPSLoggerTest
import info.nightscout.rx.weardata.EventData.ActionHeartRate import info.nightscout.rx.weardata.EventData.ActionHeartRate
import io.reactivex.rxjava3.core.Scheduler import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.disposables.Disposable import io.reactivex.rxjava3.disposables.Disposable
import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.any import org.mockito.ArgumentMatchers.any
@ -22,8 +21,9 @@ import org.mockito.Mockito.`when`
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
internal class HeartRateListenerTest { internal class HeartRateListenerTest {
private val aapsLogger = AAPSLoggerTest() private val aapsLogger = AAPSLoggerTest()
private val aapsSchedulers = object: AapsSchedulers { private val aapsSchedulers = object : AapsSchedulers {
override val main: Scheduler = mock(Scheduler::class.java) override val main: Scheduler = mock(Scheduler::class.java)
override val io: Scheduler = mock(Scheduler::class.java) override val io: Scheduler = mock(Scheduler::class.java)
override val cpu: Scheduler = mock(Scheduler::class.java) override val cpu: Scheduler = mock(Scheduler::class.java)
@ -35,11 +35,15 @@ internal class HeartRateListenerTest {
private fun create(timestampMillis: Long): HeartRateListener { private fun create(timestampMillis: Long): HeartRateListener {
val ctx = mock(Context::class.java) val ctx = mock(Context::class.java)
`when`(aapsSchedulers.io.schedulePeriodicallyDirect( `when`(
any(), eq(60_000L), eq(60_000L), eq(TimeUnit.MILLISECONDS))).thenReturn(schedule) aapsSchedulers.io.schedulePeriodicallyDirect(
any(), eq(60_000L), eq(60_000L), eq(TimeUnit.MILLISECONDS)
)
).thenReturn(schedule)
val listener = HeartRateListener(ctx, aapsLogger, aapsSchedulers, timestampMillis) val listener = HeartRateListener(ctx, aapsLogger, aapsSchedulers, timestampMillis)
verify(aapsSchedulers.io).schedulePeriodicallyDirect( verify(aapsSchedulers.io).schedulePeriodicallyDirect(
any(), eq(60_000L), eq(60_000L), eq(TimeUnit.MILLISECONDS)) any(), eq(60_000L), eq(60_000L), eq(TimeUnit.MILLISECONDS)
)
listener.sendHeartRate = { hr -> heartRates.add(hr) } listener.sendHeartRate = { hr -> heartRates.add(hr) }
return listener return listener
} }
@ -49,10 +53,11 @@ internal class HeartRateListenerTest {
timestamp: Long, timestamp: Long,
heartRate: Int, heartRate: Int,
sensorType: Int? = Sensor.TYPE_HEART_RATE, sensorType: Int? = Sensor.TYPE_HEART_RATE,
accuracy: Int = SensorManager.SENSOR_STATUS_ACCURACY_HIGH) { accuracy: Int = SensorManager.SENSOR_STATUS_ACCURACY_HIGH
) {
listener.onSensorChanged(sensorType, accuracy, timestamp, floatArrayOf(heartRate.toFloat())) listener.onSensorChanged(sensorType, accuracy, timestamp, floatArrayOf(heartRate.toFloat()))
} }
@BeforeEach @BeforeEach
fun before() { fun before() {
heartRates.clear() heartRates.clear()
@ -66,7 +71,7 @@ internal class HeartRateListenerTest {
Mockito.verifyNoInteractions(aapsSchedulers.newThread) Mockito.verifyNoInteractions(aapsSchedulers.newThread)
verify(schedule).dispose() verify(schedule).dispose()
} }
@Test @Test
fun onSensorChanged() { fun onSensorChanged() {
val start = System.currentTimeMillis() val start = System.currentTimeMillis()
@ -74,14 +79,13 @@ internal class HeartRateListenerTest {
val d2 = 20_000L val d2 = 20_000L
val listener = create(start) val listener = create(start)
assertNull(listener.currentHeartRateBpm) assertThat(listener.currentHeartRateBpm).isNull()
sendSensorEvent(listener, start + d1, 80) sendSensorEvent(listener, start + d1, 80)
assertEquals(0, heartRates.size) assertThat(heartRates).isEmpty()
assertEquals(80, listener.currentHeartRateBpm) assertThat(listener.currentHeartRateBpm).isEqualTo(80)
listener.send(start + d2) listener.send(start + d2)
assertEquals(1, heartRates.size) assertThat(heartRates).containsExactly(ActionHeartRate(d2, start + d2, 80.0, device))
assertEquals(ActionHeartRate(d2, start + d2, 80.0, device), heartRates.first())
listener.dispose() listener.dispose()
} }
@ -93,16 +97,15 @@ internal class HeartRateListenerTest {
val listener = create(start) val listener = create(start)
sendSensorEvent(listener, start, 80) sendSensorEvent(listener, start, 80)
assertEquals(0, heartRates.size) assertThat(heartRates).isEmpty()
assertEquals(80, listener.currentHeartRateBpm) assertThat(listener.currentHeartRateBpm).isEqualTo(80)
sendSensorEvent(listener, start + d1,100) sendSensorEvent(listener, start + d1, 100)
assertEquals(0, heartRates.size) assertThat(heartRates).isEmpty()
assertEquals(100, listener.currentHeartRateBpm) assertThat(listener.currentHeartRateBpm).isEqualTo(100)
listener.send(start + d2) listener.send(start + d2)
assertEquals(1, heartRates.size) assertThat(heartRates).containsExactly(ActionHeartRate(d2, start + d2, 95.0, device))
assertEquals(ActionHeartRate(d2, start + d2, 95.0, device), heartRates.first())
listener.dispose() listener.dispose()
} }
@ -115,15 +118,15 @@ internal class HeartRateListenerTest {
sendSensorEvent(listener, start, 80) sendSensorEvent(listener, start, 80)
listener.send(start + d1) listener.send(start + d1)
assertEquals(1, heartRates.size) assertThat(heartRates).hasSize(1)
sendSensorEvent(listener, start + d1,100) sendSensorEvent(listener, start + d1, 100)
assertEquals(1, heartRates.size) assertThat(heartRates).hasSize(1)
listener.send(start + d2) listener.send(start + d2)
assertEquals(2, heartRates.size) assertThat(heartRates).containsExactly(
ActionHeartRate(d1, start + d1, 80.0, device),
assertEquals(ActionHeartRate(d1, start + d1, 80.0, device), heartRates[0]) ActionHeartRate(d2 - d1, start + d2, 100.0, device),
assertEquals(ActionHeartRate(d2 - d1, start + d2, 100.0, device), heartRates[1]) ).inOrder()
listener.dispose() listener.dispose()
} }
@ -136,11 +139,10 @@ internal class HeartRateListenerTest {
sendSensorEvent(listener, start, 80) sendSensorEvent(listener, start, 80)
sendSensorEvent(listener, start + d1, 100, accuracy = SensorManager.SENSOR_STATUS_NO_CONTACT) sendSensorEvent(listener, start + d1, 100, accuracy = SensorManager.SENSOR_STATUS_NO_CONTACT)
assertNull(listener.currentHeartRateBpm) assertThat(listener.currentHeartRateBpm).isNull()
listener.send(start + d2) listener.send(start + d2)
assertEquals(1, heartRates.size) assertThat(heartRates).containsExactly(ActionHeartRate(d2, start + d2, 80.0, device))
assertEquals(ActionHeartRate(d2, start + d2, 80.0, device), heartRates.first())
listener.dispose() listener.dispose()
} }
@ -157,8 +159,7 @@ internal class HeartRateListenerTest {
sendSensorEvent(listener, start + d2, 100) sendSensorEvent(listener, start + d2, 100)
listener.send(start + d3) listener.send(start + d3)
assertEquals(1, heartRates.size) assertThat(heartRates).containsExactly(ActionHeartRate(d3, start + d3, 95.0, device))
assertEquals(ActionHeartRate(d3, start + d3, 95.0, device), heartRates.first())
listener.dispose() listener.dispose()
} }
} }