Add joda time and unit tests

* Add datetime tests
* Add repeat tests
* Disable instrumentTest on travis build
* Update project properties
pull/73/head
Alex Baker 11 years ago
parent 26e8dc62c8
commit 49d99258df

@ -40,4 +40,4 @@ before_script:
script:
- ./gradlew test --stacktrace --info
- ./gradlew :astrid:connectedInstrumentTest --stacktrace --info
# - ./gradlew :astrid:connectedInstrumentTest --stacktrace --info

@ -8,5 +8,5 @@
# project structure.
# Project target.
target=android-18
target=android-19
android.library=true

@ -30,4 +30,6 @@ android {
dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile group: 'joda-time', name: 'joda-time', version: '2.3', transitive: false
}

@ -9,4 +9,4 @@
android.library=true
# Project target.
target=android-18
target=android-19

@ -18,6 +18,8 @@ import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
public class DateUtilities {
@ -50,12 +52,12 @@ public class DateUtilities {
/** Returns unixtime for current time */
public static long now() {
return System.currentTimeMillis();
return currentTimeMillis();
}
/** Returns unixtime one month from now */
public static long oneMonthFromNow() {
Date date = new Date();
Date date = new Date(now());
date.setMonth(date.getMonth() + 1);
return date.getTime();
}

@ -0,0 +1,9 @@
package org.tasks.date;
public class DateTimeUtils {
public static long currentTimeMillis() {
return org.joda.time.DateTimeUtils.currentTimeMillis();
}
}

@ -22,6 +22,7 @@
-keep class com.google.gson.stream.JsonWriter {
public void setLenient(boolean);
}
-keep class org.joda.**
# ignore reflection-based access from google libraries
-dontwarn com.google.**

@ -10,7 +10,7 @@
# Indicates whether an apk should be generated for each density.
split.density=false
# Project target.
target=android-18
target=android-19
apk-configurations=
android.library.reference.1=../api
android.library.reference.2=../android-aac-enc

@ -280,7 +280,7 @@ public class RepeatTaskCompleteListener extends BroadcastReceiver {
}
}
private static long handleSubdayRepeat(Date startDate, RRule rrule) {
static long handleSubdayRepeat(Date startDate, RRule rrule) {
long millis;
switch(rrule.getFreq()) {
case HOURLY:
@ -293,8 +293,7 @@ public class RepeatTaskCompleteListener extends BroadcastReceiver {
throw new RuntimeException("Error handing subday repeat: " + rrule.getFreq()); //$NON-NLS-1$
}
long newDueDate = startDate.getTime() + millis * rrule.getInterval();
return Task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME,
newDueDate);
return Task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME, newDueDate);
}
}

@ -7,17 +7,21 @@ package com.todoroo.andlib.utility;
import com.todoroo.andlib.test.TodorooRobolectricTestCase;
import org.joda.time.DateTime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.tasks.Snippet;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import static com.todoroo.andlib.utility.DateUtilities.oneMonthFromNow;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.tasks.Freeze.freezeAt;
@RunWith(RobolectricTestRunner.class)
public class DateUtilitiesTest extends TodorooRobolectricTestCase {
@ -115,4 +119,34 @@ public class DateUtilitiesTest extends TodorooRobolectricTestCase {
assertEquals(0, cal.get(Calendar.MINUTE));
assertEquals(0, cal.get(Calendar.SECOND));
}
@Test
public void oneMonthFromEndOfDecember() {
DateTime now = new DateTime(2013, 12, 31, 16, 31, 20, 597);
final long expected = new DateTime(2014, 1, 31, 16, 31, 20, 597).getMillis();
freezeAt(now).thawAfter(new Snippet() {{
assertEquals(expected, oneMonthFromNow());
}});
}
@Test
public void oneMonthFromEndOfJanuary() {
DateTime now = new DateTime(2014, 1, 31, 12, 54, 33, 175);
final long expected = new DateTime(2014, 3, 3, 12, 54, 33, 175).getMillis();
freezeAt(now).thawAfter(new Snippet() {{
assertEquals(expected, oneMonthFromNow());
}});
}
@Test
public void oneMonthFromEndOfFebruary() {
DateTime now = new DateTime(2014, 2, 28, 9, 19, 7, 990);
final long expected = new DateTime(2014, 3, 28, 9, 19, 7, 990).getMillis();
freezeAt(now).thawAfter(new Snippet() {{
assertEquals(expected, oneMonthFromNow());
}});
}
}

@ -0,0 +1,100 @@
package com.todoroo.astrid.data;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
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_NEXT_MONTH;
import static com.todoroo.astrid.data.Task.URGENCY_NEXT_WEEK;
import static com.todoroo.astrid.data.Task.URGENCY_NONE;
import static com.todoroo.astrid.data.Task.URGENCY_SPECIFIC_DAY;
import static com.todoroo.astrid.data.Task.URGENCY_SPECIFIC_DAY_TIME;
import static com.todoroo.astrid.data.Task.URGENCY_TODAY;
import static com.todoroo.astrid.data.Task.URGENCY_TOMORROW;
import static com.todoroo.astrid.data.Task.createDueDate;
import static org.junit.Assert.assertEquals;
import static org.tasks.Freeze.freezeAt;
import static org.tasks.Freeze.thaw;
@RunWith(RobolectricTestRunner.class)
public class TaskTest {
private static final DateTime now = new DateTime(2013, 12, 31, 16, 10, 53, 452);
private static final DateTime specificDueDate = new DateTime(2014, 3, 17, 9, 54, 27, 959);
@Before
public void before() {
freezeAt(now);
}
@After
public void after() {
thaw();
}
@Test
public void createDueDateNoUrgency() {
assertEquals(0, createDueDate(URGENCY_NONE, 1L));
}
@Test
public void createDueDateToday() {
long expected = new DateTime(2013, 12, 31, 12, 0, 0, 0).getMillis();
assertEquals(expected, createDueDate(URGENCY_TODAY, -1L));
}
@Test
public void createDueDateTomorrow() {
long expected = new DateTime(2014, 1, 1, 12, 0, 0, 0).getMillis();
assertEquals(expected, createDueDate(URGENCY_TOMORROW, -1L));
}
@Test
public void createDueDateDayAfter() {
long expected = new DateTime(2014, 1, 2, 12, 0, 0, 0).getMillis();
assertEquals(expected, createDueDate(URGENCY_DAY_AFTER, -1L));
}
@Test
public void createDueDateNextWeek() {
long expected = new DateTime(2014, 1, 7, 12, 0, 0, 0).getMillis();
assertEquals(expected, createDueDate(URGENCY_NEXT_WEEK, -1L));
}
@Test
public void createDueDateInTwoWeeks() {
long expected = new DateTime(2014, 1, 14, 12, 0, 0, 0).getMillis();
assertEquals(expected, createDueDate(URGENCY_IN_TWO_WEEKS, -1L));
}
@Test
public void createDueDateNextMonth() {
long expected = new DateTime(2014, 1, 31, 12, 0, 0, 0).getMillis();
assertEquals(expected, createDueDate(URGENCY_NEXT_MONTH, -1L));
}
@Test
public void removeTimeForSpecificDay() {
long expected = specificDueDate
.withHourOfDay(12)
.withMinuteOfHour(0)
.withSecondOfMinute(0)
.withMillisOfSecond(0)
.getMillis();
assertEquals(expected, createDueDate(URGENCY_SPECIFIC_DAY, specificDueDate.getMillis()));
}
@Test
public void removeSecondsForSpecificTime() {
long expected = specificDueDate
.withSecondOfMinute(1)
.withMillisOfSecond(0)
.getMillis();
assertEquals(expected, createDueDate(URGENCY_SPECIFIC_DAY_TIME, specificDueDate.getMillis()));
}
}

@ -0,0 +1,53 @@
package com.todoroo.astrid.repeats;
import com.google.ical.values.Frequency;
import com.google.ical.values.RRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.util.Date;
import static com.todoroo.astrid.repeats.RepeatTaskCompleteListener.handleSubdayRepeat;
import static org.junit.Assert.assertEquals;
@RunWith(RobolectricTestRunner.class)
public class RepeatTaskCompleteListenerTest {
private static final Date start = new Date(113, 11, 31, 8, 31, 59);
@Test
public void repeatEveryMinute() {
assertEquals(
new Date(113, 11, 31, 8, 32, 1).getTime(), // astrid always sets seconds to one
handleSubdayRepeat(start, newRule(1, Frequency.MINUTELY)));
}
@Test
public void repeatEveryOtherMinute() {
assertEquals(
new Date(113, 11, 31, 8, 33, 1).getTime(),
handleSubdayRepeat(start, newRule(2, Frequency.MINUTELY)));
}
@Test
public void repeatEveryHour() {
assertEquals(
new Date(113, 11, 31, 9, 31, 1).getTime(),
handleSubdayRepeat(start, newRule(1, Frequency.HOURLY)));
}
@Test
public void repeatEveryOtherHour() {
assertEquals(
new Date(113, 11, 31, 10, 31, 1).getTime(),
handleSubdayRepeat(start, newRule(2, Frequency.HOURLY)));
}
private RRule newRule(int interval, Frequency frequency) {
RRule rule = new RRule();
rule.setInterval(interval);
rule.setFreq(frequency);
return rule;
}
}

@ -0,0 +1,24 @@
package org.tasks;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
public class Freeze {
public static Freeze freezeAt(DateTime dateTime) {
return freezeAt(dateTime.getMillis());
}
public static Freeze freezeAt(long millis) {
DateTimeUtils.setCurrentMillisFixed(millis);
return new Freeze();
}
public static void thaw() {
DateTimeUtils.setCurrentMillisSystem();
}
public void thawAfter(Snippet snippet) {
thaw();
}
}

@ -0,0 +1,4 @@
package org.tasks;
public class Snippet {
}

@ -2,4 +2,5 @@
<lint>
<issue id="InconsistentArrays" severity="error"/>
<issue id="MissingTranslation" severity="ignore"/>
<issue id="TypographyEllipsis" severity="ignore"/>
</lint>

Loading…
Cancel
Save