Update unit tests for FixedSpeedTrackPathDescriptor

This commit is contained in:
Jimmy Shih
2014-01-14 14:53:51 -08:00
parent 5f436abbda
commit 8a1574be72
2 changed files with 69 additions and 35 deletions
@@ -37,16 +37,33 @@ public class FixedSpeedTrackPathDescriptor implements TrackPathDescriptor {
sharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() { sharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() {
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
boolean updateSlowSpeed = false;
boolean updateNormalSpeed = false;
if (key == null if (key == null
|| key.equals(PreferencesUtils.getKey(context, R.string.track_color_mode_slow_key))) { || key.equals(PreferencesUtils.getKey(context, R.string.track_color_mode_slow_key))) {
updateSlowSpeed = true;
}
if (key == null || key.equals(
PreferencesUtils.getKey(context, R.string.track_color_mode_medium_key))) {
updateNormalSpeed = true;
}
if (key == null
|| key.equals(PreferencesUtils.getKey(context, R.string.stats_units_key))) {
updateSlowSpeed = true;
updateNormalSpeed = true;
}
if (updateSlowSpeed) {
slowSpeed = PreferencesUtils.getInt(context, R.string.track_color_mode_slow_key, slowSpeed = PreferencesUtils.getInt(context, R.string.track_color_mode_slow_key,
PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT); PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT);
if (!PreferencesUtils.isMetricUnits(context)) { if (!PreferencesUtils.isMetricUnits(context)) {
slowSpeed = slowSpeed * UnitConversions.MI_TO_KM; slowSpeed = slowSpeed * UnitConversions.MI_TO_KM;
} }
} }
if (key == null || key.equals(
PreferencesUtils.getKey(context, R.string.track_color_mode_medium_key))) { if (updateNormalSpeed) {
normalSpeed = PreferencesUtils.getInt(context, R.string.track_color_mode_medium_key, normalSpeed = PreferencesUtils.getInt(context, R.string.track_color_mode_medium_key,
PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT); PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT);
if (!PreferencesUtils.isMetricUnits(context)) { if (!PreferencesUtils.isMetricUnits(context)) {
@@ -62,7 +79,6 @@ public class FixedSpeedTrackPathDescriptor implements TrackPathDescriptor {
public FixedSpeedTrackPathDescriptor(Context context) { public FixedSpeedTrackPathDescriptor(Context context) {
this.context = context; this.context = context;
context.getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE) context.getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE)
.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); .registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null); sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null);
@@ -16,59 +16,77 @@
package com.google.android.apps.mytracks.maps; package com.google.android.apps.mytracks.maps;
import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.content.Context; import android.content.Context;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
/** /**
* Tests for the {@link DynamicSpeedTrackPathDescriptor}. * Tests for the {@link FixedSpeedTrackPathDescriptor}.
* *
* @author Youtao Liu * @author Youtao Liu
*/ */
public class FixedSpeedTrackPathDescriptorTest extends AndroidTestCase { public class FixedSpeedTrackPathDescriptorTest extends AndroidTestCase {
private Context context;
@Override @Override
protected void setUp() throws Exception { protected void tearDown() throws Exception {
super.setUp(); Context context = getContext();
context = getContext(); PreferencesUtils.setString(
context, R.string.stats_units_key, PreferencesUtils.STATS_UNITS_DEFAULT);
PreferencesUtils.setInt(context, R.string.track_color_mode_slow_key,
PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT);
PreferencesUtils.setInt(context, R.string.track_color_mode_medium_key,
PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT);
super.tearDown();
} }
/** /**
* Tests the initialization of slowSpeed and normalSpeed in {@link DynamicSpeedTrackPathDescriptor#DynamicSpeedTrackPathDescriptor(Context)} * Tests when the slow speed and the normal speed are both zero.
* .
*/ */
public void testConstructor() { public void testGetSpeed_zero() {
int[] slowSpeedExpectations = { 0, 1, 99, PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT }; testSpeed(0, 0);
int[] normalSpeedExpectations = { 0, 1, 99, PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT };
for (int i = 0; i < slowSpeedExpectations.length; i++) {
PreferencesUtils.setInt(
context, R.string.track_color_mode_slow_key, slowSpeedExpectations[i]);
PreferencesUtils.setInt(
context, R.string.track_color_mode_medium_key, normalSpeedExpectations[i]);
FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor(
context);
assertEquals(slowSpeedExpectations[i], (int) fixedSpeedTrackPathDescriptor.getSlowSpeed());
assertEquals(
normalSpeedExpectations[i], (int) fixedSpeedTrackPathDescriptor.getNormalSpeed());
}
} }
/** /**
* Tests {@link FixedSpeedTrackPathDescriptor#getSlowSpeed()} and * Tests when the slow speed and the normal speed are both one.
* {@link FixedSpeedTrackPathDescriptor#getNormalSpeed()}.
*/ */
public void testGetSpeed() { public void testGetSpeed_one() {
testSpeed(1, 1);
}
/**
* Tests when the slow speed and the normal speed are both large number. E.g.,
* 99 and 100.
*/
public void testGetSpeed_large() {
testSpeed(99, 100);
}
/**
* Tests when the slow speed and the normal speed are both the default value.
*/
public void testGetSpeed_default() {
testSpeed(PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT,
PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT);
}
private void testSpeed(int slowSpeed, int normalSpeed) {
testSpeed(slowSpeed, normalSpeed, true);
testSpeed(slowSpeed, normalSpeed, false);
}
private void testSpeed(int slowSpeed, int normalSpeed, boolean metric) {
Context context = getContext();
PreferencesUtils.setString(context, R.string.stats_units_key,
context.getString(metric ? R.string.stats_units_metric : R.string.stats_units_imperial));
PreferencesUtils.setInt(context, R.string.track_color_mode_slow_key, slowSpeed);
PreferencesUtils.setInt(context, R.string.track_color_mode_medium_key, normalSpeed);
FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor( FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor(
context); context);
double slowSpeed = fixedSpeedTrackPathDescriptor.getSlowSpeed(); double expectedSlowSpeed = metric ? slowSpeed : slowSpeed * UnitConversions.MI_TO_KM;
double normalSpeed = fixedSpeedTrackPathDescriptor.getNormalSpeed(); double expectedNormalSpeed = metric ? normalSpeed : normalSpeed * UnitConversions.MI_TO_KM;
// Change value in shared preferences assertEquals(expectedSlowSpeed, fixedSpeedTrackPathDescriptor.getSlowSpeed());
PreferencesUtils.setInt(context, R.string.track_color_mode_slow_key, (int) (slowSpeed + 2)); assertEquals(expectedNormalSpeed, fixedSpeedTrackPathDescriptor.getNormalSpeed());
PreferencesUtils.setInt(context, R.string.track_color_mode_medium_key, (int) (normalSpeed + 2));
assertEquals(slowSpeed, fixedSpeedTrackPathDescriptor.getSlowSpeed());
assertEquals(normalSpeed, fixedSpeedTrackPathDescriptor.getNormalSpeed());
} }
} }