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
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);
}

View file

@ -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);
}
}