fix tests

This commit is contained in:
Roumen Georgiev 2019-09-16 12:31:51 +03:00
parent d66eb78415
commit e7953b33af
2 changed files with 15 additions and 13 deletions

View file

@ -37,6 +37,7 @@ public class TriggerTimeRange extends Trigger {
// in minutes since midnight 60 means 1AM // in minutes since midnight 60 means 1AM
private int start; private int start;
private int end; private int end;
long timeZoneOffset = DateUtil.getTimeZoneOffsetMs();
public TriggerTimeRange() { public TriggerTimeRange() {
@ -128,8 +129,8 @@ public class TriggerTimeRange extends Trigger {
} }
TriggerTimeRange period(int start, int end) { TriggerTimeRange period(int start, int end) {
this.start = start; this.start = getMinSinceMidnight(start*60000);
this.end = end; this.end = getMinSinceMidnight(end*60000);
return this; return this;
} }
@ -152,7 +153,7 @@ public class TriggerTimeRange extends Trigger {
if (time < 1441) if (time < 1441)
return (int) time; return (int) time;
Date date = new Date(time); Date date = new Date(time);
Calendar calendar = GregorianCalendar.getInstance(); Calendar calendar = DateUtil.gregorianCalendar();
calendar.setTime(date); calendar.setTime(date);
return (calendar.get(Calendar.HOUR_OF_DAY) * 60) + calendar.get(Calendar.MINUTE); return (calendar.get(Calendar.HOUR_OF_DAY) * 60) + calendar.get(Calendar.MINUTE);
} }

View file

@ -19,6 +19,7 @@ import info.AAPSMocker;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.T;
import static org.powermock.api.mockito.PowerMockito.when; import static org.powermock.api.mockito.PowerMockito.when;
@ -27,21 +28,21 @@ import static org.powermock.api.mockito.PowerMockito.when;
public class TriggerTimeRangeTest { public class TriggerTimeRangeTest {
int now = 754; 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 @Test
public void shouldRunTest() { public void shouldRunTest() {
TriggerTimeRange t; TriggerTimeRange t;
// when(System.currentTimeMillis()).thenReturn((long) now*60000); now = now + timeZoneOffset;
when(DateUtil.now()).thenReturn((long) now*60000);
// range starts 1 min in the future // 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()); Assert.assertEquals(false, t.shouldRun());
// range starts 30 min back // 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()); Assert.assertEquals(true, t.shouldRun());
// Period is all day long // Period is all day long
@ -65,8 +66,8 @@ public class TriggerTimeRangeTest {
TriggerTimeRange t = new TriggerTimeRange().period(120 , 180); TriggerTimeRange t = new TriggerTimeRange().period(120 , 180);
TriggerTimeRange t2 = (TriggerTimeRange) Trigger.instantiate(new JSONObject(t.toJSON())); TriggerTimeRange t2 = (TriggerTimeRange) Trigger.instantiate(new JSONObject(t.toJSON()));
Assert.assertEquals(now - 1, t2.period(753 , 360).getStart()); Assert.assertEquals(timeZoneOffset + now - 1, t2.period(753 , 360).getStart());
Assert.assertEquals(360, t2.period(753 , 360).getEnd()); Assert.assertEquals(timeZoneOffset + 360, t2.period(753 , 360).getEnd());
} }
@Test @Test
@ -100,10 +101,10 @@ public class TriggerTimeRangeTest {
AAPSMocker.mockBus(); AAPSMocker.mockBus();
PowerMockito.mockStatic(DateUtil.class); PowerMockito.mockStatic(DateUtil.class);
when(DateUtil.now()).thenReturn((long) now*60*60*10000); when(DateUtil.now()).thenReturn((long) now * 60000);
GregorianCalendar calendar = new GregorianCalendar(); GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(now); calendar.setTimeInMillis((long) now * 60000);
when(DateUtil.gregorianCalendar()).thenReturn(calendar); when(DateUtil.gregorianCalendar()).thenReturn(calendar);
} }
} }