Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bd276424 authored by Beverly's avatar Beverly
Browse files

Fix flaky test on daylight savings

When it's daylight savings, the one day offset also
adds another hour to the calendar. This made it so that the
"now" time in the test was no longer within the schedule times
so the test would fail. This sets the "now" time so that it will
still be within the schedule even with the daylight savings hour.

Test: atest ScheduleCalendarTest
Fixes: 128296836
Change-Id: Ifc8791a45bf1bb875cb7a91869363f8995f1cdc4
parent 77ad5ff0
Loading
Loading
Loading
Loading
+42 −2
Original line number Diff line number Diff line
@@ -414,6 +414,39 @@ public class ScheduleCalendarTest extends UiServiceTestCase {
        assertFalse(mScheduleCalendar.isInSchedule(cal.getTimeInMillis()));
    }

    @Test
    public void testIsAlarmInSchedule_alarmAndNowInSchedule_sameScheduleTrigger_daylightSavings() {
        Calendar alarm = getDaylightSavingsDay();
        alarm.set(Calendar.HOUR_OF_DAY, 23);
        alarm.set(Calendar.MINUTE, 15);
        alarm.set(Calendar.SECOND, 0);
        alarm.set(Calendar.MILLISECOND, 0);

        Calendar now = getDaylightSavingsDay();
        now.set(Calendar.HOUR_OF_DAY, 2);
        now.set(Calendar.MINUTE, 10);
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MILLISECOND, 0);
        now.add(Calendar.DATE, 1); // add a day, on daylight savings this becomes 3:10am

        final Calendar tempToday = getDaylightSavingsDay();
        final Calendar tempTomorrow = getDaylightSavingsDay();
        tempTomorrow.add(Calendar.DATE, 1);
        mScheduleInfo.days = new int[] {tempToday.get(Calendar.DAY_OF_WEEK),
                tempTomorrow.get(Calendar.DAY_OF_WEEK)};

        mScheduleInfo.startHour = 22;
        mScheduleInfo.startMinute = 15;
        mScheduleInfo.endHour = 3;
        mScheduleInfo.endMinute = 15;
        mScheduleCalendar.setSchedule(mScheduleInfo);

        assertTrue(mScheduleCalendar.isInSchedule(alarm.getTimeInMillis()));
        assertTrue(mScheduleCalendar.isInSchedule(now.getTimeInMillis()));
        assertTrue(mScheduleCalendar.isAlarmInSchedule(alarm.getTimeInMillis(),
                now.getTimeInMillis()));
    }

    @Test
    public void testIsAlarmInSchedule_alarmAndNowInSchedule_sameScheduleTrigger() {
        Calendar alarm = new GregorianCalendar();
@@ -424,10 +457,10 @@ public class ScheduleCalendarTest extends UiServiceTestCase {

        Calendar now = new GregorianCalendar();
        now.set(Calendar.HOUR_OF_DAY, 2);
        now.set(Calendar.MINUTE, 15);
        now.set(Calendar.MINUTE, 10);
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MILLISECOND, 0);
        now.add(Calendar.DATE, 1); // add a day
        now.add(Calendar.DATE, 1); // add a day, on daylight savings this becomes 3:10am

        mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay(1)};
        mScheduleInfo.startHour = 22;
@@ -481,4 +514,11 @@ public class ScheduleCalendarTest extends UiServiceTestCase {
        cal.add(Calendar.DATE, offset);
        return cal.get(Calendar.DAY_OF_WEEK);
    }


    private Calendar getDaylightSavingsDay() {
        // the day before daylight savings in the US - March 9, 2019
        Calendar daylightSavingsDay = new GregorianCalendar(2019, 2, 9);
        return daylightSavingsDay;
    }
}