mirror of https://github.com/tasks/tasks
Clean up repeat tests
parent
730d39e541
commit
8ab433081b
@ -1,141 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2012 Todoroo Inc
|
|
||||||
*
|
|
||||||
* See the file "LICENSE" for the full license governing this code.
|
|
||||||
*/
|
|
||||||
package com.todoroo.astrid.repeats;
|
|
||||||
|
|
||||||
import com.google.ical.values.Frequency;
|
|
||||||
import com.google.ical.values.RRule;
|
|
||||||
import com.google.ical.values.Weekday;
|
|
||||||
import com.google.ical.values.WeekdayNum;
|
|
||||||
import com.todoroo.andlib.test.TodorooRobolectricTestCase;
|
|
||||||
import com.todoroo.andlib.utility.DateUtilities;
|
|
||||||
import com.todoroo.astrid.data.Task;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.robolectric.RobolectricTestRunner;
|
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.tasks.date.DateTimeUtils.newDate;
|
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
|
||||||
public class RepeatAfterCompleteTest extends TodorooRobolectricTestCase {
|
|
||||||
|
|
||||||
private Task task;
|
|
||||||
private long nextDueDate;
|
|
||||||
private RRule rrule;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() throws Exception {
|
|
||||||
super.setUp();
|
|
||||||
task = new Task();
|
|
||||||
task.setValue(Task.COMPLETION_DATE, DateUtilities.now());
|
|
||||||
rrule = new RRule();
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- date with time tests
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSubDailyFreqs() throws ParseException {
|
|
||||||
task.setValue(Task.DUE_DATE, DateUtilities.now() - DateUtilities.ONE_WEEK);
|
|
||||||
|
|
||||||
for(Frequency freq : Frequency.values()) {
|
|
||||||
long interval;
|
|
||||||
switch(freq) {
|
|
||||||
case MINUTELY:
|
|
||||||
interval = DateUtilities.ONE_MINUTE; break;
|
|
||||||
case HOURLY:
|
|
||||||
interval = DateUtilities.ONE_HOUR; break;
|
|
||||||
default:
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
buildRRule(1, freq);
|
|
||||||
nextDueDate = RepeatTaskCompleteListener.computeNextDueDate(task, rrule.toIcal(), true);
|
|
||||||
|
|
||||||
buildRRule(6, freq);
|
|
||||||
nextDueDate = RepeatTaskCompleteListener.computeNextDueDate(task, rrule.toIcal(), true);
|
|
||||||
assertDateTimeEquals(freq.toString() + "x6", DateUtilities.now() + 6 * interval, nextDueDate);
|
|
||||||
task.setValue(Task.DUE_DATE, nextDueDate);
|
|
||||||
assertTrue(task.hasDueTime());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDailyAndGreaterFreqs() throws ParseException {
|
|
||||||
task.setValue(Task.DUE_DATE,
|
|
||||||
Task.createDueDate(Task.URGENCY_SPECIFIC_DAY,
|
|
||||||
DateUtilities.now() - DateUtilities.ONE_WEEK));
|
|
||||||
|
|
||||||
for(int interval = 1; interval < 7; interval++) {
|
|
||||||
for(Frequency freq : Frequency.values()) {
|
|
||||||
long next = DateUtilities.now();
|
|
||||||
switch(freq) {
|
|
||||||
case DAILY:
|
|
||||||
next += interval * DateUtilities.ONE_DAY; break;
|
|
||||||
case WEEKLY:
|
|
||||||
next += interval * DateUtilities.ONE_WEEK; break;
|
|
||||||
case MONTHLY:
|
|
||||||
next = DateUtilities.addCalendarMonthsToUnixtime(next, interval); break;
|
|
||||||
case YEARLY:
|
|
||||||
next = DateUtilities.addCalendarMonthsToUnixtime(next, interval * 12); break;
|
|
||||||
default:
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
buildRRule(interval, freq);
|
|
||||||
nextDueDate = RepeatTaskCompleteListener.computeNextDueDate(task, rrule.toIcal(), true);
|
|
||||||
assertDateEquals(freq.toString() + "x" + interval, next, nextDueDate);
|
|
||||||
task.setValue(Task.DUE_DATE, nextDueDate);
|
|
||||||
assertFalse(task.hasDueTime());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testTimeZoneLate() throws ParseException {
|
|
||||||
task.setValue(Task.DUE_DATE, DateUtilities.now() + DateUtilities.ONE_WEEK);
|
|
||||||
nextDueDate = RepeatTaskCompleteListener.computeNextDueDate(task, rrule.toIcal(), true);
|
|
||||||
|
|
||||||
long expected = Task.createDueDate(Task.URGENCY_TOMORROW, 0);
|
|
||||||
assertDateEquals("tomorrow", expected, nextDueDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- helpers
|
|
||||||
|
|
||||||
private void buildRRule(int interval, Frequency freq, Weekday... weekdays) {
|
|
||||||
rrule.setInterval(interval);
|
|
||||||
rrule.setFreq(freq);
|
|
||||||
setRRuleDays(rrule, weekdays);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void assertDateTimeEquals(String message, long expected, long actual) {
|
|
||||||
expected = expected / 60000L * 60000;
|
|
||||||
actual = actual / 60000L * 60000;
|
|
||||||
assertEquals(message + ": Expected: " + newDate(expected) + ", Actual: " + newDate(actual),
|
|
||||||
expected, actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void assertDateEquals(String message, long expected, long actual) {
|
|
||||||
expected = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, expected);
|
|
||||||
actual = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, actual);
|
|
||||||
assertEquals(message + ": Due Date is '" + DateUtilities.getDateStringWithWeekday(newDate(actual))
|
|
||||||
+ "', expected '" + DateUtilities.getDateStringWithWeekday(newDate(expected)) + "'",
|
|
||||||
expected, actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setRRuleDays(RRule rrule, Weekday... weekdays) {
|
|
||||||
ArrayList<WeekdayNum> days = new ArrayList<>();
|
|
||||||
for(Weekday wd : weekdays)
|
|
||||||
days.add(new WeekdayNum(0, wd));
|
|
||||||
rrule.setByDay(days);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,54 +1,99 @@
|
|||||||
package com.todoroo.astrid.repeats;
|
package com.todoroo.astrid.repeats;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
|
||||||
import com.google.ical.values.Frequency;
|
import com.google.ical.values.Frequency;
|
||||||
import com.google.ical.values.RRule;
|
import com.google.ical.values.RRule;
|
||||||
|
import com.todoroo.astrid.data.Task;
|
||||||
|
|
||||||
|
import org.joda.time.DateTime;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.text.ParseException;
|
||||||
|
|
||||||
import static com.todoroo.astrid.repeats.RepeatTaskCompleteListener.handleSubdayRepeat;
|
import static com.google.ical.values.Frequency.DAILY;
|
||||||
|
import static com.google.ical.values.Frequency.HOURLY;
|
||||||
|
import static com.google.ical.values.Frequency.MINUTELY;
|
||||||
|
import static com.google.ical.values.Frequency.MONTHLY;
|
||||||
|
import static com.google.ical.values.Frequency.WEEKLY;
|
||||||
|
import static com.google.ical.values.Frequency.YEARLY;
|
||||||
|
import static com.todoroo.andlib.utility.DateUtilities.addCalendarMonthsToUnixtime;
|
||||||
|
import static com.todoroo.astrid.repeats.RepeatTaskCompleteListener.computeNextDueDate;
|
||||||
|
import static java.util.concurrent.TimeUnit.DAYS;
|
||||||
|
import static java.util.concurrent.TimeUnit.HOURS;
|
||||||
|
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.tasks.date.DateTimeUtils.newDate;
|
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
|
||||||
|
|
||||||
|
@SuppressLint("NewApi")
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class RepeatTaskCompleteListenerTest {
|
public class RepeatTaskCompleteListenerTest {
|
||||||
private static final Date start = newDate(2013, 12, 31, 8, 31, 59);
|
|
||||||
|
private final Task task = new Task();
|
||||||
|
private final long dueDate;
|
||||||
|
private final long completionDate;
|
||||||
|
|
||||||
|
{
|
||||||
|
completionDate = currentTimeMillis();
|
||||||
|
dueDate = completionDate - DAYS.toMillis(7);
|
||||||
|
task.setValue(Task.DUE_DATE, dueDate);
|
||||||
|
task.setValue(Task.COMPLETION_DATE, completionDate);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void repeatEveryMinute() {
|
public void minutelyRepeat() {
|
||||||
assertEquals(
|
checkFrequency(6, MINUTES.toMillis(1), MINUTELY);
|
||||||
newDate(2013, 12, 31, 8, 32, 1).getTime(), // astrid always sets seconds to one
|
|
||||||
handleSubdayRepeat(start, newRule(1, Frequency.MINUTELY)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void repeatEveryOtherMinute() {
|
public void hourlyRepeat() {
|
||||||
assertEquals(
|
checkFrequency(6, HOURS.toMillis(1), HOURLY);
|
||||||
newDate(2013, 12, 31, 8, 33, 1).getTime(),
|
|
||||||
handleSubdayRepeat(start, newRule(2, Frequency.MINUTELY)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void repeatEveryHour() {
|
public void dailyRepeat() {
|
||||||
assertEquals(
|
checkFrequency(6, DAYS.toMillis(1), DAILY);
|
||||||
newDate(2013, 12, 31, 9, 31, 1).getTime(),
|
|
||||||
handleSubdayRepeat(start, newRule(1, Frequency.HOURLY)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void repeatEveryOtherHour() {
|
public void weeklyRepeat() {
|
||||||
assertEquals(
|
checkFrequency(6, DAYS.toMillis(7), WEEKLY);
|
||||||
newDate(2013, 12, 31, 10, 31, 1).getTime(),
|
}
|
||||||
handleSubdayRepeat(start, newRule(2, Frequency.HOURLY)));
|
|
||||||
|
@Test
|
||||||
|
public void monthlyRepeat() {
|
||||||
|
checkExpected(6, addCalendarMonthsToUnixtime(dueDate, 6), MONTHLY, false);
|
||||||
|
checkExpected(6, addCalendarMonthsToUnixtime(completionDate, 6), MONTHLY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void yearlyRepeat() {
|
||||||
|
checkExpected(6, addCalendarMonthsToUnixtime(dueDate, 6 * 12), YEARLY, false);
|
||||||
|
checkExpected(6, addCalendarMonthsToUnixtime(completionDate, 6 * 12), YEARLY, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private RRule newRule(int interval, Frequency frequency) {
|
private void checkFrequency(int count, long interval, Frequency frequency) {
|
||||||
RRule rule = new RRule();
|
checkExpected(count, dueDate + count * interval, frequency, false);
|
||||||
rule.setInterval(interval);
|
checkExpected(count, completionDate + count * interval, frequency, true);
|
||||||
rule.setFreq(frequency);
|
}
|
||||||
return rule;
|
|
||||||
|
private void checkExpected(int count, long expected, Frequency frequency, boolean repeatAfterCompletion) {
|
||||||
|
RRule rrule = new RRule();
|
||||||
|
rrule.setInterval(count);
|
||||||
|
rrule.setFreq(frequency);
|
||||||
|
long nextDueDate;
|
||||||
|
try {
|
||||||
|
nextDueDate = computeNextDueDate(task, rrule.toIcal(), repeatAfterCompletion);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
assertEquals(
|
||||||
|
new DateTime(expected)
|
||||||
|
.withSecondOfMinute(1)
|
||||||
|
.withMillisOfSecond(0)
|
||||||
|
.getMillis(),
|
||||||
|
nextDueDate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue