Add relative day tests

pull/73/head
Alex Baker 11 years ago
parent e87a01e747
commit a5802cb447

@ -7,7 +7,6 @@ package com.todoroo.andlib.utility;
import android.content.Context;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import org.joda.time.DateTime;
import org.tasks.api.R;
@ -150,11 +149,9 @@ public class DateUtilities {
* @return date, with month, day, and year
*/
public static String getDateStringHideYear(Date date) {
String month = DateUtils.getMonthString(date.getMonth() +
Calendar.JANUARY, DateUtils.LENGTH_MEDIUM);
String month = new SimpleDateFormat("MMM").format(date);
String value;
Locale locale = Locale.getDefault();
// united states, you are special
if (arrayBinaryContains(locale.getLanguage(), "ja", "ko", "zh")
|| arrayBinaryContains(locale.getCountry(), "BZ", "CA", "KE", "MN" ,"US")) {
value = "'#' d";
@ -187,8 +184,7 @@ public class DateUtilities {
* @return weekday
*/
public static String getWeekdayShort(Date date) {
return DateUtils.getDayOfWeekString(date.getDay() + Calendar.SUNDAY,
DateUtils.LENGTH_MEDIUM);
return new SimpleDateFormat("EEE").format(date);
}
/**
@ -221,8 +217,7 @@ public class DateUtilities {
return context.getString(abbreviated ? R.string.yest : R.string.yesterday).toLowerCase();
}
if(today + DateUtilities.ONE_WEEK >= input &&
today - DateUtilities.ONE_WEEK <= input) {
if(today + DateUtilities.ONE_WEEK >= input && today - DateUtilities.ONE_WEEK <= input) {
return abbreviated ? DateUtilities.getWeekdayShort(newDate(date)) : DateUtilities.getWeekday(newDate(date));
}

@ -5,8 +5,6 @@
*/
package com.todoroo.andlib.utility;
import android.content.Context;
import com.todoroo.andlib.test.TodorooRobolectricTestCase;
import org.joda.time.DateTime;
@ -18,18 +16,15 @@ import org.tasks.Snippet;
import java.util.Date;
import static com.todoroo.andlib.utility.DateUtilities.clearTime;
import static com.todoroo.andlib.utility.DateUtilities.getRelativeDay;
import static com.todoroo.andlib.utility.DateUtilities.getStartOfDay;
import static com.todoroo.andlib.utility.DateUtilities.getWeekday;
import static com.todoroo.andlib.utility.DateUtilities.getWeekdayShort;
import static com.todoroo.andlib.utility.DateUtilities.isEndOfMonth;
import static com.todoroo.andlib.utility.DateUtilities.oneMonthFromNow;
import static org.joda.time.DateTime.now;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.robolectric.Robolectric.getShadowApplication;
import static org.tasks.Freeze.freezeAt;
import static org.tasks.Freeze.freezeClock;
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
import static org.tasks.date.DateTimeUtils.newDate;
@RunWith(RobolectricTestRunner.class)
@ -163,32 +158,24 @@ public class DateUtilitiesTest extends TodorooRobolectricTestCase {
}
@Test
public void relativeDayIsToday() {
final Context context = getShadowApplication().getApplicationContext();
freezeClock().thawAfter(new Snippet() {{
final long today = currentTimeMillis();
assertEquals("today", getRelativeDay(context, today));
assertEquals("today", getRelativeDay(context, today, false));
}});
public void getWeekdayLongString() {
assertEquals("Sunday", getWeekday(newDate(2013, 12, 29)));
assertEquals("Monday", getWeekday(newDate(2013, 12, 30)));
assertEquals("Tuesday", getWeekday(newDate(2013, 12, 31)));
assertEquals("Wednesday", getWeekday(newDate(2014, 1, 1)));
assertEquals("Thursday", getWeekday(newDate(2014, 1, 2)));
assertEquals("Friday", getWeekday(newDate(2014, 1, 3)));
assertEquals("Saturday", getWeekday(newDate(2014, 1, 4)));
}
@Test
public void relativeDayIsTomorrow() {
final Context context = getShadowApplication().getApplicationContext();
freezeClock().thawAfter(new Snippet() {{
final long tomorrow = now().plusDays(1).getMillis();
assertEquals("tmrw", getRelativeDay(context, tomorrow));
assertEquals("tomorrow", getRelativeDay(context, tomorrow, false));
}});
}
@Test
public void relativeDayIsYesterday() {
final Context context = getShadowApplication().getApplicationContext();
freezeClock().thawAfter(new Snippet() {{
final long yesterday = now().minusDays(1).getMillis();
assertEquals("yest", getRelativeDay(context, yesterday));
assertEquals("yesterday", getRelativeDay(context, yesterday, false));
}});
public void getWeekdayShortString() {
assertEquals("Sun", getWeekdayShort(new DateTime(2013, 12, 29, 11, 12, 13, 14).toDate()));
assertEquals("Mon", getWeekdayShort(newDate(2013, 12, 30)));
assertEquals("Tue", getWeekdayShort(newDate(2013, 12, 31)));
assertEquals("Wed", getWeekdayShort(newDate(2014, 1, 1)));
assertEquals("Thu", getWeekdayShort(newDate(2014, 1, 2)));
assertEquals("Fri", getWeekdayShort(newDate(2014, 1, 3)));
assertEquals("Sat", getWeekdayShort(newDate(2014, 1, 4)));
}
}

@ -0,0 +1,82 @@
package com.todoroo.andlib.utility;
import android.content.Context;
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 java.util.Locale;
import static com.todoroo.andlib.utility.DateUtilities.getRelativeDay;
import static org.joda.time.DateTime.now;
import static org.junit.Assert.assertEquals;
import static org.robolectric.Robolectric.getShadowApplication;
import static org.tasks.Freeze.freezeAt;
import static org.tasks.Freeze.thaw;
@RunWith(RobolectricTestRunner.class)
public class RelativeDayTest {
private static Locale defaultLocale;
private static final DateTime now = new DateTime(2013, 12, 31, 11, 9, 42, 357);
@Before
public void before() {
defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.ENGLISH);
freezeAt(now);
}
@After
public void after() {
Locale.setDefault(defaultLocale);
thaw();
}
@Test
public void relativeDayIsToday() {
checkRelativeDay(now(), "today", "today");
}
@Test
public void relativeDayIsTomorrow() {
checkRelativeDay(now().plusDays(1), "tomorrow", "tmrw");
}
@Test
public void relativeDayIsYesterday() {
checkRelativeDay(now().minusDays(1), "yesterday", "yest");
}
@Test
public void relativeDayTwo() {
checkRelativeDay(now().minusDays(2), "Sunday", "Sun");
checkRelativeDay(now().plusDays(2), "Thursday", "Thu");
}
@Test
public void relativeDayOneWeek() {
checkRelativeDay(now().minusDays(7), "Tuesday", "Tue");
checkRelativeDay(now().plusDays(7), "Tuesday", "Tue");
}
@Test
public void relativeDayMoreThanOneWeek() {
checkRelativeDay(now().minusDays(8), "23 Dec", "23 Dec");
}
@Test
public void relativeDayNextYear() {
checkRelativeDay(now().plusDays(8), "8 Jan\n2014", "8 Jan\n2014");
}
private void checkRelativeDay(DateTime now, String full, String abbreviated) {
final Context context = getShadowApplication().getApplicationContext();
assertEquals(full, getRelativeDay(context, now.getMillis(), false));
assertEquals(abbreviated, getRelativeDay(context, now.getMillis()));
}
}
Loading…
Cancel
Save