mirror of https://github.com/tasks/tasks
added reminder service tests, got many to pass
parent
05234dde7f
commit
a453628a8a
@ -0,0 +1,179 @@
|
|||||||
|
package com.todoroo.astrid.reminders;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import android.app.Notification;
|
||||||
|
import android.content.Intent;
|
||||||
|
|
||||||
|
import com.timsu.astrid.R;
|
||||||
|
import com.todoroo.andlib.service.Autowired;
|
||||||
|
import com.todoroo.andlib.service.NotificationManager;
|
||||||
|
import com.todoroo.andlib.utility.DateUtilities;
|
||||||
|
import com.todoroo.astrid.dao.TaskDao;
|
||||||
|
import com.todoroo.astrid.model.Task;
|
||||||
|
import com.todoroo.astrid.test.DatabaseTestCase;
|
||||||
|
import com.todoroo.astrid.utility.Preferences;
|
||||||
|
|
||||||
|
public class NotificationTests extends DatabaseTestCase {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
TaskDao taskDao;
|
||||||
|
|
||||||
|
public class MutableBoolean {
|
||||||
|
boolean value = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** test that a normal task gets a notification */
|
||||||
|
public void testAlarmToNotification() {
|
||||||
|
final Task task = new Task();
|
||||||
|
task.setValue(Task.TITLE, "rubberduck");
|
||||||
|
taskDao.persist(task);
|
||||||
|
|
||||||
|
final MutableBoolean triggered = new MutableBoolean();
|
||||||
|
|
||||||
|
Notifications.setNotificationManager(new TestNotificationManager() {
|
||||||
|
public void notify(int id, Notification notification) {
|
||||||
|
assertNotNull(notification.contentIntent);
|
||||||
|
triggered.value = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
Intent intent = new Intent();
|
||||||
|
intent.putExtra(Notifications.ID_KEY, task.getId());
|
||||||
|
intent.putExtra(Notifications.TYPE_KEY, ReminderService.TYPE_DUE);
|
||||||
|
new Notifications().onReceive(getContext(), intent);
|
||||||
|
assertTrue(triggered.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** test that a deleted task doesn't get a notification */
|
||||||
|
public void testDeletedTask() {
|
||||||
|
final Task task = new Task();
|
||||||
|
task.setValue(Task.TITLE, "gooeyduck");
|
||||||
|
task.setValue(Task.DELETION_DATE, DateUtilities.now());
|
||||||
|
taskDao.persist(task);
|
||||||
|
|
||||||
|
Notifications.setNotificationManager(new NotificationManager() {
|
||||||
|
|
||||||
|
public void cancel(int id) {
|
||||||
|
// allowed
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancelAll() {
|
||||||
|
fail("wtf cancel all?");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void notify(int id, Notification notification) {
|
||||||
|
fail("sent a notification, you shouldn't have...");
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
Intent intent = new Intent();
|
||||||
|
intent.putExtra(Notifications.ID_KEY, task.getId());
|
||||||
|
intent.putExtra(Notifications.TYPE_KEY, ReminderService.TYPE_DUE);
|
||||||
|
new Notifications().onReceive(getContext(), intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** test that a completed task doesn't get a notification */
|
||||||
|
public void testCompletedTask() {
|
||||||
|
final Task task = new Task();
|
||||||
|
task.setValue(Task.TITLE, "rubberduck");
|
||||||
|
task.setValue(Task.COMPLETION_DATE, DateUtilities.now());
|
||||||
|
taskDao.persist(task);
|
||||||
|
|
||||||
|
Notifications.setNotificationManager(new NotificationManager() {
|
||||||
|
|
||||||
|
public void cancel(int id) {
|
||||||
|
// allowed
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancelAll() {
|
||||||
|
fail("wtf cancel all?");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void notify(int id, Notification notification) {
|
||||||
|
fail("sent a notification, you shouldn't have...");
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
Intent intent = new Intent();
|
||||||
|
intent.putExtra(Notifications.ID_KEY, task.getId());
|
||||||
|
intent.putExtra(Notifications.TYPE_KEY, ReminderService.TYPE_DUE);
|
||||||
|
new Notifications().onReceive(getContext(), intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** test of quiet hours */
|
||||||
|
public void testQuietHours() {
|
||||||
|
final Task task = new Task();
|
||||||
|
task.setValue(Task.TITLE, "rubberduck");
|
||||||
|
taskDao.persist(task);
|
||||||
|
Intent intent = new Intent();
|
||||||
|
intent.putExtra(Notifications.ID_KEY, task.getId());
|
||||||
|
|
||||||
|
int hour = new Date().getHours();
|
||||||
|
Preferences.setStringFromInteger(R.string.p_notif_quietStart, hour - 1);
|
||||||
|
Preferences.setStringFromInteger(R.string.p_notif_quietEnd, hour + 1);
|
||||||
|
|
||||||
|
// due date notification has vibrate
|
||||||
|
Notifications.setNotificationManager(new TestNotificationManager() {
|
||||||
|
public void notify(int id, Notification notification) {
|
||||||
|
assertNull(notification.sound);
|
||||||
|
assertTrue((notification.defaults & Notification.DEFAULT_SOUND) == 0);
|
||||||
|
assertNotNull(notification.vibrate);
|
||||||
|
assertTrue(notification.vibrate.length > 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
intent.putExtra(Notifications.TYPE_KEY, ReminderService.TYPE_DUE);
|
||||||
|
new Notifications().onReceive(getContext(), intent);
|
||||||
|
|
||||||
|
// random notification does not
|
||||||
|
Notifications.setNotificationManager(new TestNotificationManager() {
|
||||||
|
public void notify(int id, Notification notification) {
|
||||||
|
assertNull(notification.sound);
|
||||||
|
assertTrue((notification.defaults & Notification.DEFAULT_SOUND) == 0);
|
||||||
|
assertTrue(notification.vibrate == null ||
|
||||||
|
notification.vibrate.length == 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
intent.removeExtra(Notifications.TYPE_KEY);
|
||||||
|
intent.putExtra(Notifications.TYPE_KEY, ReminderService.TYPE_RANDOM);
|
||||||
|
new Notifications().onReceive(getContext(), intent);
|
||||||
|
|
||||||
|
// wrapping works
|
||||||
|
Preferences.setStringFromInteger(R.string.p_notif_quietStart, hour + 2);
|
||||||
|
Preferences.setStringFromInteger(R.string.p_notif_quietEnd, hour + 1);
|
||||||
|
|
||||||
|
Notifications.setNotificationManager(new TestNotificationManager() {
|
||||||
|
public void notify(int id, Notification notification) {
|
||||||
|
assertNull(notification.sound);
|
||||||
|
assertTrue((notification.defaults & Notification.DEFAULT_SOUND) == 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
intent.removeExtra(Notifications.TYPE_KEY);
|
||||||
|
intent.putExtra(Notifications.TYPE_KEY, ReminderService.TYPE_DUE);
|
||||||
|
new Notifications().onReceive(getContext(), intent);
|
||||||
|
|
||||||
|
// nonstop notification still sounds
|
||||||
|
task.setValue(Task.REMINDER_FLAGS, Task.NOTIFY_NONSTOP);
|
||||||
|
task.save();
|
||||||
|
Notifications.setNotificationManager(new TestNotificationManager() {
|
||||||
|
public void notify(int id, Notification notification) {
|
||||||
|
assertTrue(notification.sound != null ||
|
||||||
|
(notification.defaults & Notification.DEFAULT_SOUND) > 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
new Notifications().onReceive(getContext(), intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract public class TestNotificationManager implements NotificationManager {
|
||||||
|
public void cancel(int id) {
|
||||||
|
fail("wtf cance?");
|
||||||
|
}
|
||||||
|
public void cancelAll() {
|
||||||
|
fail("wtf cancel all?");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,172 @@
|
|||||||
|
package com.todoroo.astrid.reminders;
|
||||||
|
|
||||||
|
import com.todoroo.andlib.service.Autowired;
|
||||||
|
import com.todoroo.andlib.utility.DateUtilities;
|
||||||
|
import com.todoroo.astrid.dao.TaskDao;
|
||||||
|
import com.todoroo.astrid.model.Task;
|
||||||
|
import com.todoroo.astrid.reminders.ReminderService.AlarmScheduler;
|
||||||
|
import com.todoroo.astrid.test.DatabaseTestCase;
|
||||||
|
import com.todoroo.astrid.utility.Preferences;
|
||||||
|
|
||||||
|
public class ReminderServiceTests extends DatabaseTestCase {
|
||||||
|
|
||||||
|
ReminderService service;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
TaskDao taskDao;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
service = new ReminderService();
|
||||||
|
Preferences.setPreferenceDefaults();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** tests with no alarms */
|
||||||
|
public void testNoReminders() {
|
||||||
|
service.setScheduler(new NoAlarmExpected());
|
||||||
|
|
||||||
|
Task task = new Task();
|
||||||
|
task.setValue(Task.TITLE, "water");
|
||||||
|
task.setValue(Task.REMINDER_FLAGS, 0);
|
||||||
|
taskDao.save(task, false);
|
||||||
|
service.scheduleAlarm(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** tests with due date */
|
||||||
|
public void testDueDates() {
|
||||||
|
// test due date in the past
|
||||||
|
service.setScheduler(new NoAlarmExpected());
|
||||||
|
final Task task = new Task();
|
||||||
|
task.setValue(Task.TITLE, "water");
|
||||||
|
task.setValue(Task.DUE_DATE, DateUtilities.now() - DateUtilities.ONE_DAY);
|
||||||
|
task.setValue(Task.REMINDER_FLAGS, Task.NOTIFY_AT_DEADLINE);
|
||||||
|
taskDao.save(task, false);
|
||||||
|
service.scheduleAlarm(task);
|
||||||
|
|
||||||
|
// test due date in the future
|
||||||
|
task.setValue(Task.DUE_DATE, DateUtilities.now() + DateUtilities.ONE_DAY);
|
||||||
|
taskDao.save(task, false);
|
||||||
|
service.setScheduler(new AlarmExpected() {
|
||||||
|
@Override
|
||||||
|
public void createAlarm(Task task, long time, int type) {
|
||||||
|
super.createAlarm(task, time, type);
|
||||||
|
assertEquals((long)task.getValue(Task.DUE_DATE), time);
|
||||||
|
assertEquals(type, ReminderService.TYPE_DUE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
service.scheduleAlarm(task);
|
||||||
|
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** tests with random */
|
||||||
|
public void testRandom() {
|
||||||
|
// test random
|
||||||
|
final Task task = new Task();
|
||||||
|
task.setValue(Task.TITLE, "water");
|
||||||
|
task.setValue(Task.REMINDER_PERIOD, DateUtilities.ONE_WEEK);
|
||||||
|
taskDao.save(task, false);
|
||||||
|
service.setScheduler(new AlarmExpected() {
|
||||||
|
@Override
|
||||||
|
public void createAlarm(Task task, long time, int type) {
|
||||||
|
super.createAlarm(task, time, type);
|
||||||
|
assertTrue(time > DateUtilities.now());
|
||||||
|
assertTrue(time < DateUtilities.now() + 1.2 * DateUtilities.ONE_WEEK);
|
||||||
|
assertEquals(type, ReminderService.TYPE_RANDOM);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
service.scheduleAlarm(task);
|
||||||
|
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
|
||||||
|
|
||||||
|
// test random with last notify way in the past
|
||||||
|
task.setValue(Task.REMINDER_LAST, DateUtilities.now() - 2 * DateUtilities.ONE_WEEK);
|
||||||
|
((AlarmExpected)service.getScheduler()).alarmCreated = false;
|
||||||
|
service.scheduleAlarm(task);
|
||||||
|
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** tests with overdue */
|
||||||
|
public void testOverdue() {
|
||||||
|
// test due date in the future
|
||||||
|
service.setScheduler(new NoAlarmExpected());
|
||||||
|
final Task task = new Task();
|
||||||
|
task.setValue(Task.TITLE, "water");
|
||||||
|
task.setValue(Task.DUE_DATE, DateUtilities.now() + DateUtilities.ONE_DAY);
|
||||||
|
task.setValue(Task.REMINDER_FLAGS, Task.NOTIFY_AFTER_DEADLINE);
|
||||||
|
taskDao.save(task, false);
|
||||||
|
service.scheduleAlarm(task);
|
||||||
|
|
||||||
|
// test due date in the past
|
||||||
|
task.setValue(Task.DUE_DATE, DateUtilities.now() - DateUtilities.ONE_DAY);
|
||||||
|
taskDao.save(task, false);
|
||||||
|
service.setScheduler(new AlarmExpected() {
|
||||||
|
@Override
|
||||||
|
public void createAlarm(Task task, long time, int type) {
|
||||||
|
super.createAlarm(task, time, type);
|
||||||
|
assertTrue(time > DateUtilities.now());
|
||||||
|
assertTrue(time < DateUtilities.now() + DateUtilities.ONE_DAY);
|
||||||
|
assertEquals(type, ReminderService.TYPE_OVERDUE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
service.scheduleAlarm(task);
|
||||||
|
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** tests with multiple */
|
||||||
|
public void testMultipleReminders() {
|
||||||
|
// test due date in the future, enable random
|
||||||
|
final Task task = new Task();
|
||||||
|
task.setValue(Task.TITLE, "water");
|
||||||
|
task.setValue(Task.DUE_DATE, DateUtilities.now() + DateUtilities.ONE_WEEK);
|
||||||
|
task.setValue(Task.REMINDER_FLAGS, Task.NOTIFY_AT_DEADLINE);
|
||||||
|
task.setValue(Task.REMINDER_PERIOD, DateUtilities.ONE_DAY);
|
||||||
|
taskDao.save(task, false);
|
||||||
|
service.setScheduler(new AlarmExpected() {
|
||||||
|
@Override
|
||||||
|
public void createAlarm(Task task, long time, int type) {
|
||||||
|
super.createAlarm(task, time, type);
|
||||||
|
assertTrue(time > DateUtilities.now());
|
||||||
|
assertTrue(time < DateUtilities.now() + DateUtilities.ONE_DAY);
|
||||||
|
assertEquals(type, ReminderService.TYPE_RANDOM);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
service.scheduleAlarm(task);
|
||||||
|
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
|
||||||
|
|
||||||
|
// now set the due date in the past
|
||||||
|
task.setValue(Task.DUE_DATE, DateUtilities.now() - DateUtilities.ONE_WEEK);
|
||||||
|
((AlarmExpected)service.getScheduler()).alarmCreated = false;
|
||||||
|
service.scheduleAlarm(task);
|
||||||
|
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
|
||||||
|
|
||||||
|
// now set the due date before the random
|
||||||
|
task.setValue(Task.DUE_DATE, DateUtilities.now() + DateUtilities.ONE_HOUR);
|
||||||
|
taskDao.save(task, false);
|
||||||
|
service.setScheduler(new AlarmExpected() {
|
||||||
|
@Override
|
||||||
|
public void createAlarm(Task task, long time, int type) {
|
||||||
|
super.createAlarm(task, time, type);
|
||||||
|
assertEquals((long)task.getValue(Task.DUE_DATE), time);
|
||||||
|
assertEquals(type, ReminderService.TYPE_DUE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
service.scheduleAlarm(task);
|
||||||
|
assertTrue(((AlarmExpected)service.getScheduler()).alarmCreated);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- helper classes
|
||||||
|
|
||||||
|
public class NoAlarmExpected implements AlarmScheduler {
|
||||||
|
public void createAlarm(Task task, long time, int type) {
|
||||||
|
fail("created alarm, no alarm expected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AlarmExpected implements AlarmScheduler {
|
||||||
|
public boolean alarmCreated = false;
|
||||||
|
public void createAlarm(Task task, long time, int type) {
|
||||||
|
alarmCreated = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue