Add time and time change tests for PodSessionState

This commit is contained in:
Bart Sopers 2019-12-21 00:55:32 +01:00
parent 0bcdf30a7e
commit 78d789db4e
3 changed files with 112 additions and 17 deletions

View file

@ -63,7 +63,7 @@ public class PodSessionState extends PodState {
this.lot = lot;
this.tid = tid;
this.nonceState = new NonceState(lot, tid);
store();
handleUpdates();
}
public void setStateChangedHandler(PodStateChangedHandler handler) {
@ -80,20 +80,20 @@ public class PodSessionState extends PodState {
public void putConfiguredAlert(AlertSlot alertSlot, AlertType alertType) {
configuredAlerts.put(alertSlot, alertType);
store();
handleUpdates();
}
public void removeConfiguredAlert(AlertSlot alertSlot) {
configuredAlerts.remove(alertSlot);
store();
handleUpdates();
}
public DateTime getActivatedAt() {
return activatedAt;
return activatedAt == null ? null : activatedAt.withZone(timeZone);
}
public DateTime getExpiresAt() {
return expiresAt;
return expiresAt == null ? null : expiresAt.withZone(timeZone);
}
public String getExpiryDateAsString() {
@ -128,7 +128,7 @@ public class PodSessionState extends PodState {
int seed = ((sum & 0xFFFF) ^ syncWord);
this.nonceState = new NonceState(lot, tid, (byte) (seed & 0xFF));
store();
handleUpdates();
}
public int getCurrentNonce() {
@ -137,7 +137,7 @@ public class PodSessionState extends PodState {
public synchronized void advanceToNextNonce() {
nonceState.advanceToNextNonce();
store();
handleUpdates();
}
public SetupProgress getSetupProgress() {
@ -149,7 +149,7 @@ public class PodSessionState extends PodState {
throw new IllegalArgumentException("Setup state cannot be null");
}
this.setupProgress = setupProgress;
store();
handleUpdates();
}
public boolean isSuspended() {
@ -173,11 +173,12 @@ public class PodSessionState extends PodState {
throw new IllegalArgumentException("Time zone can not be null");
}
this.timeZone = timeZone;
store();
handleUpdates();
}
public DateTime getTime() {
return DateTime.now().withZone(timeZone);
DateTime now = DateTime.now();
return now.withZone(timeZone);
}
public Duration getScheduleOffset() {
@ -194,13 +195,13 @@ public class PodSessionState extends PodState {
@Override
public void setPacketNumber(int packetNumber) {
super.setPacketNumber(packetNumber);
store();
handleUpdates();
}
@Override
public void setMessageNumber(int messageNumber) {
super.setMessageNumber(messageNumber);
store();
handleUpdates();
}
public BasalSchedule getBasalSchedule() {
@ -209,7 +210,7 @@ public class PodSessionState extends PodState {
public void setBasalSchedule(BasalSchedule basalSchedule) {
this.basalSchedule = basalSchedule;
store();
handleUpdates();
}
public DeliveryStatus getLastDeliveryStatus() {
@ -220,7 +221,7 @@ public class PodSessionState extends PodState {
public void setFaultEvent(PodInfoFaultEvent faultEvent) {
super.setFaultEvent(faultEvent);
suspended = true;
store();
handleUpdates();
}
@Override
@ -238,10 +239,10 @@ public class PodSessionState extends PodState {
activeAlerts = statusResponse.getAlerts();
lastDeliveryStatus = statusResponse.getDeliveryStatus();
reservoirLevel = statusResponse.getReservoirLevel();
store();
handleUpdates();
}
private void store() {
private void handleUpdates() {
Gson gson = OmnipodUtil.getGsonInstance();
SP.putString(OmnipodConst.Prefs.PodState, gson.toJson(this));
if (stateChangedHandler != null) {

View file

@ -0,0 +1,94 @@
package info.nightscout.androidaps.plugins.pump.omnipod.defs.state;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.joda.time.Duration;
import org.junit.After;
import org.junit.Test;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.FirmwareVersion;
import static org.junit.Assert.assertEquals;
// FIXME these tests fail because of some android stuff:
// java.lang.NoClassDefFoundError: android/app/Application
// it's caused by the PodSessionState class using the SP class for storage
public class PodSessionStateTest {
@Test
public void times() {
DateTimeZone timeZone = DateTimeZone.UTC;
DateTimeZone.setDefault(timeZone);
DateTime now = new DateTime(2020, 1, 1, 1, 2, 3, timeZone);
DateTime initialized = now.minus(Duration.standardDays(1));
DateTimeUtils.setCurrentMillisFixed(now.getMillis());
PodSessionState podSessionState = new PodSessionState(timeZone, 0x0, initialized,
new FirmwareVersion(1, 1, 1),
new FirmwareVersion(2, 2, 2),
0, 0, 0, 0);
assertEquals(initialized, podSessionState.getActivatedAt());
assertEquals(now, podSessionState.getTime());
assertEquals(Duration.standardHours(1).plus(Duration.standardMinutes(2).plus(Duration.standardSeconds(3))), podSessionState.getScheduleOffset());
}
@Test
public void changeSystemTimeZoneWithoutChangingPodTimeZone() {
DateTimeZone timeZone = DateTimeZone.UTC;
DateTimeZone.setDefault(timeZone);
DateTime now = new DateTime(2020, 1, 1, 1, 2, 3, timeZone);
DateTime initialized = now.minus(Duration.standardDays(1));
DateTimeUtils.setCurrentMillisFixed(now.getMillis());
PodSessionState podSessionState = new PodSessionState(timeZone, 0x0, initialized,
new FirmwareVersion(1, 1, 1),
new FirmwareVersion(2, 2, 2),
0, 0, 0, 0);
DateTimeZone newTimeZone = DateTimeZone.forOffsetHours(2);
DateTimeZone.setDefault(newTimeZone);
// The system time zone has been updated, but the pod session state's time zone hasn't
// So the pods time should not have been changed
assertEquals(initialized, podSessionState.getActivatedAt());
assertEquals(now, podSessionState.getTime());
assertEquals(Duration.standardHours(1).plus(Duration.standardMinutes(2).plus(Duration.standardSeconds(3))), podSessionState.getScheduleOffset());
}
@Test
public void changeSystemTimeZoneAndChangePodTimeZone() {
DateTimeZone timeZone = DateTimeZone.UTC;
DateTimeZone.setDefault(timeZone);
DateTime now = new DateTime(2020, 1, 1, 1, 2, 3, timeZone);
DateTime initialized = now.minus(Duration.standardDays(1));
DateTimeUtils.setCurrentMillisFixed(now.getMillis());
PodSessionState podSessionState = new PodSessionState(timeZone, 0x0, initialized,
new FirmwareVersion(1, 1, 1),
new FirmwareVersion(2, 2, 2),
0, 0, 0, 0);
DateTimeZone newTimeZone = DateTimeZone.forOffsetHours(2);
DateTimeZone.setDefault(newTimeZone);
podSessionState.setTimeZone(newTimeZone);
// Both the system time zone have been updated
// So the pods time should have been changed (to +2 hours)
assertEquals(initialized.withZone(newTimeZone), podSessionState.getActivatedAt());
assertEquals(now.withZone(newTimeZone), podSessionState.getTime());
assertEquals(Duration.standardHours(3).plus(Duration.standardMinutes(2).plus(Duration.standardSeconds(3))), podSessionState.getScheduleOffset());
}
@After
public void tearDown() {
DateTimeUtils.setCurrentMillisSystem();
}
}