From e7953b33af64544c580311425e0f56295e97e40e Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Mon, 16 Sep 2019 12:31:51 +0300 Subject: [PATCH] fix tests --- .../automation/triggers/TriggerTimeRange.java | 7 ++++--- .../triggers/TriggerTimeRangeTest.java | 21 ++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.java index 37887e67d6..944f0f6120 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.java @@ -37,6 +37,7 @@ public class TriggerTimeRange extends Trigger { // in minutes since midnight 60 means 1AM private int start; private int end; + long timeZoneOffset = DateUtil.getTimeZoneOffsetMs(); public TriggerTimeRange() { @@ -128,8 +129,8 @@ public class TriggerTimeRange extends Trigger { } TriggerTimeRange period(int start, int end) { - this.start = start; - this.end = end; + this.start = getMinSinceMidnight(start*60000); + this.end = getMinSinceMidnight(end*60000); return this; } @@ -152,7 +153,7 @@ public class TriggerTimeRange extends Trigger { if (time < 1441) return (int) time; Date date = new Date(time); - Calendar calendar = GregorianCalendar.getInstance(); + Calendar calendar = DateUtil.gregorianCalendar(); calendar.setTime(date); return (calendar.get(Calendar.HOUR_OF_DAY) * 60) + calendar.get(Calendar.MINUTE); } 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 index 02b96ff3f4..e8dc894175 100644 --- 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 @@ -19,6 +19,7 @@ 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; @@ -27,21 +28,21 @@ import static org.powermock.api.mockito.PowerMockito.when; public class TriggerTimeRangeTest { int now = 754; - String timeJson = "{\"data\":{\"lastRun\":0,\"start\":753,\"end\":784},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTimeRange\"}"; + int timeZoneOffset = (int) (DateUtil.getTimeZoneOffsetMs() / 60000); + + String timeJson = "{\"data\":{\"lastRun\":0,\"start\":873,\"end\":904},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTimeRange\"}"; @Test public void shouldRunTest() { TriggerTimeRange t; -// when(System.currentTimeMillis()).thenReturn((long) now*60000); - when(DateUtil.now()).thenReturn((long) now*60000); - + now = now + timeZoneOffset; // range starts 1 min in the future - t = new TriggerTimeRange().period(now + 1, now + 30); + t = new TriggerTimeRange().period(now + 1 - timeZoneOffset, now + 30 - timeZoneOffset); Assert.assertEquals(false, t.shouldRun()); // range starts 30 min back - t = new TriggerTimeRange().period((int) (System.currentTimeMillis() - 30*60000), now + 30); + t = new TriggerTimeRange().period(now - 30 - timeZoneOffset, now + 30 - timeZoneOffset); Assert.assertEquals(true, t.shouldRun()); // Period is all day long @@ -65,8 +66,8 @@ public class TriggerTimeRangeTest { TriggerTimeRange t = new TriggerTimeRange().period(120 , 180); TriggerTimeRange t2 = (TriggerTimeRange) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(now - 1, t2.period(753 , 360).getStart()); - Assert.assertEquals(360, t2.period(753 , 360).getEnd()); + Assert.assertEquals(timeZoneOffset + now - 1, t2.period(753 , 360).getStart()); + Assert.assertEquals(timeZoneOffset + 360, t2.period(753 , 360).getEnd()); } @Test @@ -100,10 +101,10 @@ public class TriggerTimeRangeTest { AAPSMocker.mockBus(); PowerMockito.mockStatic(DateUtil.class); - when(DateUtil.now()).thenReturn((long) now*60*60*10000); + when(DateUtil.now()).thenReturn((long) now * 60000); GregorianCalendar calendar = new GregorianCalendar(); - calendar.setTimeInMillis(now); + calendar.setTimeInMillis((long) now * 60000); when(DateUtil.gregorianCalendar()).thenReturn(calendar); } }