Converting unit tests

pull/143/head
Alex Baker 12 years ago
parent 2a313e22fe
commit d63549723c

@ -54,4 +54,15 @@ public class TodorooTestCase extends AndroidTestCase {
getContext().getResources().updateConfiguration(config, metrics); getContext().getResources().updateConfiguration(config, metrics);
} }
/**
* Loop through each locale and call runnable
*/
public void forEachLocale(Runnable r) {
Locale[] locales = Locale.getAvailableLocales();
for(Locale locale : locales) {
setLocale(locale);
r.run();
}
}
} }

@ -8,7 +8,6 @@ package com.todoroo.andlib.test;
import android.content.res.Resources; import android.content.res.Resources;
import org.junit.Test;
import org.tasks.R; import org.tasks.R;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -18,9 +17,6 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.tasks.date.DateTimeUtils.newDate; import static org.tasks.date.DateTimeUtils.newDate;
/** /**
@ -31,7 +27,7 @@ import static org.tasks.date.DateTimeUtils.newDate;
* @author Tim Su <tim@todoroo.com> * @author Tim Su <tim@todoroo.com>
* *
*/ */
abstract public class TranslationTests extends TodorooRobolectricTestCase { abstract public class TranslationTests extends TodorooTestCase {
// --- abstract methods // --- abstract methods
@ -114,7 +110,6 @@ abstract public class TranslationTests extends TodorooRobolectricTestCase {
/** /**
* Internal test of format string parser * Internal test of format string parser
*/ */
@Test
public void testFormatStringParser() { public void testFormatStringParser() {
String s = "abc"; String s = "abc";
FormatStringData data = new FormatStringData(s); FormatStringData data = new FormatStringData(s);
@ -148,9 +143,8 @@ abstract public class TranslationTests extends TodorooRobolectricTestCase {
* Test that the format specifiers in translations match exactly the * Test that the format specifiers in translations match exactly the
* translations in the default text * translations in the default text
*/ */
@Test
public void testFormatStringsMatch() throws Exception { public void testFormatStringsMatch() throws Exception {
final Resources r = getRobolectricContext().getResources(); final Resources r = getContext().getResources();
final int[] strings = getResourceIds(getStringResources()); final int[] strings = getResourceIds(getStringResources());
final FormatStringData[] formatStrings = new FormatStringData[strings.length]; final FormatStringData[] formatStrings = new FormatStringData[strings.length];
@ -173,8 +167,11 @@ abstract public class TranslationTests extends TodorooRobolectricTestCase {
Locale locale = r.getConfiguration().locale; Locale locale = r.getConfiguration().locale;
for(int i = 0; i < strings.length; i++) { for(int i = 0; i < strings.length; i++) {
try { try {
if (strings[i] == R.string.premium_speech_bubble_2) // Special exception--this string contains a % character switch(strings[i]) {
continue; case R.string.premium_speech_bubble_2: // Special exception--this string contains a % character
case R.string.abc_shareactionprovider_share_with_application:
continue;
}
String string = r.getString(strings[i]); String string = r.getString(strings[i]);
FormatStringData newFS = new FormatStringData(string); FormatStringData newFS = new FormatStringData(string);
if(!newFS.matches(formatStrings[i])) { if(!newFS.matches(formatStrings[i])) {
@ -197,9 +194,8 @@ abstract public class TranslationTests extends TodorooRobolectricTestCase {
/** /**
* Test that date formatters parse correctly * Test that date formatters parse correctly
*/ */
@Test
public void testDateFormats() throws Exception { public void testDateFormats() throws Exception {
final Resources r = getRobolectricContext().getResources(); final Resources r = getContext().getResources();
final StringBuilder failures = new StringBuilder(); final StringBuilder failures = new StringBuilder();
final int[] dateStrings = getDateFormatStrings(); final int[] dateStrings = getDateFormatStrings();
@ -234,9 +230,8 @@ abstract public class TranslationTests extends TodorooRobolectricTestCase {
/** /**
* Test that there are the same number of array entries in each locale * Test that there are the same number of array entries in each locale
*/ */
@Test
public void testArraySizesMatch() throws Exception { public void testArraySizesMatch() throws Exception {
final Resources r = getRobolectricContext().getResources(); final Resources r = getContext().getResources();
final int[] arrays = getResourceIds(getArrayResources()); final int[] arrays = getResourceIds(getArrayResources());
final int[] sizes = new int[arrays.length]; final int[] sizes = new int[arrays.length];
final StringBuilder failures = new StringBuilder(); final StringBuilder failures = new StringBuilder();

@ -9,16 +9,10 @@ import android.content.res.Resources;
import com.todoroo.andlib.test.TranslationTests; import com.todoroo.andlib.test.TranslationTests;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.tasks.R; import org.tasks.R;
import java.util.Locale; import java.util.Locale;
import static org.junit.Assert.assertEquals;
@RunWith(RobolectricTestRunner.class)
public class AstridTranslationTest extends TranslationTests { public class AstridTranslationTest extends TranslationTests {
@Override @Override
@ -55,9 +49,8 @@ public class AstridTranslationTest extends TranslationTests {
/** /**
* Test dollar sign resources * Test dollar sign resources
*/ */
@Test
public void testSpecialStringsMatch() throws Exception { public void testSpecialStringsMatch() throws Exception {
final Resources r = getRobolectricContext().getResources(); final Resources r = getContext().getResources();
final StringBuilder failures = new StringBuilder(); final StringBuilder failures = new StringBuilder();
forEachLocale(new Runnable() { forEachLocale(new Runnable() {

@ -1,14 +1,10 @@
package com.todoroo.astrid.utility; package com.todoroo.astrid.utility;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.test.AndroidTestCase;
import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.utility.Preferences;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.tasks.R; import org.tasks.R;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -17,31 +13,23 @@ import static com.todoroo.andlib.utility.Preferences.getBoolean;
import static com.todoroo.andlib.utility.Preferences.getInt; import static com.todoroo.andlib.utility.Preferences.getInt;
import static com.todoroo.andlib.utility.Preferences.setStringFromInteger; import static com.todoroo.andlib.utility.Preferences.setStringFromInteger;
import static com.todoroo.astrid.utility.AstridDefaultPreferenceSpec.migrateToNewQuietHours; import static com.todoroo.astrid.utility.AstridDefaultPreferenceSpec.migrateToNewQuietHours;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.tasks.TestUtilities.clearPreferences; import static org.tasks.TestUtilities.clearPreferences;
@RunWith(RobolectricTestRunner.class) public class AstridDefaultPreferenceSpecTest extends AndroidTestCase {
public class AstridDefaultPreferenceSpecTest {
@SuppressLint("NewApi") @SuppressLint("NewApi")
private static final int MILLIS_PER_HOUR = (int) TimeUnit.HOURS.toMillis(1); private static final int MILLIS_PER_HOUR = (int) TimeUnit.HOURS.toMillis(1);
@Before @Override
public void before() { public void setUp() throws Exception {
clearPreferences(); super.setUp();
assertFalse(hasMigrated()); clearPreferences(getContext());
}
@After assertFalse(hasMigrated());
public void after() {
assertTrue(hasMigrated());
} }
@Test public void testQuietHoursEnabledAfterMigration() {
public void quietHoursEnabledAfterMigration() {
setOldQuietHoursStart(1); setOldQuietHoursStart(1);
migrateToNewQuietHours(); migrateToNewQuietHours();
@ -49,8 +37,7 @@ public class AstridDefaultPreferenceSpecTest {
assertTrue(quietHoursEnabled()); assertTrue(quietHoursEnabled());
} }
@Test public void testQuietHoursDisabledAfterMigration() {
public void quietHoursDisabledAfterMigration() {
Preferences.setString(R.string.p_rmd_quietStart_old, ""); Preferences.setString(R.string.p_rmd_quietStart_old, "");
migrateToNewQuietHours(); migrateToNewQuietHours();
@ -58,8 +45,7 @@ public class AstridDefaultPreferenceSpecTest {
assertFalse(quietHoursEnabled()); assertFalse(quietHoursEnabled());
} }
@Test public void testMigrateFromStartOfDefaultReminderTimeArray() {
public void migrateFromStartOfDefaultReminderTimeArray() {
setOldReminderTime(0); setOldReminderTime(0);
migrateToNewQuietHours(); migrateToNewQuietHours();
@ -67,8 +53,7 @@ public class AstridDefaultPreferenceSpecTest {
assertEquals(0, newReminderTime()); assertEquals(0, newReminderTime());
} }
@Test public void testMigrateFromEndOfDefaultReminderTimeArray() {
public void migrateFromEndOfDefaultReminderTimeArray() {
setOldReminderTime(23); setOldReminderTime(23);
migrateToNewQuietHours(); migrateToNewQuietHours();
@ -76,8 +61,7 @@ public class AstridDefaultPreferenceSpecTest {
assertEquals(23 * MILLIS_PER_HOUR, newReminderTime()); assertEquals(23 * MILLIS_PER_HOUR, newReminderTime());
} }
@Test public void testMigrateFromStartOfQuietHourStartArray() {
public void migrateFromStartOfQuietHourStartArray() {
setOldQuietHoursStart(1); setOldQuietHoursStart(1);
migrateToNewQuietHours(); migrateToNewQuietHours();
@ -85,8 +69,7 @@ public class AstridDefaultPreferenceSpecTest {
assertEquals(MILLIS_PER_HOUR, newQuietHoursStartTime()); assertEquals(MILLIS_PER_HOUR, newQuietHoursStartTime());
} }
@Test public void testMigrateFromEndOfQuietHoursStartArray() {
public void migrateFromEndOfQuietHoursStartArray() {
setOldQuietHoursStart(23); setOldQuietHoursStart(23);
migrateToNewQuietHours(); migrateToNewQuietHours();
@ -94,8 +77,7 @@ public class AstridDefaultPreferenceSpecTest {
assertEquals(23 * MILLIS_PER_HOUR, newQuietHoursStartTime()); assertEquals(23 * MILLIS_PER_HOUR, newQuietHoursStartTime());
} }
@Test public void testMigrateFromStartOfQuietHoursEndArray() {
public void migrateFromStartOfQuietHoursEndArray() {
setOldQuietHoursStart(1); setOldQuietHoursStart(1);
setOldQuietHoursEnd(0); setOldQuietHoursEnd(0);
@ -104,8 +86,7 @@ public class AstridDefaultPreferenceSpecTest {
assertEquals(0, newQuietHoursEndTime()); assertEquals(0, newQuietHoursEndTime());
} }
@Test public void testMigrateFromEndOfQuietHoursEndArray() {
public void migrateFromEndOfQuietHoursEndArray() {
setOldQuietHoursStart(1); setOldQuietHoursStart(1);
setOldQuietHoursEnd(23); setOldQuietHoursEnd(23);

@ -1,20 +1,21 @@
package org.tasks; package org.tasks;
import android.content.Context;
import com.todoroo.astrid.utility.AstridPreferences; import com.todoroo.astrid.utility.AstridPreferences;
import static com.todoroo.andlib.utility.Preferences.getPrefs; import static com.todoroo.andlib.utility.Preferences.getPrefs;
import static org.robolectric.Robolectric.getShadowApplication;
public class TestUtilities { public class TestUtilities {
public static void clearPreferences() { public static void clearPreferences(Context context) {
getPrefs(getShadowApplication().getApplicationContext()) getPrefs(context)
.edit() .edit()
.clear() .clear()
.commit(); .commit();
} }
public static void resetPreferences() { public static void resetPreferences(Context context) {
clearPreferences(); clearPreferences(context);
AstridPreferences.setPreferenceDefaults(); AstridPreferences.setPreferenceDefaults();
} }
} }

@ -1,52 +1,44 @@
package org.tasks.date; package org.tasks.date;
import android.test.AndroidTestCase;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.tasks.Snippet; import org.tasks.Snippet;
import java.util.Date; import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.tasks.Freeze.freezeAt; import static org.tasks.Freeze.freezeAt;
import static org.tasks.date.DateTimeUtils.currentTimeMillis; import static org.tasks.date.DateTimeUtils.currentTimeMillis;
import static org.tasks.date.DateTimeUtils.newDate; import static org.tasks.date.DateTimeUtils.newDate;
import static org.tasks.date.DateTimeUtils.newDateUtc; import static org.tasks.date.DateTimeUtils.newDateUtc;
@RunWith(RobolectricTestRunner.class) public class DateTimeUtilsTest extends AndroidTestCase {
public class DateTimeUtilsTest {
private final DateTime now = new DateTime(2014, 1, 1, 15, 17, 53, 0); private final DateTime now = new DateTime(2014, 1, 1, 15, 17, 53, 0);
private final Date nowAsDate = new Date(114, 0, 1, 15, 17, 53); private final Date nowAsDate = new Date(114, 0, 1, 15, 17, 53);
@Test public void testGetCurrentTime() {
public void getCurrentTime() {
freezeAt(now).thawAfter(new Snippet() {{ freezeAt(now).thawAfter(new Snippet() {{
assertEquals(now.getMillis(), currentTimeMillis()); assertEquals(now.getMillis(), currentTimeMillis());
}}); }});
} }
@Test public void testGetNowAsDate() {
public void getNowAsDate() {
freezeAt(now).thawAfter(new Snippet() {{ freezeAt(now).thawAfter(new Snippet() {{
assertEquals(nowAsDate, newDate()); assertEquals(nowAsDate, newDate());
}}); }});
} }
@Test public void testCreateNewDate() {
public void createNewDate() {
assertEquals(new Date(114, 0, 1), newDate(2014, 1, 1)); assertEquals(new Date(114, 0, 1), newDate(2014, 1, 1));
} }
@Test public void testCreateNewDateTime() {
public void createNewDateTime() {
assertEquals(new Date(114, 0, 1, 15, 17, 53), newDate(2014, 1, 1, 15, 17, 53)); assertEquals(new Date(114, 0, 1, 15, 17, 53), newDate(2014, 1, 1, 15, 17, 53));
} }
@Test public void testCreateNewUtcDate() {
public void createNewUtcDate() {
DateTime utc = now.toDateTime(DateTimeZone.UTC); DateTime utc = now.toDateTime(DateTimeZone.UTC);
Date actual = newDateUtc(utc.getYear(), utc.getMonthOfYear(), utc.getDayOfMonth(), utc.getHourOfDay(), utc.getMinuteOfHour(), utc.getSecondOfMinute()); Date actual = newDateUtc(utc.getYear(), utc.getMonthOfYear(), utc.getDayOfMonth(), utc.getHourOfDay(), utc.getMinuteOfHour(), utc.getSecondOfMinute());
assertEquals(utc.getMillis(), actual.getTime()); assertEquals(utc.getMillis(), actual.getTime());

@ -42,18 +42,6 @@ public class TodorooRobolectricTestCase {
DependencyInjectionService.getInstance().inject(this); DependencyInjectionService.getInstance().inject(this);
} }
/**
* Loop through each locale and call runnable
*/
public static void forEachLocale(Runnable r) {
Locale[] locales = Locale.getAvailableLocales();
for(Locale locale : locales) {
setLocale(locale);
r.run();
}
}
/** /**
* Sets locale * Sets locale
*/ */

Loading…
Cancel
Save