Clean up repeat tests

pull/73/head
Alex Baker 11 years ago
parent 730d39e541
commit 8ab433081b

@ -171,14 +171,6 @@ public class DateUtilities {
} }
} }
/**
* @return date format as getDateFormat with weekday
*/
public static String getDateStringWithWeekday(Date date) {
String weekday = getWeekday(date);
return weekday + ", " + getDateString(date);
}
/** /**
* @return weekday * @return weekday
*/ */

@ -67,20 +67,6 @@ public class DateUtilitiesTest extends TodorooRobolectricTestCase {
}); });
} }
@Test
public void testWeekdayString() {
forEachLocale(new Runnable() {
public void run() {
Date d = newDate();
for(int i = 0; i < 7; i++) {
d.setDate(i);
DateUtilities.getDateStringWithWeekday(d);
}
}
});
}
@Test @Test
public void testParseISO8601() { public void testParseISO8601() {
String withTime = "2013-01-28T13:17:02+00:00"; String withTime = "2013-01-28T13:17:02+00:00";

@ -7,6 +7,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import static com.todoroo.astrid.data.Task.DUE_DATE;
import static com.todoroo.astrid.data.Task.URGENCY_DAY_AFTER; import static com.todoroo.astrid.data.Task.URGENCY_DAY_AFTER;
import static com.todoroo.astrid.data.Task.URGENCY_IN_TWO_WEEKS; import static com.todoroo.astrid.data.Task.URGENCY_IN_TWO_WEEKS;
import static com.todoroo.astrid.data.Task.URGENCY_NEXT_MONTH; import static com.todoroo.astrid.data.Task.URGENCY_NEXT_MONTH;
@ -101,6 +102,22 @@ public class TaskTest {
assertEquals(expected, createDueDate(URGENCY_SPECIFIC_DAY_TIME, specificDueDate.getMillis())); assertEquals(expected, createDueDate(URGENCY_SPECIFIC_DAY_TIME, specificDueDate.getMillis()));
} }
@Test
public void taskHasDueTime() {
Task task = new Task();
task.setValue(DUE_DATE, 1388516076000L);
assertTrue(task.hasDueTime());
assertTrue(task.hasDueDate());
}
@Test
public void taskHasDueDate() {
Task task = new Task();
task.setValue(DUE_DATE, 1388469600000L);
assertFalse(task.hasDueTime());
assertTrue(task.hasDueDate());
}
@Test @Test
public void doesHaveDueTime() { public void doesHaveDueTime() {
assertTrue(hasDueTime(1388516076000L)); assertTrue(hasDueTime(1388516076000L));

@ -179,7 +179,7 @@ public class AdvancedRepeatTest extends TodorooRobolectricTestCase {
computeNextDueDate(true); computeNextDueDate(true);
long expected = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, THIS, wday.javaDayNum); long expected = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, THIS, wday.javaDayNum);
nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate); nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate);
assertDateEquals(nextDueDate, expected); assertEquals(expected, nextDueDate);
} }
for(Weekday wday1 : Weekday.values()) { for(Weekday wday1 : Weekday.values()) {
@ -192,7 +192,7 @@ public class AdvancedRepeatTest extends TodorooRobolectricTestCase {
long nextTwo = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, THIS, wday2.javaDayNum); long nextTwo = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, THIS, wday2.javaDayNum);
computeNextDueDate(true); computeNextDueDate(true);
nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate); nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate);
assertDateEquals(nextDueDate, Math.min(nextOne, nextTwo)); assertEquals(Math.min(nextOne, nextTwo), nextDueDate);
} }
} }
} }
@ -205,7 +205,7 @@ public class AdvancedRepeatTest extends TodorooRobolectricTestCase {
computeNextDueDate(true); computeNextDueDate(true);
long expected = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, NEXT, wday.javaDayNum); long expected = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, NEXT, wday.javaDayNum);
nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate); nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate);
assertDateEquals(nextDueDate, expected); assertEquals(expected, nextDueDate);
} }
for(Weekday wday1 : Weekday.values()) { for(Weekday wday1 : Weekday.values()) {
@ -218,7 +218,7 @@ public class AdvancedRepeatTest extends TodorooRobolectricTestCase {
long nextTwo = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, NEXT, wday2.javaDayNum); long nextTwo = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, NEXT, wday2.javaDayNum);
computeNextDueDate(true); computeNextDueDate(true);
nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate); nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate);
assertDateEquals(nextDueDate, Math.min(nextOne, nextTwo)); assertEquals(Math.min(nextOne, nextTwo), nextDueDate);
} }
} }
} }
@ -237,7 +237,7 @@ public class AdvancedRepeatTest extends TodorooRobolectricTestCase {
private void assertDueDate(long actual, int expectedWhich, int expectedDayOfWeek) { private void assertDueDate(long actual, int expectedWhich, int expectedDayOfWeek) {
long expected = getDate(task.getValue(Task.DUE_DATE), expectedWhich, expectedDayOfWeek); long expected = getDate(task.getValue(Task.DUE_DATE), expectedWhich, expectedDayOfWeek);
assertDateEquals(actual, expected); assertEquals(expected, actual);
} }
public static void assertDateTimeEquals(long date, long other) { public static void assertDateTimeEquals(long date, long other) {
@ -245,12 +245,6 @@ public class AdvancedRepeatTest extends TodorooRobolectricTestCase {
date, other); date, other);
} }
private void assertDateEquals(long actual, long expected) {
assertEquals("Due Date is '" + DateUtilities.getDateStringWithWeekday(newDate(actual))
+ "', expected '" + DateUtilities.getDateStringWithWeekday(newDate(expected)) + "'",
expected, actual);
}
private void setRRuleDays(RRule rrule, Weekday... weekdays) { private void setRRuleDays(RRule rrule, Weekday... weekdays) {
ArrayList<WeekdayNum> days = new ArrayList<WeekdayNum>(); ArrayList<WeekdayNum> days = new ArrayList<WeekdayNum>();
for(Weekday wd : weekdays) for(Weekday wd : weekdays)

@ -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);
} }
} }

@ -18,7 +18,7 @@ public class Freeze {
DateTimeUtils.setCurrentMillisSystem(); DateTimeUtils.setCurrentMillisSystem();
} }
public void thawAfter(Snippet snippet) { public void thawAfter(@SuppressWarnings("UnusedParameters") Snippet snippet) {
thaw(); thaw();
} }
} }
Loading…
Cancel
Save