From f17b37e8dfc132c518cb36fe1dfaa2649284fe63 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 3 Sep 2023 11:28:05 +0200 Subject: [PATCH] TherapyEventExtensionKtTest --- .../core/extensions/TherapyEventExtension.kt | 9 +++-- .../implementation/pump/WarnColorsImpl.kt | 12 +++--- .../extensions/TherapyEventExtensionKtTest.kt | 39 +++++++++++++++++++ 3 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 plugins/insulin/src/test/java/info/nightscout/core/extensions/TherapyEventExtensionKtTest.kt diff --git a/core/main/src/main/java/info/nightscout/core/extensions/TherapyEventExtension.kt b/core/main/src/main/java/info/nightscout/core/extensions/TherapyEventExtension.kt index de8d5df631..5acf5ad5aa 100644 --- a/core/main/src/main/java/info/nightscout/core/extensions/TherapyEventExtension.kt +++ b/core/main/src/main/java/info/nightscout/core/extensions/TherapyEventExtension.kt @@ -2,13 +2,14 @@ package info.nightscout.core.extensions import info.nightscout.database.entities.TherapyEvent import info.nightscout.interfaces.GlucoseUnit +import info.nightscout.shared.utils.DateUtil -fun TherapyEvent.isOlderThan(hours: Double): Boolean { - return getHoursFromStart() > hours +fun TherapyEvent.isOlderThan(hours: Double, dateUtil: DateUtil): Boolean { + return getHoursFromStart(dateUtil) > hours } -fun TherapyEvent.getHoursFromStart(): Double { - return (System.currentTimeMillis() - timestamp) / (60 * 60 * 1000.0) +fun TherapyEvent.getHoursFromStart(dateUtil: DateUtil): Double { + return (dateUtil.now() - timestamp) / (60 * 60 * 1000.0) } fun TherapyEvent.GlucoseUnit.Companion.fromConstant(units: GlucoseUnit): TherapyEvent.GlucoseUnit = diff --git a/implementation/src/main/java/info/nightscout/implementation/pump/WarnColorsImpl.kt b/implementation/src/main/java/info/nightscout/implementation/pump/WarnColorsImpl.kt index a05ca3e0ad..9074b60dab 100644 --- a/implementation/src/main/java/info/nightscout/implementation/pump/WarnColorsImpl.kt +++ b/implementation/src/main/java/info/nightscout/implementation/pump/WarnColorsImpl.kt @@ -5,11 +5,13 @@ import info.nightscout.core.extensions.isOlderThan import info.nightscout.database.entities.TherapyEvent import info.nightscout.interfaces.pump.WarnColors import info.nightscout.shared.interfaces.ResourceHelper +import info.nightscout.shared.utils.DateUtil import javax.inject.Inject import javax.inject.Singleton -@Singleton -class WarnColorsImpl @Inject constructor(val rh: ResourceHelper): WarnColors { +@Singleton class WarnColorsImpl @Inject constructor( + private val rh: ResourceHelper, private val dateUtil: DateUtil +) : WarnColors { override fun setColor(view: TextView?, value: Double, warnLevel: Double, urgentLevel: Double) { view?.setTextColor( @@ -39,9 +41,9 @@ class WarnColorsImpl @Inject constructor(val rh: ResourceHelper): WarnColors { view?.setTextColor( rh.gac( view.context, when { - therapyEvent.isOlderThan(urgentThreshold) -> info.nightscout.core.ui.R.attr.lowColor - therapyEvent.isOlderThan(warnThreshold) -> info.nightscout.core.ui.R.attr.highColor - else -> info.nightscout.core.ui.R.attr.defaultTextColor + therapyEvent.isOlderThan(urgentThreshold, dateUtil) -> info.nightscout.core.ui.R.attr.lowColor + therapyEvent.isOlderThan(warnThreshold, dateUtil) -> info.nightscout.core.ui.R.attr.highColor + else -> info.nightscout.core.ui.R.attr.defaultTextColor } ) ) diff --git a/plugins/insulin/src/test/java/info/nightscout/core/extensions/TherapyEventExtensionKtTest.kt b/plugins/insulin/src/test/java/info/nightscout/core/extensions/TherapyEventExtensionKtTest.kt new file mode 100644 index 0000000000..c9e4b43e99 --- /dev/null +++ b/plugins/insulin/src/test/java/info/nightscout/core/extensions/TherapyEventExtensionKtTest.kt @@ -0,0 +1,39 @@ +package info.nightscout.core.extensions + +import info.nightscout.androidaps.TestBaseWithProfile +import info.nightscout.database.entities.TherapyEvent +import info.nightscout.database.entities.embedments.InterfaceIDs +import info.nightscout.shared.utils.T +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import org.mockito.Mockito + +class TherapyEventExtensionKtTest : TestBaseWithProfile() { + + private val now = 1000000L + + @Test + fun isOlderThan() { + val therapyEvent = TherapyEvent( + timestamp = now, + isValid = true, + type = TherapyEvent.Type.ANNOUNCEMENT, + note = "c", + enteredBy = "dddd", + glucose = 101.0, + glucoseType = TherapyEvent.MeterType.FINGER, + glucoseUnit = TherapyEvent.GlucoseUnit.MGDL, + duration = 3600000, + interfaceIDs_backing = InterfaceIDs( + nightscoutId = "nightscoutId", + pumpId = 11000, + pumpType = InterfaceIDs.PumpType.DANA_I, + pumpSerial = "b" + ) + ) + Mockito.`when`(dateUtil.now()).thenReturn(now + T.mins(30).msecs()) + Assertions.assertFalse(therapyEvent.isOlderThan(1.0, dateUtil)) + Mockito.`when`(dateUtil.now()).thenReturn(now + T.hours(2).msecs()) + Assertions.assertTrue(therapyEvent.isOlderThan(1.0, dateUtil)) + } +} \ No newline at end of file