forked from upstream-mirrors/OpenTracks
Make changes in DynamicSpeedTrackPathDescriptor :
Add field: CriticalDifference Add helper method isDifferenceSignificant to test needsRedraw Add getSpeedMargin,setAverageMovingSpeed getAverageMovingSpeed methods Change some code style of DynamicSpeedTrackPathDescriptor Add test cases for FixedSpeedTrackPathDescriptor and DynamicSpeedTrackPathDescriptor
This commit is contained in:
+72
-35
@@ -22,20 +22,20 @@ import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
|
|||||||
import com.google.android.apps.mytracks.content.Track;
|
import com.google.android.apps.mytracks.content.Track;
|
||||||
import com.google.android.apps.mytracks.stats.TripStatistics;
|
import com.google.android.apps.mytracks.stats.TripStatistics;
|
||||||
import com.google.android.maps.mytracks.R;
|
import com.google.android.maps.mytracks.R;
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A dynamic speed path descriptor.
|
* A dynamic speed path descriptor.
|
||||||
*
|
*
|
||||||
* @author Vangelis S.
|
* @author Vangelis S.
|
||||||
*/
|
*/
|
||||||
public class DynamicSpeedTrackPathDescriptor
|
public class DynamicSpeedTrackPathDescriptor implements TrackPathDescriptor,
|
||||||
implements TrackPathDescriptor, OnSharedPreferenceChangeListener {
|
OnSharedPreferenceChangeListener {
|
||||||
|
|
||||||
private int slowSpeed;
|
private int slowSpeed;
|
||||||
private int normalSpeed;
|
private int normalSpeed;
|
||||||
@@ -43,13 +43,15 @@ public class DynamicSpeedTrackPathDescriptor
|
|||||||
private final int speedMarginDefault;
|
private final int speedMarginDefault;
|
||||||
private double averageMovingSpeed;
|
private double averageMovingSpeed;
|
||||||
private final Context context;
|
private final Context context;
|
||||||
|
@VisibleForTesting
|
||||||
|
static final int CRITICAL_DIFFERENCE_PERCENTAGE = 20;
|
||||||
|
|
||||||
public DynamicSpeedTrackPathDescriptor(Context context){
|
public DynamicSpeedTrackPathDescriptor(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
speedMarginDefault = Integer.parseInt(
|
speedMarginDefault = Integer.parseInt(context
|
||||||
context.getString(R.string.color_mode_dynamic_percentage_default));
|
.getString(R.string.color_mode_dynamic_percentage_default));
|
||||||
SharedPreferences prefs = context.getSharedPreferences(
|
SharedPreferences prefs = context.getSharedPreferences(Constants.SETTINGS_NAME,
|
||||||
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
Context.MODE_PRIVATE);
|
||||||
|
|
||||||
if (prefs == null) {
|
if (prefs == null) {
|
||||||
speedMargin = speedMarginDefault;
|
speedMargin = speedMarginDefault;
|
||||||
@@ -60,7 +62,8 @@ public class DynamicSpeedTrackPathDescriptor
|
|||||||
speedMargin = getSpeedMargin(prefs);
|
speedMargin = getSpeedMargin(prefs);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getSpeedMargin(SharedPreferences sharedPreferences) {
|
@VisibleForTesting
|
||||||
|
int getSpeedMargin(SharedPreferences sharedPreferences) {
|
||||||
try {
|
try {
|
||||||
return Integer.parseInt(sharedPreferences.getString(
|
return Integer.parseInt(sharedPreferences.getString(
|
||||||
context.getString(R.string.track_color_mode_dynamic_speed_variation_key),
|
context.getString(R.string.track_color_mode_dynamic_speed_variation_key),
|
||||||
@@ -72,20 +75,20 @@ public class DynamicSpeedTrackPathDescriptor
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the slow speed calculated based on the % below the average speed.
|
* Get the slow speed calculated based on the % below the average speed.
|
||||||
|
*
|
||||||
* @return The speed limit considered as slow.
|
* @return The speed limit considered as slow.
|
||||||
*/
|
*/
|
||||||
public int getSlowSpeed()
|
public int getSlowSpeed() {
|
||||||
{
|
|
||||||
slowSpeed = (int) (averageMovingSpeed - (averageMovingSpeed * speedMargin / 100));
|
slowSpeed = (int) (averageMovingSpeed - (averageMovingSpeed * speedMargin / 100));
|
||||||
return slowSpeed;
|
return slowSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the medium speed calculated based on the % above the average speed.
|
* Gets the medium speed calculated based on the % above the average speed.
|
||||||
|
*
|
||||||
* @return The speed limit considered as normal.
|
* @return The speed limit considered as normal.
|
||||||
*/
|
*/
|
||||||
public int getNormalSpeed()
|
public int getNormalSpeed() {
|
||||||
{
|
|
||||||
normalSpeed = (int) (averageMovingSpeed + (averageMovingSpeed * speedMargin / 100));
|
normalSpeed = (int) (averageMovingSpeed + (averageMovingSpeed * speedMargin / 100));
|
||||||
return normalSpeed;
|
return normalSpeed;
|
||||||
}
|
}
|
||||||
@@ -94,11 +97,9 @@ public class DynamicSpeedTrackPathDescriptor
|
|||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||||
Log.d(TAG, "DynamicSpeedTrackPathDescriptor: onSharedPreferences changed " + key);
|
Log.d(TAG, "DynamicSpeedTrackPathDescriptor: onSharedPreferences changed " + key);
|
||||||
if (key == null
|
if (key == null
|
||||||
|| !key.equals(context.getString(R.string.track_color_mode_dynamic_speed_variation_key))) {
|
|| !key.equals(context.getString(R.string.track_color_mode_dynamic_speed_variation_key))) { return; }
|
||||||
return;
|
SharedPreferences prefs = context.getSharedPreferences(Constants.SETTINGS_NAME,
|
||||||
}
|
Context.MODE_PRIVATE);
|
||||||
SharedPreferences prefs = context.getSharedPreferences(
|
|
||||||
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
|
||||||
|
|
||||||
if (prefs == null) {
|
if (prefs == null) {
|
||||||
speedMargin = speedMarginDefault;
|
speedMargin = speedMarginDefault;
|
||||||
@@ -110,33 +111,69 @@ public class DynamicSpeedTrackPathDescriptor
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean needsRedraw() {
|
public boolean needsRedraw() {
|
||||||
SharedPreferences prefs = context.getSharedPreferences(
|
SharedPreferences prefs = context.getSharedPreferences(Constants.SETTINGS_NAME,
|
||||||
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
Context.MODE_PRIVATE);
|
||||||
long currentTrackId = prefs.getLong(context.getString(R.string.selected_track_key), -1);
|
long currentTrackId = prefs.getLong(context.getString(R.string.selected_track_key), -1);
|
||||||
if(currentTrackId == -1) {
|
if (currentTrackId == -1) {
|
||||||
// Could not find track.
|
// Could not find track.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Track track = MyTracksProviderUtils.Factory.get(context).getTrack(currentTrackId);
|
Track track = MyTracksProviderUtils.Factory.get(context).getTrack(currentTrackId);
|
||||||
TripStatistics stats = track.getStatistics();
|
TripStatistics stats = track.getStatistics();
|
||||||
double newaverageSpeed = (int) Math.floor(stats.getAverageMovingSpeed() * 3.6);
|
double newAverageMovingSpeed = (int) Math.floor(stats.getAverageMovingSpeed() * 3.6);
|
||||||
|
|
||||||
if(averageMovingSpeed == 0) {
|
return isDifferenceSignificant(averageMovingSpeed, newAverageMovingSpeed);
|
||||||
averageMovingSpeed = newaverageSpeed;
|
}
|
||||||
return true;
|
|
||||||
|
/**
|
||||||
|
* Checks whether the old speed and the new speed differ significantly or not.
|
||||||
|
*/
|
||||||
|
public boolean isDifferenceSignificant(double oldAverageMovingSpeed, double newAverageMovingSpeed) {
|
||||||
|
if (oldAverageMovingSpeed == 0) {
|
||||||
|
if (newAverageMovingSpeed == 0) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
averageMovingSpeed = newAverageMovingSpeed;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
double difference = Math.max(averageMovingSpeed, newaverageSpeed);
|
// Here, both oldAverageMovingSpeed and newAverageMovingSpeed are not zero.
|
||||||
|
double maxValue = Math.max(oldAverageMovingSpeed, newAverageMovingSpeed);
|
||||||
if (difference == 0.0) {
|
double differencePercentage = Math.abs(oldAverageMovingSpeed - newAverageMovingSpeed)
|
||||||
difference = 0.0;
|
/ maxValue * 100;
|
||||||
} else {
|
if (differencePercentage >= CRITICAL_DIFFERENCE_PERCENTAGE) {
|
||||||
difference = Math.abs(averageMovingSpeed - newaverageSpeed) / difference * 100;
|
averageMovingSpeed = newAverageMovingSpeed;
|
||||||
}
|
|
||||||
if(difference >= 20) {
|
|
||||||
averageMovingSpeed = newaverageSpeed;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of variable speedMargin to check the result of test.
|
||||||
|
* @return the value of speedMargin.
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
int getSpeedMargin() {
|
||||||
|
return speedMargin;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of newAverageMovingSpeed to test the method isDifferenceSignificant.
|
||||||
|
* @param newAverageMovingSpeed the value to set.
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
void setAverageMovingSpeed(double newAverageMovingSpeed) {
|
||||||
|
averageMovingSpeed = newAverageMovingSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of averageMovingSpeed to check the result of test.
|
||||||
|
*
|
||||||
|
* @return the value of averageMovingSpeed
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
double getAverageMovingSpeed() {
|
||||||
|
return averageMovingSpeed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+176
@@ -0,0 +1,176 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of
|
||||||
|
* the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations under
|
||||||
|
* the License.
|
||||||
|
*/
|
||||||
|
package com.google.android.apps.mytracks.maps;
|
||||||
|
|
||||||
|
import com.google.android.apps.mytracks.Constants;
|
||||||
|
import com.google.android.maps.mytracks.R;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.SharedPreferences.Editor;
|
||||||
|
import android.test.AndroidTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for the {@link DynamicSpeedTrackPathDescriptor}.
|
||||||
|
*
|
||||||
|
* @author Youtao Liu
|
||||||
|
*/
|
||||||
|
public class DynamicSpeedTrackPathDescriptorTest extends AndroidTestCase {
|
||||||
|
|
||||||
|
private Context context;
|
||||||
|
private SharedPreferences sharedPreferences;
|
||||||
|
private Editor sharedPreferencesEditor;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
context = getContext();
|
||||||
|
sharedPreferences = context.getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
||||||
|
sharedPreferencesEditor = sharedPreferences.edit();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the method {@link DynamicSpeedTrackPathDescriptor#getSpeedMargin()}
|
||||||
|
* with zero, normal and illegal value.
|
||||||
|
*/
|
||||||
|
public void testGetSpeedMargin() {
|
||||||
|
String[] actuals = { "0", "50", "99", "" };
|
||||||
|
// The default value of speedMargin is 25.
|
||||||
|
int[] expectations = { 0, 50, 99, 25 };
|
||||||
|
// Test
|
||||||
|
for (int i = 0; i < expectations.length; i++) {
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_dynamic_speed_variation_key), actuals[i]);
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
DynamicSpeedTrackPathDescriptor dynamicSpeedTrackPathDescriptor = new DynamicSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
assertEquals(expectations[i],
|
||||||
|
dynamicSpeedTrackPathDescriptor.getSpeedMargin(sharedPreferences));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link DynamicSpeedTrackPathDescriptor#OnSharedPreferenceChanged()}
|
||||||
|
* when the key is null.
|
||||||
|
*/
|
||||||
|
public void testOnSharedPreferenceChanged_nullKey() {
|
||||||
|
DynamicSpeedTrackPathDescriptor dynamicSpeedTrackPathDescriptor = new DynamicSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
int speedMargin = dynamicSpeedTrackPathDescriptor.getSpeedMargin(sharedPreferences);
|
||||||
|
// Change value in shared preferences.
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_dynamic_speed_variation_key),
|
||||||
|
Integer.toString(speedMargin + 2));
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
|
||||||
|
dynamicSpeedTrackPathDescriptor.onSharedPreferenceChanged(sharedPreferences, null);
|
||||||
|
assertEquals(speedMargin, dynamicSpeedTrackPathDescriptor.getSpeedMargin());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link DynamicSpeedTrackPathDescriptor#OnSharedPreferenceChanged()}
|
||||||
|
* when the key is not null, and not trackColorModeDynamicVariation.
|
||||||
|
*/
|
||||||
|
public void testOnSharedPreferenceChanged_otherKey() {
|
||||||
|
DynamicSpeedTrackPathDescriptor dynamicSpeedTrackPathDescriptor = new DynamicSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
int speedMargin = dynamicSpeedTrackPathDescriptor.getSpeedMargin(sharedPreferences);
|
||||||
|
// Change value in shared preferences.
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_dynamic_speed_variation_key),
|
||||||
|
Integer.toString(speedMargin + 2));
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
|
||||||
|
dynamicSpeedTrackPathDescriptor.onSharedPreferenceChanged(sharedPreferences, "anyKey");
|
||||||
|
assertEquals(speedMargin, dynamicSpeedTrackPathDescriptor.getSpeedMargin());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link DynamicSpeedTrackPathDescriptor#OnSharedPreferenceChanged()}
|
||||||
|
* when the key is trackColorModeDynamicVariation.
|
||||||
|
*/
|
||||||
|
public void testOnSharedPreferenceChanged_trackColorModeDynamicVariationKey() {
|
||||||
|
DynamicSpeedTrackPathDescriptor dynamicSpeedTrackPathDescriptor = new DynamicSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
int speedMargin = dynamicSpeedTrackPathDescriptor.getSpeedMargin(sharedPreferences);
|
||||||
|
// Change value in shared preferences.
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
"trackColorModeDynamicVariation",
|
||||||
|
Integer.toString(speedMargin + 2));
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
|
||||||
|
dynamicSpeedTrackPathDescriptor.onSharedPreferenceChanged(sharedPreferences,
|
||||||
|
"trackColorModeDynamicVariation");
|
||||||
|
assertEquals(speedMargin + 2, dynamicSpeedTrackPathDescriptor.getSpeedMargin());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link DynamicSpeedTrackPathDescriptor#OnSharedPreferenceChanged()}
|
||||||
|
* when the values of speedMargin is "".
|
||||||
|
*/
|
||||||
|
public void testOnSharedPreferenceChanged_emptyValue() {
|
||||||
|
DynamicSpeedTrackPathDescriptor dynamicSpeedTrackPathDescriptor = new DynamicSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
// Change value in shared preferences
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_dynamic_speed_variation_key), "");
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
|
||||||
|
dynamicSpeedTrackPathDescriptor.onSharedPreferenceChanged(sharedPreferences,
|
||||||
|
context.getString(R.string.track_color_mode_dynamic_speed_variation_key));
|
||||||
|
// The default value of speedMargin is 25.
|
||||||
|
assertEquals(25, dynamicSpeedTrackPathDescriptor.getSpeedMargin());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link DynamicSpeedTrackPathDescriptor#NeedsRedraw()} by wrong track
|
||||||
|
* id.
|
||||||
|
*/
|
||||||
|
public void testNeedsRedraw_WrongTrackId() {
|
||||||
|
long trackId = -1;
|
||||||
|
sharedPreferencesEditor.putLong(context.getString(R.string.selected_track_key), trackId);
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
DynamicSpeedTrackPathDescriptor dynamicSpeedTrackPathDescriptor = new DynamicSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
assertEquals(false, dynamicSpeedTrackPathDescriptor.needsRedraw());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link DynamicSpeedTrackPathDescriptor#NeedsRedraw()} by different
|
||||||
|
* averageMovingSpeed.
|
||||||
|
*/
|
||||||
|
public void testIsDiffereceSignificant() {
|
||||||
|
DynamicSpeedTrackPathDescriptor dynamicSpeedTrackPathDescriptor = new DynamicSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
double[] averageMovingSpeeds = { 0, 30, 30, 30 };
|
||||||
|
double[] newAverageMovingSpeed = { 20, 30,
|
||||||
|
// Difference is less than CRITICAL_DIFFERENCE_PERCENTAGE
|
||||||
|
30 * (1 + (DynamicSpeedTrackPathDescriptor.CRITICAL_DIFFERENCE_PERCENTAGE / 100) / 2),
|
||||||
|
// Difference is more than CRITICAL_DIFFERENCE_PERCENTAGE
|
||||||
|
30 * (1 + (DynamicSpeedTrackPathDescriptor.CRITICAL_DIFFERENCE_PERCENTAGE / 100.00) * 2) };
|
||||||
|
boolean[] expectedValues = { true, false, false, true };
|
||||||
|
double[] expectedAverageMovingSpeed = { 20, 30, 30,
|
||||||
|
30 * (1 + (DynamicSpeedTrackPathDescriptor.CRITICAL_DIFFERENCE_PERCENTAGE / 100.00) * 2) };
|
||||||
|
// Test
|
||||||
|
for (int i = 0; i < newAverageMovingSpeed.length; i++) {
|
||||||
|
dynamicSpeedTrackPathDescriptor.setAverageMovingSpeed(averageMovingSpeeds[i]);
|
||||||
|
assertEquals(expectedValues[i], dynamicSpeedTrackPathDescriptor.isDifferenceSignificant(
|
||||||
|
averageMovingSpeeds[i], newAverageMovingSpeed[i]));
|
||||||
|
assertEquals(expectedAverageMovingSpeed[i],
|
||||||
|
dynamicSpeedTrackPathDescriptor.getAverageMovingSpeed());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+183
@@ -0,0 +1,183 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of
|
||||||
|
* the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations under
|
||||||
|
* the License.
|
||||||
|
*/
|
||||||
|
package com.google.android.apps.mytracks.maps;
|
||||||
|
|
||||||
|
import com.google.android.apps.mytracks.Constants;
|
||||||
|
import com.google.android.maps.mytracks.R;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.SharedPreferences.Editor;
|
||||||
|
import android.test.AndroidTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for the {@link DynamicSpeedTrackPathDescriptor}.
|
||||||
|
*
|
||||||
|
* @author Youtao Liu
|
||||||
|
*/
|
||||||
|
public class FixedSpeedTrackPathDescriptorTest extends AndroidTestCase {
|
||||||
|
private Context context;
|
||||||
|
private SharedPreferences sharedPreferences;
|
||||||
|
private Editor sharedPreferencesEditor;
|
||||||
|
private int slowDefault;
|
||||||
|
private int normalDefault;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
context = getContext();
|
||||||
|
sharedPreferences = context.getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
||||||
|
sharedPreferencesEditor = sharedPreferences.edit();
|
||||||
|
// Get the default value
|
||||||
|
slowDefault = 9;
|
||||||
|
normalDefault = 15;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the initialization of slowSpeed and normalSpeed in
|
||||||
|
* {@link DynamicSpeedTrackPathDescriptor#DynamicSpeedTrackPathDescriptor(Context)}
|
||||||
|
* .
|
||||||
|
*/
|
||||||
|
public void testConstructor() {
|
||||||
|
String[] slowSpeedsInShPre = { "0", "1", "99", "" };
|
||||||
|
int[] slowSpeedExpectations = { 0, 1, 99, slowDefault };
|
||||||
|
String[] normalSpeedsInShPre = { "0", "1", "99", "" };
|
||||||
|
int[] normalSpeedExpectations = { 0, 1, 99, normalDefault };
|
||||||
|
for (int i = 0; i < slowSpeedsInShPre.length; i++) {
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_slow_key), slowSpeedsInShPre[i]);
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_medium_key),
|
||||||
|
normalSpeedsInShPre[i]);
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
assertEquals(slowSpeedExpectations[i], fixedSpeedTrackPathDescriptor.getSlowSpeed());
|
||||||
|
assertEquals(normalSpeedExpectations[i], fixedSpeedTrackPathDescriptor.getNormalSpeed());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link DynamicSpeedTrackPathDescriptor#onSharedPreferenceChanged()}
|
||||||
|
* when the key is null.
|
||||||
|
*/
|
||||||
|
public void testOnSharedPreferenceChanged_null_key() {
|
||||||
|
FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
int slowSpeed = fixedSpeedTrackPathDescriptor.getSlowSpeed();
|
||||||
|
int normalSpeed = fixedSpeedTrackPathDescriptor.getNormalSpeed();
|
||||||
|
// Change value in shared preferences
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_slow_key),
|
||||||
|
Integer.toString(slowSpeed + 2));
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_medium_key),
|
||||||
|
Integer.toString(normalSpeed + 2));
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
|
||||||
|
fixedSpeedTrackPathDescriptor.onSharedPreferenceChanged(sharedPreferences, null);
|
||||||
|
assertEquals(slowSpeed, fixedSpeedTrackPathDescriptor.getSlowSpeed());
|
||||||
|
assertEquals(normalSpeed, fixedSpeedTrackPathDescriptor.getNormalSpeed());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link DynamicSpeedTrackPathDescriptor#onSharedPreferenceChanged()}
|
||||||
|
* when the key is not null, and not slowSpeed and not normalSpeed.
|
||||||
|
*/
|
||||||
|
public void testOnSharedPreferenceChanged_other_key() {
|
||||||
|
FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
int slowSpeed = fixedSpeedTrackPathDescriptor.getSlowSpeed();
|
||||||
|
int normalSpeed = fixedSpeedTrackPathDescriptor.getNormalSpeed();
|
||||||
|
// Change value in shared preferences
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_slow_key),
|
||||||
|
Integer.toString(slowSpeed + 2));
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_medium_key),
|
||||||
|
Integer.toString(normalSpeed + 2));
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
fixedSpeedTrackPathDescriptor.onSharedPreferenceChanged(sharedPreferences, "anyKey");
|
||||||
|
assertEquals(slowSpeed, fixedSpeedTrackPathDescriptor.getSlowSpeed());
|
||||||
|
assertEquals(normalSpeed, fixedSpeedTrackPathDescriptor.getNormalSpeed());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link DynamicSpeedTrackPathDescriptor#onSharedPreferenceChanged()}
|
||||||
|
* when the key is slowSpeed.
|
||||||
|
*/
|
||||||
|
public void testOnSharedPreferenceChanged_slowSpeedKey() {
|
||||||
|
FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
int slowSpeed = fixedSpeedTrackPathDescriptor.getSlowSpeed();
|
||||||
|
int normalSpeed = fixedSpeedTrackPathDescriptor.getNormalSpeed();
|
||||||
|
// Change value in shared preferences
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_slow_key),
|
||||||
|
Integer.toString(slowSpeed + 2));
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_medium_key),
|
||||||
|
Integer.toString(normalSpeed + 2));
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
fixedSpeedTrackPathDescriptor.onSharedPreferenceChanged(sharedPreferences,
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_slow_key));
|
||||||
|
assertEquals(slowSpeed + 2, fixedSpeedTrackPathDescriptor.getSlowSpeed());
|
||||||
|
assertEquals(normalSpeed + 2, fixedSpeedTrackPathDescriptor.getNormalSpeed());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link DynamicSpeedTrackPathDescriptor#onSharedPreferenceChanged()}
|
||||||
|
* when the key is normalSpeed.
|
||||||
|
*/
|
||||||
|
public void testOnSharedPreferenceChanged_normalSpeedKey() {
|
||||||
|
FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
int slowSpeed = fixedSpeedTrackPathDescriptor.getSlowSpeed();
|
||||||
|
int normalSpeed = fixedSpeedTrackPathDescriptor.getNormalSpeed();
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_slow_key),
|
||||||
|
Integer.toString(slowSpeed + 4));
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_medium_key),
|
||||||
|
Integer.toString(normalSpeed + 4));
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
|
||||||
|
fixedSpeedTrackPathDescriptor.onSharedPreferenceChanged(sharedPreferences,
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_medium_key));
|
||||||
|
assertEquals(slowSpeed + 4, fixedSpeedTrackPathDescriptor.getSlowSpeed());
|
||||||
|
assertEquals(normalSpeed + 4, fixedSpeedTrackPathDescriptor.getNormalSpeed());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link DynamicSpeedTrackPathDescriptor#onSharedPreferenceChanged()}
|
||||||
|
* when the values of slowSpeed and normalSpeed in SharedPreference is "". In
|
||||||
|
* such situation, the default value should get returned.
|
||||||
|
*/
|
||||||
|
public void testOnSharedPreferenceChanged_emptyValue() {
|
||||||
|
FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor(
|
||||||
|
context);
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_slow_key), "");
|
||||||
|
sharedPreferencesEditor.putString(
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_medium_key), "");
|
||||||
|
sharedPreferencesEditor.commit();
|
||||||
|
|
||||||
|
fixedSpeedTrackPathDescriptor.onSharedPreferenceChanged(sharedPreferences,
|
||||||
|
context.getString(R.string.track_color_mode_fixed_speed_medium_key));
|
||||||
|
assertEquals(slowDefault, fixedSpeedTrackPathDescriptor.getSlowSpeed());
|
||||||
|
assertEquals(normalDefault, fixedSpeedTrackPathDescriptor.getNormalSpeed());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user