From 45312d9ffef4242222c461b82b64fe8b28e0f12c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 21 Apr 2019 23:48:05 +0200 Subject: [PATCH] ActionProfileSwitchPercent --- .../actions/ActionProfileSwitchPercent.java | 86 +++++++++++++++++++ .../actions/ActionStartTempTarget.java | 2 +- .../dialogs/ChooseActionDialog.java | 2 + app/src/main/res/values/strings.xml | 1 + app/src/test/java/info/AAPSMocker.java | 3 +- .../ActionProfileSwitchPercentTest.java | 86 +++++++++++++++++++ 6 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercentTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java new file mode 100644 index 0000000000..1c6dcf56d5 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java @@ -0,0 +1,86 @@ +package info.nightscout.androidaps.plugins.general.automation.actions; + +import android.widget.LinearLayout; + +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.plugins.configBuilder.ProfileFunctions; +import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration; +import info.nightscout.androidaps.plugins.general.automation.elements.InputPercent; +import info.nightscout.androidaps.plugins.general.automation.elements.Label; +import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; +import info.nightscout.androidaps.queue.Callback; +import info.nightscout.androidaps.utils.JsonHelper; + +public class ActionProfileSwitchPercent extends Action { + InputPercent pct = new InputPercent(); + InputDuration duration = new InputDuration(0, InputDuration.TimeUnit.MINUTES); + + @Override + public int friendlyName() { + return R.string.profilepercentage; + } + + @Override + public String shortDescription() { + return MainApp.gs(R.string.startprofile, (int) pct.getValue(), (int) duration.getValue()); + } + + @Override + public void doAction(Callback callback) { + ProfileFunctions.doProfileSwitch((int) duration.getValue(), (int) pct.getValue(), 0); + if (callback != null) + callback.result(new PumpEnactResult().success(true).comment(R.string.ok)).run(); + } + + @Override + public void generateDialog(LinearLayout root) { + new LayoutBuilder() + .add(new Label(MainApp.gs(R.string.percent_u), "", pct)) + .add(new Label(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", duration)) + .build(root); + } + + @Override + public boolean hasDialog() { + return true; + } + + @Override + public String toJSON() { + JSONObject o = new JSONObject(); + try { + o.put("type", ActionProfileSwitchPercent.class.getName()); + JSONObject data = new JSONObject(); + data.put("percentage", pct.getValue()); + data.put("durationInMinutes", duration.getMinutes()); + o.put("data", data); + } catch (JSONException e) { + e.printStackTrace(); + } + return o.toString(); + } + + @Override + public Action fromJSON(String data) { + try { + JSONObject d = new JSONObject(data); + pct.setValue(JsonHelper.safeGetInt(d, "percentage")); + duration.setMinutes(JsonHelper.safeGetInt(d, "durationInMinutes")); + } catch (JSONException e) { + e.printStackTrace(); + } + return this; + } + + @Override + public Optional icon() { + return Optional.of(R.drawable.remove); + } +} 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 0fdd8e3051..d74ceef78e 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 @@ -66,7 +66,7 @@ public class ActionStartTempTarget extends Action { new LayoutBuilder() .add(new Label(MainApp.gs(R.string.careportal_temporarytarget), MainApp.gs(unitResId), value)) - .add(new Label(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "min", duration)) + .add(new Label(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", duration)) .build(root); } 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 351adf34f8..aa154c02a7 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.ActionNotification; +import info.nightscout.androidaps.plugins.general.automation.actions.ActionProfileSwitchPercent; import info.nightscout.androidaps.plugins.general.automation.actions.ActionStartTempTarget; import info.nightscout.androidaps.plugins.general.automation.actions.ActionStopTempTarget; @@ -42,6 +43,7 @@ public class ChooseActionDialog extends DialogFragment { add(new ActionStartTempTarget()); add(new ActionStopTempTarget()); add(new ActionNotification()); + add(new ActionProfileSwitchPercent()); }}; private Unbinder mUnbinder; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4c71d1eb24..b327bdcdcf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1395,6 +1395,7 @@ Msg: Profile percentage Percent [%]: + Start profile %1$d%% for %2$d min %1$d day diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java index fd2661f6eb..10d00fb751 100644 --- a/app/src/test/java/info/AAPSMocker.java +++ b/app/src/test/java/info/AAPSMocker.java @@ -57,6 +57,7 @@ public class AAPSMocker { public static CommandQueue queue; public static ConfigBuilderPlugin configBuilderPlugin; + public static ProfileFunctions profileFunctions; public static void mockStrings() { Locale.setDefault(new Locale("en", "US")); @@ -278,7 +279,7 @@ public class AAPSMocker { public static void mockProfileFunctions() { PowerMockito.mockStatic(ProfileFunctions.class); - ProfileFunctions profileFunctions = PowerMockito.mock(ProfileFunctions.class); + profileFunctions = PowerMockito.mock(ProfileFunctions.class); PowerMockito.when(ProfileFunctions.getInstance()).thenReturn(profileFunctions); profile = getValidProfile(); PowerMockito.when(ProfileFunctions.getInstance().getProfile()).thenReturn(profile); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercentTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercentTest.java new file mode 100644 index 0000000000..27f676d007 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercentTest.java @@ -0,0 +1,86 @@ +package info.nightscout.androidaps.plugins.general.automation.actions; + +import com.google.common.base.Optional; +import com.squareup.otto.Bus; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.InputDuration; +import info.nightscout.androidaps.plugins.general.automation.elements.InputPercent; +import info.nightscout.androidaps.queue.Callback; +import info.nightscout.androidaps.utils.SP; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, SP.class, Bus.class, ProfileFunctions.class}) +public class ActionProfileSwitchPercentTest { + private ActionProfileSwitchPercent actionProfileSwitchPercent; + + @Test + public void friendlyNameTest() { + Assert.assertEquals(R.string.profilepercentage, actionProfileSwitchPercent.friendlyName()); + } + + @Test + public void shortDescriptionTest() { + actionProfileSwitchPercent = new ActionProfileSwitchPercent(); + actionProfileSwitchPercent.pct = new InputPercent().setValue(100); + actionProfileSwitchPercent.duration = new InputDuration(30, InputDuration.TimeUnit.MINUTES); + Assert.assertNull(actionProfileSwitchPercent.shortDescription()); // not mocked + } + + @Test + public void iconTest() { + Assert.assertEquals(Optional.of(R.drawable.remove), actionProfileSwitchPercent.icon()); + } + + @Test + public void doActionTest() { + actionProfileSwitchPercent.doAction(new Callback() { + @Override + public void run() { + Assert.assertTrue(result.success); + } + }); + } + + @Test + public void hasDialogTest() { + Assert.assertTrue(actionProfileSwitchPercent.hasDialog()); + } + + @Test + public void toJSONTest() { + actionProfileSwitchPercent = new ActionProfileSwitchPercent(); + actionProfileSwitchPercent.pct = new InputPercent().setValue(100); + actionProfileSwitchPercent.duration = new InputDuration(30, InputDuration.TimeUnit.MINUTES); + Assert.assertEquals("{\"data\":{\"percentage\":100,\"durationInMinutes\":30},\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.ActionProfileSwitchPercent\"}", actionProfileSwitchPercent.toJSON()); + } + + @Test + public void fromJSONTest() { + actionProfileSwitchPercent = new ActionProfileSwitchPercent(); + actionProfileSwitchPercent.fromJSON("{\"percentage\":100,\"durationInMinutes\":30}"); + Assert.assertEquals(100, actionProfileSwitchPercent.pct.getValue(), 0.001d); + Assert.assertEquals(30, actionProfileSwitchPercent.duration.getMinutes(), 0.001); + } + + @Before + public void prepareTest() { + AAPSMocker.mockMainApp(); + AAPSMocker.mockSP(); + AAPSMocker.mockBus(); + AAPSMocker.mockStrings(); + AAPSMocker.mockProfileFunctions(); + + actionProfileSwitchPercent = new ActionProfileSwitchPercent(); + } +}