Fix unit tests to run in any timezone.

This commit is contained in:
Jimmy Shih
2012-04-12 13:14:35 -07:00
parent d045b0440f
commit 08e460d208
2 changed files with 14 additions and 13 deletions
@@ -17,6 +17,7 @@
package com.google.android.apps.mytracks.content; package com.google.android.apps.mytracks.content;
import com.google.android.apps.mytracks.stats.TripStatistics; import com.google.android.apps.mytracks.stats.TripStatistics;
import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
@@ -29,6 +30,7 @@ import android.util.Pair;
*/ */
public class DescriptionGeneratorImplTest extends AndroidTestCase { public class DescriptionGeneratorImplTest extends AndroidTestCase {
private static final long START_TIME = 1288721514000L;
private DescriptionGeneratorImpl descriptionGenerator; private DescriptionGeneratorImpl descriptionGenerator;
@Override @Override
@@ -52,7 +54,7 @@ public class DescriptionGeneratorImplTest extends AndroidTestCase {
stats.setTotalElevationGain(6000); stats.setTotalElevationGain(6000);
stats.setMaxGrade(0.42); stats.setMaxGrade(0.42);
stats.setMinGrade(0.11); stats.setMinGrade(0.11);
stats.setStartTime(1288721514000L); stats.setStartTime(START_TIME);
track.setStatistics(stats); track.setStatistics(stats);
track.setCategory("hiking"); track.setCategory("hiking");
String expected = "Created by" String expected = "Created by"
@@ -71,7 +73,7 @@ public class DescriptionGeneratorImplTest extends AndroidTestCase {
+ "Elevation gain: 6000 m (19685 ft)<br>" + "Elevation gain: 6000 m (19685 ft)<br>"
+ "Max grade: 42 %<br>" + "Max grade: 42 %<br>"
+ "Min grade: 11 %<br>" + "Min grade: 11 %<br>"
+ "Recorded: 11/2/2010 11:11 AM<br>" + "Recorded: " + StringUtils.formatDateTime(getContext(), START_TIME) + "<br>"
+ "Activity type: hiking<br>"; + "Activity type: hiking<br>";
assertEquals(expected, descriptionGenerator.generateTrackDescription(track, null, null)); assertEquals(expected, descriptionGenerator.generateTrackDescription(track, null, null));
} }
@@ -91,7 +93,7 @@ public class DescriptionGeneratorImplTest extends AndroidTestCase {
stats.setTotalElevationGain(6000); stats.setTotalElevationGain(6000);
stats.setMaxGrade(0.42); stats.setMaxGrade(0.42);
stats.setMinGrade(0.11); stats.setMinGrade(0.11);
stats.setStartTime(1288721514000L); stats.setStartTime(START_TIME);
waypoint.setStatistics(stats); waypoint.setStatistics(stats);
String expected = "Total distance: 20.00 km (12.4 mi)\n" String expected = "Total distance: 20.00 km (12.4 mi)\n"
+ "Total time: 10:00\n" + "Total time: 10:00\n"
@@ -107,7 +109,7 @@ public class DescriptionGeneratorImplTest extends AndroidTestCase {
+ "Elevation gain: 6000 m (19685 ft)\n" + "Elevation gain: 6000 m (19685 ft)\n"
+ "Max grade: 42 %\n" + "Max grade: 42 %\n"
+ "Min grade: 11 %\n" + "Min grade: 11 %\n"
+ "Recorded: 11/2/2010 11:11 AM\n"; + "Recorded: " + StringUtils.formatDateTime(getContext(), START_TIME) + "\n";
assertEquals(expected, descriptionGenerator.generateWaypointDescription(waypoint)); assertEquals(expected, descriptionGenerator.generateWaypointDescription(waypoint));
} }
@@ -13,9 +13,11 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.google.android.apps.mytracks.util; package com.google.android.apps.mytracks.util;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.text.format.DateFormat;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.TimeZone; import java.util.TimeZone;
@@ -31,22 +33,19 @@ public class StringUtilsTest extends AndroidTestCase {
* Tests {@link StringUtils#formatDateTime(android.content.Context, long)}. * Tests {@link StringUtils#formatDateTime(android.content.Context, long)}.
*/ */
public void testFormatTime() { public void testFormatTime() {
// Unix time 0 in UTC is "4:00 PM" in Pacific Standard time zone. assertEquals(DateFormat.getTimeFormat(getContext()).format(0L),
// This test can break if run on a different time zone or with a different StringUtils.formatTime(getContext(), 0L));
// time format preference.
assertEquals("4:00 PM", StringUtils.formatTime(getContext(), 0L));
} }
/** /**
* Tests {@link StringUtils#formatDateTime(android.content.Context, long)}. * Tests {@link StringUtils#formatDateTime(android.content.Context, long)}.
*/ */
public void testFormatDateTime() { public void testFormatDateTime() {
// Unix time 0 in UTC is "12/31/1969 4:00 PM" in Pacific Standard time zone. String expected = DateFormat.getDateFormat(getContext()).format(0L) + " "
// This test can break if run on a different time zone or with a different + DateFormat.getTimeFormat(getContext()).format(0L);
// date/time format preference. assertEquals(expected, StringUtils.formatDateTime(getContext(), 0L));
assertEquals("12/31/1969 4:00 PM", StringUtils.formatDateTime(getContext(), 0L));
} }
/** /**
* Tests {@link StringUtils#formatDateTimeIso8601(long)}. * Tests {@link StringUtils#formatDateTimeIso8601(long)}.
*/ */