From e4f2049405712a553776397582240fa78a1d7c27 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 29 Mar 2018 18:44:16 +0200 Subject: [PATCH] PumpEnactTestResult --- .../androidaps/data/PumpEnactResult.java | 95 ++++----- .../androidaps/plugins/Loop/LoopFragment.java | 5 +- app/src/test/java/info/AAPSMocker.java | 10 + .../androidaps/data/PumpEnactResultTest.java | 182 ++++++++++++++++++ 4 files changed, 246 insertions(+), 46 deletions(-) create mode 100644 app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java index 264a153c5e..eebd42f5d2 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java +++ b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java @@ -21,19 +21,19 @@ public class PumpEnactResult { public String comment = ""; // Result of basal change - public Integer duration = -1; // duration set [minutes] + public int duration = -1; // duration set [minutes] public double absolute = -1d; // absolute rate [U/h] , isPercent = false public int percent = -1; // percent of current basal [%] (100% = current basal), isPercent = true public boolean isPercent = false; // if true percent is used, otherwise absolute public boolean isTempCancel = false; // if true we are caceling temp basal // Result of treatment delivery - public Double bolusDelivered = 0d; // real value of delivered insulin - public Double carbsDelivered = 0d; // real value of delivered carbs + public double bolusDelivered = 0d; // real value of delivered insulin + public double carbsDelivered = 0d; // real value of delivered carbs public boolean queued = false; public PumpEnactResult success(boolean success) { - this.success = success; + this.success = success; return this; } @@ -87,76 +87,83 @@ public class PumpEnactResult { return this; } - public String log() { - return "Success: " + success + " Enacted: " + enacted + " Comment: " + comment + " Duration: " + duration + " Absolute: " + absolute + " Percent: " + percent + " IsPercent: " + isPercent + " Queued: " + queued; + public String log() { + return "Success: " + success + + " Enacted: " + enacted + + " Comment: " + comment + + " Duration: " + duration + + " Absolute: " + absolute + + " Percent: " + percent + + " IsPercent: " + isPercent + + " IsTempCancel: " + isTempCancel + + " bolusDelivered: " + bolusDelivered + + " carbsDelivered: " + carbsDelivered + + " Queued: " + queued; } public String toString() { - String ret = MainApp.sResources.getString(R.string.success) + ": " + success; + String ret = MainApp.gs(R.string.success) + ": " + success; if (enacted) { if (bolusDelivered > 0) { - ret += "\n" + MainApp.sResources.getString(R.string.enacted) + ": " + enacted; - ret += "\n" + MainApp.sResources.getString(R.string.comment) + ": " + comment; - ret += "\n" + MainApp.sResources.getString(R.string.smb_shortname) + ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted; + ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment; + ret += "\n" + MainApp.gs(R.string.smb_shortname) + ": " + bolusDelivered + " " + MainApp.gs(R.string.insulin_unit_shortname); } else if (isTempCancel) { - ret += "\n" + MainApp.sResources.getString(R.string.enacted) + ": " + enacted; + ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "\n" + MainApp.sResources.getString(R.string.comment) + ": " + comment; - ret += MainApp.sResources.getString(R.string.canceltemp); + ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment; + ret += "\n" + MainApp.gs(R.string.canceltemp); } else if (isPercent) { - ret += "\n" + MainApp.sResources.getString(R.string.enacted) + ": " + enacted; + ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "\n" + MainApp.sResources.getString(R.string.comment) + ": " + comment; - ret += "\n" + MainApp.sResources.getString(R.string.duration) + ": " + duration + " min"; - ret += "\n" + MainApp.sResources.getString(R.string.percent) + ": " + percent + "%"; + ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment; + ret += "\n" + MainApp.gs(R.string.duration) + ": " + duration + " min"; + ret += "\n" + MainApp.gs(R.string.percent) + ": " + percent + "%"; } else { - ret += "\n" + MainApp.sResources.getString(R.string.enacted) + ": " + enacted; + ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "\n" + MainApp.sResources.getString(R.string.comment) + ": " + comment; - ret += "\n" + MainApp.sResources.getString(R.string.duration) + ": " + duration + " min"; - ret += "\n" + MainApp.sResources.getString(R.string.absolute) + ": " + absolute + " U/h"; + ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment; + ret += "\n" + MainApp.gs(R.string.duration) + ": " + duration + " min"; + ret += "\n" + MainApp.gs(R.string.absolute) + ": " + absolute + " U/h"; } } else { - ret += "\n" + MainApp.sResources.getString(R.string.comment) + ": " + comment; + ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment; } return ret; } - public Spanned toSpanned() { - String ret = "" + MainApp.sResources.getString(R.string.success) + ": " + success; + public String toHtml() { + String ret = "" + MainApp.gs(R.string.success) + ": " + success; if (queued) { - ret = MainApp.sResources.getString(R.string.waitingforpumpresult); + ret = MainApp.gs(R.string.waitingforpumpresult); } else if (enacted) { if (bolusDelivered > 0) { - ret += "
" + MainApp.sResources.getString(R.string.enacted) + ": " + enacted; + ret += "
" + MainApp.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "
" + MainApp.sResources.getString(R.string.comment) + ": " + comment; - ret += "
" + MainApp.sResources.getString(R.string.smb_shortname) + ": " + bolusDelivered + " " + MainApp.gs(R.string.insulin_unit_shortname); + ret += "
" + MainApp.gs(R.string.comment) + ": " + comment; + ret += "
" + MainApp.gs(R.string.smb_shortname) + ": " + bolusDelivered + " " + MainApp.gs(R.string.insulin_unit_shortname); } else if (isTempCancel) { - ret += "
" + MainApp.sResources.getString(R.string.enacted) + ": " + enacted; - ret += "
" + MainApp.sResources.getString(R.string.comment) + ": " + comment + - "
" + MainApp.sResources.getString(R.string.canceltemp); + ret += "
" + MainApp.gs(R.string.enacted) + ": " + enacted; + ret += "
" + MainApp.gs(R.string.comment) + ": " + comment + + "
" + MainApp.gs(R.string.canceltemp); } else if (isPercent && percent != -1) { - ret += "
" + MainApp.sResources.getString(R.string.enacted) + ": " + enacted; + ret += "
" + MainApp.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "
" + MainApp.sResources.getString(R.string.comment) + ": " + comment; - ret += "
" + MainApp.sResources.getString(R.string.duration) + ": " + duration + " min"; - ret += "
" + MainApp.sResources.getString(R.string.percent) + ": " + percent + "%"; + ret += "
" + MainApp.gs(R.string.comment) + ": " + comment; + ret += "
" + MainApp.gs(R.string.duration) + ": " + duration + " min"; + ret += "
" + MainApp.gs(R.string.percent) + ": " + percent + "%"; } else if (absolute != -1) { - ret += "
" + MainApp.sResources.getString(R.string.enacted) + ": " + enacted; + ret += "
" + MainApp.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "
" + MainApp.sResources.getString(R.string.comment) + ": " + comment; - ret += "
" + MainApp.sResources.getString(R.string.duration) + ": " + duration + " min"; - ret += "
" + MainApp.sResources.getString(R.string.absolute) + ": " + DecimalFormatter.to2Decimal(absolute) + " U/h"; + ret += "
" + MainApp.gs(R.string.comment) + ": " + comment; + ret += "
" + MainApp.gs(R.string.duration) + ": " + duration + " min"; + ret += "
" + MainApp.gs(R.string.absolute) + ": " + DecimalFormatter.to2Decimal(absolute) + " U/h"; } } else { - ret += "
" + MainApp.sResources.getString(R.string.comment) + ": " + comment; + ret += "
" + MainApp.gs(R.string.comment) + ": " + comment; } - return Html.fromHtml(ret); - } - - public PumpEnactResult() { + return ret; } public JSONObject json(Profile profile) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java index 19ed6496e7..5285dc7831 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.Loop; import android.app.Activity; import android.os.Bundle; +import android.text.Html; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -95,8 +96,8 @@ public class LoopFragment extends SubscriberFragment { sourceView.setText(lastRun.source != null ? lastRun.source : ""); lastRunView.setText(lastRun.lastAPSRun != null && lastRun.lastAPSRun.getTime() != 0 ? lastRun.lastAPSRun.toLocaleString() : ""); lastEnactView.setText(lastRun.lastEnact != null && lastRun.lastEnact.getTime() != 0 ? lastRun.lastEnact.toLocaleString() : ""); - tbrSetByPumpView.setText(lastRun.tbrSetByPump != null ? lastRun.tbrSetByPump.toSpanned() : ""); - smbSetByPumpView.setText(lastRun.smbSetByPump != null ? lastRun.smbSetByPump.toSpanned() : ""); + tbrSetByPumpView.setText(lastRun.tbrSetByPump != null ? Html.fromHtml(lastRun.tbrSetByPump.toHtml()) : ""); + smbSetByPumpView.setText(lastRun.smbSetByPump != null ? Html.fromHtml(lastRun.smbSetByPump.toHtml()) : ""); String constraints = ""; if (lastRun.constraintsProcessed != null) { diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java index b407bc360f..346f4728c3 100644 --- a/app/src/test/java/info/AAPSMocker.java +++ b/app/src/test/java/info/AAPSMocker.java @@ -66,6 +66,16 @@ public class AAPSMocker { when(MainApp.gs(R.string.short_avgdelta)).thenReturn("Short avg. delta"); when(MainApp.gs(R.string.long_avgdelta)).thenReturn("Long avg. delta"); when(MainApp.gs(R.string.zerovalueinprofile)).thenReturn("Invalid profile: %s"); + when(MainApp.gs(R.string.success)).thenReturn("Success"); + when(MainApp.gs(R.string.enacted)).thenReturn("Enacted"); + when(MainApp.gs(R.string.comment)).thenReturn("Comment"); + when(MainApp.gs(R.string.smb_shortname)).thenReturn("SMB"); + when(MainApp.gs(R.string.canceltemp)).thenReturn("Cancel temp basal"); + when(MainApp.gs(R.string.duration)).thenReturn("Duration"); + when(MainApp.gs(R.string.percent)).thenReturn("Percent"); + when(MainApp.gs(R.string.absolute)).thenReturn("Absolute"); + when(MainApp.gs(R.string.waitingforpumpresult)).thenReturn("Waiting for result"); + when(MainApp.gs(R.string.insulin_unit_shortname)).thenReturn("U"); } public static MainApp mockMainApp() { diff --git a/app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.java b/app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.java new file mode 100644 index 0000000000..9889c9185a --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.java @@ -0,0 +1,182 @@ +package info.nightscout.androidaps.data; + +import android.text.Html; +import android.text.Spanned; + +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.skyscreamer.jsonassert.JSONAssert; + +import info.AAPSMocker; +import info.nightscout.androidaps.MainApp; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.powermock.api.mockito.PowerMockito.when; + +/** + * Created by mike on 29.03.2018. + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, Html.class}) +public class PumpEnactResultTest { + + PumpEnactResult per = new PumpEnactResult(); + + @Test + public void successTest() throws Exception { + per.success(true); + Assert.assertEquals(true, per.success); + } + + @Test + public void enactedTest() throws Exception { + per.enacted(true); + Assert.assertEquals(true, per.enacted); + } + + @Test + public void commentTest() throws Exception { + per.comment("SomeComment"); + Assert.assertEquals("SomeComment", per.comment); + } + + @Test + public void durationTest() throws Exception { + per.duration(10); + Assert.assertEquals(10, per.duration); + } + + @Test + public void absoluteTest() throws Exception { + per.absolute(11d); + Assert.assertEquals(11d, per.absolute, 0.01d); + } + + @Test + public void percentTest() throws Exception { + per.percent(10); + Assert.assertEquals((int) 10, per.percent); + } + + @Test + public void isPercentTest() throws Exception { + per.isPercent(true); + Assert.assertEquals(true, per.isPercent); + } + + @Test + public void isTempCancelTest() throws Exception { + per.isTempCancel(true); + Assert.assertEquals(true, per.isTempCancel); + } + + @Test + public void bolusDeliveredTest() throws Exception { + per.bolusDelivered(11d); + Assert.assertEquals(11d, per.bolusDelivered, 0.01d); + } + + @Test + public void carbsDeliveredTest() throws Exception { + per.carbsDelivered(11d); + Assert.assertEquals(11d, per.carbsDelivered, 0.01d); + } + + @Test + public void queuedTest() throws Exception { + per.queued(true); + Assert.assertEquals(true, per.queued); + } + + @Test + public void logTest() throws Exception { + per = new PumpEnactResult(); + Assert.assertEquals("Success: false Enacted: false Comment: Duration: -1 Absolute: -1.0 Percent: -1 IsPercent: false IsTempCancel: false bolusDelivered: 0.0 carbsDelivered: 0.0 Queued: false", per.log()); + } + + @Test + public void toStringTest() throws Exception { + per = new PumpEnactResult().enacted(true).bolusDelivered(10).comment("AAA"); + Assert.assertEquals("Success: false\n" + + "Enacted: true\n" + + "Comment: AAA\n" + + "SMB: 10.0 U", per.toString()); + + per = new PumpEnactResult().enacted(true).isTempCancel(true).comment("AAA"); + Assert.assertEquals("Success: false\n" + + "Enacted: true\n" + + "Comment: AAA\n" + + "Cancel temp basal", per.toString()); + + per = new PumpEnactResult().enacted(true).isPercent(true).percent(90).duration(20).comment("AAA"); + Assert.assertEquals("Success: false\n" + + "Enacted: true\n" + + "Comment: AAA\n" + + "Duration: 20 min\n" + + "Percent: 90%", per.toString()); + + per = new PumpEnactResult().enacted(true).isPercent(false).absolute(1).duration(30).comment("AAA"); + Assert.assertEquals("Success: false\n" + + "Enacted: true\n" + + "Comment: AAA\n" + + "Duration: 30 min\n" + + "Absolute: 1.0 U/h", per.toString()); + + per = new PumpEnactResult().enacted(false).comment("AAA"); + Assert.assertEquals("Success: false\n" + + "Comment: AAA", per.toString()); + } + + @Test + public void toHtmlTest() throws Exception { + per = new PumpEnactResult().enacted(true).bolusDelivered(10).comment("AAA"); + Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
SMB: 10.0 U", per.toHtml()); + + per = new PumpEnactResult().enacted(true).isTempCancel(true).comment("AAA"); + Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
Cancel temp basal", per.toHtml()); + + per = new PumpEnactResult().enacted(true).isPercent(true).percent(90).duration(20).comment("AAA"); + Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
Duration: 20 min
Percent: 90%", per.toHtml()); + + per = new PumpEnactResult().enacted(true).isPercent(false).absolute(1).duration(30).comment("AAA"); + Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
Duration: 30 min
Absolute: 1.00 U/h", per.toHtml()); + + per = new PumpEnactResult().enacted(false).comment("AAA"); + Assert.assertEquals("Success: false
Comment: AAA", per.toHtml()); + } + + @Test + public void jsonTest() throws Exception { + JSONObject o; + per = new PumpEnactResult().enacted(true).bolusDelivered(10).comment("AAA"); + o = per.json(AAPSMocker.getValidProfile()); + JSONAssert.assertEquals("{\"smb\":10}", o, false); + + per = new PumpEnactResult().enacted(true).isTempCancel(true).comment("AAA"); + o = per.json(AAPSMocker.getValidProfile()); + JSONAssert.assertEquals("{\"rate\":0,\"duration\":0}", o, false); + + per = new PumpEnactResult().enacted(true).isPercent(true).percent(90).duration(20).comment("AAA"); + o = per.json(AAPSMocker.getValidProfile()); + JSONAssert.assertEquals("{\"rate\":0.9,\"duration\":20}", o, false); + + per = new PumpEnactResult().enacted(true).isPercent(false).absolute(1).duration(30).comment("AAA"); + o = per.json(AAPSMocker.getValidProfile()); + JSONAssert.assertEquals("{\"rate\":1,\"duration\":30}", o, false); + } + + @Before + public void prepareMock() { + AAPSMocker.mockMainApp(); + AAPSMocker.mockStrings(); + } +}