mirror of https://github.com/tasks/tasks
Fixed unit test compilation errors. now to run them and etc.
parent
bb0d0e0354
commit
073d076c1f
@ -0,0 +1,175 @@
|
||||
package com.todoroo.astrid.utility;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.content.res.Resources;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import com.timsu.astrid.R;
|
||||
import com.todoroo.andlib.service.ContextManager;
|
||||
import com.todoroo.astrid.model.Task;
|
||||
|
||||
public class Preferences {
|
||||
|
||||
private static final String P_CURRENT_VERSION = "cv"; //$NON-NLS-1$
|
||||
private static final String P_READ_INTRODUCTION = "ri"; //$NON-NLS-1$
|
||||
|
||||
/** Set preference defaults, if unset. called at startup */
|
||||
public static void setPreferenceDefaults() {
|
||||
Context context = ContextManager.getContext();
|
||||
SharedPreferences prefs = getPrefs(context);
|
||||
Editor editor = prefs.edit();
|
||||
Resources r = context.getResources();
|
||||
|
||||
if(getIntegerFromString(R.string.EPr_default_urgency_key) == null) {
|
||||
editor.putString(r.getString(R.string.EPr_default_urgency_key),
|
||||
Integer.toString(Task.URGENCY_THIS_WEEK));
|
||||
}
|
||||
if(getIntegerFromString(R.string.EPr_default_importance_key) == null) {
|
||||
editor.putString(r.getString(R.string.EPr_default_importance_key),
|
||||
Integer.toString(Task.IMPORTANCE_SHOULD_DO));
|
||||
}
|
||||
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/* ======================================================================
|
||||
* ========================================================= system prefs
|
||||
* ====================================================================== */
|
||||
|
||||
/** CurrentVersion: the currently installed version of Astrid */
|
||||
public static int getCurrentVersion() {
|
||||
Context context = ContextManager.getContext();
|
||||
return getPrefs(context).getInt(P_CURRENT_VERSION, 0);
|
||||
}
|
||||
|
||||
/** CurrentVersion: the currently installed version of Astrid */
|
||||
public static void setCurrentVersion(int version) {
|
||||
Context context = ContextManager.getContext();
|
||||
Editor editor = getPrefs(context).edit();
|
||||
editor.putInt(P_CURRENT_VERSION, version);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/** ReadIntroduction: whether the user has read the introductory notes */
|
||||
public static boolean hasReadIntroduction() {
|
||||
Context context = ContextManager.getContext();
|
||||
return getPrefs(context).getBoolean(P_READ_INTRODUCTION, false);
|
||||
}
|
||||
|
||||
/** ReadIntroduction: whether the user has read the introductory notes */
|
||||
public static void setReadIntroduction(boolean value) {
|
||||
Context context = ContextManager.getContext();
|
||||
Editor editor = getPrefs(context).edit();
|
||||
editor.putBoolean(P_READ_INTRODUCTION, value);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/* ======================================================================
|
||||
* ======================================================= helper methods
|
||||
* ====================================================================== */
|
||||
|
||||
/** Get preferences object from the context */
|
||||
public static SharedPreferences getPrefs(Context context) {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context);
|
||||
}
|
||||
|
||||
// --- preference fetching
|
||||
|
||||
/** Gets an string value from a string preference. Returns null
|
||||
* if the value is not set
|
||||
*
|
||||
* @param context
|
||||
* @param key
|
||||
* @return integer value, or null on error
|
||||
*/
|
||||
public static String getStringValue(String key) {
|
||||
Context context = ContextManager.getContext();
|
||||
return getPrefs(context).getString(key, null);
|
||||
}
|
||||
|
||||
/** Gets an string value from a string preference. Returns null
|
||||
* if the value is not set
|
||||
*
|
||||
* @param context
|
||||
* @param key
|
||||
* @return integer value, or null on error
|
||||
*/
|
||||
public static String getStringValue(int keyResource) {
|
||||
Context context = ContextManager.getContext();
|
||||
return getPrefs(context).getString(context.getResources().getString(keyResource), null);
|
||||
}
|
||||
|
||||
/** Gets an integer value from a string key. Returns null
|
||||
* if the value is not set or not an integer.
|
||||
*
|
||||
* @param context
|
||||
* @param key
|
||||
* @return integer value, or null on error
|
||||
*/
|
||||
public static Integer getIntegerFromString(String key) {
|
||||
Context context = ContextManager.getContext();
|
||||
String value = getPrefs(context).getString(key, null);
|
||||
if(value == null)
|
||||
return null;
|
||||
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Gets an integer value from a string preference. Returns null
|
||||
* if the value is not set or not an integer.
|
||||
*
|
||||
* @param context
|
||||
* @param keyResource resource from string.xml
|
||||
* @return integer value, or null on error
|
||||
*/
|
||||
public static Integer getIntegerFromString(int keyResource) {
|
||||
Context context = ContextManager.getContext();
|
||||
Resources r = context.getResources();
|
||||
String value = getPrefs(context).getString(r.getString(keyResource), null);
|
||||
if(value == null)
|
||||
return null;
|
||||
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Gets an float value from a string preference. Returns null
|
||||
* if the value is not set or not an flat.
|
||||
*
|
||||
* @param context
|
||||
* @param keyResource resource from string.xml
|
||||
* @return
|
||||
*/
|
||||
public static Float getFloatFromString(int keyResource) {
|
||||
Context context = ContextManager.getContext();
|
||||
Resources r = context.getResources();
|
||||
String value = getPrefs(context).getString(r.getString(keyResource), ""); //$NON-NLS-1$
|
||||
|
||||
try {
|
||||
return Float.parseFloat(value);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Gets a boolean preference (e.g. a CheckBoxPreference setting)
|
||||
*
|
||||
* @param context
|
||||
* @param key
|
||||
* @param defValue
|
||||
* @return default if value is unset otherwise the value
|
||||
*/
|
||||
public static boolean getBoolean(String key, boolean defValue) {
|
||||
Context context = ContextManager.getContext();
|
||||
return getPrefs(context).getBoolean(key, defValue);
|
||||
}
|
||||
}
|
||||
@ -1,130 +0,0 @@
|
||||
package com.todoroo.astrid.reminders;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.todoroo.andlib.test.utility.DateUtilities;
|
||||
import com.todoroo.astrid.model.Task;
|
||||
|
||||
public class AlarmReceiverTests extends PluginTestCase {
|
||||
|
||||
/** simple test of task at deadline */
|
||||
public void testDeadlineReminder() {
|
||||
Task task = new Task();
|
||||
task.setValue(Task.TITLE, "poop");
|
||||
task.setValue(Task.DUE_DATE, DateUtilities.now());
|
||||
|
||||
long id = createTask(task, null);
|
||||
Intent alarmIntent = new Intent();
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_TASK_ID, id);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_DEADLINE, true);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_ALARMCLOCK, false);
|
||||
|
||||
AlarmReceiver rx = new AlarmReceiver();
|
||||
AssertiveNotificationManager nm = new AssertiveNotificationManager(getContext());
|
||||
AlarmReceiver.notificationManager = nm;
|
||||
rx.onReceive(getContext(), alarmIntent);
|
||||
nm.assertNotified();
|
||||
}
|
||||
|
||||
/** task at deadline, except hidden. no notification should sound */
|
||||
public void testDeadlineReminderExceptHidden() {
|
||||
Task task = new Task();
|
||||
task.setValue(Task.TITLE, "poop");
|
||||
task.setValue(Task.DUE_DATE, DateUtilities.now());
|
||||
task.setValue(Task.HIDDEN_UNTIL, DateUtilities.now() + 100);
|
||||
|
||||
long id = createTask(task, null);
|
||||
Intent alarmIntent = new Intent();
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_TASK_ID, id);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_DEADLINE, true);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_ALARMCLOCK, false);
|
||||
|
||||
AlarmReceiver rx = new AlarmReceiver();
|
||||
AssertiveNotificationManager nm = new AssertiveNotificationManager(getContext());
|
||||
AlarmReceiver.notificationManager = nm;
|
||||
rx.onReceive(getContext(), alarmIntent);
|
||||
nm.assertNotNotified();
|
||||
}
|
||||
|
||||
/** task upcoming */
|
||||
public void testUpcomingReminder() {
|
||||
Task task = new Task();
|
||||
task.setValue(Task.TITLE, "poop");
|
||||
task.setValue(Task.DUE_DATE, DateUtilities.now() + 100);
|
||||
|
||||
long id = createTask(task, null);
|
||||
Intent alarmIntent = new Intent();
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_TASK_ID, id);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_DEADLINE, false);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_ALARMCLOCK, false);
|
||||
|
||||
AlarmReceiver rx = new AlarmReceiver();
|
||||
AssertiveNotificationManager nm = new AssertiveNotificationManager(getContext());
|
||||
AlarmReceiver.notificationManager = nm;
|
||||
rx.onReceive(getContext(), alarmIntent);
|
||||
nm.assertNotified();
|
||||
}
|
||||
|
||||
/** task overdue */
|
||||
public void testOverdueReminder() {
|
||||
Task task = new Task();
|
||||
task.setValue(Task.TITLE, "poop");
|
||||
task.setValue(Task.DUE_DATE, DateUtilities.now() - 100);
|
||||
|
||||
long id = createTask(task, null);
|
||||
Intent alarmIntent = new Intent();
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_TASK_ID, id);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_DEADLINE, false);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_ALARMCLOCK, false);
|
||||
|
||||
AlarmReceiver rx = new AlarmReceiver();
|
||||
AssertiveNotificationManager nm = new AssertiveNotificationManager(getContext());
|
||||
AlarmReceiver.notificationManager = nm;
|
||||
rx.onReceive(getContext(), alarmIntent);
|
||||
nm.assertNotified();
|
||||
}
|
||||
|
||||
/** task alarm clock */
|
||||
public void testAlarmClock() {
|
||||
Task task = new Task();
|
||||
task.setValue(Task.TITLE, "poop");
|
||||
task.setValue(Task.DUE_DATE, DateUtilities.now());
|
||||
|
||||
long id = createTask(task, null);
|
||||
Intent alarmIntent = new Intent();
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_TASK_ID, id);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_DEADLINE, true);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_ALARMCLOCK, true);
|
||||
|
||||
AlarmReceiver rx = new AlarmReceiver();
|
||||
AssertiveNotificationManager nm = new AssertiveNotificationManager(getContext());
|
||||
AlarmReceiver.notificationManager = nm;
|
||||
rx.onReceive(getContext(), alarmIntent);
|
||||
nm.assertNotified();
|
||||
assertTrue((nm.getNotification().flags & Notification.FLAG_INSISTENT) > 0);
|
||||
}
|
||||
|
||||
/** test the intent that the alarm receiver creates */
|
||||
public void testOpenIntent() throws Exception {
|
||||
Task task = new Task();
|
||||
task.setValue(Task.TITLE, "poop");
|
||||
task.setValue(Task.DUE_DATE, DateUtilities.now());
|
||||
|
||||
long id = createTask(task, null);
|
||||
Intent alarmIntent = new Intent();
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_TASK_ID, id);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_DEADLINE, true);
|
||||
alarmIntent.putExtra(AlarmReceiver.TOKEN_IS_ALARMCLOCK, false);
|
||||
|
||||
AlarmReceiver rx = new AlarmReceiver();
|
||||
AssertiveNotificationManager nm = new AssertiveNotificationManager(getContext());
|
||||
AlarmReceiver.notificationManager = nm;
|
||||
rx.onReceive(getContext(), alarmIntent);
|
||||
nm.assertNotified();
|
||||
|
||||
PendingIntent intent = nm.getNotification().contentIntent;
|
||||
intent.send();
|
||||
}
|
||||
}
|
||||
@ -1,47 +0,0 @@
|
||||
package com.todoroo.astrid.reminders;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.content.Context;
|
||||
|
||||
import com.todoroo.andlib.service.NotificationManager.AndroidNotificationManager;
|
||||
|
||||
/**
|
||||
* Notification manager that provides notifications and adds an
|
||||
* extra method for verification.
|
||||
*
|
||||
* @author timsu
|
||||
*
|
||||
*/
|
||||
public class AssertiveNotificationManager extends AndroidNotificationManager {
|
||||
|
||||
Notification notification = null;
|
||||
|
||||
public AssertiveNotificationManager(Context context) {
|
||||
super(context);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notify(int id, Notification notification) {
|
||||
super.notify(id, notification);
|
||||
this.notification = notification;
|
||||
}
|
||||
|
||||
public void assertNotified() {
|
||||
if(notification == null)
|
||||
throw new AssertionError("Notification was not triggered");
|
||||
}
|
||||
|
||||
public void assertNotNotified() {
|
||||
if(notification != null)
|
||||
throw new AssertionError("Notification was triggered");
|
||||
}
|
||||
|
||||
public Notification getNotification() {
|
||||
return notification;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
notification = null;
|
||||
}
|
||||
}
|
||||
@ -1,110 +0,0 @@
|
||||
package com.todoroo.astrid.reminders;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.content.res.Resources;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.test.AndroidTestCase;
|
||||
|
||||
import com.todoroo.astrid.R;
|
||||
import com.todoroo.astrid.reminders.service.NotificationService;
|
||||
|
||||
public class NotificationServiceTests extends AndroidTestCase {
|
||||
|
||||
/**
|
||||
* Test quiet hour determination logic
|
||||
*/
|
||||
public void testQuietHoursWrapped() {
|
||||
Context context = getContext();
|
||||
|
||||
// test wrapped quiet hours
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
Editor editor = prefs.edit();
|
||||
Resources r = getContext().getResources();
|
||||
|
||||
editor.putString(r.getString(R.string.rmd_EPr_quiet_hours_start_key),
|
||||
Integer.toString(22));
|
||||
editor.putString(r.getString(R.string.rmd_EPr_quiet_hours_end_key),
|
||||
Integer.toString(8));
|
||||
editor.commit();
|
||||
|
||||
Date date = new Date();
|
||||
date.setHours(21);
|
||||
date.setMinutes(59);
|
||||
assertFalse(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(22);
|
||||
date.setMinutes(0);
|
||||
assertTrue(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(23);
|
||||
assertTrue(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(0);
|
||||
assertTrue(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(7);
|
||||
date.setMinutes(59);
|
||||
assertTrue(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(8);
|
||||
date.setMinutes(0);
|
||||
assertFalse(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(12);
|
||||
assertFalse(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(20);
|
||||
assertFalse(NotificationService.isInQuietHours(context, date));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test quiet hour determination logic
|
||||
*/
|
||||
public void testQuietHoursUnwrapped() {
|
||||
Context context = getContext();
|
||||
|
||||
// test wrapped quiet hours
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
Editor editor = prefs.edit();
|
||||
Resources r = getContext().getResources();
|
||||
|
||||
editor.putString(r.getString(R.string.rmd_EPr_quiet_hours_start_key),
|
||||
Integer.toString(10));
|
||||
editor.putString(r.getString(R.string.rmd_EPr_quiet_hours_end_key),
|
||||
Integer.toString(16));
|
||||
editor.commit();
|
||||
|
||||
Date date = new Date();
|
||||
date.setHours(9);
|
||||
date.setMinutes(59);
|
||||
assertFalse(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(10);
|
||||
date.setMinutes(0);
|
||||
assertTrue(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(11);
|
||||
assertTrue(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(13);
|
||||
assertTrue(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(15);
|
||||
date.setMinutes(59);
|
||||
assertTrue(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(16);
|
||||
date.setMinutes(0);
|
||||
assertFalse(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(23);
|
||||
assertFalse(NotificationService.isInQuietHours(context, date));
|
||||
|
||||
date.setHours(0);
|
||||
assertFalse(NotificationService.isInQuietHours(context, date));
|
||||
}
|
||||
}
|
||||
@ -1,43 +0,0 @@
|
||||
package com.todoroo.astrid.reminders;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.todoroo.andlib.service.Autowired;
|
||||
import com.todoroo.astrid.dao.MetadataDao;
|
||||
import com.todoroo.astrid.dao.TaskDao;
|
||||
import com.todoroo.astrid.model.Metadata;
|
||||
import com.todoroo.astrid.model.Task;
|
||||
import com.todoroo.astrid.test.DatabaseTestCase;
|
||||
|
||||
public class PluginTestCase extends DatabaseTestCase {
|
||||
|
||||
@Autowired
|
||||
public TaskDao taskDao;
|
||||
|
||||
@Autowired
|
||||
public MetadataDao metadataDao;
|
||||
|
||||
/**
|
||||
* Helper method to create a task.
|
||||
* @param task
|
||||
* @param metadata
|
||||
* @return task id
|
||||
*/
|
||||
public long createTask(Task task, HashMap<String, String> metadata) {
|
||||
taskDao.save(database, task, false);
|
||||
|
||||
if(metadata != null) {
|
||||
Metadata metadataItem = new Metadata();
|
||||
metadataItem.setValue(Metadata.TASK, task.getId());
|
||||
for(Entry<String, String> entry : metadata.entrySet()) {
|
||||
metadataItem.setValue(Metadata.KEY, entry.getKey());
|
||||
metadataItem.setValue(Metadata.VALUE, entry.getValue());
|
||||
metadataDao.save(database, metadataItem);
|
||||
}
|
||||
}
|
||||
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,282 +0,0 @@
|
||||
package com.todoroo.astrid.reminders.service;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import android.database.sqlite.SQLiteQueryBuilder;
|
||||
|
||||
import com.todoroo.andlib.test.data.TodorooCursor;
|
||||
import com.todoroo.andlib.test.utility.DateUtilities;
|
||||
import com.todoroo.astrid.model.Task;
|
||||
import com.todoroo.astrid.reminders.AlarmReceiver;
|
||||
import com.todoroo.astrid.reminders.Constants;
|
||||
import com.todoroo.astrid.reminders.PluginTestCase;
|
||||
import com.todoroo.astrid.reminders.service.ReminderService.Alarm;
|
||||
|
||||
public class ReminderServiceTests extends PluginTestCase {
|
||||
|
||||
/**
|
||||
* Test task fetching mechanism for reminders
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void testTaskReminderFetch() {
|
||||
ReminderService service = new ReminderService(getContext());
|
||||
|
||||
SQLiteQueryBuilder baseQuery = service.createBaseQuery();
|
||||
TodorooCursor<Task> cursor = service.fetchTasksWithReminders(baseQuery);
|
||||
assertEquals(0, cursor.getCount());
|
||||
cursor.close();
|
||||
|
||||
// add new tasks
|
||||
int now = DateUtilities.now();
|
||||
Task task = new Task();
|
||||
HashMap<String, String> metadata = new HashMap<String, String>();
|
||||
|
||||
task.setValue(Task.TITLE, "normal");
|
||||
task.setValue(Task.DUE_DATE, now + 10);
|
||||
metadata.put(Constants.KEY_REMINDER, Integer.toString(Constants.REMINDER_AT_DEADLINE));
|
||||
long normalId = createTask(task, metadata);
|
||||
|
||||
task.clear();
|
||||
task.setValue(Task.TITLE, "completed");
|
||||
task.setValue(Task.DUE_DATE, now + 10);
|
||||
task.setValue(Task.COMPLETION_DATE, now + 10);
|
||||
metadata.put(Constants.KEY_REMINDER, Integer.toString(Constants.REMINDER_ALARM_CLOCK));
|
||||
long completedId = createTask(task, metadata);
|
||||
|
||||
task.clear();
|
||||
task.setValue(Task.TITLE, "hidden");
|
||||
task.setValue(Task.DUE_DATE, now + 10);
|
||||
task.setValue(Task.HIDDEN_UNTIL, now + 10);
|
||||
metadata.put(Constants.KEY_REMINDER, Integer.toString(Constants.REMINDER_INCREASING));
|
||||
long hiddenId = createTask(task, metadata);
|
||||
|
||||
task.clear();
|
||||
task.setValue(Task.TITLE, "noalarm");
|
||||
task.setValue(Task.DUE_DATE, now + 10);
|
||||
metadata.put(Constants.KEY_REMINDER, Integer.toString(Constants.REMINDER_NONE));
|
||||
long noAlarmId = createTask(task, metadata);
|
||||
|
||||
task.clear();
|
||||
task.setValue(Task.TITLE, "overdue");
|
||||
task.setValue(Task.DUE_DATE, now - 10);
|
||||
metadata.put(Constants.KEY_REMINDER, Integer.toString(Constants.REMINDER_AT_DEADLINE));
|
||||
long overdueId = createTask(task, metadata);
|
||||
|
||||
cursor = service.fetchTasksWithReminders(baseQuery);
|
||||
assertEquals(2, cursor.getCount());
|
||||
cursor.moveToFirst();
|
||||
task.readFromCursor(cursor, ReminderService.PROPERTIES);
|
||||
assertEquals(task.getId(), normalId);
|
||||
assertEquals(task.getValue(ReminderService.REMINDER_MODE),
|
||||
Integer.toString(Constants.REMINDER_AT_DEADLINE));
|
||||
cursor.moveToNext();
|
||||
task.readFromCursor(cursor, ReminderService.PROPERTIES);
|
||||
assertEquals(task.getId(), hiddenId);
|
||||
assertEquals(task.getValue(ReminderService.REMINDER_MODE),
|
||||
Integer.toString(Constants.REMINDER_INCREASING));
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test task fetching mechanism for overdue interval
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void testTaskOverdueFetch() {
|
||||
ReminderService service = new ReminderService(getContext());
|
||||
|
||||
SQLiteQueryBuilder baseQuery = service.createBaseQuery();
|
||||
TodorooCursor<Task> cursor = service.fetchTasksWithOverdueReminders(baseQuery);
|
||||
assertEquals(0, cursor.getCount());
|
||||
cursor.close();
|
||||
|
||||
// add new tasks
|
||||
int now = DateUtilities.now();
|
||||
Task task = new Task();
|
||||
HashMap<String, String> metadata = new HashMap<String, String>();
|
||||
|
||||
task.setValue(Task.TITLE, "normal");
|
||||
task.setValue(Task.DUE_DATE, now - 10);
|
||||
metadata.put(Constants.KEY_OVERDUE, Integer.toString(1500));
|
||||
long normalId = createTask(task, metadata);
|
||||
|
||||
task.clear();
|
||||
task.setValue(Task.TITLE, "completed");
|
||||
task.setValue(Task.DUE_DATE, now - 10);
|
||||
task.setValue(Task.COMPLETION_DATE, now - 10);
|
||||
metadata.put(Constants.KEY_OVERDUE, Integer.toString(1500));
|
||||
long completedId = createTask(task, metadata);
|
||||
|
||||
task.clear();
|
||||
task.setValue(Task.TITLE, "hidden");
|
||||
task.setValue(Task.DUE_DATE, now - 10);
|
||||
task.setValue(Task.HIDDEN_UNTIL, now + 10);
|
||||
metadata.put(Constants.KEY_OVERDUE, Integer.toString(1500));
|
||||
long hiddenId = createTask(task, metadata);
|
||||
|
||||
task.clear();
|
||||
task.setValue(Task.TITLE, "noalarm");
|
||||
task.setValue(Task.DUE_DATE, now - 10);
|
||||
metadata.put(Constants.KEY_OVERDUE, Integer.toString(0));
|
||||
long noAlarmId = createTask(task, metadata);
|
||||
|
||||
task.clear();
|
||||
task.setValue(Task.TITLE, "not due");
|
||||
task.setValue(Task.DUE_DATE, now + 10);
|
||||
metadata.put(Constants.KEY_OVERDUE, Integer.toString(0));
|
||||
long notDueId = createTask(task, metadata);
|
||||
|
||||
cursor = service.fetchTasksWithOverdueReminders(baseQuery);
|
||||
assertEquals(2, cursor.getCount());
|
||||
cursor.moveToFirst();
|
||||
task.readFromCursor(cursor, ReminderService.PROPERTIES);
|
||||
assertEquals(task.getId(), normalId);
|
||||
assertEquals(task.getValue(ReminderService.OVERDUE_INTERVAL),
|
||||
Integer.valueOf(1500));
|
||||
cursor.moveToNext();
|
||||
task.readFromCursor(cursor, ReminderService.PROPERTIES);
|
||||
assertEquals(task.getId(), hiddenId);
|
||||
assertEquals(task.getValue(ReminderService.OVERDUE_INTERVAL),
|
||||
Integer.valueOf(1500));
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test task fetching mechanism for reminders
|
||||
*/
|
||||
public void testTaskFetchForReminders() {
|
||||
ReminderService service = new ReminderService(getContext());
|
||||
|
||||
// try invalid
|
||||
assertNull("could fetch invalid", service.fetchTask(-99));
|
||||
|
||||
// add new tasks
|
||||
Task task = new Task();
|
||||
HashMap<String, String> metadata = new HashMap<String, String>();
|
||||
task.setValue(Task.TITLE, "salami");
|
||||
task.setValue(Task.DUE_DATE, 100);
|
||||
metadata.put(Constants.KEY_OVERDUE, "123");
|
||||
metadata.put(Constants.KEY_LAST_NOTIFIED, "345");
|
||||
metadata.put(Constants.KEY_REMINDER, "567");
|
||||
long id = createTask(task, metadata);
|
||||
|
||||
task = service.fetchTask(id);
|
||||
assertEquals(task.getValue(Task.TITLE), "salami");
|
||||
assertEquals(task.getValue(ReminderService.OVERDUE_INTERVAL), Integer.valueOf(123));
|
||||
assertEquals(task.getValue(ReminderService.LAST_NOTIFIED), Integer.valueOf(345));
|
||||
assertEquals(task.getValue(ReminderService.REMINDER_MODE), Integer.valueOf(567));
|
||||
|
||||
task.setValue(ReminderService.OVERDUE_INTERVAL, 321);
|
||||
task.setValue(ReminderService.LAST_NOTIFIED, 543);
|
||||
task.setValue(ReminderService.REMINDER_MODE, 765);
|
||||
|
||||
service.writeReminderOptions(task);
|
||||
task = service.fetchTask(id);
|
||||
|
||||
assertEquals(task.getValue(ReminderService.OVERDUE_INTERVAL), Integer.valueOf(321));
|
||||
assertEquals(task.getValue(ReminderService.LAST_NOTIFIED), Integer.valueOf(543));
|
||||
assertEquals(task.getValue(ReminderService.REMINDER_MODE), Integer.valueOf(765));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test alarm generation
|
||||
*/
|
||||
public void testAlarmGeneration() {
|
||||
ReminderService service = new ReminderService(getContext());
|
||||
int now = DateUtilities.now();
|
||||
|
||||
// test simple task with deadline
|
||||
Task task = new Task();
|
||||
task.setValue(Task.TITLE, "blah");
|
||||
task.setValue(Task.DUE_DATE, now + 10000);
|
||||
task.setValue(ReminderService.REMINDER_MODE, Constants.REMINDER_AT_DEADLINE);
|
||||
Alarm alarm = service.createReminderAlarm(task);
|
||||
assertEquals(task.getValue(Task.DUE_DATE), Integer.valueOf(alarm.alarmTime));
|
||||
|
||||
// test simple task no reminders
|
||||
task.setValue(ReminderService.REMINDER_MODE, Constants.REMINDER_NONE);
|
||||
alarm = service.createReminderAlarm(task);
|
||||
assertNull(alarm);
|
||||
|
||||
// test simple task with deadline passed and no overdue interval
|
||||
task.setValue(Task.DUE_DATE, now - 10);
|
||||
task.setValue(ReminderService.REMINDER_MODE, Constants.REMINDER_AT_DEADLINE);
|
||||
alarm = service.createReminderAlarm(task);
|
||||
assertNull(alarm);
|
||||
|
||||
// test alarm clock task
|
||||
task.setValue(Task.DUE_DATE, now + 10000);
|
||||
task.setValue(ReminderService.REMINDER_MODE, Constants.REMINDER_ALARM_CLOCK);
|
||||
alarm = service.createReminderAlarm(task);
|
||||
assertEquals(task.getValue(Task.DUE_DATE), Integer.valueOf(alarm.alarmTime));
|
||||
|
||||
// test overdue alarm clock task
|
||||
task.setValue(Task.DUE_DATE, now - 10);
|
||||
alarm = service.createReminderAlarm(task);
|
||||
assertNull(alarm);
|
||||
|
||||
// test increasing 30 days from now
|
||||
task.setValue(ReminderService.REMINDER_MODE, Constants.REMINDER_INCREASING);
|
||||
task.setValue(ReminderService.LAST_NOTIFIED, now);
|
||||
task.setValue(Task.DUE_DATE, now + 30 * 86400);
|
||||
alarm = service.createReminderAlarm(task);
|
||||
assertTrue(alarm.alarmTime > now + 5 * 86400 &&
|
||||
alarm.alarmTime < now + 25 * 86400);
|
||||
|
||||
// test increasing 30 days from now, with last notified of 100 days ago
|
||||
task.setValue(ReminderService.REMINDER_MODE, Constants.REMINDER_INCREASING);
|
||||
task.setValue(Task.DUE_DATE, now + 30 * 86400);
|
||||
task.setValue(ReminderService.LAST_NOTIFIED, now - 100 * 86400);
|
||||
alarm = service.createReminderAlarm(task);
|
||||
assertTrue(alarm.alarmTime > 0 &&
|
||||
alarm.alarmTime < now + 1 * 86400);
|
||||
|
||||
// test increasing when we're within the day boundary and task is
|
||||
// not defined with a specific time
|
||||
task.setValue(Task.URGENCY, Task.URGENCY_THIS_WEEK);
|
||||
task.setValue(Task.DUE_DATE, now + 80000);
|
||||
task.setValue(ReminderService.LAST_NOTIFIED, now);
|
||||
alarm = service.createReminderAlarm(task);
|
||||
assertTrue(alarm.intent.getBooleanExtra(AlarmReceiver.TOKEN_IS_DEADLINE, false));
|
||||
assertEquals(task.getValue(Task.DUE_DATE), Integer.valueOf(alarm.alarmTime));
|
||||
|
||||
// now same due date, but we've defined specific time
|
||||
task.setValue(Task.URGENCY, Task.URGENCY_SPECIFIC_DAY_TIME);
|
||||
alarm = service.createReminderAlarm(task);
|
||||
assertTrue(DateUtilities.now() < alarm.alarmTime &&
|
||||
alarm.alarmTime < task.getValue(Task.DUE_DATE));
|
||||
|
||||
// move the due date right up against actual
|
||||
task.setValue(Task.DUE_DATE, now + 1000);
|
||||
alarm = service.createReminderAlarm(task);
|
||||
assertEquals(task.getValue(Task.DUE_DATE), Integer.valueOf(alarm.alarmTime));
|
||||
assertTrue(alarm.intent.getBooleanExtra(AlarmReceiver.TOKEN_IS_DEADLINE, false));
|
||||
|
||||
// and after due date
|
||||
task.setValue(Task.DUE_DATE, now - 80000);
|
||||
alarm = service.createReminderAlarm(task);
|
||||
assertNull(alarm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test alarm generation (overdue)
|
||||
*/
|
||||
public void testOverdueAlarmGeneration() {
|
||||
ReminderService service = new ReminderService(getContext());
|
||||
int now = DateUtilities.now();
|
||||
|
||||
// test simple overdue
|
||||
Task task = new Task();
|
||||
task.setValue(Task.TITLE, "blah");
|
||||
|
||||
task.setValue(Task.DUE_DATE, now - 100);
|
||||
task.setValue(ReminderService.OVERDUE_INTERVAL, 1000);
|
||||
Alarm alarm = service.createOverdueAlarm(task);
|
||||
assertTrue(now + 500 < alarm.alarmTime && alarm.alarmTime < now + 1500);
|
||||
|
||||
// same case, but almost exact due as normal
|
||||
task.setValue(Task.DUE_DATE, DateUtilities.now());
|
||||
alarm = service.createOverdueAlarm(task);
|
||||
assertTrue(now + 500 < alarm.alarmTime && alarm.alarmTime < now + 1500);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
package com.todoroo.astrid.rmilk;
|
||||
|
||||
public class RTMTestCase {
|
||||
String token = "cb4281f7154114ed0e4ccc3a02d373c5a2429532";
|
||||
}
|
||||
@ -1,139 +0,0 @@
|
||||
package com.todoroo.astrid.service;
|
||||
import com.todoroo.andlib.service.Autowired;
|
||||
import com.todoroo.andlib.test.data.TodorooCursor;
|
||||
import com.todoroo.andlib.test.utility.DateUtilities;
|
||||
import com.todoroo.astrid.api.Filter;
|
||||
import com.todoroo.astrid.dao.Database;
|
||||
import com.todoroo.astrid.dao.TaskDao;
|
||||
import com.todoroo.astrid.model.Task;
|
||||
import com.todoroo.astrid.test.DatabaseTestCase;
|
||||
|
||||
|
||||
public class TaskServiceTests extends DatabaseTestCase {
|
||||
|
||||
@Autowired
|
||||
TaskService taskService;
|
||||
|
||||
@Autowired
|
||||
TaskDao taskDao;
|
||||
|
||||
/**
|
||||
* Since a lot of the service-layer methods call through to the dao,
|
||||
* we don't need to do a lot of extensive testing here, just various
|
||||
* boundary conditions.
|
||||
*/
|
||||
public void testTaskDaoExposedMethods() {
|
||||
Task task = new Task();
|
||||
task.setValue(Task.TITLE, "normal");
|
||||
assertTrue(taskService.save(task, false));
|
||||
|
||||
// empty fetch w/ filter
|
||||
Filter filter = new Filter("bla", "bla", "WHERE 1", null);
|
||||
TodorooCursor<Task> cursor =
|
||||
taskService.fetchFiltered(TaskService.idProperties(), filter);
|
||||
assertEquals(1, cursor.getCount());
|
||||
cursor.close();
|
||||
|
||||
filter.sqlQuery = "WHERE " + Task.TITLE + " = \"bob\"";
|
||||
cursor = taskService.fetchFiltered(TaskService.idProperties(), filter);
|
||||
assertEquals(0, cursor.getCount());
|
||||
cursor.close();
|
||||
|
||||
Task fetched = taskService.fetchById(TaskService.idProperties(), task.getId());
|
||||
assertNotNull(fetched);
|
||||
fetched = taskService.fetchById(TaskService.idProperties(), task.getId() + 1);
|
||||
assertNull(fetched);
|
||||
|
||||
taskService.setComplete(task, true);
|
||||
assertTrue(task.isCompleted());
|
||||
|
||||
fetched = taskService.fetchById(Task.PROPERTIES, task.getId());
|
||||
assertTrue(fetched.isCompleted());
|
||||
taskService.setComplete(task, false);
|
||||
fetched = taskService.fetchById(Task.PROPERTIES, task.getId());
|
||||
assertFalse(fetched.isCompleted());
|
||||
assertFalse(task.isCompleted());
|
||||
|
||||
long id = task.getId();
|
||||
taskService.delete(id);
|
||||
fetched = taskService.fetchById(Task.PROPERTIES, id);
|
||||
assertNull(fetched);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test cleanup
|
||||
*/
|
||||
public void testCleanup() throws Exception {
|
||||
TodorooCursor<Task> cursor;
|
||||
|
||||
// save task with a name
|
||||
Task task1 = new Task();
|
||||
task1.setValue(Task.TITLE, "normal");
|
||||
assertTrue(taskService.save(task1, false));
|
||||
|
||||
// save task without a name
|
||||
Task task2 = new Task();
|
||||
task2.setValue(Task.TITLE, "");
|
||||
task2.setValue(Task.HIDDEN_UNTIL, DateUtilities.now() + 10000);
|
||||
assertTrue(taskService.save(task2, false));
|
||||
|
||||
// save task #2 without a name
|
||||
Task task3 = new Task();
|
||||
task3.setValue(Task.TITLE, "");
|
||||
task3.setValue(Task.DUE_DATE, DateUtilities.now() + 10000);
|
||||
assertTrue(taskService.save(task3, false));
|
||||
|
||||
cursor = taskDao.fetch(database, Task.PROPERTIES, null, null);
|
||||
assertEquals(3, cursor.getCount());
|
||||
cursor.close();
|
||||
|
||||
taskService.cleanup();
|
||||
|
||||
cursor = taskDao.fetch(database, Task.PROPERTIES, null, null);
|
||||
assertEquals(1, cursor.getCount());
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the sql invoked from a filter on new task creation
|
||||
*/
|
||||
public void testInvokeNewTaskSql() {
|
||||
TodorooCursor<Task> cursor;
|
||||
Filter filter = new Filter("bla", "bla", "WHERE 1", null);
|
||||
|
||||
Task task = new Task();
|
||||
task.setValue(Task.TITLE, "evils");
|
||||
assertTrue(taskService.save(task, false));
|
||||
|
||||
cursor = taskDao.fetch(database, Task.PROPERTIES, null, null);
|
||||
assertEquals(1, cursor.getCount());
|
||||
cursor.moveToNext();
|
||||
task = new Task(cursor, Task.PROPERTIES);
|
||||
assertEquals("evils", task.getValue(Task.TITLE));
|
||||
cursor.close();
|
||||
|
||||
filter.sqlForNewTasks = String.format("UPDATE %s set %s = \"good\"",
|
||||
Database.TASK_TABLE, Task.TITLE);
|
||||
taskService.invokeSqlForNewTask(filter, task);
|
||||
|
||||
cursor = taskDao.fetch(database, Task.PROPERTIES, null, null);
|
||||
assertEquals(1, cursor.getCount());
|
||||
cursor.moveToNext();
|
||||
task = new Task(cursor, Task.PROPERTIES);
|
||||
assertEquals("good", task.getValue(Task.TITLE));
|
||||
cursor.close();
|
||||
|
||||
filter.sqlForNewTasks = String.format("UPDATE %s set %s = \"yum\" WHERE %s = $ID",
|
||||
Database.TASK_TABLE, Task.TITLE, Task.ID);
|
||||
taskService.invokeSqlForNewTask(filter, task);
|
||||
|
||||
cursor = taskDao.fetch(database, Task.PROPERTIES, null, null);
|
||||
assertEquals(1, cursor.getCount());
|
||||
cursor.moveToNext();
|
||||
task = new Task(cursor, Task.PROPERTIES);
|
||||
assertEquals("yum", task.getValue(Task.TITLE));
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,96 +0,0 @@
|
||||
package com.todoroo.astrid.test;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.test.ActivityInstrumentationTestCase2;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import com.todoroo.andlib.service.Autowired;
|
||||
import com.todoroo.andlib.service.DependencyInjectionService;
|
||||
import com.todoroo.andlib.service.TestDependencyInjector;
|
||||
import com.todoroo.astrid.dao.Database;
|
||||
import com.todoroo.astrid.service.AstridDependencyInjector;
|
||||
import com.todoroo.astrid.test.DatabaseTestCase.AstridTestDatabase;
|
||||
|
||||
/**
|
||||
* ActivityTestCase is a helper for testing Todoroo Activities.
|
||||
* <p>
|
||||
* It initializes a test database before the activity is created.
|
||||
*
|
||||
* @author Tim Su <tim@todoroo.com>
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class AstridActivityTestCase<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
|
||||
|
||||
@Autowired
|
||||
public Database database;
|
||||
|
||||
static {
|
||||
AstridDependencyInjector.initialize();
|
||||
TestDependencyInjector.initialize("db").addInjectable("database",
|
||||
new AstridTestDatabase());
|
||||
}
|
||||
|
||||
public AstridActivityTestCase(String packageName, Class<T> activityClass) {
|
||||
super(packageName, activityClass);
|
||||
DependencyInjectionService.getInstance().inject(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
// create new test database
|
||||
AstridTestDatabase.dropTables(getInstrumentation().getTargetContext());
|
||||
database.openForWriting();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
|
||||
if(database != null)
|
||||
database.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call to just tear this activity down
|
||||
*/
|
||||
protected void tearActivityDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call to just set this activity up
|
||||
*/
|
||||
protected void setActivityUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
/** Calls various lifecycle methods, makes sure all of them work */
|
||||
public void testLifecycle() throws Exception {
|
||||
T activity = getActivity();
|
||||
getInstrumentation().callActivityOnPause(activity);
|
||||
Thread.sleep(500);
|
||||
getInstrumentation().callActivityOnResume(activity);
|
||||
|
||||
getInstrumentation().sendPointerSync(MotionEvent.obtain(500, 500, MotionEvent.ACTION_DOWN, 10, 30, 0));
|
||||
|
||||
getInstrumentation().callActivityOnPause(activity);
|
||||
getInstrumentation().callActivityOnStop(activity);
|
||||
Thread.sleep(500);
|
||||
getInstrumentation().callActivityOnRestart(activity);
|
||||
getInstrumentation().callActivityOnStart(activity);
|
||||
getInstrumentation().callActivityOnResume(activity);
|
||||
|
||||
getInstrumentation().sendPointerSync(MotionEvent.obtain(500, 500, MotionEvent.ACTION_DOWN, 10, 30, 0));
|
||||
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
||||
Thread.sleep(500);
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue