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

View file

@ -640,7 +640,7 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface
throw new IllegalArgumentException("Profile can not be null"); throw new IllegalArgumentException("Profile can not be null");
} }
Profile.ProfileValue[] basalValues = profile.getBasalValues(); Profile.ProfileValue[] basalValues = profile.getBasalValues();
if(basalValues == null) { if (basalValues == null) {
throw new IllegalArgumentException("Basal values can not be null"); throw new IllegalArgumentException("Basal values can not be null");
} }
List<BasalScheduleEntry> entries = new ArrayList<>(); List<BasalScheduleEntry> entries = new ArrayList<>();

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