diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java index 6a0fe8483a..0797abe793 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java @@ -32,10 +32,6 @@ public class ActionStartTempTarget extends Action { value = new InputBg(Constants.MGDL); } - public ActionStartTempTarget(String units) { - value = new InputBg(units); - } - @Override public int friendlyName() { return R.string.starttemptarget; @@ -43,7 +39,7 @@ public class ActionStartTempTarget extends Action { @Override public String shortDescription() { - return MainApp.gs(R.string.resumeloop) + ": " + tempTarget.toString(); + return MainApp.gs(R.string.starttemptarget) + ": " + (tempTarget == null ? "null" : tempTarget.toString()); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTarget.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTarget.java new file mode 100644 index 0000000000..ad8aec56fb --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTarget.java @@ -0,0 +1,77 @@ +package info.nightscout.androidaps.plugins.general.automation.actions; + +import com.google.common.base.Optional; + +import org.json.JSONException; +import org.json.JSONObject; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.PumpEnactResult; +import info.nightscout.androidaps.db.Source; +import info.nightscout.androidaps.db.TempTarget; +import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; +import info.nightscout.androidaps.queue.Callback; +import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.JsonHelper; + +public class ActionStopTempTarget extends Action { + String reason = ""; + TempTarget tempTarget; + + public ActionStopTempTarget() { + } + + @Override + public int friendlyName() { + return R.string.stoptemptarget; + } + + @Override + public String shortDescription() { + return MainApp.gs(R.string.stoptemptarget); + } + + @Override + public void doAction(Callback callback) { + tempTarget = new TempTarget().date(DateUtil.now()).duration(0).reason(reason).source(Source.USER).low(0).high(0); + TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); + if (callback != null) + callback.result(new PumpEnactResult().success(true).comment(R.string.ok)).run(); + } + + @Override + public boolean hasDialog() { + return false; + } + + @Override + public String toJSON() { + JSONObject o = new JSONObject(); + try { + o.put("type", ActionStopTempTarget.class.getName()); + JSONObject data = new JSONObject(); + data.put("reason", reason); + o.put("data", data); + } catch (JSONException e) { + e.printStackTrace(); + } + return o.toString(); + } + + @Override + public Action fromJSON(String data) { + try { + JSONObject d = new JSONObject(data); + reason = JsonHelper.safeGetString(d, "reason"); + } catch (JSONException e) { + e.printStackTrace(); + } + return this; + } + + @Override + public Optional icon() { + return Optional.of(R.drawable.ic_stop_24dp); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java index 12956dd108..21a5e8a774 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java @@ -23,6 +23,7 @@ import info.nightscout.androidaps.plugins.general.automation.actions.ActionLoopE import info.nightscout.androidaps.plugins.general.automation.actions.ActionLoopResume; import info.nightscout.androidaps.plugins.general.automation.actions.ActionLoopSuspend; import info.nightscout.androidaps.plugins.general.automation.actions.ActionStartTempTarget; +import info.nightscout.androidaps.plugins.general.automation.actions.ActionStopTempTarget; public class ChooseActionDialog extends DialogFragment { @@ -38,6 +39,7 @@ public class ChooseActionDialog extends DialogFragment { add(new ActionLoopResume()); add(new ActionLoopSuspend()); add(new ActionStartTempTarget()); + add(new ActionStopTempTarget()); }}; private Unbinder mUnbinder; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6ddda0c3e8..b577a80c30 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1330,6 +1330,7 @@ Resume loop Not suspended Start temp target + Stop temp target is lesser than is equal or lesser than is equal to diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java index e56ac11211..f45d0aab08 100644 --- a/app/src/test/java/info/AAPSMocker.java +++ b/app/src/test/java/info/AAPSMocker.java @@ -151,6 +151,12 @@ public class AAPSMocker { when(MainApp.gs(R.string.pumpsuspended)).thenReturn("Pump suspended"); when(MainApp.gs(R.string.cob)).thenReturn("COB"); when(MainApp.gs(R.string.value_unavailable_short)).thenReturn("n/a"); + when(MainApp.gs(R.string.starttemptarget)).thenReturn("Start temp target"); + when(MainApp.gs(R.string.stoptemptarget)).thenReturn("Stop temp target"); + when(MainApp.gs(R.string.disableloop)).thenReturn("Disable loop"); + when(MainApp.gs(R.string.enableloop)).thenReturn("Enable loop"); + when(MainApp.gs(R.string.resumeloop)).thenReturn("Resume loop"); + when(MainApp.gs(R.string.suspendloop)).thenReturn("Suspend loop"); } public static MainApp mockMainApp() { diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopDisableTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopDisableTest.java index bef79c75c5..257392ca6e 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopDisableTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopDisableTest.java @@ -35,7 +35,7 @@ public class ActionLoopDisableTest { @Test public void shortDescriptionTest() { - Assert.assertEquals(R.string.disableloop, actionLoopDisable.friendlyName()); + Assert.assertEquals("Disable loop", actionLoopDisable.shortDescription()); } @Test @@ -69,6 +69,7 @@ public class ActionLoopDisableTest { AAPSMocker.mockSP(); AAPSMocker.mockConfigBuilder(); AAPSMocker.mockCommandQueue(); + AAPSMocker.mockStrings(); VirtualPumpPlugin pump = mock(VirtualPumpPlugin.class); when(pump.specialEnableCondition()).thenReturn(true); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopEnableTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopEnableTest.java index 3a9696c6fa..c5d9b81f56 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopEnableTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopEnableTest.java @@ -35,7 +35,7 @@ public class ActionLoopEnableTest { @Test public void shortDescriptionTest() { - Assert.assertEquals(R.string.enableloop, actionLoopEnable.friendlyName()); + Assert.assertEquals(MainApp.gs(R.string.enableloop), actionLoopEnable.shortDescription()); } @Test diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopResumeTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopResumeTest.java index 205670294d..755c56024d 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopResumeTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopResumeTest.java @@ -32,7 +32,7 @@ public class ActionLoopResumeTest { @Test public void shortDescriptionTest() { - Assert.assertEquals(R.string.resumeloop, actionLoopResume.friendlyName()); + Assert.assertEquals("Resume loop", actionLoopResume.shortDescription()); } @Test @@ -62,6 +62,7 @@ public class ActionLoopResumeTest { public void prepareTest() { AAPSMocker.mockMainApp(); AAPSMocker.mockApplicationContext(); + AAPSMocker.mockStrings(); AAPSMocker.mockBus(); AAPSMocker.mockSP(); AAPSMocker.mockConfigBuilder(); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspendTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspendTest.java index 87adba09b5..a50b19fdec 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspendTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspendTest.java @@ -36,7 +36,7 @@ public class ActionLoopSuspendTest { @Test public void shortDescriptionTest() { - Assert.assertEquals(R.string.suspendloop, actionLoopSuspend.friendlyName()); + Assert.assertEquals("Suspend loop", actionLoopSuspend.shortDescription()); } @Test @@ -77,6 +77,7 @@ public class ActionLoopSuspendTest { public void prepareTest() { AAPSMocker.mockMainApp(); AAPSMocker.mockApplicationContext(); + AAPSMocker.mockStrings(); AAPSMocker.mockBus(); AAPSMocker.mockSP(); AAPSMocker.mockConfigBuilder(); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTargetTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTargetTest.java index 1fcc64fe0f..e7527a828c 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTargetTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTargetTest.java @@ -43,7 +43,7 @@ public class ActionStartTempTargetTest { @Test public void shortDescriptionTest() { - Assert.assertEquals(R.string.starttemptarget, actionStartTempTarget.friendlyName()); + Assert.assertEquals("Start temp target: null", actionStartTempTarget.shortDescription()); } @Test @@ -62,15 +62,6 @@ public class ActionStartTempTargetTest { Assert.assertNotEquals(null, tempTargetAdded); } - @Test - public void applyTest() { - ActionLoopSuspend a = new ActionLoopSuspend(); - a.minutes = 20; - ActionLoopSuspend b = new ActionLoopSuspend(); - b.apply(a); - Assert.assertEquals(20, b.minutes); - } - @Test public void hasDialogTest() { Assert.assertTrue(actionStartTempTarget.hasDialog()); @@ -89,7 +80,7 @@ public class ActionStartTempTargetTest { @Test public void fromJSONTest() { actionStartTempTarget = new ActionStartTempTarget(); - actionStartTempTarget.fromJSON("{\"reason\":\"Test\",\"valueInMg\":100,\"durationInMinutes\":30,\"units\":\"mg/dl\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.ActionStartTempTarget\""); + actionStartTempTarget.fromJSON("{\"reason\":\"Test\",\"valueInMg\":100,\"durationInMinutes\":30,\"units\":\"mg/dl\"}"); Assert.assertEquals(Constants.MGDL, actionStartTempTarget.value.getUnits()); Assert.assertEquals(100, actionStartTempTarget.value.getValue(), 0.001d); Assert.assertEquals(30, actionStartTempTarget.duration.getMinutes(), 0.001); @@ -99,6 +90,7 @@ public class ActionStartTempTargetTest { public void prepareTest() { AAPSMocker.mockMainApp(); AAPSMocker.mockSP(); + AAPSMocker.mockStrings(); treatmentsPlugin = AAPSMocker.mockTreatmentPlugin(); Mockito.doAnswer(invocation -> { diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTargetTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTargetTest.java new file mode 100644 index 0000000000..4ec52c6c5b --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTargetTest.java @@ -0,0 +1,87 @@ +package info.nightscout.androidaps.plugins.general.automation.actions; + +import com.google.common.base.Optional; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +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.treatments.TreatmentsPlugin; +import info.nightscout.androidaps.queue.Callback; +import info.nightscout.androidaps.utils.SP; + +import static org.mockito.ArgumentMatchers.any; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, SP.class, TreatmentsPlugin.class}) +public class ActionStopTempTargetTest { + ActionStopTempTarget actionStopTempTarget = new ActionStopTempTarget(); + TreatmentsPlugin treatmentsPlugin; + TempTarget tempTargetAdded; + + @Test + public void friendlyNameTest() { + Assert.assertEquals(R.string.stoptemptarget, actionStopTempTarget.friendlyName()); + } + + @Test + public void shortDescriptionTest() { + Assert.assertEquals("Stop temp target", actionStopTempTarget.shortDescription()); + } + + @Test + public void iconTest() { + Assert.assertEquals(Optional.of(R.drawable.ic_stop_24dp), actionStopTempTarget.icon()); + } + + @Test + public void doActionTest() { + + actionStopTempTarget.doAction(new Callback() { + @Override + public void run() { + } + }); + Assert.assertNotEquals(null, tempTargetAdded); + } + + @Test + public void hasDialogTest() { + Assert.assertFalse(actionStopTempTarget.hasDialog()); + } + + @Test + public void toJSONTest() { + actionStopTempTarget = new ActionStopTempTarget(); + actionStopTempTarget.reason = "Test"; + Assert.assertEquals("{\"data\":{\"reason\":\"Test\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.ActionStopTempTarget\"}", actionStopTempTarget.toJSON()); + } + + @Test + public void fromJSONTest() { + actionStopTempTarget = new ActionStopTempTarget(); + actionStopTempTarget.fromJSON("{\"reason\":\"Test\"}"); + Assert.assertEquals("Test", actionStopTempTarget.reason); + } + + @Before + public void prepareTest() { + AAPSMocker.mockMainApp(); + AAPSMocker.mockSP(); + AAPSMocker.mockStrings(); + treatmentsPlugin = AAPSMocker.mockTreatmentPlugin(); + + Mockito.doAnswer(invocation -> { + tempTargetAdded = invocation.getArgument(0); + return null; + }).when(treatmentsPlugin).addToHistoryTempTarget(any(TempTarget.class)); + } +}