|
|
|
@ -5,16 +5,17 @@
|
|
|
|
|
*/
|
|
|
|
|
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.utility.DateUtilities
|
|
|
|
|
import com.todoroo.astrid.data.Task
|
|
|
|
|
import net.fortuna.ical4j.model.Recur
|
|
|
|
|
import net.fortuna.ical4j.model.Recur.Frequency
|
|
|
|
|
import net.fortuna.ical4j.model.WeekDay
|
|
|
|
|
import net.fortuna.ical4j.model.WeekDay.*
|
|
|
|
|
import org.junit.Assert.assertEquals
|
|
|
|
|
import org.junit.Before
|
|
|
|
|
import org.junit.Test
|
|
|
|
|
import org.tasks.date.DateTimeUtils
|
|
|
|
|
import org.tasks.repeats.RecurrenceUtils.newRecur
|
|
|
|
|
import org.tasks.time.DateTime
|
|
|
|
|
import org.tasks.time.DateTimeUtils.printTimestamp
|
|
|
|
|
import java.text.ParseException
|
|
|
|
@ -25,27 +26,27 @@ import kotlin.math.min
|
|
|
|
|
class AdvancedRepeatTest {
|
|
|
|
|
private var task: Task? = null
|
|
|
|
|
private var nextDueDate: Long = 0
|
|
|
|
|
private var rrule: RRule? = null
|
|
|
|
|
private var recur: Recur? = null
|
|
|
|
|
|
|
|
|
|
// --- date with time tests
|
|
|
|
|
@Before
|
|
|
|
|
fun setUp() {
|
|
|
|
|
task = Task()
|
|
|
|
|
task!!.completionDate = DateUtilities.now()
|
|
|
|
|
rrule = RRule()
|
|
|
|
|
recur = newRecur()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@Throws(ParseException::class)
|
|
|
|
|
fun testDueDateSpecificTime() {
|
|
|
|
|
buildRRule(1, Frequency.DAILY)
|
|
|
|
|
buildRecur(1, Frequency.DAILY)
|
|
|
|
|
|
|
|
|
|
// test specific day & time
|
|
|
|
|
val dayWithTime = Task.createDueDate(
|
|
|
|
|
Task.URGENCY_SPECIFIC_DAY_TIME, DateTime(2010, 8, 1, 10, 4, 0).millis)
|
|
|
|
|
task!!.dueDate = dayWithTime
|
|
|
|
|
val nextDayWithTime = dayWithTime + DateUtilities.ONE_DAY
|
|
|
|
|
nextDueDate = RepeatTaskHelper.computeNextDueDate(task!!, rrule!!.toIcal(), false)
|
|
|
|
|
nextDueDate = RepeatTaskHelper.computeNextDueDate(task!!, recur!!.toString(), false)
|
|
|
|
|
assertDateTimeEquals(nextDayWithTime, nextDueDate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -53,7 +54,7 @@ class AdvancedRepeatTest {
|
|
|
|
|
@Test
|
|
|
|
|
@Throws(ParseException::class)
|
|
|
|
|
fun testCompletionDateSpecificTime() {
|
|
|
|
|
buildRRule(1, Frequency.DAILY)
|
|
|
|
|
buildRecur(1, Frequency.DAILY)
|
|
|
|
|
|
|
|
|
|
// test specific day & time
|
|
|
|
|
val dayWithTime = Task.createDueDate(
|
|
|
|
@ -63,7 +64,7 @@ class AdvancedRepeatTest {
|
|
|
|
|
var nextDayWithTimeLong = todayWithTime.millis
|
|
|
|
|
nextDayWithTimeLong += DateUtilities.ONE_DAY
|
|
|
|
|
nextDayWithTimeLong = nextDayWithTimeLong / 1000L * 1000
|
|
|
|
|
nextDueDate = RepeatTaskHelper.computeNextDueDate(task!!, rrule!!.toIcal(), true)
|
|
|
|
|
nextDueDate = RepeatTaskHelper.computeNextDueDate(task!!, recur!!.toString(), true)
|
|
|
|
|
assertDateTimeEquals(nextDayWithTimeLong, nextDueDate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -71,7 +72,7 @@ class AdvancedRepeatTest {
|
|
|
|
|
@Test
|
|
|
|
|
@Throws(Exception::class)
|
|
|
|
|
fun testDueDateInPastSingleWeekMultiDay() {
|
|
|
|
|
buildRRule(1, Frequency.WEEKLY, Weekday.MO, Weekday.WE, Weekday.FR)
|
|
|
|
|
buildRecur(1, Frequency.WEEKLY, MO, WE, FR)
|
|
|
|
|
setTaskDueDate(THIS, Calendar.SUNDAY)
|
|
|
|
|
computeNextDueDate(false)
|
|
|
|
|
assertDueDate(nextDueDate, THIS, Calendar.MONDAY)
|
|
|
|
@ -87,7 +88,7 @@ class AdvancedRepeatTest {
|
|
|
|
|
@Test
|
|
|
|
|
@Throws(Exception::class)
|
|
|
|
|
fun testDueDateSingleDay() {
|
|
|
|
|
buildRRule(1, Frequency.WEEKLY, Weekday.MO)
|
|
|
|
|
buildRecur(1, Frequency.WEEKLY, MO)
|
|
|
|
|
setTaskDueDate(PREV_PREV, Calendar.MONDAY)
|
|
|
|
|
computeNextDueDate(false)
|
|
|
|
|
assertDueDate(nextDueDate, NEXT, Calendar.MONDAY)
|
|
|
|
@ -112,7 +113,7 @@ class AdvancedRepeatTest {
|
|
|
|
|
@Test
|
|
|
|
|
@Throws(Exception::class)
|
|
|
|
|
fun testDueDateSingleWeekMultiDay() {
|
|
|
|
|
buildRRule(1, Frequency.WEEKLY, Weekday.MO, Weekday.WE, Weekday.FR)
|
|
|
|
|
buildRecur(1, Frequency.WEEKLY, MO, WE, FR)
|
|
|
|
|
setTaskDueDate(THIS, Calendar.SUNDAY)
|
|
|
|
|
computeNextDueDate(false)
|
|
|
|
|
assertDueDate(nextDueDate, THIS, Calendar.MONDAY)
|
|
|
|
@ -128,7 +129,7 @@ class AdvancedRepeatTest {
|
|
|
|
|
@Test
|
|
|
|
|
@Throws(Exception::class)
|
|
|
|
|
fun testDueDateMultiWeekMultiDay() {
|
|
|
|
|
buildRRule(2, Frequency.WEEKLY, Weekday.MO, Weekday.WE, Weekday.FR)
|
|
|
|
|
buildRecur(2, Frequency.WEEKLY, MO, WE, FR)
|
|
|
|
|
setTaskDueDate(THIS, Calendar.SUNDAY)
|
|
|
|
|
computeNextDueDate(false)
|
|
|
|
|
assertDueDate(nextDueDate, NEXT, Calendar.MONDAY)
|
|
|
|
@ -144,21 +145,21 @@ class AdvancedRepeatTest {
|
|
|
|
|
@Test
|
|
|
|
|
@Throws(Exception::class)
|
|
|
|
|
fun testCompleteDateSingleWeek() {
|
|
|
|
|
for (wday in Weekday.values()) {
|
|
|
|
|
buildRRule(1, Frequency.WEEKLY, wday)
|
|
|
|
|
for (wday in weekdays) {
|
|
|
|
|
buildRecur(1, Frequency.WEEKLY, wday)
|
|
|
|
|
computeNextDueDate(true)
|
|
|
|
|
val expected = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, THIS, wday.javaDayNum)
|
|
|
|
|
val expected = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, THIS, getCalendarDay(wday))
|
|
|
|
|
nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate)
|
|
|
|
|
assertEquals(expected, nextDueDate)
|
|
|
|
|
}
|
|
|
|
|
for (wday1 in Weekday.values()) {
|
|
|
|
|
for (wday2 in Weekday.values()) {
|
|
|
|
|
for (wday1 in weekdays) {
|
|
|
|
|
for (wday2 in weekdays) {
|
|
|
|
|
if (wday1 == wday2) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
buildRRule(1, Frequency.WEEKLY, wday1, wday2)
|
|
|
|
|
val nextOne = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, THIS, wday1.javaDayNum)
|
|
|
|
|
val nextTwo = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, THIS, wday2.javaDayNum)
|
|
|
|
|
buildRecur(1, Frequency.WEEKLY, wday1, wday2)
|
|
|
|
|
val nextOne = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, THIS, getCalendarDay(wday1))
|
|
|
|
|
val nextTwo = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, THIS, getCalendarDay(wday2))
|
|
|
|
|
computeNextDueDate(true)
|
|
|
|
|
nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate)
|
|
|
|
|
assertEquals(min(nextOne, nextTwo), nextDueDate)
|
|
|
|
@ -170,21 +171,21 @@ class AdvancedRepeatTest {
|
|
|
|
|
@Test
|
|
|
|
|
@Throws(Exception::class)
|
|
|
|
|
fun testCompleteDateMultiWeek() {
|
|
|
|
|
for (wday in Weekday.values()) {
|
|
|
|
|
buildRRule(2, Frequency.WEEKLY, wday)
|
|
|
|
|
for (wday in weekdays) {
|
|
|
|
|
buildRecur(2, Frequency.WEEKLY, wday)
|
|
|
|
|
computeNextDueDate(true)
|
|
|
|
|
val expected = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, NEXT, wday.javaDayNum)
|
|
|
|
|
val expected = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, NEXT, getCalendarDay(wday))
|
|
|
|
|
nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate)
|
|
|
|
|
assertEquals(expected, nextDueDate)
|
|
|
|
|
}
|
|
|
|
|
for (wday1 in Weekday.values()) {
|
|
|
|
|
for (wday2 in Weekday.values()) {
|
|
|
|
|
for (wday1 in weekdays) {
|
|
|
|
|
for (wday2 in weekdays) {
|
|
|
|
|
if (wday1 == wday2) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
buildRRule(2, Frequency.WEEKLY, wday1, wday2)
|
|
|
|
|
val nextOne = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, NEXT, wday1.javaDayNum)
|
|
|
|
|
val nextTwo = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, NEXT, wday2.javaDayNum)
|
|
|
|
|
buildRecur(2, Frequency.WEEKLY, wday1, wday2)
|
|
|
|
|
val nextOne = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, NEXT, getCalendarDay(wday1))
|
|
|
|
|
val nextTwo = getDate(DateUtilities.now() + DateUtilities.ONE_DAY, NEXT, getCalendarDay(wday2))
|
|
|
|
|
computeNextDueDate(true)
|
|
|
|
|
nextDueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, nextDueDate)
|
|
|
|
|
assertEquals(min(nextOne, nextTwo), nextDueDate)
|
|
|
|
@ -194,13 +195,14 @@ class AdvancedRepeatTest {
|
|
|
|
|
|
|
|
|
|
@Throws(ParseException::class)
|
|
|
|
|
private fun computeNextDueDate(fromComplete: Boolean) {
|
|
|
|
|
nextDueDate = RepeatTaskHelper.computeNextDueDate(task!!, rrule!!.toIcal(), fromComplete)
|
|
|
|
|
nextDueDate = RepeatTaskHelper.computeNextDueDate(task!!, recur!!.toString(), fromComplete)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun buildRRule(interval: Int, freq: Frequency, vararg weekdays: Weekday) {
|
|
|
|
|
rrule!!.interval = interval
|
|
|
|
|
rrule!!.freq = freq
|
|
|
|
|
setRRuleDays(rrule, *weekdays)
|
|
|
|
|
private fun buildRecur(interval: Int, freq: Frequency, vararg weekdays: WeekDay) {
|
|
|
|
|
recur!!.interval = interval
|
|
|
|
|
recur!!.setFrequency(freq.name)
|
|
|
|
|
recur!!.dayList.clear()
|
|
|
|
|
recur!!.dayList.addAll(weekdays)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun assertDueDate(actual: Long, expectedWhich: Int, expectedDayOfWeek: Int) {
|
|
|
|
@ -208,14 +210,6 @@ class AdvancedRepeatTest {
|
|
|
|
|
assertEquals(expected, actual)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun setRRuleDays(rrule: RRule?, vararg weekdays: Weekday) {
|
|
|
|
|
val days = ArrayList<WeekdayNum>()
|
|
|
|
|
for (wd in weekdays) {
|
|
|
|
|
days.add(WeekdayNum(0, wd))
|
|
|
|
|
}
|
|
|
|
|
rrule!!.byDay = days
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun setTaskDueDate(which: Int, day: Int) {
|
|
|
|
|
val time = getDate(DateUtilities.now(), which, day)
|
|
|
|
|
task!!.dueDate = time
|
|
|
|
@ -238,6 +232,8 @@ class AdvancedRepeatTest {
|
|
|
|
|
private const val THIS = 1
|
|
|
|
|
private const val NEXT = 2
|
|
|
|
|
|
|
|
|
|
private val weekdays = listOf(SU, MO, TU, WE, TH, FR, SA)
|
|
|
|
|
|
|
|
|
|
fun assertDateTimeEquals(date: Long, other: Long) {
|
|
|
|
|
assertEquals("Expected: ${printTimestamp(date)}, Actual: ${printTimestamp(other)}", date, other)
|
|
|
|
|
}
|
|
|
|
|