diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt index df46890d50..1f2531d95b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt @@ -56,6 +56,10 @@ class InputWeekDay(injector: HasAndroidInjector) : Element(injector) { for (day in DayOfWeek.values()) set(day, false) } + fun setAll(value:Boolean) { + for (day in DayOfWeek.values()) set(day, value) + } + operator fun set(day: DayOfWeek, value: Boolean): InputWeekDay { weekdays[day.ordinal] = value return this diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.kt index 7fb9aaa560..d6f8a607ff 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.kt @@ -20,7 +20,7 @@ class TriggerAutosensValue(injector: HasAndroidInjector) : Trigger(injector) { private val maxValue = (sp.getDouble(R.string.key_openapsama_autosens_max, 1.2) * 100).toInt() private val step = 1.0 private val decimalFormat = DecimalFormat("1") - private var autosens: InputDouble = InputDouble(injector, 100.0, minValue.toDouble(), maxValue.toDouble(), step, decimalFormat) + var autosens: InputDouble = InputDouble(injector, 100.0, minValue.toDouble(), maxValue.toDouble(), step, decimalFormat) var comparator: Comparator = Comparator(injector) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.kt index 2766d2e8f1..75b30edb3b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.kt @@ -17,7 +17,7 @@ import info.nightscout.androidaps.utils.JsonHelper import org.json.JSONObject class TriggerBg(injector: HasAndroidInjector) : Trigger(injector) { - private var bg = InputBg(injector) + var bg = InputBg(injector) var comparator = Comparator(injector) constructor(injector: HasAndroidInjector, value: Double, units: String, compare: Comparator.Compare) : this(injector) { @@ -30,6 +30,21 @@ class TriggerBg(injector: HasAndroidInjector) : Trigger(injector) { comparator = Comparator(injector, triggerBg.comparator.value) } + fun setUnits(units: String): TriggerBg { + bg.units = units + return this + } + + fun setValue(value: Double): TriggerBg { + bg.value = value + return this + } + + fun comparator(comparator: Comparator.Compare): TriggerBg { + this.comparator.value = comparator + return this + } + override fun shouldRun(): Boolean { val glucoseStatus = GlucoseStatus(injector).getGlucoseStatusData() if (glucoseStatus == null && comparator.value == Comparator.Compare.IS_NOT_AVAILABLE) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.kt index 9a7b6e9563..db57bb159a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.kt @@ -16,7 +16,7 @@ import info.nightscout.androidaps.utils.JsonHelper.safeGetString import org.json.JSONObject class TriggerBolusAgo(injector: HasAndroidInjector) : Trigger(injector) { - private var minutesAgo: InputDuration = InputDuration(injector, 0, InputDuration.TimeUnit.MINUTES) + var minutesAgo: InputDuration = InputDuration(injector, 0, InputDuration.TimeUnit.MINUTES) var comparator: Comparator = Comparator(injector) private constructor(injector: HasAndroidInjector, triggerBolusAgo: TriggerBolusAgo) : this(injector) { @@ -24,6 +24,16 @@ class TriggerBolusAgo(injector: HasAndroidInjector) : Trigger(injector) { comparator = Comparator(injector, triggerBolusAgo.comparator.value) } + fun setValue(value: Int): TriggerBolusAgo { + minutesAgo.value = value + return this + } + + fun comparator(comparator: Comparator.Compare): TriggerBolusAgo { + this.comparator.value = comparator + return this + } + override fun shouldRun(): Boolean { val lastBolusTime = treatmentsPlugin.getLastBolusTime(false) if (lastBolusTime == 0L) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOB.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOB.kt index 1d1299c93b..82aa8cd6f1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOB.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOB.kt @@ -18,7 +18,7 @@ import java.text.DecimalFormat class TriggerCOB(injector: HasAndroidInjector) : Trigger(injector) { private val minValue = 0 private val maxValue = sp.getInt(R.string.key_treatmentssafety_maxcarbs, 48) - private var cob: InputDouble = InputDouble(injector, 0.0, minValue.toDouble(), maxValue.toDouble(), 1.0, DecimalFormat("1")) + var cob: InputDouble = InputDouble(injector, 0.0, minValue.toDouble(), maxValue.toDouble(), 1.0, DecimalFormat("1")) var comparator: Comparator = Comparator(injector) private constructor(injector: HasAndroidInjector, triggerCOB: TriggerCOB) : this(injector) { @@ -26,6 +26,16 @@ class TriggerCOB(injector: HasAndroidInjector) : Trigger(injector) { comparator = Comparator(injector, triggerCOB.comparator.value) } + fun setValue(value:Double) : TriggerCOB { + cob.value = value + return this + } + + fun comparator(comparator: Comparator.Compare) : TriggerCOB { + this.comparator.value = comparator + return this + } + override fun shouldRun(): Boolean { val cobInfo = iobCobCalculatorPlugin.getCobInfo(false, "AutomationTriggerCOB") if (cobInfo.displayCob == null) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.kt index 01c64f917f..0a7972461a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.kt @@ -21,7 +21,7 @@ import java.text.DecimalFormat class TriggerDelta(injector: HasAndroidInjector) : Trigger(injector) { var units: String = Constants.MGDL - private var delta: InputDelta = InputDelta(injector) + var delta: InputDelta = InputDelta(injector) var comparator: Comparator = Comparator(injector) companion object { @@ -41,6 +41,22 @@ class TriggerDelta(injector: HasAndroidInjector) : Trigger(injector) { comparator = Comparator(injector, triggerDelta.comparator.value) } + fun units(units: String): TriggerDelta { + this.units = units + return this + } + + fun setValue(value: Double, type: DeltaType): TriggerDelta { + this.delta.value = value + this.delta.deltaType = type + return this + } + + fun comparator(comparator: Comparator.Compare): TriggerDelta { + this.comparator.value = comparator + return this + } + override fun shouldRun(): Boolean { val glucoseStatus = GlucoseStatus(injector).getGlucoseStatusData() ?: return if (comparator.value == Comparator.Compare.IS_NOT_AVAILABLE) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.kt index 09d4a49892..0db45fcf9c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.kt @@ -15,7 +15,7 @@ import info.nightscout.androidaps.utils.JsonHelper import org.json.JSONObject class TriggerIob(injector: HasAndroidInjector) : Trigger(injector) { - private var insulin = InputInsulin(injector) + var insulin = InputInsulin(injector) var comparator: Comparator = Comparator(injector) constructor(injector: HasAndroidInjector, triggerIob: TriggerIob) : this(injector) { @@ -23,12 +23,20 @@ class TriggerIob(injector: HasAndroidInjector) : Trigger(injector) { comparator = Comparator(injector, triggerIob.comparator.value) } - val value: Double = 0.0 + fun setValue(value: Double): TriggerIob { + insulin.value = value + return this + } + + fun comparator(comparator: Comparator.Compare): TriggerIob { + this.comparator.value = comparator + return this + } override fun shouldRun(): Boolean { val profile = profileFunction.getProfile() ?: return false val iob = iobCobCalculatorPlugin.calculateFromTreatmentsAndTempsSynchronized(DateUtil.now(), profile) - if (comparator.value.check(iob.iob, value)) { + if (comparator.value.check(iob.iob, insulin.value)) { aapsLogger.debug(LTag.AUTOMATION, "Ready for execution: " + friendlyDescription()) return true } @@ -38,7 +46,7 @@ class TriggerIob(injector: HasAndroidInjector) : Trigger(injector) { @Synchronized override fun toJSON(): String { val data = JSONObject() - .put("insulin", value) + .put("insulin", insulin.value) .put("comparator", comparator.value.toString()) return JSONObject() .put("type", this::class.java.name) @@ -56,7 +64,7 @@ class TriggerIob(injector: HasAndroidInjector) : Trigger(injector) { override fun friendlyName(): Int = R.string.iob override fun friendlyDescription(): String = - resourceHelper.gs(R.string.iobcompared, resourceHelper.gs(comparator.value.stringRes), value) + resourceHelper.gs(R.string.iobcompared, resourceHelper.gs(comparator.value.stringRes), insulin.value) override fun icon(): Optional = Optional.of(R.drawable.ic_keyboard_capslock) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.kt index 67bcb4196f..1dea3cd15f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.kt @@ -14,7 +14,7 @@ import info.nightscout.androidaps.utils.JsonHelper import org.json.JSONObject class TriggerProfilePercent(injector: HasAndroidInjector) : Trigger(injector) { - private var pct = InputPercent(injector) + var pct = InputPercent(injector) var comparator = Comparator(injector) constructor(injector: HasAndroidInjector, value: Double, compare: Comparator.Compare) : this(injector) { @@ -27,6 +27,16 @@ class TriggerProfilePercent(injector: HasAndroidInjector) : Trigger(injector) { comparator = Comparator(injector, triggerProfilePercent.comparator.value) } + fun setValue(value: Double): TriggerProfilePercent { + this.pct.value = value + return this + } + + fun comparator(comparator: Comparator.Compare): TriggerProfilePercent { + this.comparator.value = comparator + return this + } + override fun shouldRun(): Boolean { val profile = profileFunction.getProfile() if (profile == null && comparator.value == Comparator.Compare.IS_NOT_AVAILABLE) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnection.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnection.kt index 235f14d6a1..e0fc0fa46d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnection.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnection.kt @@ -16,8 +16,8 @@ import info.nightscout.androidaps.utils.JsonHelper.safeGetString import org.json.JSONObject class TriggerPumpLastConnection(injector: HasAndroidInjector) : Trigger(injector) { - private var minutesAgo = InputDuration(injector) - private var comparator = Comparator(injector) + var minutesAgo = InputDuration(injector) + var comparator = Comparator(injector) constructor(injector: HasAndroidInjector, value: Int, unit: InputDuration.TimeUnit, compare: Comparator.Compare) : this(injector) { minutesAgo = InputDuration(injector, value, unit) @@ -29,6 +29,16 @@ class TriggerPumpLastConnection(injector: HasAndroidInjector) : Trigger(injector comparator = Comparator(injector, triggerPumpLastConnection.comparator.value) } + fun setValue(value: Int): TriggerPumpLastConnection { + minutesAgo.value = value + return this + } + + fun comparator(comparator: Comparator.Compare): TriggerPumpLastConnection { + this.comparator.value = comparator + return this + } + override fun shouldRun(): Boolean { val lastConnection = activePlugin.activePumpPlugin?.lastDataTime() ?: return false if (lastConnection == 0L && comparator.value === Comparator.Compare.IS_NOT_AVAILABLE) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt index 459bef5e43..11dd1e0475 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt @@ -18,8 +18,8 @@ import org.json.JSONObject import java.util.* class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) { - private val days = InputWeekDay(injector) - private val time = InputTime(injector) + val days = InputWeekDay(injector) + val time = InputTime(injector) constructor(injector: HasAndroidInjector, triggerRecurringTime: TriggerRecurringTime) : this(injector) { this.time.value = triggerRecurringTime.time.value @@ -27,6 +27,11 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) { System.arraycopy(triggerRecurringTime.days.weekdays, 0, days.weekdays, 0, triggerRecurringTime.days.weekdays.size) } + fun time(minutes:Int): TriggerRecurringTime { + time.value = minutes + return this + } + override fun shouldRun(): Boolean { val currentMinSinceMidnight = getMinSinceMidnight(DateUtil.now()) val scheduledDayOfWeek = Calendar.getInstance()[Calendar.DAY_OF_WEEK] diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.kt index 56cd9ab527..015cfce079 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.kt @@ -5,6 +5,7 @@ import com.google.common.base.Optional import dagger.android.HasAndroidInjector import info.nightscout.androidaps.R import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator import info.nightscout.androidaps.plugins.general.automation.elements.ComparatorExists import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel @@ -22,6 +23,11 @@ class TriggerTempTarget(injector: HasAndroidInjector) : Trigger(injector) { comparator = ComparatorExists(injector, triggerTempTarget.comparator.value) } + fun comparator(comparator: ComparatorExists.Compare): TriggerTempTarget { + this.comparator.value = comparator + return this + } + override fun shouldRun(): Boolean { val tt = treatmentsPlugin.tempTargetFromHistory if (tt == null && comparator.value == ComparatorExists.Compare.NOT_EXISTS) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.kt index 6404387af8..4726eb5e95 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.kt @@ -24,6 +24,11 @@ class TriggerTime(injector: HasAndroidInjector) : Trigger(injector) { this.time.value = triggerTime.time.value } + fun runAt(time: Long): TriggerTime { + this.time.value = time + return this + } + override fun shouldRun(): Boolean { val now = DateUtil.now() if (now >= time.value && now - time.value < T.mins(5).msecs()) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt index 82d749752e..46edd9a0b2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt @@ -31,6 +31,12 @@ class TriggerTimeRange(injector: HasAndroidInjector) : Trigger(injector) { range.end = triggerTimeRange.range.end } + fun period(start: Int, end:Int) : TriggerTimeRange { + this.range.start = start + this.range.end = end + return this + } + override fun shouldRun(): Boolean { val currentMinSinceMidnight = getMinSinceMidnight(DateUtil.now()) var doRun = false @@ -46,8 +52,8 @@ class TriggerTimeRange(injector: HasAndroidInjector) : Trigger(injector) { override fun toJSON(): String { val data = JSONObject() - .put("start", getMinSinceMidnight(range.start.toLong())) - .put("end", getMinSinceMidnight(range.end.toLong())) + .put("start", range.start) + .put("end", range.end) return JSONObject() .put("type", this::class.java.name) .put("data", data) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.kt index 3c1ac20375..341a8fdf68 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.kt @@ -15,7 +15,7 @@ import info.nightscout.androidaps.utils.JsonHelper import org.json.JSONObject class TriggerWifiSsid(injector: HasAndroidInjector) : Trigger(injector) { - private var ssid = InputString(injector) + var ssid = InputString(injector) var comparator = Comparator(injector) constructor(injector: HasAndroidInjector, ssid: String, compare: Comparator.Compare) : this(injector) { @@ -28,6 +28,16 @@ class TriggerWifiSsid(injector: HasAndroidInjector) : Trigger(injector) { comparator = Comparator(injector, triggerWifiSsid.comparator.value) } + fun setValue(ssid: String): TriggerWifiSsid { + this.ssid.value = ssid + return this + } + + fun comparator(comparator: Comparator.Compare): TriggerWifiSsid { + this.comparator.value = comparator + return this + } + override fun shouldRun(): Boolean { val eventNetworkChange = NetworkChangeReceiver.getLastEvent() ?: return false if (!eventNetworkChange.wifiConnected && comparator.value == Comparator.Compare.IS_NOT_AVAILABLE) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/AutosensData.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/AutosensData.java index e6eebaa799..6b3b4ef3de 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/AutosensData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/AutosensData.java @@ -33,7 +33,7 @@ public class AutosensData implements DataPointWithLabelInterface { @Inject SensitivityAAPSPlugin sensitivityAAPSPlugin; @Inject ProfileFunction profileFunction; - AutosensData(HasAndroidInjector injector) { + public AutosensData(HasAndroidInjector injector) { injector.androidInjector().inject(this); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.java deleted file mode 100644 index b266b44d38..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.java +++ /dev/null @@ -1,123 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; -import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.utils.SP; - -import static org.mockito.ArgumentMatchers.anyDouble; -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ProfileFunctions.class, DateUtil.class, IobCobCalculatorPlugin.class, SP.class}) -public class TriggerAutosensValueTest { - - long now = 1514766900000L; - - @Test - public void shouldRunTest() { - Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d); - Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d); - when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(generateAutosensData()); - TriggerAutosensValue t = new TriggerAutosensValue().setValue(110).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(110, t.getValue(), 0.01d); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t.getComparator().getValue()); - Assert.assertFalse(t.shouldRun()); - t = new TriggerAutosensValue().setValue(100).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(100, t.getValue(), 0.01d); - Assert.assertTrue(t.shouldRun()); - t = new TriggerAutosensValue().setValue(50).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerAutosensValue().setValue(310).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerAutosensValue().setValue(420).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - t = new TriggerAutosensValue().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerAutosensValue().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertFalse(t.shouldRun()); - t = new TriggerAutosensValue().setValue(20).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerAutosensValue().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - - when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(new AutosensData()); - t = new TriggerAutosensValue().setValue(80).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertFalse(t.shouldRun()); - - // Test autosensData == null and Comparator == IS_NOT_AVAILABLE - when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(null); - t = new TriggerAutosensValue().comparator(Comparator.Compare.IS_NOT_AVAILABLE); - Assert.assertTrue(t.shouldRun()); - - t = new TriggerAutosensValue().setValue(214).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - - } - - @Test - public void copyConstructorTest() { - TriggerAutosensValue t = new TriggerAutosensValue().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - TriggerAutosensValue t1 = (TriggerAutosensValue) t.duplicate(); - Assert.assertEquals(213, t1.getValue(), 0.01d); - Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); - } - - String ASJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"value\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}"; - - @Test - public void toJSONTest() { - TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(ASJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL); - - TriggerAutosensValue t2 = (TriggerAutosensValue) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); - Assert.assertEquals(410, t2.getValue(), 0.01d); - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.as), new TriggerAutosensValue().icon()); - } - - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockIobCobCalculatorPlugin(); - AAPSMocker.mockProfileFunctions(); - AAPSMocker.mockSP(); - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn(now); - - } - - AutosensData generateAutosensData() { - AutosensData data = new AutosensData(); - return data; - } - -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.kt new file mode 100644 index 0000000000..71e9e83e5e --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.kt @@ -0,0 +1,128 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.SP +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentMatchers +import org.mockito.Mockito +import org.mockito.Mockito.`when` +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(ProfileFunctions::class, DateUtil::class, IobCobCalculatorPlugin::class, SP::class) +class TriggerAutosensValueTest : TriggerTestBase() { + + var now = 1514766900000L + @Test fun shouldRunTest() { + `when`(sp.getDouble(Mockito.eq(R.string.key_openapsama_autosens_max), ArgumentMatchers.anyDouble())).thenReturn(1.2) + `when`(sp.getDouble(Mockito.eq(R.string.key_openapsama_autosens_min), ArgumentMatchers.anyDouble())).thenReturn(0.7) + `when`(iobCobCalculatorPlugin.getLastAutosensData("Automation trigger")).thenReturn(generateAutosensData()) + var t = TriggerAutosensValue(injector) + t.autosens.value = 110.0 + t.comparator.value = Comparator.Compare.IS_EQUAL + Assert.assertEquals(110.0, t.autosens.value, 0.01) + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t.comparator.value) + Assert.assertFalse(t.shouldRun()) + t = TriggerAutosensValue(injector) + t.autosens.value = 100.0 + t.comparator.value = Comparator.Compare.IS_EQUAL + Assert.assertEquals(100.0, t.autosens.value, 0.01) + Assert.assertTrue(t.shouldRun()) + t = TriggerAutosensValue(injector) + t.autosens.value = 50.0 + t.comparator.value = Comparator.Compare.IS_EQUAL_OR_GREATER + Assert.assertTrue(t.shouldRun()) + t = TriggerAutosensValue(injector) + t.autosens.value = 310.0 + t.comparator.value = Comparator.Compare.IS_EQUAL_OR_LESSER + Assert.assertTrue(t.shouldRun()) + t = TriggerAutosensValue(injector) + t.autosens.value = 420.0 + t.comparator.value = Comparator.Compare.IS_EQUAL + Assert.assertFalse(t.shouldRun()) + t = TriggerAutosensValue(injector) + t.autosens.value = 390.0 + t.comparator.value = Comparator.Compare.IS_EQUAL_OR_LESSER + Assert.assertTrue(t.shouldRun()) + t = TriggerAutosensValue(injector) + t.autosens.value = 390.0 + t.comparator.value = Comparator.Compare.IS_EQUAL_OR_GREATER + Assert.assertFalse(t.shouldRun()) + t = TriggerAutosensValue(injector) + t.autosens.value = 20.0 + t.comparator.value = Comparator.Compare.IS_EQUAL_OR_GREATER + Assert.assertTrue(t.shouldRun()) + t = TriggerAutosensValue(injector) + t.autosens.value = 390.0 + t.comparator.value = Comparator.Compare.IS_EQUAL_OR_LESSER + Assert.assertTrue(t.shouldRun()) + PowerMockito.`when`(iobCobCalculatorPlugin.getLastAutosensData("Automation trigger")).thenReturn(AutosensData(injector)) + t = TriggerAutosensValue(injector) + t.autosens.value = 80.0 + t.comparator.value = Comparator.Compare.IS_EQUAL_OR_LESSER + Assert.assertFalse(t.shouldRun()) + + // Test autosensData == null and Comparator == IS_NOT_AVAILABLE + PowerMockito.`when`(iobCobCalculatorPlugin.getLastAutosensData("Automation trigger")).thenReturn(null) + t = TriggerAutosensValue(injector) + t.comparator.value = Comparator.Compare.IS_NOT_AVAILABLE + Assert.assertTrue(t.shouldRun()) + } + + @Test + fun copyConstructorTest() { + val t = TriggerAutosensValue(injector) + t.autosens.value = 213.0 + t.comparator.value = Comparator.Compare.IS_EQUAL_OR_LESSER + val t1 = t.duplicate() as TriggerAutosensValue + Assert.assertEquals(213.0, t1.autosens.value, 0.01) + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) + } + + private var asJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"value\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}" + + @Test + fun toJSONTest() { + val t = TriggerAutosensValue(injector) + t.autosens.value = 410.0 + t.comparator.value = Comparator.Compare.IS_EQUAL + Assert.assertEquals(asJson, t.toJSON()) + } + + @Test + fun fromJSONTest() { + val t = TriggerAutosensValue(injector) + t.autosens.value = 410.0 + t.comparator.value = Comparator.Compare.IS_EQUAL + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerAutosensValue + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) + Assert.assertEquals(410.0, t2.autosens.value, 0.01) + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.`as`), TriggerAutosensValue(injector).icon()) + } + + @Before + fun mock() { + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.`when`(DateUtil.now()).thenReturn(now) + } + + private fun generateAutosensData(): AutosensData { + return AutosensData(injector) + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBgTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBgTest.java deleted file mode 100644 index 1fb94492c9..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBgTest.java +++ /dev/null @@ -1,132 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.util.ArrayList; -import java.util.List; - -import info.AAPSMocker; -import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.db.BgReading; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; -import info.nightscout.androidaps.plugins.general.nsclient.data.NSSgv; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; -import info.nightscout.androidaps.utils.DateUtil; - -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ProfileFunctions.class, DateUtil.class, IobCobCalculatorPlugin.class}) -public class TriggerBgTest { - - long now = 1514766900000L; - - @Test - public void shouldRunTest() { - when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(generateOneCurrentRecordBgData()); - - TriggerBg t = new TriggerBg().setUnits(Constants.MMOL).setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - t = new TriggerBg().setUnits(Constants.MGDL).setValue(214).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertTrue(t.shouldRun()); - t = new TriggerBg().setUnits(Constants.MGDL).setValue(214).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerBg().setUnits(Constants.MGDL).setValue(214).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerBg().setUnits(Constants.MGDL).setValue(215).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - t = new TriggerBg().setUnits(Constants.MGDL).setValue(215).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerBg().setUnits(Constants.MGDL).setValue(215).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertFalse(t.shouldRun()); - t = new TriggerBg().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerBg().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertFalse(t.shouldRun()); - - when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(new ArrayList<>()); - t = new TriggerBg().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertFalse(t.shouldRun()); - t = new TriggerBg().comparator(Comparator.Compare.IS_NOT_AVAILABLE); - Assert.assertTrue(t.shouldRun()); - - t = new TriggerBg().setUnits(Constants.MGDL).setValue(214).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - - } - - @Test - public void copyConstructorTest() { - TriggerBg t = new TriggerBg().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - TriggerBg t1 = (TriggerBg) t.duplicate(); - Assert.assertEquals(213d, t1.getValue(), 0.01d); - Assert.assertEquals(Constants.MGDL, t1.getUnits()); - Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); - } - - @Test - public void executeTest() { - TriggerBg t = new TriggerBg().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - t.executed(1); - Assert.assertEquals(1l, t.getLastRun()); - } - - String bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"bg\":4.1,\"units\":\"mmol\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBg\"}"; - - @Test - public void toJSONTest() { - TriggerBg t = new TriggerBg().setUnits(Constants.MMOL).setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(bgJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerBg t = new TriggerBg().setUnits(Constants.MMOL).setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL); - - TriggerBg t2 = (TriggerBg) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); - Assert.assertEquals(4.1d, t2.getValue(), 0.01d); - Assert.assertEquals(Constants.MMOL, t2.getUnits()); - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.icon_cp_bgcheck), new TriggerBg().icon()); - } - - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockIobCobCalculatorPlugin(); - AAPSMocker.mockProfileFunctions(); - - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn(now); - - } - - List generateOneCurrentRecordBgData() { - List list = new ArrayList<>(); - try { - list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":214,\"mills\":" + (now - 1) + ",\"direction\":\"Flat\"}")))); - } catch (JSONException e) { - throw new RuntimeException(e); - } - return list; - } - -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBgTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBgTest.kt new file mode 100644 index 0000000000..61c1f869ba --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBgTest.kt @@ -0,0 +1,101 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.R +import info.nightscout.androidaps.db.BgReading +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import info.nightscout.androidaps.plugins.general.nsclient.data.NSSgv +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin +import info.nightscout.androidaps.utils.DateUtil +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito.`when` +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner +import java.util.* + +@RunWith(PowerMockRunner::class) +@PrepareForTest(DateUtil::class, IobCobCalculatorPlugin::class, ProfileFunction::class) +class TriggerBgTest : TriggerTestBase() { + + var now = 1514766900000L + + @Before + fun prepare() { + `when`(profileFunction.getUnits()).thenReturn(Constants.MGDL) + `when`(iobCobCalculatorPlugin.dataLock).thenReturn(Unit) + PowerMockito.mockStatic(DateUtil::class.java) + `when`(DateUtil.now()).thenReturn(now) + } + + @Test + fun shouldRunTest() { + `when`(iobCobCalculatorPlugin.bgReadings).thenReturn(generateOneCurrentRecordBgData()) + var t: TriggerBg = TriggerBg(injector).setUnits(Constants.MMOL).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerBg(injector).setUnits(Constants.MGDL).setValue(214.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertTrue(t.shouldRun()) + t = TriggerBg(injector).setUnits(Constants.MGDL).setValue(214.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerBg(injector).setUnits(Constants.MGDL).setValue(214.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerBg(injector).setUnits(Constants.MGDL).setValue(215.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerBg(injector).setUnits(Constants.MGDL).setValue(215.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerBg(injector).setUnits(Constants.MGDL).setValue(215.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertFalse(t.shouldRun()) + t = TriggerBg(injector).setUnits(Constants.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerBg(injector).setUnits(Constants.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertFalse(t.shouldRun()) + `when`(iobCobCalculatorPlugin.bgReadings).thenReturn(ArrayList()) + t = TriggerBg(injector).setUnits(Constants.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertFalse(t.shouldRun()) + t = TriggerBg(injector).comparator(Comparator.Compare.IS_NOT_AVAILABLE) + Assert.assertTrue(t.shouldRun()) + } + + @Test + fun copyConstructorTest() { + val t: TriggerBg = TriggerBg(injector).setUnits(Constants.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + val t1 = t.duplicate() as TriggerBg + Assert.assertEquals(213.0, t1.bg.value, 0.01) + Assert.assertEquals(Constants.MGDL, t1.bg.units) + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) + } + + private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"bg\":4.1,\"units\":\"mmol\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBg\"}" + + @Test + fun toJSONTest() { + val t: TriggerBg = TriggerBg(injector).setUnits(Constants.MMOL).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(bgJson, t.toJSON()) + } + + @Test + fun fromJSONTest() { + val t: TriggerBg = TriggerBg(injector).setUnits(Constants.MMOL).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerBg + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) + Assert.assertEquals(4.1, t2.bg.value, 0.01) + Assert.assertEquals(Constants.MMOL, t2.bg.units) + } + + @Test + fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.icon_cp_bgcheck), TriggerBg(injector).icon()) + } + + private fun generateOneCurrentRecordBgData(): List { + val list: MutableList = ArrayList() + list.add(BgReading(NSSgv(JSONObject("{\"mgdl\":214,\"mills\":" + (now - 1) + ",\"direction\":\"Flat\"}")))) + return list + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgoTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgoTest.java deleted file mode 100644 index 1d3a45a05e..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgoTest.java +++ /dev/null @@ -1,118 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; -import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; -import info.nightscout.androidaps.utils.DateUtil; - -import static org.mockito.ArgumentMatchers.anyDouble; -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ProfileFunctions.class, DateUtil.class, TreatmentsPlugin.class}) -public class TriggerBolusAgoTest { - - long now = 1514766900000L; - - @Test - public void shouldRunTest() { - when(DateUtil.now()).thenReturn(now); // set current time to now + 10 min - TriggerBolusAgo t = new TriggerBolusAgo().setValue(110).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - when(TreatmentsPlugin.getPlugin().getLastBolusTime(false)).thenReturn(now); // Set last bolus time to now - when(DateUtil.now()).thenReturn(now + (10*60*1000)); // set current time to now + 10 min - t = new TriggerBolusAgo().setValue(110).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(110, t.getValue(), 0.01d); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t.getComparator().getValue()); - Assert.assertFalse(t.shouldRun()); - t = new TriggerBolusAgo().setValue(10).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(10, t.getValue(), 0.01d); - Assert.assertTrue(t.shouldRun()); - t = new TriggerBolusAgo().setValue(5).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerBolusAgo().setValue(310).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerBolusAgo().setValue(420).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - t = new TriggerBolusAgo().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerBolusAgo().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertFalse(t.shouldRun()); - t = new TriggerBolusAgo().setValue(2).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerBolusAgo().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - - when(TreatmentsPlugin.getPlugin().getLastBolusTime(false)).thenReturn(0l); // Set last bolus time to 0 - t = new TriggerBolusAgo().comparator(Comparator.Compare.IS_NOT_AVAILABLE); - Assert.assertTrue(t.shouldRun()); - - t = new TriggerBolusAgo().setValue(214).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - - } - - @Test - public void copyConstructorTest() { - TriggerBolusAgo t = new TriggerBolusAgo().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - TriggerBolusAgo t1 = (TriggerBolusAgo) t.duplicate(); - Assert.assertEquals(213, t1.getValue(), 0.01d); - Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); - } - - @Test - public void executeTest() { - TriggerBolusAgo t = new TriggerBolusAgo().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - t.executed(1); - Assert.assertEquals(1l, t.getLastRun()); - } - - String LBJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"minutesAgo\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBolusAgo\"}"; - - @Test - public void toJSONTest() { - TriggerBolusAgo t = new TriggerBolusAgo().setValue(410).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(LBJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerBolusAgo t = new TriggerBolusAgo().setValue(410).comparator(Comparator.Compare.IS_EQUAL); - - TriggerBolusAgo t2 = (TriggerBolusAgo) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); - Assert.assertEquals(410, t2.getValue(), 0.01d); - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.icon_bolus), new TriggerBolusAgo().icon()); - } - - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockProfileFunctions(); - PowerMockito.mockStatic(DateUtil.class); - AAPSMocker.mockTreatmentPlugin(); - when(DateUtil.now()).thenReturn(now); - - } - -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgoTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgoTest.kt new file mode 100644 index 0000000000..7971bbc7ef --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgoTest.kt @@ -0,0 +1,85 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.utils.DateUtil +import org.json.JSONException +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito.`when` +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(ProfileFunctions::class, DateUtil::class, TreatmentsPlugin::class) +class TriggerBolusAgoTest : TriggerTestBase() { + + var now = 1514766900000L + + @Before + fun mock() { + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.`when`(DateUtil.now()).thenReturn(now) + } + + @Test + fun shouldRunTest() { + `when`(treatmentsPlugin.getLastBolusTime(false)).thenReturn(now) // Set last bolus time to now + `when`(DateUtil.now()).thenReturn(now + 10 * 60 * 1000) // set current time to now + 10 min + var t = TriggerBolusAgo(injector).setValue(110).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(110, t.minutesAgo.value) + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t.comparator.value) + Assert.assertFalse(t.shouldRun()) + t = TriggerBolusAgo(injector).setValue(10).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(10, t.minutesAgo.value) + Assert.assertTrue(t.shouldRun()) + t = TriggerBolusAgo(injector).setValue(5).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerBolusAgo(injector).setValue(310).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerBolusAgo(injector).setValue(420).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerBolusAgo(injector).setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerBolusAgo(injector).setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertFalse(t.shouldRun()) + t = TriggerBolusAgo(injector).setValue(2).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerBolusAgo(injector).setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + PowerMockito.`when`(treatmentsPlugin.getLastBolusTime(false)).thenReturn(0L) // Set last bolus time to 0 + t = TriggerBolusAgo(injector).comparator(Comparator.Compare.IS_NOT_AVAILABLE) + Assert.assertTrue(t.shouldRun()) + } + + @Test fun copyConstructorTest() { + val t: TriggerBolusAgo = TriggerBolusAgo(injector).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + val t1 = t.duplicate() as TriggerBolusAgo + Assert.assertEquals(213, t1.minutesAgo.value) + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) + } + + private var lbJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"minutesAgo\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBolusAgo\"}" + @Test fun toJSONTest() { + val t: TriggerBolusAgo = TriggerBolusAgo(injector).setValue(410).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(lbJson, t.toJSON()) + } + + @Test @Throws(JSONException::class) fun fromJSONTest() { + val t: TriggerBolusAgo = TriggerBolusAgo(injector).setValue(410).comparator(Comparator.Compare.IS_EQUAL) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerBolusAgo + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) + Assert.assertEquals(410, t2.minutesAgo.value) + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.icon_bolus), TriggerBolusAgo(injector).icon()) + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOBTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOBTest.java deleted file mode 100644 index 725cc6cfd5..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOBTest.java +++ /dev/null @@ -1,116 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.CobInfo; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; -import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.utils.SP; - -import static org.mockito.ArgumentMatchers.any; -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ProfileFunctions.class, DateUtil.class, IobCobCalculatorPlugin.class, SP.class, L.class}) -public class TriggerCOBTest { - - long now = 1514766900000L; - IobCobCalculatorPlugin iobCobCalculatorPlugin; - - @Test - public void shouldRunTest() { - // COB value is 6 - PowerMockito.when(IobCobCalculatorPlugin.getPlugin().getCobInfo(false, "AutomationTriggerCOB")).thenReturn(new CobInfo(6d, 2d)); TriggerCOB t = new TriggerCOB().setValue(1).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - t = new TriggerCOB().setValue(6).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertTrue(t.shouldRun()); - t = new TriggerCOB().setValue(5).comparator(Comparator.Compare.IS_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerCOB().setValue(5).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerCOB().setValue(6).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerCOB().setValue(1).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - t = new TriggerCOB().setValue(10).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerCOB().setValue(5).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertFalse(t.shouldRun()); - - t = new TriggerCOB().setValue(1).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - - } - - @Test - public void copyConstructorTest() { - TriggerCOB t = new TriggerCOB().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - TriggerCOB t1 = (TriggerCOB) t.duplicate(); - Assert.assertEquals(213d, t.getValue(), 0.01d); - Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); - } - - @Test - public void executeTest() { - TriggerCOB t = new TriggerCOB().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - t.executed(1); - Assert.assertEquals(1l, t.getLastRun()); - } - - String bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"carbs\":4},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerCOB\"}"; - - @Test - public void toJSONTest() { - TriggerCOB t = new TriggerCOB().setValue(4).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(bgJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerCOB t = new TriggerCOB().setValue(4).comparator(Comparator.Compare.IS_EQUAL); - - TriggerCOB t2 = (TriggerCOB) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); - Assert.assertEquals(4, t2.getValue(), 0.01d); - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.icon_cp_bolus_carbs), new TriggerCOB().icon()); - } - - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - iobCobCalculatorPlugin = AAPSMocker.mockIobCobCalculatorPlugin(); - AAPSMocker.mockProfileFunctions(); - AAPSMocker.mockSP(); - AAPSMocker.mockL(); - - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn(now); - when(SP.getInt(any(), any())).thenReturn(48); - } - - public CobInfo generateCobInfo(){ - return new CobInfo(6d, 0d); - } - -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOBTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOBTest.kt new file mode 100644 index 0000000000..24ebc07404 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOBTest.kt @@ -0,0 +1,80 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.CobInfo +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.SP +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentMatchers +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(DateUtil::class, IobCobCalculatorPlugin::class, SP::class) +class TriggerCOBTest : TriggerTestBase() { + + var now = 1514766900000L + + @Before fun mock() { + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.`when`(DateUtil.now()).thenReturn(now) + PowerMockito.`when`(sp.getInt(ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())).thenReturn(48) + } + + @Test fun shouldRunTest() { + // COB value is 6 + PowerMockito.`when`(iobCobCalculatorPlugin.getCobInfo(false, "AutomationTriggerCOB")).thenReturn(CobInfo(6.0, 2.0)) + var t: TriggerCOB = TriggerCOB(injector).setValue(1.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerCOB(injector).setValue(6.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertTrue(t.shouldRun()) + t = TriggerCOB(injector).setValue(5.0).comparator(Comparator.Compare.IS_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerCOB(injector).setValue(5.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerCOB(injector).setValue(6.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerCOB(injector).setValue(1.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerCOB(injector).setValue(10.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerCOB(injector).setValue(5.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertFalse(t.shouldRun()) + } + + @Test fun copyConstructorTest() { + val t: TriggerCOB = TriggerCOB(injector).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertEquals(213.0, t.cob.value, 0.01) + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) + } + + private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"carbs\":4},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerCOB\"}" + @Test fun toJSONTest() { + val t: TriggerCOB = TriggerCOB(injector).setValue(4.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(bgJson, t.toJSON()) + } + + @Test + fun fromJSONTest() { + val t: TriggerCOB = TriggerCOB(injector).setValue(4.0).comparator(Comparator.Compare.IS_EQUAL) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerCOB + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) + Assert.assertEquals(4.0, t2.cob.value, 0.01) + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.icon_cp_bolus_carbs), TriggerCOB(injector).icon()) + } + + fun generateCobInfo(): CobInfo { + return CobInfo(6.0, 0.0) + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java deleted file mode 100644 index 3ee983511c..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java +++ /dev/null @@ -1,155 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.util.ArrayList; -import java.util.List; - -import info.AAPSMocker; -import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.db.BgReading; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; -import info.nightscout.androidaps.plugins.general.automation.elements.InputDelta.DeltaType; -import info.nightscout.androidaps.plugins.general.nsclient.data.NSSgv; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; -import info.nightscout.androidaps.utils.DateUtil; - -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ProfileFunctions.class, DateUtil.class, IobCobCalculatorPlugin.class, GlucoseStatus.class}) -public class TriggerDeltaTest { - - long now = 1514766900000L; - - @Test - public void shouldRunTest() { - when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(generateValidBgData()); - //Test if time passed is less than 5 min - TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER).lastRun(now-1); - Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(73d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - Assert.assertEquals(DeltaType.LONG_AVERAGE, t.getType()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d, DeltaType.SHORT_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - Assert.assertEquals(DeltaType.SHORT_AVERAGE, t.getType()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - Assert.assertEquals(DeltaType.DELTA, t.getType()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.3d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.1d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.5d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertFalse(t.shouldRun()); - - when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(new ArrayList<>()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().comparator(Comparator.Compare.IS_NOT_AVAILABLE); - Assert.assertTrue(t.shouldRun()); - - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(214, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - - - - } - - @Test - public void copyConstructorTest() { - TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - TriggerDelta t1 = (TriggerDelta) t.duplicate(); - Assert.assertEquals(213d, t1.getValue(), 0.01d); - Assert.assertEquals(Constants.MGDL, t1.getUnits()); - Assert.assertEquals(DeltaType.DELTA, t.getType()); - Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); - } - - @Test - public void executeTest() { - TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - t.executed(1); - Assert.assertEquals(1l, t.getLastRun()); - } - - String deltaJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"deltaType\":\"DELTA\",\"units\":\"mmol\",\"value\":4.1},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerDelta\"}"; - - @Test - public void toJSONTest() { - TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(deltaJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); - - TriggerDelta t2 = (TriggerDelta) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); - Assert.assertEquals(4.1d, t2.getValue(), 0.01d); - Assert.assertEquals(Constants.MMOL, t2.getUnits()); - Assert.assertEquals(DeltaType.DELTA, t2.getType()); - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.icon_auto_delta), new TriggerDelta().icon()); - } - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockIobCobCalculatorPlugin(); - AAPSMocker.mockProfileFunctions(); - AAPSMocker.mockApplicationContext(); - - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn(now); - - } - - @Test - public void initializerTest(){ - TriggerDelta t = new TriggerDelta(); - Assert.assertTrue(t.getUnits().equals(Constants.MGDL)); - } - - List generateValidBgData() { - List list = new ArrayList<>(); - try { - list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":214,\"mills\":1514766900000,\"direction\":\"Flat\"}")))); - list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":216,\"mills\":1514766600000,\"direction\":\"Flat\"}")))); // +2 - list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":219,\"mills\":1514766300000,\"direction\":\"Flat\"}")))); // +3 - list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":223,\"mills\":1514766000000,\"direction\":\"Flat\"}")))); // +4 - list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":222,\"mills\":1514765700000,\"direction\":\"Flat\"}")))); - list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":224,\"mills\":1514765400000,\"direction\":\"Flat\"}")))); - list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":226,\"mills\":1514765100000,\"direction\":\"Flat\"}")))); - list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":228,\"mills\":1514764800000,\"direction\":\"Flat\"}")))); - } catch (JSONException e) { - throw new RuntimeException(e); - } - return list; - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.kt new file mode 100644 index 0000000000..47422cd41f --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.kt @@ -0,0 +1,116 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.R +import info.nightscout.androidaps.db.BgReading +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import info.nightscout.androidaps.plugins.general.automation.elements.InputDelta.DeltaType +import info.nightscout.androidaps.plugins.general.nsclient.data.NSSgv +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin +import info.nightscout.androidaps.utils.DateUtil +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito.`when` +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner +import java.util.* + +@RunWith(PowerMockRunner::class) +@PrepareForTest(DateUtil::class, IobCobCalculatorPlugin::class, ProfileFunction::class) +class TriggerDeltaTest : TriggerTestBase() { + + var now = 1514766900000L + + @Before + fun mock() { + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.`when`(DateUtil.now()).thenReturn(now) + `when`(iobCobCalculatorPlugin.dataLock).thenReturn(Unit) + `when`(profileFunction.getUnits()).thenReturn(Constants.MGDL) + } + + @Test fun shouldRunTest() { + `when`(iobCobCalculatorPlugin.bgReadings).thenReturn(generateValidBgData()) + var t = TriggerDelta(injector).units(Constants.MGDL).setValue(73.0, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + Assert.assertEquals(DeltaType.LONG_AVERAGE, t.delta.deltaType) + t = TriggerDelta(injector).units(Constants.MGDL).setValue(-2.0, DeltaType.SHORT_AVERAGE).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + Assert.assertEquals(DeltaType.SHORT_AVERAGE, t.delta.deltaType) + t = TriggerDelta(injector).units(Constants.MGDL).setValue(-3.0, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + Assert.assertEquals(DeltaType.DELTA, t.delta.deltaType) + t = TriggerDelta(injector).units(Constants.MGDL).setValue(2.0, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerDelta(injector).units(Constants.MGDL).setValue(2.0, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerDelta(injector).units(Constants.MGDL).setValue(0.3, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerDelta(injector).units(Constants.MGDL).setValue(0.1, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertFalse(t.shouldRun()) + t = TriggerDelta(injector).units(Constants.MGDL).setValue(-0.5, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertFalse(t.shouldRun()) + t = TriggerDelta(injector).units(Constants.MGDL).setValue(-0.2, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + `when`(iobCobCalculatorPlugin.bgReadings).thenReturn(ArrayList()) + t = TriggerDelta(injector).units(Constants.MGDL).setValue(213.0, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertFalse(t.shouldRun()) + t = TriggerDelta(injector).comparator(Comparator.Compare.IS_NOT_AVAILABLE) + Assert.assertTrue(t.shouldRun()) + } + + @Test fun copyConstructorTest() { + val t: TriggerDelta = TriggerDelta(injector).units(Constants.MGDL).setValue(213.0, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + val t1 = t.duplicate() as TriggerDelta + Assert.assertEquals(213.0, t1.delta.value, 0.01) + Assert.assertEquals(Constants.MGDL, t1.units) + Assert.assertEquals(DeltaType.DELTA, t.delta.deltaType) + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) + } + + private var deltaJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"deltaType\":\"DELTA\",\"units\":\"mg/dl\",\"value\":4.1},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerDelta\"}" + + @Test + fun toJSONTest() { + val t: TriggerDelta = TriggerDelta(injector).units(Constants.MGDL).setValue(4.1, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(deltaJson, t.toJSON()) + } + + @Test + fun fromJSONTest() { + val t: TriggerDelta = TriggerDelta(injector).units(Constants.MMOL).setValue(4.1, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerDelta + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) + Assert.assertEquals(4.1, t2.delta.value, 0.01) + Assert.assertEquals(Constants.MMOL, t2.units) + Assert.assertEquals(DeltaType.DELTA, t2.delta.deltaType) + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.icon_auto_delta), TriggerDelta(injector).icon()) + } + + @Test fun initializerTest() { + val t = TriggerDelta(injector) + Assert.assertTrue(t.units == Constants.MGDL) + } + + private fun generateValidBgData(): List { + val list: MutableList = ArrayList() + list.add(BgReading(NSSgv(JSONObject("{\"mgdl\":214,\"mills\":1514766900000,\"direction\":\"Flat\"}")))) + list.add(BgReading(NSSgv(JSONObject("{\"mgdl\":216,\"mills\":1514766600000,\"direction\":\"Flat\"}")))) // +2 + list.add(BgReading(NSSgv(JSONObject("{\"mgdl\":219,\"mills\":1514766300000,\"direction\":\"Flat\"}")))) // +3 + list.add(BgReading(NSSgv(JSONObject("{\"mgdl\":223,\"mills\":1514766000000,\"direction\":\"Flat\"}")))) // +4 + list.add(BgReading(NSSgv(JSONObject("{\"mgdl\":222,\"mills\":1514765700000,\"direction\":\"Flat\"}")))) + list.add(BgReading(NSSgv(JSONObject("{\"mgdl\":224,\"mills\":1514765400000,\"direction\":\"Flat\"}")))) + list.add(BgReading(NSSgv(JSONObject("{\"mgdl\":226,\"mills\":1514765100000,\"direction\":\"Flat\"}")))) + list.add(BgReading(NSSgv(JSONObject("{\"mgdl\":228,\"mills\":1514764800000,\"direction\":\"Flat\"}")))) + return list + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIobTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIobTest.java deleted file mode 100644 index db39c05d2c..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIobTest.java +++ /dev/null @@ -1,119 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.IobTotal; -import info.nightscout.androidaps.data.Profile; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; -import info.nightscout.androidaps.utils.DateUtil; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ProfileFunctions.class, DateUtil.class, IobCobCalculatorPlugin.class}) -public class TriggerIobTest { - - long now = 1514766900000L; - IobCobCalculatorPlugin iobCobCalculatorPlugin; - - @Test - public void shouldRunTest() { - PowerMockito.when(iobCobCalculatorPlugin.calculateFromTreatmentsAndTempsSynchronized(anyLong(), any(Profile.class))).thenReturn(generateIobRecordData()); - - TriggerIob t = new TriggerIob().setValue(1.1d).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - t = new TriggerIob().setValue(1d).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertTrue(t.shouldRun()); - t = new TriggerIob().setValue(0.8d).comparator(Comparator.Compare.IS_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerIob().setValue(0.8d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerIob().setValue(0.9d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerIob().setValue(1.2d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerIob().setValue(1.1d).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - t = new TriggerIob().setValue(1d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerIob().setValue(0.9d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertFalse(t.shouldRun()); - - t = new TriggerIob().setValue(1d).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - - } - - @Test - public void copyConstructorTest() { - TriggerIob t = new TriggerIob().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - TriggerIob t1 = (TriggerIob) t.duplicate(); - Assert.assertEquals(213d, t.getValue(), 0.01d); - Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); - } - - @Test - public void executeTest() { - TriggerIob t = new TriggerIob().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - t.executed(1); - Assert.assertEquals(1l, t.getLastRun()); - } - - String bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"insulin\":4.1,\"lastRun\":0},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerIob\"}"; - - @Test - public void toJSONTest() { - TriggerIob t = new TriggerIob().setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(bgJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerIob t = new TriggerIob().setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL); - - TriggerIob t2 = (TriggerIob) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); - Assert.assertEquals(4.1d, t2.getValue(), 0.01d); - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.ic_keyboard_capslock), new TriggerIob().icon()); - } - - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - iobCobCalculatorPlugin = AAPSMocker.mockIobCobCalculatorPlugin(); - AAPSMocker.mockProfileFunctions(); - - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn(now); - - } - - IobTotal generateIobRecordData() { - IobTotal iobTotal = new IobTotal(1); - iobTotal.iob = 1; - return iobTotal; - } - -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIobTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIobTest.kt new file mode 100644 index 0000000000..7df8805f20 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIobTest.kt @@ -0,0 +1,84 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.R +import info.nightscout.androidaps.data.IobTotal +import info.nightscout.androidaps.data.Profile +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin +import info.nightscout.androidaps.utils.DateUtil +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentMatchers +import org.mockito.Mockito.`when` +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(DateUtil::class, IobCobCalculatorPlugin::class) +class TriggerIobTest : TriggerTestBase() { + + var now = 1514766900000L + + @Before fun mock() { + PowerMockito.mockStatic(DateUtil::class.java) + `when`(DateUtil.now()).thenReturn(now) + `when`(profileFunction.getProfile()).thenReturn(validProfile) + } + + @Test fun shouldRunTest() { + `when`(iobCobCalculatorPlugin.calculateFromTreatmentsAndTempsSynchronized(ArgumentMatchers.anyLong(), ArgumentMatchers.any(Profile::class.java))).thenReturn(generateIobRecordData()) + var t: TriggerIob = TriggerIob(injector).setValue(1.1).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerIob(injector).setValue(1.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertTrue(t.shouldRun()) + t = TriggerIob(injector).setValue(0.8).comparator(Comparator.Compare.IS_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerIob(injector).setValue(0.8).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerIob(injector).setValue(0.9).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerIob(injector).setValue(1.2).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerIob(injector).setValue(1.1).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerIob(injector).setValue(1.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerIob(injector).setValue(0.9).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertFalse(t.shouldRun()) + } + + @Test fun copyConstructorTest() { + val t: TriggerIob = TriggerIob(injector).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertEquals(213.0, t.insulin.value, 0.01) + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) + } + + private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"insulin\":4.1},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerIob\"}" + @Test fun toJSONTest() { + val t: TriggerIob = TriggerIob(injector).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(bgJson, t.toJSON()) + } + + @Test + fun fromJSONTest() { + val t: TriggerIob = TriggerIob(injector).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerIob + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) + Assert.assertEquals(4.1, t2.insulin.value, 0.01) + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.ic_keyboard_capslock), TriggerIob(injector).icon()) + } + + private fun generateIobRecordData(): IobTotal { + val iobTotal = IobTotal(1) + iobTotal.iob = 1.0 + return iobTotal + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocationTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocationTest.java deleted file mode 100644 index 1c4f986b7a..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocationTest.java +++ /dev/null @@ -1,166 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import android.location.Location; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.MockitoAnnotations; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.automation.elements.InputLocationMode; -import info.nightscout.androidaps.services.LocationService; -import info.nightscout.androidaps.utils.DateUtil; - -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ProfileFunctions.class, DateUtil.class, LocationService.class}) - -public class TriggerLocationTest { - - long now = 1514766900000L; - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockApplicationContext(); - - PowerMockito.mockStatic(DateUtil.class); - PowerMockito.mockStatic(LocationService.class); - when(DateUtil.now()).thenReturn(now); - PowerMockito.spy(LocationService.class); - PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocation()); - - - MockitoAnnotations.initMocks(this); - - } - - - @Test - public void copyConstructorTest() { - TriggerLocation t = new TriggerLocation(); - t.latitude.setValue(213); - t.longitude.setValue(212); - t.distance.setValue(2); - t.modeSelected.setValue(InputLocationMode.Mode.INSIDE); - - - TriggerLocation t1 = (TriggerLocation) t.duplicate(); - Assert.assertEquals(213d, t1.latitude.getValue(), 0.01d); - Assert.assertEquals(212d, t1.longitude.getValue(), 0.01d); - Assert.assertEquals(2d, t1.distance.getValue(), 0.01d); - Assert.assertEquals(InputLocationMode.Mode.INSIDE, t1.modeSelected.getValue()); - } - - @Test - public void shouldRunTest() { - TriggerLocation t = new TriggerLocation(); - t.latitude.setValue(213); - t.longitude.setValue(212); - t.distance.setValue(2); -// t.modeSelected.setValue(InputLocationMode.Mode.OUTSIDE); - PowerMockito.when(LocationService.getLastLocation()).thenReturn(null); - Assert.assertFalse(t.shouldRun()); - PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocation()); - Assert.assertTrue(t.shouldRun()); - t.lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - - t = new TriggerLocation(); - t.distance.setValue(-500); - Assert.assertFalse(t.shouldRun()); - - //Test of GOING_IN - last mode should be OUTSIDE, and current mode should be INSIDE - t = new TriggerLocation(); - t.distance.setValue(50); - t.lastMode = t.currentMode(55d); - PowerMockito.when(LocationService.getLastLocation()).thenReturn(null); - PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocationOut()); - t.modeSelected.setValue(InputLocationMode.Mode.GOING_IN); - Assert.assertEquals(t.lastMode, InputLocationMode.Mode.OUTSIDE); - Assert.assertEquals(t.currentMode(5d), InputLocationMode.Mode.INSIDE); - Assert.assertTrue(t.shouldRun()); - - //Test of GOING_OUT - last mode should be INSIDE, and current mode should be OUTSIDE - // Currently unavailable due to problems with Location mocking - } - - String locationJson = "{\"data\":{\"mode\":\"OUTSIDE\",\"distance\":2,\"lastRun\":0,\"latitude\":213,\"name\":\"\",\"longitude\":212},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerLocation\"}"; - - @Test - public void toJSONTest() { - TriggerLocation t = new TriggerLocation(); - t.latitude.setValue(213); - t.longitude.setValue(212); - t.distance.setValue(2); - t.modeSelected = t.modeSelected.setValue(InputLocationMode.Mode.OUTSIDE); - Assert.assertEquals(locationJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerLocation t = new TriggerLocation(); - t.latitude.setValue(213); - t.longitude.setValue(212); - t.distance.setValue(2); - t.modeSelected.setValue(InputLocationMode.Mode.INSIDE); - - TriggerLocation t2 = (TriggerLocation) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(t.latitude.getValue(), t2.latitude.getValue(), 0.01d); - Assert.assertEquals(t.longitude.getValue(), t2.longitude.getValue(), 0.01d); - Assert.assertEquals(t.distance.getValue(), t2.distance.getValue(), 0.01d); - Assert.assertEquals(t.modeSelected.getValue(), t2.modeSelected.getValue()); - } - - @Test - public void friendlyNameTest() { - Assert.assertEquals(R.string.location, new TriggerLocation().friendlyName()); - } - - @Test - public void friendlyDescriptionTest() { - Assert.assertEquals(null, new TriggerLocation().friendlyDescription()); //not mocked } - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.ic_location_on), new TriggerLocation().icon()); - - } - - @Test - public void lastRunTest() { - TriggerLocation t = new TriggerLocation(); - t.lastRun(now); - Assert.assertEquals(t.lastRun, 1514766900000L, 0d); - } - - public Location mockedLocation() { - Location newLocation = new Location("test"); - newLocation.setLatitude(10); - newLocation.setLongitude(11); - newLocation.setAccuracy(1f); - return newLocation; - } - - public Location mockedLocationOut() { - Location newLocation = new Location("test"); - newLocation.setLatitude(12f); - newLocation.setLongitude(13f); - newLocation.setAccuracy(1f); - return newLocation; - } -} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocationTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocationTest.kt new file mode 100644 index 0000000000..e3c40047a7 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocationTest.kt @@ -0,0 +1,129 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import android.location.Location +import com.google.common.base.Optional +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.general.automation.elements.InputLocationMode +import info.nightscout.androidaps.services.LocationService +import info.nightscout.androidaps.utils.DateUtil +import org.json.JSONException +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito.`when` +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(ProfileFunctions::class, DateUtil::class, LocationService::class) +class TriggerLocationTest : TriggerTestBase() { + + var now = 1514766900000L + + @Before fun mock() { + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.mockStatic(LocationService::class.java) + `when`(DateUtil.now()).thenReturn(now) + PowerMockito.spy(LocationService::class.java) + `when`(locationDataContainer.lastLocation).thenReturn(mockedLocation()) +// MockitoAnnotations.initMocks(this) + } + + @Test fun copyConstructorTest() { + val t = TriggerLocation(injector) + t.latitude.setValue(213.0) + t.longitude.setValue(212.0) + t.distance.setValue(2.0) + t.modeSelected.value = InputLocationMode.Mode.INSIDE + val t1 = t.duplicate() as TriggerLocation + Assert.assertEquals(213.0, t1.latitude.value, 0.01) + Assert.assertEquals(212.0, t1.longitude.value, 0.01) + Assert.assertEquals(2.0, t1.distance.value, 0.01) + Assert.assertEquals(InputLocationMode.Mode.INSIDE, t1.modeSelected.value) + } + + @Test fun shouldRunTest() { + var t = TriggerLocation(injector) + t.latitude.setValue(213.0) + t.longitude.setValue(212.0) + t.distance.setValue(2.0) + // t.modeSelected.setValue(InputLocationMode.Mode.OUTSIDE); + `when`(locationDataContainer.lastLocation).thenReturn(null) + Assert.assertFalse(t.shouldRun()) + `when`(locationDataContainer.lastLocation).thenReturn(mockedLocation()) + Assert.assertTrue(t.shouldRun()) + t = TriggerLocation(injector) + t.distance.setValue(-500.0) + Assert.assertFalse(t.shouldRun()) + + //Test of GOING_IN - last mode should be OUTSIDE, and current mode should be INSIDE + t = TriggerLocation(injector) + t.distance.setValue(50.0) + t.lastMode = t.currentMode(55.0) + `when`(locationDataContainer.lastLocation).thenReturn(null) + `when`(locationDataContainer.lastLocation).thenReturn(mockedLocationOut()) + t.modeSelected.value = InputLocationMode.Mode.GOING_IN + Assert.assertEquals(t.lastMode, InputLocationMode.Mode.OUTSIDE) + Assert.assertEquals(t.currentMode(5.0), InputLocationMode.Mode.INSIDE) + Assert.assertTrue(t.shouldRun()) + + //Test of GOING_OUT - last mode should be INSIDE, and current mode should be OUTSIDE + // Currently unavailable due to problems with Location mocking + } + + private var locationJson = "{\"data\":{\"mode\":\"OUTSIDE\",\"distance\":2,\"latitude\":213,\"name\":\"\",\"longitude\":212},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerLocation\"}" + @Test fun toJSONTest() { + val t = TriggerLocation(injector) + t.latitude.setValue(213.0) + t.longitude.setValue(212.0) + t.distance.setValue(2.0) + t.modeSelected.value = InputLocationMode.Mode.OUTSIDE +// t.modeSelected = t.modeSelected.value + Assert.assertEquals(locationJson, t.toJSON()) + } + + @Test @Throws(JSONException::class) fun fromJSONTest() { + val t = TriggerLocation(injector) + t.latitude.setValue(213.0) + t.longitude.setValue(212.0) + t.distance.setValue(2.0) + t.modeSelected.value = InputLocationMode.Mode.INSIDE + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerLocation + Assert.assertEquals(t.latitude.value, t2.latitude.value, 0.01) + Assert.assertEquals(t.longitude.value, t2.longitude.value, 0.01) + Assert.assertEquals(t.distance.value, t2.distance.value, 0.01) + Assert.assertEquals(t.modeSelected.value, t2.modeSelected.value) + } + + @Test fun friendlyNameTest() { + Assert.assertEquals(R.string.location.toLong(), TriggerLocation(injector).friendlyName().toLong()) + } + + @Test fun friendlyDescriptionTest() { + Assert.assertEquals(null, TriggerLocation(injector).friendlyDescription()) //not mocked } + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.ic_location_on), TriggerLocation(injector).icon()) + } + + private fun mockedLocation(): Location { + val newLocation = Location("test") + newLocation.latitude = 10.0 + newLocation.longitude = 11.0 + newLocation.accuracy = 1f + return newLocation + } + + private fun mockedLocationOut(): Location { + val newLocation = Location("test") + newLocation.latitude = 12.0 + newLocation.longitude = 13.0 + newLocation.accuracy = 1f + return newLocation + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercentTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercentTest.java deleted file mode 100644 index 4b83495937..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercentTest.java +++ /dev/null @@ -1,111 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; -import info.nightscout.androidaps.utils.DateUtil; - -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ProfileFunctions.class, DateUtil.class, L.class}) -public class TriggerProfilePercentTest { - - private long now = 1514766900000L; - - @Test - public void shouldRunTest() { - - TriggerProfilePercent t = new TriggerProfilePercent().setValue(101d).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - t = new TriggerProfilePercent().setValue(100d).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertTrue(t.shouldRun()); - t = new TriggerProfilePercent().setValue(100d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerProfilePercent().setValue(90d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerProfilePercent().setValue(100d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - t = new TriggerProfilePercent().setValue(101d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertTrue(t.shouldRun()); - - t = new TriggerProfilePercent().setValue(215d).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); - t = new TriggerProfilePercent().setValue(110d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertFalse(t.shouldRun()); - t = new TriggerProfilePercent().setValue(90d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertFalse(t.shouldRun()); - - // already run - t = new TriggerProfilePercent().setValue(101d).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - } - - @Test - public void copyConstructorTest() { - TriggerProfilePercent t = new TriggerProfilePercent().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - TriggerProfilePercent t1 = (TriggerProfilePercent) t.duplicate(); - Assert.assertEquals(213d, t1.getValue(), 0.01d); - Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); - } - - @Test - public void executeTest() { - TriggerProfilePercent t = new TriggerProfilePercent().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - t.executed(1); - Assert.assertEquals(1L, t.getLastRun()); - } - - private String bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"percentage\":110},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerProfilePercent\"}"; - - @Test - public void toJSONTest() { - TriggerProfilePercent t = new TriggerProfilePercent().setValue(110d).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(bgJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerProfilePercent t = new TriggerProfilePercent().setValue(120d).comparator(Comparator.Compare.IS_EQUAL); - - TriggerProfilePercent t2 = (TriggerProfilePercent) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); - Assert.assertEquals(120d, t2.getValue(), 0.01d); - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.icon_actions_profileswitch), new TriggerProfilePercent().icon()); - } - - @Test - public void friendlyNameTest() { - Assert.assertEquals(R.string.profilepercentage, new TriggerProfilePercent().friendlyName()); // not mocked - } - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockProfileFunctions(); - AAPSMocker.mockL(); - - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn(now); - } - -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercentTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercentTest.kt new file mode 100644 index 0000000000..85af485a26 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercentTest.kt @@ -0,0 +1,79 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import info.nightscout.androidaps.utils.DateUtil +import org.json.JSONException +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito.`when` +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(ProfileFunctions::class, DateUtil::class) +class TriggerProfilePercentTest : TriggerTestBase() { + + private val now = 1514766900000L + + @Before fun mock() { + `when`(profileFunction.getProfile()).thenReturn(validProfile) + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.`when`(DateUtil.now()).thenReturn(now) + } + + @Test fun shouldRunTest() { + var t: TriggerProfilePercent = TriggerProfilePercent(injector).setValue(101.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerProfilePercent(injector).setValue(100.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertTrue(t.shouldRun()) + t = TriggerProfilePercent(injector).setValue(100.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerProfilePercent(injector).setValue(90.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerProfilePercent(injector).setValue(100.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerProfilePercent(injector).setValue(101.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerProfilePercent(injector).setValue(215.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerProfilePercent(injector).setValue(110.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertFalse(t.shouldRun()) + t = TriggerProfilePercent(injector).setValue(90.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertFalse(t.shouldRun()) + } + + @Test fun copyConstructorTest() { + val t: TriggerProfilePercent = TriggerProfilePercent(injector).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + val t1 = t.duplicate() as TriggerProfilePercent + Assert.assertEquals(213.0, t1.pct.value, 0.01) + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) + } + + private val bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"percentage\":110},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerProfilePercent\"}" + @Test fun toJSONTest() { + val t: TriggerProfilePercent = TriggerProfilePercent(injector).setValue(110.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(bgJson, t.toJSON()) + } + + @Test @Throws(JSONException::class) fun fromJSONTest() { + val t: TriggerProfilePercent = TriggerProfilePercent(injector).setValue(120.0).comparator(Comparator.Compare.IS_EQUAL) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerProfilePercent + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) + Assert.assertEquals(120.0, t2.pct.value, 0.01) + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.icon_actions_profileswitch), TriggerProfilePercent(injector).icon()) + } + + @Test fun friendlyNameTest() { + Assert.assertEquals(R.string.profilepercentage.toLong(), TriggerProfilePercent(injector).friendlyName().toLong()) // not mocked + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnectionTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnectionTest.java deleted file mode 100644 index 9ba61af960..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnectionTest.java +++ /dev/null @@ -1,110 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; -import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin; -import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; -import info.nightscout.androidaps.utils.DateUtil; - -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ProfileFunctions.class, DateUtil.class, TreatmentsPlugin.class, ConfigBuilderPlugin.class, System.class}) -public class TriggerPumpLastConnectionTest { - - long now = 1514766900000L; - - @Test - public void shouldRunTest() { -// System.currentTimeMillis() is always 0 -// and so is every last connection time - VirtualPumpPlugin virtualPumpPlugin = VirtualPumpPlugin.getPlugin(); - when(ConfigBuilderPlugin.getPlugin().getActivePump()).thenReturn(virtualPumpPlugin); - Assert.assertEquals(0L, virtualPumpPlugin.lastDataTime()); - TriggerPumpLastConnection t = new TriggerPumpLastConnection().setValue(110).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - when(DateUtil.now()).thenReturn(now + (10*60*1000)); // set current time to now + 10 min - t = new TriggerPumpLastConnection().setValue(110).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(110, t.getValue(), 0.01d); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t.getComparator().getValue()); - Assert.assertFalse(t.shouldRun()); - t = new TriggerPumpLastConnection().setValue(10).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(10, t.getValue(), 0.01d); - Assert.assertFalse(t.shouldRun()); // 0 == 10 -> FALSE - t = new TriggerPumpLastConnection().setValue(5).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); - Assert.assertTrue(t.shouldRun()); // 5 => 0 -> TRUE - t = new TriggerPumpLastConnection().setValue(310).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertFalse(t.shouldRun()); // 310 <= 0 -> FALSE - t = new TriggerPumpLastConnection().setValue(420).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertFalse(t.shouldRun()); // 420 == 0 -> FALSE - } - - @Test - public void copyConstructorTest() { - TriggerPumpLastConnection t = new TriggerPumpLastConnection().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - TriggerPumpLastConnection t1 = (TriggerPumpLastConnection) t.duplicate(); - Assert.assertEquals(213, t1.getValue(), 0.01d); - Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); - } - - @Test - public void executeTest() { - TriggerPumpLastConnection t = new TriggerPumpLastConnection().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - t.executed(1); - Assert.assertEquals(1l, t.getLastRun()); - } - - String LBJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"minutesAgo\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerPumpLastConnection\"}"; - - @Test - public void toJSONTest() { - TriggerPumpLastConnection t = new TriggerPumpLastConnection().setValue(410).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(LBJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerPumpLastConnection t = new TriggerPumpLastConnection().setValue(410).comparator(Comparator.Compare.IS_EQUAL); - - TriggerPumpLastConnection t2 = (TriggerPumpLastConnection) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); - Assert.assertEquals(410, t2.getValue(), 0.01d); - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.remove), new TriggerPumpLastConnection().icon()); - } - - @Test - public void friendlyNameTest() { - Assert.assertEquals(R.string.automation_trigger_pump_last_connection_label, new TriggerPumpLastConnection().friendlyName()); - } - - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockConfigBuilder(); - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn(now); - - } - -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnectionTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnectionTest.kt new file mode 100644 index 0000000000..72c2814fc3 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnectionTest.kt @@ -0,0 +1,83 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin +import info.nightscout.androidaps.utils.DateUtil +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(ProfileFunctions::class, DateUtil::class, VirtualPumpPlugin::class) +class TriggerPumpLastConnectionTest : TriggerTestBase() { + + @Mock lateinit var virtualPumpPlugin: VirtualPumpPlugin + + var now = 1514766900000L + + @Before + fun mock() { + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.`when`(DateUtil.now()).thenReturn(now) + } + + @Test + fun shouldRunTest() { +// System.currentTimeMillis() is always 0 +// and so is every last connection time + PowerMockito.`when`(activePlugin.activePumpPlugin).thenReturn(virtualPumpPlugin) + Assert.assertEquals(0L, virtualPumpPlugin.lastDataTime()) + PowerMockito.`when`(DateUtil.now()).thenReturn(now + 10 * 60 * 1000) // set current time to now + 10 min + var t = TriggerPumpLastConnection(injector).setValue(110).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(110, t.minutesAgo.value) + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t.comparator.value) + Assert.assertFalse(t.shouldRun()) + t = TriggerPumpLastConnection(injector).setValue(10).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(10, t.minutesAgo.value) + Assert.assertFalse(t.shouldRun()) // 0 == 10 -> FALSE + t = TriggerPumpLastConnection(injector).setValue(5).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) // 5 => 0 -> TRUE + t = TriggerPumpLastConnection(injector).setValue(310).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertFalse(t.shouldRun()) // 310 <= 0 -> FALSE + t = TriggerPumpLastConnection(injector).setValue(420).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) // 420 == 0 -> FALSE + } + + @Test fun copyConstructorTest() { + val t: TriggerPumpLastConnection = TriggerPumpLastConnection(injector).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + val t1 = t.duplicate() as TriggerPumpLastConnection + Assert.assertEquals(213, t1.minutesAgo.value) + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) + } + + private var lbJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"minutesAgo\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerPumpLastConnection\"}" + @Test fun toJSONTest() { + val t: TriggerPumpLastConnection = TriggerPumpLastConnection(injector).setValue(410).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(lbJson, t.toJSON()) + } + + @Test + fun fromJSONTest() { + val t: TriggerPumpLastConnection = TriggerPumpLastConnection(injector).setValue(410).comparator(Comparator.Compare.IS_EQUAL) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerPumpLastConnection + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) + Assert.assertEquals(410, t2.minutesAgo.value) + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.remove), TriggerPumpLastConnection(injector).icon()) + } + + @Test fun friendlyNameTest() { + Assert.assertEquals(R.string.automation_trigger_pump_last_connection_label.toLong(), TriggerPumpLastConnection(injector).friendlyName().toLong()) + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTimeTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTimeTest.java deleted file mode 100644 index c9b463dfd0..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTimeTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.util.GregorianCalendar; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.utils.T; - -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, DateUtil.class, GregorianCalendar.class}) -public class TriggerRecurringTimeTest { - - long now = 1514766900000L; - - @Test - public void shouldRunTest() { - - // limit by validTo - TriggerRecurringTime t = new TriggerRecurringTime().hour(1).minute(34).validTo(1); - t.setAll(true); - Assert.assertFalse(t.shouldRun()); - - // scheduled 1 min before -// t = new TriggerRecurringTime().hour(1).minute(34); -// t.setAll(true); -// Assert.assertTrue(t.shouldRun()); - - // already run - t = new TriggerRecurringTime().hour(1).minute(34).lastRun(now - 1); - t.setAll(true); - Assert.assertFalse(t.shouldRun()); - - } - - String timeJson = "{\"data\":{\"WEDNESDAY\":false,\"MONDAY\":false,\"THURSDAY\":false,\"lastRun\":1514766840000,\"SUNDAY\":false,\"hour\":0,\"TUESDAY\":false,\"FRIDAY\":false,\"SATURDAY\":false,\"minute\":0,\"validTo\":0},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerRecurringTime\"}"; - - @Test - public void toJSONTest() throws JSONException { - TriggerRecurringTime t = new TriggerRecurringTime().lastRun(now - T.mins(1).msecs()); - Assert.assertEquals(timeJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerRecurringTime t = new TriggerRecurringTime().lastRun(now - T.mins(1).msecs()); - - TriggerRecurringTime t2 = (TriggerRecurringTime) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(now - T.mins(1).msecs(), t2.lastRun); } - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn(now); - - GregorianCalendar calendar = new GregorianCalendar(); - calendar.setTimeInMillis(now); - when(DateUtil.gregorianCalendar()).thenReturn(calendar); - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTimeTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTimeTest.kt new file mode 100644 index 0000000000..566a0b4c7b --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTimeTest.kt @@ -0,0 +1,59 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import info.nightscout.androidaps.utils.DateUtil +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(DateUtil::class) +class TriggerRecurringTimeTest : TriggerTestBase() { + + var now = 1514766900000L + + @Before fun mock() { + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.`when`(DateUtil.now()).thenReturn(now) +// val calendar = GregorianCalendar() +// calendar.timeInMillis = now +// PowerMockito.`when`(DateUtil.gregorianCalendar()).thenReturn(calendar) + } + + @Test fun shouldRunTest() { + + // limit by validTo + var t: TriggerRecurringTime = TriggerRecurringTime(injector).time(94) + t.days.setAll(true) + Assert.assertFalse(t.shouldRun()) + + // scheduled 1 min before +// t = new TriggerRecurringTime().hour(1).minute(34); +// t.setAll(true); +// Assert.assertTrue(t.shouldRun()); + + // already run + t = TriggerRecurringTime(injector).time(94) + t.days.setAll(true) + Assert.assertFalse(t.shouldRun()) + } + + private var timeJson = "{\"data\":{\"WEDNESDAY\":false,\"MONDAY\":false,\"THURSDAY\":false,\"SUNDAY\":false,\"TUESDAY\":false,\"FRIDAY\":false,\"SATURDAY\":false,\"time\":4444},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerRecurringTime\"}" + + @Test + fun toJSONTest() { + val t = TriggerRecurringTime(injector).time(4444) + Assert.assertEquals(timeJson, t.toJSON()) + } + + @Test + fun fromJSONTest() { + val t = TriggerRecurringTime(injector).time(4444) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerRecurringTime + Assert.assertEquals(4444, t2.time.value) + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetTest.java deleted file mode 100644 index 03c7e5cda2..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetTest.java +++ /dev/null @@ -1,93 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.db.TempTarget; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.automation.elements.ComparatorExists; -import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; -import info.nightscout.androidaps.utils.DateUtil; - -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ProfileFunctions.class, DateUtil.class, TreatmentsPlugin.class}) -public class TriggerTempTargetTest { - - TreatmentsPlugin treatmentsPlugin; - - long now = 1514766900000L; - - @Test - public void shouldRunTest() { - when(treatmentsPlugin.getTempTargetFromHistory()).thenReturn(null); - - TriggerTempTarget t = new TriggerTempTarget().comparator(ComparatorExists.Compare.EXISTS); - Assert.assertFalse(t.shouldRun()); - t = new TriggerTempTarget().comparator(ComparatorExists.Compare.NOT_EXISTS); - Assert.assertTrue(t.shouldRun()); - - when(treatmentsPlugin.getTempTargetFromHistory()).thenReturn(new TempTarget()); - - t = new TriggerTempTarget().comparator(ComparatorExists.Compare.NOT_EXISTS); - Assert.assertFalse(t.shouldRun()); - t = new TriggerTempTarget().comparator(ComparatorExists.Compare.EXISTS); - Assert.assertTrue(t.shouldRun()); - - t = new TriggerTempTarget().comparator(ComparatorExists.Compare.EXISTS).lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - - } - - @Test - public void copyConstructorTest() { - TriggerTempTarget t = new TriggerTempTarget().comparator(ComparatorExists.Compare.NOT_EXISTS); - TriggerTempTarget t1 = (TriggerTempTarget) t.duplicate(); - Assert.assertEquals(ComparatorExists.Compare.NOT_EXISTS, t.getComparator().getValue()); - } - - String ttJson = "{\"data\":{\"comparator\":\"EXISTS\",\"lastRun\":0},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTempTarget\"}"; - - @Test - public void toJSONTest() { - TriggerTempTarget t = new TriggerTempTarget().comparator(ComparatorExists.Compare.EXISTS); - Assert.assertEquals(ttJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerTempTarget t = new TriggerTempTarget().comparator(ComparatorExists.Compare.NOT_EXISTS); - - TriggerTempTarget t2 = (TriggerTempTarget) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(ComparatorExists.Compare.NOT_EXISTS, t2.getComparator().getValue()); - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.ic_keyboard_tab), new TriggerTempTarget().icon()); - } - - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - treatmentsPlugin = AAPSMocker.mockTreatmentPlugin(); - - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn(now); - - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetTest.kt new file mode 100644 index 0000000000..7cf3110b04 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetTest.kt @@ -0,0 +1,66 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.R +import info.nightscout.androidaps.db.TempTarget +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.general.automation.elements.ComparatorExists +import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.utils.DateUtil +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito.`when` +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(ProfileFunctions::class, DateUtil::class, TreatmentsPlugin::class) +class TriggerTempTargetTest : TriggerTestBase() { + + var now = 1514766900000L + + @Before fun mock() { + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.`when`(DateUtil.now()).thenReturn(now) + } + + @Test fun shouldRunTest() { + `when`(treatmentsPlugin.tempTargetFromHistory).thenReturn(null) + var t: TriggerTempTarget = TriggerTempTarget(injector).comparator(ComparatorExists.Compare.EXISTS) + Assert.assertFalse(t.shouldRun()) + t = TriggerTempTarget(injector).comparator(ComparatorExists.Compare.NOT_EXISTS) + Assert.assertTrue(t.shouldRun()) + PowerMockito.`when`(treatmentsPlugin.tempTargetFromHistory).thenReturn(TempTarget()) + t = TriggerTempTarget(injector).comparator(ComparatorExists.Compare.NOT_EXISTS) + Assert.assertFalse(t.shouldRun()) + t = TriggerTempTarget(injector).comparator(ComparatorExists.Compare.EXISTS) + Assert.assertTrue(t.shouldRun()) + } + + @Test fun copyConstructorTest() { + val t: TriggerTempTarget = TriggerTempTarget(injector).comparator(ComparatorExists.Compare.NOT_EXISTS) + val t1 = t.duplicate() as TriggerTempTarget + Assert.assertEquals(ComparatorExists.Compare.NOT_EXISTS, t1.comparator.value) + } + + private var ttJson = "{\"data\":{\"comparator\":\"EXISTS\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTempTarget\"}" + @Test fun toJSONTest() { + val t: TriggerTempTarget = TriggerTempTarget(injector).comparator(ComparatorExists.Compare.EXISTS) + Assert.assertEquals(ttJson, t.toJSON()) + } + + @Test + fun fromJSONTest() { + val t: TriggerTempTarget = TriggerTempTarget(injector).comparator(ComparatorExists.Compare.NOT_EXISTS) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerTempTarget + Assert.assertEquals(ComparatorExists.Compare.NOT_EXISTS, t2.comparator.value) + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.ic_keyboard_tab), TriggerTempTarget(injector).icon()) + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTestBase.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTestBase.kt index eb6fa9ade9..2c8ea40f91 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTestBase.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTestBase.kt @@ -7,6 +7,8 @@ import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction +import info.nightscout.androidaps.plugins.general.automation.elements.InputBg +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.services.LastLocationDataContainer @@ -34,11 +36,22 @@ open class TriggerTestBase : TestBase() { it.rxBus = RxBusWrapper() it.resourceHelper = resourceHelper it.profileFunction = profileFunction + it.sp = sp it.locationDataContainer = locationDataContainer it.treatmentsPlugin = treatmentsPlugin it.activePlugin = activePlugin it.iobCobCalculatorPlugin = iobCobCalculatorPlugin } + if (it is TriggerBg) { + it.profileFunction = profileFunction + } + if (it is InputBg) { + it.profileFunction = profileFunction + } + if (it is GlucoseStatus) { + it.aapsLogger = aapsLogger + it.iobCobCalculatorPlugin = iobCobCalculatorPlugin + } } } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.java deleted file mode 100644 index 6b36cc1fd3..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.java +++ /dev/null @@ -1,107 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.util.GregorianCalendar; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.utils.DateUtil; - -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, DateUtil.class, GregorianCalendar.class}) -public class TriggerTimeRangeTest { - - int now = 754; - int timeZoneOffset = (int) (DateUtil.getTimeZoneOffsetMs() / 60000); - - String timeJson = "{\"data\":{\"lastRun\":0,\"start\":753,\"end\":784},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTimeRange\"}"; - - @Test - public void shouldRunTest() { - - TriggerTimeRange t; - now = now + timeZoneOffset; - // range starts 1 min in the future - t = new TriggerTimeRange().period(now + 1 - timeZoneOffset, now + 30 - timeZoneOffset); - Assert.assertEquals(false, t.shouldRun()); - - // range starts 30 min back - t = new TriggerTimeRange().period(now - 30 - timeZoneOffset, now + 30 - timeZoneOffset); - Assert.assertEquals(true, t.shouldRun()); - - // Period is all day long - t = new TriggerTimeRange().period(1, 1440); - Assert.assertEquals(true, t.shouldRun()); - - // already run - t = new TriggerTimeRange().lastRun((long) (now - 1)*60000); - Assert.assertFalse(t.shouldRun()); - - } - - @Test - public void toJSONTest() { - TriggerTimeRange t = new TriggerTimeRange().period(now - 1 - timeZoneOffset , now + 30 - timeZoneOffset); - Assert.assertEquals(timeJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerTimeRange t = new TriggerTimeRange().period(120 , 180); - - TriggerTimeRange t2 = (TriggerTimeRange) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(timeZoneOffset + now - 1, t2.period(753 , 360).getStart()); - Assert.assertEquals(timeZoneOffset + 360, t2.period(753 , 360).getEnd()); - } - - @Test - public void copyConstructorTest() { - TriggerTimeRange t = new TriggerTimeRange(); - t.period(now, now + 30); - - TriggerTimeRange t1 = (TriggerTimeRange) t.duplicate(); -// Assert.assertEquals(now, t1.getRunAt(), 0.01d); - } - - @Test - public void friendlyNameTest() { - Assert.assertEquals(R.string.time_range, new TriggerTimeRange().friendlyName()); - } - - @Test - public void friendlyDescriptionTest() { - Assert.assertEquals(null, new TriggerTimeRange().friendlyDescription()); //not mocked } - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.ic_access_alarm_24dp), new TriggerTimeRange().icon()); - - } - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn((long) now * 60000); - - GregorianCalendar calendar = new GregorianCalendar(); - calendar.setTimeInMillis((long) now * 60000); - when(DateUtil.gregorianCalendar()).thenReturn(calendar); - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.kt new file mode 100644 index 0000000000..fc1dbbf690 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.kt @@ -0,0 +1,77 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.R +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.MidnightTime +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(DateUtil::class) +class TriggerTimeRangeTest : TriggerTestBase() { + + var now = 754 // in minutes from midnight + private var timeJson = "{\"data\":{\"start\":753,\"end\":784},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTimeRange\"}" + + @Before + fun mock() { + var realNow = System.currentTimeMillis() + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.`when`(DateUtil.now()).thenReturn(now.toLong() * 60000 + MidnightTime.calc(realNow)) + } + + @Test + fun shouldRunTest() { + // range starts 1 min in the future + var t: TriggerTimeRange = TriggerTimeRange(injector).period(now + 1, now + 30) + Assert.assertEquals(false, t.shouldRun()) + + // range starts 30 min back + t = TriggerTimeRange(injector).period(now - 30, now + 30) + Assert.assertEquals(true, t.shouldRun()) + + // Period is all day long + t = TriggerTimeRange(injector).period(1, 1440) + Assert.assertEquals(true, t.shouldRun()) + } + + @Test + fun toJSONTest() { + val t: TriggerTimeRange = TriggerTimeRange(injector).period(now - 1, now + 30) + Assert.assertEquals(timeJson, t.toJSON()) + } + + @Test + fun fromJSONTest() { + val t: TriggerTimeRange = TriggerTimeRange(injector).period(120, 180) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerTimeRange + Assert.assertEquals(now - 1, t2.period(753, 360).range.start) + Assert.assertEquals(360, t2.period(753, 360).range.end) + } + + @Test fun copyConstructorTest() { + val t = TriggerTimeRange(injector) + t.period(now, now + 30) + val t1 = t.duplicate() as TriggerTimeRange + Assert.assertEquals(now, t1.range.start) + } + + @Test fun friendlyNameTest() { + Assert.assertEquals(R.string.time_range.toLong(), TriggerTimeRange(injector).friendlyName().toLong()) + } + + @Test fun friendlyDescriptionTest() { + Assert.assertEquals(null, TriggerTimeRange(injector).friendlyDescription()) //not mocked } + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.ic_access_alarm_24dp), TriggerTimeRange(injector).icon()) + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeTest.java deleted file mode 100644 index b4b1794481..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeTest.java +++ /dev/null @@ -1,100 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.util.GregorianCalendar; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.utils.T; - -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, DateUtil.class, GregorianCalendar.class}) -public class TriggerTimeTest { - - long now = 1514766900000L; - - @Test - public void shouldRunTest() { - - // scheduled 1 min before - TriggerTime t = new TriggerTime().runAt(now - T.mins(1).msecs()); - Assert.assertTrue(t.shouldRun()); - - // scheduled 1 min in the future - t = new TriggerTime().runAt(now + T.mins(1).msecs()); - Assert.assertFalse(t.shouldRun()); - - // already run - t = new TriggerTime().runAt(now - T.mins(1).msecs()).lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - - } - - String timeJson = "{\"data\":{\"runAt\":1514766840000,\"lastRun\":0},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime\"}"; - - @Test - public void toJSONTest() { - TriggerTime t = new TriggerTime().runAt(now - T.mins(1).msecs()); - Assert.assertEquals(timeJson, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerTime t = new TriggerTime().runAt(now - T.mins(1).msecs()); - - TriggerTime t2 = (TriggerTime) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(now - T.mins(1).msecs(), t2.getRunAt()); - } - - @Test - public void copyConstructorTest() { - TriggerTime t = new TriggerTime(); - t.runAt(now); - - TriggerTime t1 = (TriggerTime) t.duplicate(); - Assert.assertEquals(now, t1.getRunAt(), 0.01d); - } - - @Test - public void friendlyNameTest() { - Assert.assertEquals(R.string.time, new TriggerTime().friendlyName()); - } - - @Test - public void friendlyDescriptionTest() { - Assert.assertEquals(null, new TriggerTime().friendlyDescription()); //not mocked } - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.ic_access_alarm_24dp), new TriggerTime().icon()); - - } - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn(now); - - GregorianCalendar calendar = new GregorianCalendar(); - calendar.setTimeInMillis(now); - when(DateUtil.gregorianCalendar()).thenReturn(calendar); - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeTest.kt new file mode 100644 index 0000000000..e5b993c193 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeTest.kt @@ -0,0 +1,69 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.R +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.T +import org.json.JSONException +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(DateUtil::class) +class TriggerTimeTest : TriggerTestBase() { + + var now = 1514766900000L + + @Before fun mock() { + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.`when`(DateUtil.now()).thenReturn(now) + } + + @Test fun shouldRunTest() { + + // scheduled 1 min before + var t: TriggerTime = TriggerTime(injector).runAt(now - T.mins(1).msecs()) + Assert.assertTrue(t.shouldRun()) + + // scheduled 1 min in the future + t = TriggerTime(injector).runAt(now + T.mins(1).msecs()) + Assert.assertFalse(t.shouldRun()) + } + + private var timeJson = "{\"data\":{\"runAt\":1514766840000},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime\"}" + @Test fun toJSONTest() { + val t: TriggerTime = TriggerTime(injector).runAt(now - T.mins(1).msecs()) + Assert.assertEquals(timeJson, t.toJSON()) + } + + @Test @Throws(JSONException::class) fun fromJSONTest() { + val t: TriggerTime = TriggerTime(injector).runAt(now - T.mins(1).msecs()) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerTime + Assert.assertEquals(now - T.mins(1).msecs(), t2.time.value) + } + + @Test fun copyConstructorTest() { + val t = TriggerTime(injector) + t.runAt(now) + val t1 = t.duplicate() as TriggerTime + Assert.assertEquals(now, t1.time.value) + } + + @Test fun friendlyNameTest() { + Assert.assertEquals(R.string.time.toLong(), TriggerTime(injector).friendlyName().toLong()) + } + + @Test fun friendlyDescriptionTest() { + Assert.assertEquals(null, TriggerTime(injector).friendlyDescription()) //not mocked } + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.ic_access_alarm_24dp), TriggerTime(injector).icon()) + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.java deleted file mode 100644 index d6eef521ed..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.java +++ /dev/null @@ -1,111 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import com.google.common.base.Optional; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.events.EventNetworkChange; -import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; -import info.nightscout.androidaps.receivers.NetworkChangeReceiver; -import info.nightscout.androidaps.utils.DateUtil; - -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, NetworkChangeReceiver.class, DateUtil.class}) -public class TriggerWifiSsidTest { - - long now = 1514766900000L; - - @Test - public void shouldRunTest() { - EventNetworkChange e = new EventNetworkChange(); - when(NetworkChangeReceiver.getLastEvent()).thenReturn(e); - - TriggerWifiSsid t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL); - - e.setWifiConnected(false); - Assert.assertFalse(t.shouldRun()); - - e.setWifiConnected(true); - e.setSsid("otherSSID"); - Assert.assertFalse(t.shouldRun()); - - e.setWifiConnected(true); - e.setSsid("aSSID"); - Assert.assertTrue(t.shouldRun()); - - t.lastRun(now - 1); - Assert.assertFalse(t.shouldRun()); - - t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_NOT_AVAILABLE); - e.setWifiConnected(false); - Assert.assertTrue(t.shouldRun()); - - // no network data - when(NetworkChangeReceiver.getLastEvent()).thenReturn(null); - Assert.assertFalse(t.shouldRun()); - } - - @Test - public void copyConstructorTest() { - TriggerWifiSsid t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - TriggerWifiSsid t1 = (TriggerWifiSsid) t.duplicate(); - Assert.assertEquals("aSSID", t1.getValue()); - Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); - } - - String json = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"ssid\":\"aSSID\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerWifiSsid\"}"; - - @Test - public void toJSONTest() { - TriggerWifiSsid t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL); - Assert.assertEquals(json, t.toJSON()); - } - - @Test - public void fromJSONTest() throws JSONException { - TriggerWifiSsid t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL); - - TriggerWifiSsid t2 = (TriggerWifiSsid) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); - Assert.assertEquals("aSSID", t2.getValue()); - } - - @Test - public void iconTest() { - Assert.assertEquals(Optional.of(R.drawable.ic_network_wifi), new TriggerWifiSsid().icon()); - } - - @Test - public void friendlyNameTest() { - Assert.assertEquals(R.string.ns_wifi_ssids, new TriggerWifiSsid().friendlyName()); - } - - @Test - public void friendlyDescriptionTest() { - Assert.assertEquals(null, new TriggerWifiSsid().friendlyDescription()); //not mocked - } - - @Before - public void mock() { - AAPSMocker.mockMainApp(); - - PowerMockito.mockStatic(NetworkChangeReceiver.class); - - PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn(now); - } - -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.kt new file mode 100644 index 0000000000..7412701d8e --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.kt @@ -0,0 +1,83 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.R +import info.nightscout.androidaps.events.EventNetworkChange +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import info.nightscout.androidaps.receivers.NetworkChangeReceiver +import info.nightscout.androidaps.utils.DateUtil +import org.json.JSONException +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(NetworkChangeReceiver::class, DateUtil::class) +class TriggerWifiSsidTest : TriggerTestBase() { + + var now = 1514766900000L + + @Before fun mock() { + PowerMockito.mockStatic(NetworkChangeReceiver::class.java) + PowerMockito.mockStatic(DateUtil::class.java) + PowerMockito.`when`(DateUtil.now()).thenReturn(now) + } + + @Test fun shouldRunTest() { + val e = EventNetworkChange() + PowerMockito.`when`(NetworkChangeReceiver.getLastEvent()).thenReturn(e) + var t: TriggerWifiSsid = TriggerWifiSsid(injector).setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL) + e.wifiConnected = false + Assert.assertFalse(t.shouldRun()) + e.wifiConnected = true + e.ssid = "otherSSID" + Assert.assertFalse(t.shouldRun()) + e.wifiConnected = true + e.ssid = "aSSID" + Assert.assertTrue(t.shouldRun()) + t = TriggerWifiSsid(injector).setValue("aSSID").comparator(Comparator.Compare.IS_NOT_AVAILABLE) + e.wifiConnected = false + Assert.assertTrue(t.shouldRun()) + + // no network data + PowerMockito.`when`(NetworkChangeReceiver.getLastEvent()).thenReturn(null) + Assert.assertFalse(t.shouldRun()) + } + + @Test fun copyConstructorTest() { + val t: TriggerWifiSsid = TriggerWifiSsid(injector).setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + val t1 = t.duplicate() as TriggerWifiSsid + Assert.assertEquals("aSSID", t1.ssid.value) + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) + } + + var json = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"ssid\":\"aSSID\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerWifiSsid\"}" + @Test fun toJSONTest() { + val t: TriggerWifiSsid = TriggerWifiSsid(injector).setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(json, t.toJSON()) + } + + @Test @Throws(JSONException::class) fun fromJSONTest() { + val t: TriggerWifiSsid = TriggerWifiSsid(injector).setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerWifiSsid + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) + Assert.assertEquals("aSSID", t2.ssid.value) + } + + @Test fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.ic_network_wifi), TriggerWifiSsid(injector).icon()) + } + + @Test fun friendlyNameTest() { + Assert.assertEquals(R.string.ns_wifi_ssids.toLong(), TriggerWifiSsid(injector).friendlyName().toLong()) + } + + @Test fun friendlyDescriptionTest() { + Assert.assertEquals(null, TriggerWifiSsid(injector).friendlyDescription()) //not mocked + } +} \ No newline at end of file