From 1b9b20cd417b0d26f2f295cdaa2b2a58f3bccd83 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 27 Mar 2018 14:19:52 +0200 Subject: [PATCH] 1st ProfileSwitch must be with zero duration, ProfileIntervalsTest --- .../androidaps/data/ProfileIntervals.java | 16 ++-- .../androidaps/db/ProfileSwitch.java | 25 +++++ .../Dialogs/NewNSTreatmentDialog.java | 15 ++- app/src/test/java/info/AAPSMocker.java | 9 +- .../androidaps/data/ProfileIntervalsTest.java | 93 +++++++++++++++++++ 5 files changed, 141 insertions(+), 17 deletions(-) create mode 100644 app/src/test/java/info/nightscout/androidaps/data/ProfileIntervalsTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/data/ProfileIntervals.java b/app/src/main/java/info/nightscout/androidaps/data/ProfileIntervals.java index caf15b5b90..9343596eee 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ProfileIntervals.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ProfileIntervals.java @@ -38,8 +38,10 @@ public class ProfileIntervals { } public synchronized void add(T newInterval) { - rawData.put(newInterval.start(), newInterval); - merge(); + if (newInterval.isValid()) { + rawData.put(newInterval.start(), newInterval); + merge(); + } } public synchronized void add(List list) { @@ -64,10 +66,12 @@ public class ProfileIntervals { public synchronized Interval getValueToTime(long time) { int index = binarySearch(time); if (index >= 0) return rawData.valueAt(index); - // if we request data older than first record, use oldest instead - if (rawData.size() > 0) { - //log.debug("Requested profile for time: " + DateUtil.dateAndTimeString(time) + ". Providing oldest record: " + rawData.valueAt(0).toString()); - return rawData.valueAt(0); + // if we request data older than first record, use oldest with zero duration instead + for (index = 0; index < rawData.size(); index++) { + if (rawData.valueAt(index).durationInMsec() == 0) { + //log.debug("Requested profile for time: " + DateUtil.dateAndTimeString(time) + ". Providing oldest record: " + rawData.valueAt(0).toString()); + return rawData.valueAt(index); + } } return null; } diff --git a/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java b/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java index ba2f2758e2..9dca91c595 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java +++ b/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java @@ -61,6 +61,31 @@ public class ProfileSwitch implements Interval, DataPointWithLabelInterface { private Profile profile = null; + public ProfileSwitch date(long date) { + this.date = date; + return this; + } + + public ProfileSwitch profileName(String profileName) { + this.profileName = profileName; + return this; + } + + public ProfileSwitch profile(Profile profile) { + this.profile = profile; + return this; + } + + public ProfileSwitch source(int source) { + this.source = source; + return this; + } + + public ProfileSwitch duration(int duration) { + this.durationInMinutes = duration; + return this; + } + @Nullable public Profile getProfileObject() { if (profile == null) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index 72256f1a54..7aea403941 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -51,7 +51,6 @@ import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.events.EventNewBasalProfile; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity; @@ -349,19 +348,19 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick editTimeshift = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_timeshift); editTimeshift.setParams(0d, (double) Constants.CPP_MIN_TIMESHIFT, (double) Constants.CPP_MAX_TIMESHIFT, 1d, new DecimalFormat("0"), false); - ProfileSwitch ps = MainApp.getConfigBuilder().getProfileSwitchFromHistory(System.currentTimeMillis()); + ProfileSwitch ps = MainApp.getConfigBuilder().getProfileSwitchFromHistory(DateUtil.now()); if (ps != null && ps.isCPP) { final int percentage = ps.percentage; final int timeshift = ps.timeshift; reuseButton.setText(reuseButton.getText() + " " + percentage + "% " + timeshift + "h"); - reuseButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - editPercentage.setValue((double) percentage); - editTimeshift.setValue((double) timeshift); - } + reuseButton.setOnClickListener(v -> { + editPercentage.setValue((double) percentage); + editTimeshift.setValue((double) timeshift); }); } + if (ps == null) { + options.duration = false; + } showOrHide((ViewGroup) view.findViewById(R.id.careportal_newnstreatment_eventtime_layout), options.date); showOrHide((ViewGroup) view.findViewById(R.id.careportal_newnstreatment_bg_layout), options.bg); diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java index b0522df2ee..b407bc360f 100644 --- a/app/src/test/java/info/AAPSMocker.java +++ b/app/src/test/java/info/AAPSMocker.java @@ -65,6 +65,7 @@ public class AAPSMocker { when(MainApp.gs(R.string.delta)).thenReturn("Delta"); 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"); } public static MainApp mockMainApp() { @@ -107,9 +108,11 @@ public class AAPSMocker { when(MainApp.getDbHelper()).thenReturn(databaseHelper); } - public static Profile getValidProfile() throws JSONException { - if (profile == null) - profile = new Profile(new JSONObject(validProfile), Constants.MGDL); + public static Profile getValidProfile() { + try { + if (profile == null) + profile = new Profile(new JSONObject(validProfile), Constants.MGDL); + } catch (JSONException e) {} return profile; } } diff --git a/app/src/test/java/info/nightscout/androidaps/data/ProfileIntervalsTest.java b/app/src/test/java/info/nightscout/androidaps/data/ProfileIntervalsTest.java new file mode 100644 index 0000000000..12f81c66fe --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/data/ProfileIntervalsTest.java @@ -0,0 +1,93 @@ +package info.nightscout.androidaps.data; + +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 java.util.ArrayList; +import java.util.List; + +import info.AAPSMocker; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.db.ProfileSwitch; +import info.nightscout.utils.DateUtil; +import info.nightscout.utils.T; + +/** + * Created by mike on 26.03.2018. + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, Bus.class}) +public class ProfileIntervalsTest { + private final long startDate = DateUtil.now(); + ProfileIntervals list = new ProfileIntervals<>(); + + @Test + public void doTests() { + // create one 10h interval and test value in and out + list.add(new ProfileSwitch().date(startDate).duration((int) T.hours(10).mins()).profileName("1").profile(AAPSMocker.getValidProfile())); + // for older date first record should be returned only if has zero duration + Assert.assertEquals(null, list.getValueToTime(startDate - T.secs(1).msecs())); + Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate)).profileName); + Assert.assertEquals(null, list.getValueToTime(startDate + T.hours(10).msecs() + 1)); + + list.reset(); + list.add(new ProfileSwitch().date(startDate).profileName("1").profile(AAPSMocker.getValidProfile())); + Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate - T.secs(1).msecs())).profileName); + Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate)).profileName); + Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(10).msecs() + 1)).profileName); + + // switch to different profile after 5h + list.add(new ProfileSwitch().date(startDate + T.hours(5).msecs()).duration(0).profileName("2").profile(AAPSMocker.getValidProfile())); + Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate - T.secs(1).msecs())).profileName); + Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(5).msecs() - 1)).profileName); + Assert.assertEquals("2", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(5).msecs() + 1)).profileName); + + // insert 1h interval inside + list.add(new ProfileSwitch().date(startDate + T.hours(6).msecs()).duration((int) T.hours(1).mins()).profileName("3").profile(AAPSMocker.getValidProfile())); + Assert.assertEquals("2", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(6).msecs() - 1)).profileName); + Assert.assertEquals("3", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(6).msecs() + 1)).profileName); + Assert.assertEquals("2", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(7).msecs() + 1)).profileName); + } + + @Test + public void testCopyConstructor() { + list.reset(); + list.add(new ProfileSwitch().date(startDate).duration((int) T.hours(10).mins()).profileName("4").profile(AAPSMocker.getValidProfile())); + ProfileIntervals list2 = new ProfileIntervals<>(list); + Assert.assertEquals(1, list2.getList().size()); + } + + @Test + public void invalidProfilesShouldNotBeReturned() { + list.reset(); + list.add(new ProfileSwitch().date(startDate + T.hours(1).msecs()).profileName("6")); + Assert.assertEquals(null, list.get(0)); + } + + @Test + public void testReversingArrays() { + List someList = new ArrayList<>(); + someList.add(new ProfileSwitch().date(startDate).duration((int) T.hours(3).mins()).profileName("5").profile(AAPSMocker.getValidProfile())); + someList.add(new ProfileSwitch().date(startDate + T.hours(1).msecs()).duration((int) T.hours(1).mins()).profileName("6").profile(AAPSMocker.getValidProfile())); + list.reset(); + list.add(someList); + Assert.assertEquals(startDate, list.get(0).date); + Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversed(0).date); + Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversedList().get(0).date); + + } + + @Before + public void prepareMock() { + AAPSMocker.mockMainApp(); + AAPSMocker.mockStrings(); + AAPSMocker.mockBus(); + } +}