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();
    }
}
@@ -240,14 +240,303 @@
 nit.
-Test;%0A
+Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.powermock.core.classloader.annotations.PrepareForTest;%0Aimport org.powermock.modules.junit4.PowerMockRunner;%0A%0Aimport info.AAPSMocker;%0Aimport info.nightscout.androidaps.MainApp;%0Aimport info.nightscout.androidaps.logging.L;
 %0Aimp
@@ -608,16 +608,60 @@
 Version;
+%0Aimport info.nightscout.androidaps.utils.SP;
 %0A%0Aimport
@@ -704,235 +704,278 @@
 s;%0A%0A
-// FIXME these tests fail because of some android stuff:%0A//  java.lang.NoClassDefFoundError: android/app/Application%0A//  it's caused by the PodSessionState class using the SP class for storage%0Apublic class PodSessionStateTest %7B
+@RunWith(PowerMockRunner.class)%0A@PrepareForTest(%7BMainApp.class, SP.class, L.class%7D)%0Apublic class PodSessionStateTest %7B%0A    @Before%0A    public void setup() %7B%0A        AAPSMocker.mockMainApp();%0A        AAPSMocker.mockApplicationContext();%0A        AAPSMocker.mockSP();%0A    %7D
 %0A%0A  
@@ -1374,37 +1374,24 @@
 meZone, 0x0,
- initialized,
 %0A           
@@ -1505,77 +1505,8 @@
 );%0A%0A
-        assertEquals(initialized, podSessionState.getActivatedAt());%0A
     
@@ -2159,37 +2159,24 @@
 meZone, 0x0,
- initialized,
 %0A           
@@ -2556,77 +2556,8 @@
 ged%0A
-        assertEquals(initialized, podSessionState.getActivatedAt());%0A
     
@@ -3212,21 +3212,8 @@
 0x0,
- initialized,
 %0A   
@@ -3621,99 +3621,8 @@
 rs)%0A
-        assertEquals(initialized.withZone(newTimeZone), podSessionState.getActivatedAt());%0A
     

Fix PodSessionStateTest