From b2a7a9fd0897899b56a85d7d0661c358354f927a Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 28 Jan 2014 23:08:23 -0600 Subject: [PATCH] Add notify at deadline tests --- .../java/com/todoroo/astrid/data/Task.java | 4 + .../astrid/reminders/ReminderService.java | 2 +- .../reminders/NotifyAtDeadlineTest.java | 110 +++++++++++++++++- 3 files changed, 113 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/com/todoroo/astrid/data/Task.java b/api/src/main/java/com/todoroo/astrid/data/Task.java index 133c161e2..b6fc5a50d 100644 --- a/api/src/main/java/com/todoroo/astrid/data/Task.java +++ b/api/src/main/java/com/todoroo/astrid/data/Task.java @@ -490,6 +490,10 @@ public class Task extends RemoteModel { return getValue(DUE_DATE); } + public void setDueDate(int setting, long time) { + setDueDate(createDueDate(setting, time)); + } + public void setDueDate(Long dueDate) { setValue(DUE_DATE, dueDate); } diff --git a/astrid/src/main/java/com/todoroo/astrid/reminders/ReminderService.java b/astrid/src/main/java/com/todoroo/astrid/reminders/ReminderService.java index 102b5dd61..f99debdbc 100644 --- a/astrid/src/main/java/com/todoroo/astrid/reminders/ReminderService.java +++ b/astrid/src/main/java/com/todoroo/astrid/reminders/ReminderService.java @@ -335,7 +335,7 @@ public final class ReminderService { int quietHoursEnd = Preferences.getIntegerFromString(R.string.p_rmd_quietEnd, -1); Date quietHoursEndDate = newDate(); - quietHoursEndDate.setHours(quietHoursStart); + quietHoursEndDate.setHours(quietHoursEnd); quietHoursEndDate.setMinutes(0); quietHoursEndDate.setSeconds(0); diff --git a/astrid/src/test/java/com/todoroo/astrid/reminders/NotifyAtDeadlineTest.java b/astrid/src/test/java/com/todoroo/astrid/reminders/NotifyAtDeadlineTest.java index e1c70b879..67134a9bd 100644 --- a/astrid/src/test/java/com/todoroo/astrid/reminders/NotifyAtDeadlineTest.java +++ b/astrid/src/test/java/com/todoroo/astrid/reminders/NotifyAtDeadlineTest.java @@ -2,6 +2,7 @@ package com.todoroo.astrid.reminders; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.test.TodorooRobolectricTestCase; +import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.data.Task; @@ -10,6 +11,8 @@ import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; +import org.tasks.Freeze; +import org.tasks.R; import static com.todoroo.astrid.reminders.ReminderService.NO_ALARM; import static org.junit.Assert.assertEquals; @@ -21,9 +24,14 @@ import static org.tasks.date.DateTimeUtils.newDate; public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase { @Autowired - TaskDao taskDao; + private TaskDao taskDao; - ReminderService service; + private ReminderService service; + + private final Task dueAtNoon = new Task() {{ + setDueDate(Task.URGENCY_SPECIFIC_DAY, newDate(2014, 1, 27).getTime()); + setReminderFlags(Task.NOTIFY_AT_DEADLINE); + }}; @Override public void before() { @@ -85,4 +93,102 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase { dueDate.withHourOfDay(18).getMillis(), service.calculateNextDueDateReminder(task)); } + + @Test + public void duringQuietHoursSetNotificationAtEnd() { + setQuietHours(0, 10); + Freeze.freezeAt(new DateTime(2014, 1, 27, 9, 13, 37, 501)); + Preferences.setStringFromInteger(R.string.p_rmd_time, 8); + Task task = new Task() {{ + setDueDate(Task.URGENCY_SPECIFIC_DAY, newDate(2014, 1, 27).getTime()); + setReminderFlags(Task.NOTIFY_AT_DEADLINE); + }}; + assertEquals( + new DateTime(2014, 1, 27, 10, 0, 0, 0).getMillis(), + service.calculateNextDueDateReminder(task)); + } + + @Test + public void afterQuietHoursSetNotificationOnePeriodCloserToDueDate() { + setQuietHours(0, 10); + Freeze.freezeAt(new DateTime(2014, 1, 27, 11, 13, 37, 501)); + Preferences.setStringFromInteger(R.string.p_rmd_time, 8); + Task task = new Task() {{ + setDueDate(Task.URGENCY_SPECIFIC_DAY, newDate(2014, 1, 27).getTime()); + setReminderFlags(Task.NOTIFY_AT_DEADLINE); + }}; + assertEquals( + new DateTime(2014, 1, 27, 11, 25, 13, 125).getMillis(), + service.calculateNextDueDateReminder(task)); + } + + @Test + public void beforeQuietStartDueDateMoreThanOnePeriodAfterEnd() { + setQuietHours(2, 11); + Freeze.freezeAt(new DateTime(2014, 1, 27, 1, 53, 37, 509)); + Preferences.setStringFromInteger(R.string.p_rmd_time, 1); + Task task = new Task() {{ + setDueDate(Task.URGENCY_SPECIFIC_DAY, newDate(2014, 1, 27).getTime()); + setReminderFlags(Task.NOTIFY_AT_DEADLINE); + }}; + assertEquals( + new DateTime(2014, 1, 27, 11, 0, 0, 509).getMillis(), + service.calculateNextDueDateReminder(task)); + } + + @Test + public void beforeQuietStartDueDateLessThanOnePeriodAfterEnd() { + setQuietHours(3, 11); + Freeze.freezeAt(new DateTime(2014, 1, 27, 1, 53, 37, 509)); + Preferences.setStringFromInteger(R.string.p_rmd_time, 1); + assertEquals( + new DateTime(2014, 1, 27, 2, 10, 13, 259).getMillis(), + service.calculateNextDueDateReminder(dueAtNoon)); + } + + @Test + public void noAlarmAfterQuietHoursStartWithWrap() { + setQuietHours(10, 1); + Freeze.freezeAt(new DateTime(2014, 1, 27, 10, 0, 0, 0)); + Preferences.setStringFromInteger(R.string.p_rmd_time, 8); + assertEquals( + NO_ALARM, + service.calculateNextDueDateReminder(dueAtNoon)); + } + + @Test + public void setToQuietAlarmEndWithWrap() { + setQuietHours(22, 11); + Freeze.freezeAt(new DateTime(2014, 1, 27, 10, 59, 59, 999)); + Preferences.setStringFromInteger(R.string.p_rmd_time, 8); + assertEquals( + new DateTime(2014, 1, 27, 11, 0, 0, 0).getMillis(), + service.calculateNextDueDateReminder(dueAtNoon)); + } + + @Test + public void setReminderOnePeriodFromNowBeforeQuietHourStartWithWrap() { + setQuietHours(22, 11); + Freeze.freezeAt(new DateTime(2014, 1, 27, 11, 0, 0, 0)); + Preferences.setStringFromInteger(R.string.p_rmd_time, 8); + assertEquals( + // wtf? this is after due date + new DateTime(2014, 1, 27, 13, 45, 0, 0).getMillis(), + service.calculateNextDueDateReminder(dueAtNoon)); + } + + @Test + public void setReminderOnePeriodFromNowNoQuietHours() { + Freeze.freezeAt(new DateTime(2014, 1, 27, 11, 0, 0, 0)); + Preferences.setStringFromInteger(R.string.p_rmd_time, 8); + assertEquals( + new DateTime(2014, 1, 27, 11, 15, 0, 0).getMillis(), + service.calculateNextDueDateReminder(dueAtNoon)); + } + + private void setQuietHours(int start, int end) { + Preferences.setBoolean(R.string.p_rmd_enable_quiet, true); + Preferences.setStringFromInteger(R.string.p_rmd_quietStart, start); + Preferences.setStringFromInteger(R.string.p_rmd_quietEnd, end); + } }