Reminder service tests

* Port ReminderServiceTests
* Add NotifyAtDeadlineTest
pull/120/head
Alex Baker 11 years ago
parent 7a6feb1b9f
commit 4d585ce3be

@ -31,7 +31,7 @@ import static org.tasks.date.DateTimeUtils.newDate;
* @author Tim Su <tim@todoroo.com>
*
*/
public final class Task extends RemoteModel {
public class Task extends RemoteModel {
// --- table and uri

@ -81,7 +81,7 @@ public final class ReminderService {
private long now = -1; // For tracking when reminders might be scheduled all at once
private ReminderService() {
ReminderService() {
DependencyInjectionService.getInstance().inject(this);
setPreferenceDefaults();
}
@ -97,6 +97,10 @@ public final class ReminderService {
return instance;
}
void clearInstance() {
instance = null;
}
// --- preference handling
private static boolean preferencesInitialized = false;
@ -301,7 +305,7 @@ public final class ReminderService {
* If the date was indicated to not have a due time, we read from
* preferences and assign a time.
*/
private long calculateNextDueDateReminder(Task task) {
long calculateNextDueDateReminder(Task task) {
// Uses getNowValue() instead of DateUtilities.now()
if(task.hasDueDate() && task.getFlag(Task.REMINDER_FLAGS, Task.NOTIFY_AT_DEADLINE)) {
long dueDate = task.getValue(Task.DUE_DATE);
@ -309,9 +313,7 @@ public final class ReminderService {
long dueDateAlarm = NO_ALARM;
if(task.hasDueTime())
// return due date straight up
{
if(task.hasDueTime()) {
dueDateAlarm = dueDate;
} else if (DateUtilities.now() > lastReminder + DateUtilities.ONE_DAY) {
// return notification time on this day

@ -21,6 +21,7 @@ import org.tasks.Broadcaster;
import java.util.Locale;
import static org.mockito.Mockito.mock;
import static org.tasks.TestUtilities.resetPreferences;
public class TodorooRobolectricTestCase {
@ -32,8 +33,9 @@ public class TodorooRobolectricTestCase {
}
@Before
public void before() throws Exception {
public void before() {
ContextManager.setContext(getRobolectricContext());
resetPreferences();
AstridDependencyInjector.reset();
testInjector = RobolectricTestDependencyInjector.initialize("test");
addInjectable("broadcaster", mock(Broadcaster.class));

@ -31,7 +31,7 @@ public class GtasksDetailExposerTest extends TodorooRobolectricTestCase {
private GtasksListService gtasksListService;
@Override
public void before() throws Exception {
public void before() {
context = mock(Context.class);
super.before();

@ -27,7 +27,7 @@ public class TaskTest extends TodorooRobolectricTestCase {
private TaskService taskService;
@Override
public void before() throws Exception {
public void before() {
super.before();
taskService = new TaskService();

@ -0,0 +1,90 @@
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;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.tasks.R;
import static com.todoroo.astrid.reminders.ReminderService.NO_ALARM;
import static org.junit.Assert.assertEquals;
import static org.tasks.Freeze.freezeAt;
import static org.tasks.Freeze.thaw;
import static org.tasks.date.DateTimeUtils.newDate;
@RunWith(RobolectricTestRunner.class)
public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
@Autowired
TaskDao taskDao;
ReminderService service;
@Override
public void before() {
super.before();
freezeAt(new DateTime(2014, 1, 24, 17, 23, 37));
service = new ReminderService();
}
@After
public void after() {
thaw();
}
@Test
public void scheduleReminderAtDueTime() {
final DateTime dueDate = new DateTime(2014, 1, 24, 19, 23, 57);
Task task = new Task() {{
setValue(Task.DUE_DATE, dueDate.getMillis());
setValue(Task.REMINDER_FLAGS, Task.NOTIFY_AT_DEADLINE);
}};
assertEquals(dueDate.getMillis(), service.calculateNextDueDateReminder(task));
}
@Test
public void noReminderWhenNoDueDate() {
Task task = new Task() {{
setValue(Task.REMINDER_FLAGS, Task.NOTIFY_AT_DEADLINE);
}};
assertEquals(NO_ALARM, service.calculateNextDueDateReminder(task));
}
@Test
public void noReminderWhenNotifyAtDeadlineFlagNotSet() {
Task task = new Task() {{
setValue(Task.DUE_DATE, new DateTime(2014, 1, 24, 19, 23, 57).getMillis());
}};
assertEquals(NO_ALARM, service.calculateNextDueDateReminder(task));
}
@Test
public void dontNotifyMoreThanOncePerDay() {
Task task = new Task() {{
setValue(Task.DUE_DATE, newDate(2014, 1, 23).getTime());
setValue(Task.REMINDER_FLAGS, Task.NOTIFY_AT_DEADLINE);
setValue(Task.REMINDER_LAST, new DateTime(2014, 1, 23, 17, 23, 37).getMillis());
}};
assertEquals(NO_ALARM, service.calculateNextDueDateReminder(task));
}
@Test
public void notifyIfLastNotificationWasMoreThanOneDayAgo() {
final DateTime dueDate = new DateTime(2014, 1, 23, 0, 0, 0, 0);
Task task = new Task() {{
setValue(Task.DUE_DATE, dueDate.getMillis());
setValue(Task.REMINDER_FLAGS, Task.NOTIFY_AT_DEADLINE);
setValue(Task.REMINDER_LAST, new DateTime(2014, 1, 23, 17, 23, 36).getMillis());
}};
assertEquals(
dueDate.withHourOfDay(18).getMillis(),
service.calculateNextDueDateReminder(task));
}
}

@ -6,38 +6,42 @@
package com.todoroo.astrid.reminders;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.test.TodorooRobolectricTestCase;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.reminders.ReminderService.AlarmScheduler;
import com.todoroo.astrid.test.DatabaseTestCase;
import com.todoroo.astrid.utility.AstridPreferences;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.tasks.date.DateTimeUtils.newDate;
public class ReminderServiceTests extends DatabaseTestCase {
@RunWith(RobolectricTestRunner.class)
public class ReminderServiceTest extends TodorooRobolectricTestCase {
ReminderService service;
AlarmScheduler original;
@Autowired
TaskDao taskDao;
@Override
protected void setUp() throws Exception {
super.setUp();
public void before() {
super.before();
service = ReminderService.getInstance();
original = service.getScheduler();
AstridPreferences.setPreferenceDefaults();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
service.setScheduler(original);
@After
public void after() throws Exception {
service.clearInstance();
}
/** tests with no alarms */
@Test
public void testNoReminders() {
service.setScheduler(new NoAlarmExpected());
@ -49,7 +53,7 @@ public class ReminderServiceTests extends DatabaseTestCase {
service.scheduleAlarm(task);
}
/** tests with due date */
@Test
public void testDueDates() {
service.setScheduler(new AlarmExpected() {
@Override
@ -75,7 +79,7 @@ public class ReminderServiceTests extends DatabaseTestCase {
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
}
/** tests with random */
@Test
public void testRandom() {
// test random
final Task task = new Task();
@ -96,7 +100,7 @@ public class ReminderServiceTests extends DatabaseTestCase {
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
}
/** tests with overdue */
@Test
public void testOverdue() {
// test due date in the future
service.setScheduler(new AlarmExpected() {
@ -149,7 +153,7 @@ public class ReminderServiceTests extends DatabaseTestCase {
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
}
/** tests with multiple */
@Test
public void testMultipleReminders() {
// test due date in the future, enable random
final Task task = new Task();
@ -193,8 +197,7 @@ public class ReminderServiceTests extends DatabaseTestCase {
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
}
/** tests with snooze */
@Test
public void testSnoozeReminders() {
// test due date and snooze in the future
final Task task = new Task();
@ -233,7 +236,6 @@ public class ReminderServiceTests extends DatabaseTestCase {
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
}
// --- helper classes
public class NoAlarmExpected implements AlarmScheduler {
@ -250,5 +252,4 @@ public class ReminderServiceTests extends DatabaseTestCase {
alarmCreated = true;
}
}
}

@ -40,7 +40,7 @@ public class AdvancedRepeatTest extends TodorooRobolectricTestCase {
private RRule rrule;
@Before
public void before() throws Exception {
public void before() {
super.before();
task = new Task();
task.setValue(Task.COMPLETION_DATE, DateUtilities.now());

Loading…
Cancel
Save