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

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

@ -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