Convert tests to Robolectric

* DateUtilitiesTest
* TranslationTests
* AstridTranslationTest
* AdvancedRepeatTest
pull/46/head
Alex Baker 11 years ago
parent a9c6e5f28c
commit 0e6df44e95

@ -128,8 +128,7 @@ public class DateUtilities {
* @return date, with month, day, and year
*/
public static String getDateString(Context context, Date date, boolean includeYear) {
String month = DateUtils.getMonthString(date.getMonth() +
Calendar.JANUARY, DateUtils.LENGTH_MEDIUM);
String month = new SimpleDateFormat("MMM").format(date);
String value;
String standardDate;
// united states, you are special
@ -200,11 +199,9 @@ public class DateUtilities {
* @return weekday
*/
public static String getWeekday(Date date) {
return DateUtils.getDayOfWeekString(date.getDay() + Calendar.SUNDAY,
DateUtils.LENGTH_LONG);
return new SimpleDateFormat("EEEE").format(date);
}
/**
* @return weekday
*/

@ -0,0 +1,67 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.andlib.test;
import android.content.Context;
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.astrid.service.AstridDependencyInjector;
import org.junit.After;
import org.junit.Before;
import org.robolectric.Robolectric;
import java.util.Locale;
public class TodorooRobolectricTestCase {
static {
AstridDependencyInjector.initialize();
}
@Before
public void setUp() throws Exception {
ContextManager.setContext(getContext());
AstridDependencyInjector.flush();
DependencyInjectionService.getInstance().inject(this);
setLocale(Locale.ENGLISH);
}
@After
public void tearDown() throws Exception {
setLocale(Locale.getDefault());
}
/**
* Loop through each locale and call runnable
*/
public void forEachLocale(Runnable r) {
Locale[] locales = Locale.getAvailableLocales();
for(Locale locale : locales) {
setLocale(locale);
r.run();
}
}
/**
* Sets locale
*/
private void setLocale(Locale locale) {
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
getContext().getResources().updateConfiguration(config, metrics);
}
protected Context getContext() {
return Robolectric.getShadowApplication().getApplicationContext();
}
}

@ -8,6 +8,7 @@ package com.todoroo.andlib.test;
import android.content.res.Resources;
import org.junit.Test;
import org.tasks.R;
import java.lang.reflect.Field;
@ -17,6 +18,10 @@ import java.util.Date;
import java.util.List;
import java.util.Locale;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Tests translations for consistency with the default values. You must
* extend this class and create it with your own values for strings
@ -25,7 +30,7 @@ import java.util.Locale;
* @author Tim Su <tim@todoroo.com>
*
*/
abstract public class TranslationTests extends TodorooTestCase {
abstract public class TranslationTests extends TodorooRobolectricTestCase {
// --- abstract methods
@ -108,6 +113,7 @@ abstract public class TranslationTests extends TodorooTestCase {
/**
* Internal test of format string parser
*/
@Test
public void testFormatStringParser() {
String s = "abc";
FormatStringData data = new FormatStringData(s);
@ -141,6 +147,7 @@ abstract public class TranslationTests extends TodorooTestCase {
* Test that the format specifiers in translations match exactly the
* translations in the default text
*/
@Test
public void testFormatStringsMatch() throws Exception {
final Resources r = getContext().getResources();
final int[] strings = getResourceIds(getStringResources());
@ -189,6 +196,7 @@ abstract public class TranslationTests extends TodorooTestCase {
/**
* Test that date formatters parse correctly
*/
@Test
public void testDateFormats() throws Exception {
final Resources r = getContext().getResources();
@ -225,6 +233,7 @@ abstract public class TranslationTests extends TodorooTestCase {
/**
* Test that there are the same number of array entries in each locale
*/
@Test
public void testArraySizesMatch() throws Exception {
final Resources r = getContext().getResources();
final int[] arrays = getResourceIds(getArrayResources());

@ -5,19 +5,28 @@
*/
package com.todoroo.andlib.utility;
import com.todoroo.andlib.test.TodorooTestCase;
import com.todoroo.andlib.test.TodorooRobolectricTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class DateUtilitiesTest extends TodorooTestCase {
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@RunWith(RobolectricTestRunner.class)
public class DateUtilitiesTest extends TodorooRobolectricTestCase {
public void set24Hour(boolean is24) {
DateUtilities.is24HourOverride = is24;
}
@Test
public void testTimeString() {
forEachLocale(new Runnable() {
public void run() {
@ -38,6 +47,7 @@ public class DateUtilitiesTest extends TodorooTestCase {
});
}
@Test
public void testDateString() {
forEachLocale(new Runnable() {
public void run() {
@ -49,9 +59,9 @@ public class DateUtilitiesTest extends TodorooTestCase {
}
}
});
}
@Test
public void testWeekdayString() {
forEachLocale(new Runnable() {
public void run() {
@ -63,9 +73,9 @@ public class DateUtilitiesTest extends TodorooTestCase {
}
}
});
}
@Test
public void testParseISO8601() {
String withTime = "2013-01-28T13:17:02+00:00";
long date;
@ -86,6 +96,7 @@ public class DateUtilitiesTest extends TodorooTestCase {
assertEquals(2, cal.get(Calendar.SECOND));
}
@Test
public void testParseISO8601NoTime() {
String withTime = "2013-01-28";
long date;

@ -9,17 +9,24 @@ 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.TodorooTestCase;
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 java.util.Calendar;
import java.util.Date;
public class AdvancedRepeatTests extends TodorooTestCase {
import static org.junit.Assert.assertEquals;
@RunWith(RobolectricTestRunner.class)
public class AdvancedRepeatTest extends TodorooRobolectricTestCase {
private static final int PREV_PREV = -2;
private static final int PREV = -1;
@ -31,8 +38,8 @@ public class AdvancedRepeatTests extends TodorooTestCase {
private long nextDueDate;
private RRule rrule;
@Override
protected void setUp() throws Exception {
@Before
public void setUp() throws Exception {
super.setUp();
task = new Task();
task.setValue(Task.COMPLETION_DATE, DateUtilities.now());
@ -41,6 +48,7 @@ public class AdvancedRepeatTests extends TodorooTestCase {
// --- date with time tests
@Test
public void testDueDateSpecificTime() throws ParseException {
buildRRule(1, Frequency.DAILY);
@ -53,6 +61,7 @@ public class AdvancedRepeatTests extends TodorooTestCase {
assertDateTimeEquals(nextDayWithTime, nextDueDate);
}
@Test
public void testCompletionDateSpecificTime() throws ParseException {
buildRRule(1, Frequency.DAILY);
@ -75,8 +84,8 @@ public class AdvancedRepeatTests extends TodorooTestCase {
// --- due date tests
/** test multiple days per week - DUE DATE */
@Test
public void testDueDateInPastSingleWeekMultiDay() throws Exception {
buildRRule(1, Frequency.WEEKLY, Weekday.MO, Weekday.WE, Weekday.FR);
setTaskDueDate(THIS, Calendar.SUNDAY);
@ -93,8 +102,8 @@ public class AdvancedRepeatTests extends TodorooTestCase {
}
/** test single day repeats - DUE DATE */
@Test
public void testDueDateSingleDay() throws Exception {
buildRRule(1, Frequency.WEEKLY, Weekday.MO);
setTaskDueDate(PREV_PREV, Calendar.MONDAY);
@ -123,6 +132,7 @@ public class AdvancedRepeatTests extends TodorooTestCase {
}
/** test multiple days per week - DUE DATE */
@Test
public void testDueDateSingleWeekMultiDay() throws Exception {
buildRRule(1, Frequency.WEEKLY, Weekday.MO, Weekday.WE, Weekday.FR);
@ -141,8 +151,8 @@ public class AdvancedRepeatTests extends TodorooTestCase {
}
/** test multiple days per week, multiple intervals - DUE DATE */
@Test
public void testDueDateMultiWeekMultiDay() throws Exception {
buildRRule(2, Frequency.WEEKLY, Weekday.MO, Weekday.WE, Weekday.FR);
setTaskDueDate(THIS, Calendar.SUNDAY);
@ -161,8 +171,8 @@ public class AdvancedRepeatTests extends TodorooTestCase {
// --- completion tests
/** test multiple days per week - COMPLETE DATE */
@Test
public void testCompleteDateSingleWeek() throws Exception {
for(Weekday wday : Weekday.values()) {
buildRRule(1, Frequency.WEEKLY, wday);
computeNextDueDate(true);
@ -187,8 +197,8 @@ public class AdvancedRepeatTests extends TodorooTestCase {
}
/** test multiple days per week, multiple intervals - COMPLETE DATE */
@Test
public void testCompleteDateMultiWeek() throws Exception {
for(Weekday wday : Weekday.values()) {
buildRRule(2, Frequency.WEEKLY, wday);
computeNextDueDate(true);
@ -247,7 +257,6 @@ public class AdvancedRepeatTests extends TodorooTestCase {
rrule.setByDay(days);
}
private void setTaskDueDate(int which, int day) {
long time = getDate(DateUtilities.now(), which, day);
@ -265,5 +274,4 @@ public class AdvancedRepeatTests extends TodorooTestCase {
c.add(Calendar.DAY_OF_MONTH, (Math.abs(which) - 1) * direction * 7);
return Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, c.getTimeInMillis());
}
}

@ -5,17 +5,21 @@
*/
package com.todoroo.astrid.test;
import android.content.res.Resources;
import com.todoroo.andlib.test.TranslationTests;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.tasks.R;
import java.util.Locale;
public class AstridTranslationTests extends TranslationTests {
import static org.junit.Assert.assertEquals;
@RunWith(RobolectricTestRunner.class)
public class AstridTranslationTest extends TranslationTests {
@Override
public Class<?> getArrayResources() {
@ -51,6 +55,7 @@ public class AstridTranslationTests extends TranslationTests {
/**
* Test dollar sign resources
*/
@Test
public void testSpecialStringsMatch() throws Exception {
final Resources r = getContext().getResources();
final StringBuilder failures = new StringBuilder();
@ -70,5 +75,4 @@ public class AstridTranslationTests extends TranslationTests {
assertEquals(failures.toString(), 0,
failures.toString().replaceAll("[^\n]", "").length());
}
}
Loading…
Cancel
Save