From 53ee9d31bc98ab3a6d1e1aa2641724d3b097b36e Mon Sep 17 00:00:00 2001 From: ase69s Date: Mon, 19 Sep 2011 20:25:41 +0200 Subject: [PATCH] Review issues corrected, added test case for remaining class TrackPathPainterFactory --- .../android/apps/mytracks/MapOverlay.java | 2 +- .../apps/mytracks/SettingsActivity.java | 46 +++++-------- .../maps/DynamicSpeedTrackPathDescriptor.java | 4 +- .../maps/FixedSpeedTrackPathDescriptor.java | 14 ++-- .../maps/TrackPathPainterFactory.java | 6 +- .../TrackPathDescriptorDynamicSpeedTest.java | 47 +++++++++++++ .../TrackPathDescriptorFixedSpeedTest.java | 47 +++++++++++++ .../maps/TrackPathPainterFactoryTest.java | 68 +++++++++++++++++++ .../maps/TrackPathPainterSingleColorTest.java | 47 +++++++++++++ .../maps/TrackPathPainterTestCase.java | 43 ++++++++++++ 10 files changed, 283 insertions(+), 41 deletions(-) create mode 100644 MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathDescriptorDynamicSpeedTest.java create mode 100644 MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathDescriptorFixedSpeedTest.java create mode 100644 MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterFactoryTest.java create mode 100644 MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterSingleColorTest.java create mode 100644 MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterTestCase.java diff --git a/MyTracks/src/com/google/android/apps/mytracks/MapOverlay.java b/MyTracks/src/com/google/android/apps/mytracks/MapOverlay.java index 1705c481d..b26d4a0b0 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/MapOverlay.java +++ b/MyTracks/src/com/google/android/apps/mytracks/MapOverlay.java @@ -462,7 +462,7 @@ public class MapOverlay extends Overlay implements OnSharedPreferenceChangeListe @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - Log.d(Constants.TAG, "MapOverlay: onSharedPreferences changed " + key); + Log.d(TAG, "MapOverlay: onSharedPreferences changed " + key); if (key != null) { if (key.equals(context.getString(R.string.track_color_mode_key))) { trackPathPainter = TrackPathPainterFactory.getTrackPathPainter(context); diff --git a/MyTracks/src/com/google/android/apps/mytracks/SettingsActivity.java b/MyTracks/src/com/google/android/apps/mytracks/SettingsActivity.java index 594b14ac8..db84871dc 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/SettingsActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/SettingsActivity.java @@ -468,46 +468,32 @@ public class SettingsActivity extends PreferenceActivity { * Sets the TrackColorMode preference listeners. */ private void setTrackColorModePreferenceListeners() { - EditTextPreference trackColorModeFixedSlowPreference = - (EditTextPreference) findPreference(getString(R.string.track_color_mode_fixed_speed_slow_display_key)); - trackColorModeFixedSlowPreference.setOnPreferenceChangeListener( + setTrackColorModePreferenceListener(R.string.track_color_mode_fixed_speed_slow_display_key, + R.string.track_color_mode_fixed_speed_slow_key); + setTrackColorModePreferenceListener(R.string.track_color_mode_fixed_speed_medium_display_key, + R.string.track_color_mode_fixed_speed_medium_key); + } + + /** + * Sets a TrackColorMode preference listener. + */ + private void setTrackColorModePreferenceListener(int displayKey, final int metricKey) { + EditTextPreference trackColorModePreference = + (EditTextPreference) findPreference(getString(displayKey)); + trackColorModePreference.setOnPreferenceChangeListener( new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - validateTrackColorModeSettings((EditTextPreference) preference, (String) newValue, - R.string.track_color_mode_fixed_speed_slow_key); + validateTrackColorModeSettings((EditTextPreference) preference, (String) newValue, metricKey); return true; } }); - trackColorModeFixedSlowPreference.setOnPreferenceClickListener( + trackColorModePreference.setOnPreferenceClickListener( new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { - viewTrackColorModeSettings((EditTextPreference) preference, - R.string.track_color_mode_fixed_speed_slow_key); - return true; - } - }); - - EditTextPreference trackColorModeFixedMediumPreference = - (EditTextPreference) findPreference(getString(R.string.track_color_mode_fixed_speed_medium_display_key)); - trackColorModeFixedMediumPreference.setOnPreferenceChangeListener( - new OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, - Object newValue) { - validateTrackColorModeSettings((EditTextPreference) preference, (String) newValue, - R.string.track_color_mode_fixed_speed_slow_key); - return true; - } - }); - trackColorModeFixedMediumPreference.setOnPreferenceClickListener( - new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - viewTrackColorModeSettings((EditTextPreference) preference, - R.string.track_color_mode_fixed_speed_medium_key); + viewTrackColorModeSettings((EditTextPreference) preference, metricKey); return true; } }); diff --git a/MyTracks/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathDescriptor.java b/MyTracks/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathDescriptor.java index d1f0d5ca4..f6fc968d5 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathDescriptor.java +++ b/MyTracks/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathDescriptor.java @@ -15,6 +15,8 @@ */ package com.google.android.apps.mytracks.maps; +import static com.google.android.apps.mytracks.Constants.TAG; + import com.google.android.apps.mytracks.Constants; import com.google.android.apps.mytracks.content.MyTracksProviderUtils; import com.google.android.apps.mytracks.content.Track; @@ -76,7 +78,7 @@ public class DynamicSpeedTrackPathDescriptor @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - Log.d(Constants.TAG, "DynamicSpeedTrackPathDescriptor: onSharedPreferences changed " + key); + Log.d(TAG, "DynamicSpeedTrackPathDescriptor: onSharedPreferences changed " + key); if (key == null || !key.equals(context.getString(R.string.track_color_mode_dynamic_speed_variation_key))) { return; diff --git a/MyTracks/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptor.java b/MyTracks/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptor.java index 7a528ffe9..d00ed9e0b 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptor.java +++ b/MyTracks/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptor.java @@ -15,6 +15,8 @@ */ package com.google.android.apps.mytracks.maps; +import static com.google.android.apps.mytracks.Constants.TAG; + import com.google.android.apps.mytracks.Constants; import com.google.android.maps.mytracks.R; @@ -34,7 +36,7 @@ public class FixedSpeedTrackPathDescriptor implements TrackPathDescriptor, OnSha private int normalSpeed; private final Context context; - public FixedSpeedTrackPathDescriptor(Context context){ + public FixedSpeedTrackPathDescriptor(Context context) { this.context = context; context.getSharedPreferences(Constants.SETTINGS_NAME, 0) @@ -65,8 +67,7 @@ public class FixedSpeedTrackPathDescriptor implements TrackPathDescriptor, OnSha * Gets the slow speed for reference. * @return The speed limit considered as slow. */ - public int getSlowSpeed() - { + public int getSlowSpeed() { return slowSpeed; } @@ -74,14 +75,13 @@ public class FixedSpeedTrackPathDescriptor implements TrackPathDescriptor, OnSha * Gets the normal speed for reference. * @return The speed limit considered as normal. */ - public int getNormalSpeed() - { + public int getNormalSpeed() { return normalSpeed; } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - Log.d(Constants.TAG, "FixedSpeedTrackPathDescriptor: onSharedPreferences changed " + key); + Log.d(TAG, "FixedSpeedTrackPathDescriptor: onSharedPreferences changed " + key); if (key == null || (!key.equals(context.getString(R.string.track_color_mode_fixed_speed_slow_key)) && !key.equals(context.getString(R.string.track_color_mode_fixed_speed_medium_key)))) { @@ -104,7 +104,7 @@ public class FixedSpeedTrackPathDescriptor implements TrackPathDescriptor, OnSha normalSpeed = Integer.parseInt(prefs.getString(context.getString( R.string.track_color_mode_fixed_speed_medium_key), "17")); } catch (NumberFormatException e) { - normalSpeed = 17; + normalSpeed = 17; } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/maps/TrackPathPainterFactory.java b/MyTracks/src/com/google/android/apps/mytracks/maps/TrackPathPainterFactory.java index 4c3c86afa..14f8f29cd 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/maps/TrackPathPainterFactory.java +++ b/MyTracks/src/com/google/android/apps/mytracks/maps/TrackPathPainterFactory.java @@ -15,6 +15,8 @@ */ package com.google.android.apps.mytracks.maps; +import static com.google.android.apps.mytracks.Constants.TAG; + import com.google.android.apps.mytracks.Constants; import com.google.android.maps.mytracks.R; @@ -45,7 +47,7 @@ public class TrackPathPainterFactory { } String colorMode = prefs.getString(context.getString(R.string.track_color_mode_key), null); - Log.i(Constants.TAG, "Creating track path painter of type: " + colorMode); + Log.i(TAG, "Creating track path painter of type: " + colorMode); if (colorMode == null || colorMode.equals(context.getString(R.string.track_color_mode_none))) { return new SingleColorTrackPathPainter(context); @@ -56,7 +58,7 @@ public class TrackPathPainterFactory { return new DynamicSpeedTrackPathPainter(context, new DynamicSpeedTrackPathDescriptor(context)); } else { - Log.w(Constants.TAG, "Using default track path painter. Unrecognized painter: " + colorMode); + Log.w(TAG, "Using default track path painter. Unrecognized painter: " + colorMode); return new SingleColorTrackPathPainter(context); } } diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathDescriptorDynamicSpeedTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathDescriptorDynamicSpeedTest.java new file mode 100644 index 000000000..07fbbba03 --- /dev/null +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathDescriptorDynamicSpeedTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2011 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 android.location.Location; + +/** + * Tests for the MyTracks track path descriptors and painters. + * + * @author Vangelis S. + */ +public class TrackPathDescriptorDynamicSpeedTest extends TrackPathPainterTestCase { + + public void testDynamicSpeedTrackPathDescriptor() throws Exception { + Location location = new Location("gps"); + location.setLatitude(10); + + for (int i = 0; i < 100; ++i) { + location = new Location("gps"); + location.setLatitude(20 + i / 2); + location.setLongitude(150 - i); + myTracksOverlay.addLocation(location); + } + + TrackPathPainter painter = new DynamicSpeedTrackPathPainter(getContext(), new DynamicSpeedTrackPathDescriptor(getContext())); + int startLocationIdx = 0; + Boolean alwaysVisible = true; + + painter.updatePath(myTracksOverlay.getMapProjection(mockView), + myTracksOverlay.getMapViewRect(mockView), startLocationIdx, alwaysVisible, + myTracksOverlay.getPoints()); + painter.drawTrack(canvas); + } +} diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathDescriptorFixedSpeedTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathDescriptorFixedSpeedTest.java new file mode 100644 index 000000000..4a46efd98 --- /dev/null +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathDescriptorFixedSpeedTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2011 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 android.location.Location; + +/** + * Tests for the MyTracks track path descriptors and painters. + * + * @author Vangelis S. + */ +public class TrackPathDescriptorFixedSpeedTest extends TrackPathPainterTestCase { + + public void testFixedSpeedTrackPathDescriptor() throws Exception { + Location location = new Location("gps"); + location.setLatitude(10); + + for (int i = 0; i < 100; ++i) { + location = new Location("gps"); + location.setLatitude(20 + i / 2); + location.setLongitude(150 - i); + myTracksOverlay.addLocation(location); + } + + TrackPathPainter painter = new DynamicSpeedTrackPathPainter(getContext(), new FixedSpeedTrackPathDescriptor(getContext())); + int startLocationIdx = 0; + Boolean alwaysVisible = true; + + painter.updatePath(myTracksOverlay.getMapProjection(mockView), + myTracksOverlay.getMapViewRect(mockView), startLocationIdx, alwaysVisible, + myTracksOverlay.getPoints()); + painter.drawTrack(canvas); + } +} diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterFactoryTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterFactoryTest.java new file mode 100644 index 000000000..4e3d64280 --- /dev/null +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterFactoryTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2011 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.location.Location; + +/** + * Tests for the MyTracks track path painter factory. + * + * @author Vangelis S. + */ +public class TrackPathPainterFactoryTest extends TrackPathPainterTestCase { + + public void testTrackPathPainterFactory() throws Exception { + Location location = new Location("gps"); + location.setLatitude(10); + + for (int i = 0; i < 100; ++i) { + location = new Location("gps"); + location.setLatitude(20 + i / 2); + location.setLongitude(150 - i); + myTracksOverlay.addLocation(location); + } + + Context context = getContext(); + SharedPreferences prefs = context.getSharedPreferences(Constants.SETTINGS_NAME, 0); + if (prefs == null) { + return; + } + + testTrackPathPainterFactorySpecific(context, prefs, R.string.track_color_mode_none); + testTrackPathPainterFactorySpecific(context, prefs, R.string.track_color_mode_fixed); + testTrackPathPainterFactorySpecific(context, prefs, R.string.track_color_mode_dynamic); + } + + private void testTrackPathPainterFactorySpecific(Context context, SharedPreferences prefs, int track_color_mode) { + prefs.edit().putString(context.getString(track_color_mode), + context.getString(R.string.track_color_mode_key)).commit(); + + int startLocationIdx = 0; + Boolean alwaysVisible = true; + + TrackPathPainter painter = TrackPathPainterFactory.getTrackPathPainter(context); + + painter.updatePath(myTracksOverlay.getMapProjection(mockView), + myTracksOverlay.getMapViewRect(mockView), startLocationIdx, alwaysVisible, + myTracksOverlay.getPoints()); + painter.drawTrack(canvas); + } +} diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterSingleColorTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterSingleColorTest.java new file mode 100644 index 000000000..b7bdf04af --- /dev/null +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterSingleColorTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2011 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 android.location.Location; + +/** + * Tests for the MyTracks track path descriptors and painters. + * + * @author Vangelis S. + */ +public class TrackPathPainterSingleColorTest extends TrackPathPainterTestCase { + + public void testSimpeColorTrackPathPainter() throws Exception { + Location location = new Location("gps"); + location.setLatitude(10); + + for (int i = 0; i < 100; ++i) { + location = new Location("gps"); + location.setLatitude(20 + i / 2); + location.setLongitude(150 - i); + myTracksOverlay.addLocation(location); + } + + TrackPathPainter painter = new SingleColorTrackPathPainter(getContext()); + int startLocationIdx = 0; + Boolean alwaysVisible = true; + + painter.updatePath(myTracksOverlay.getMapProjection(mockView), + myTracksOverlay.getMapViewRect(mockView), startLocationIdx, alwaysVisible, + myTracksOverlay.getPoints()); + painter.drawTrack(canvas); + } +} diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterTestCase.java b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterTestCase.java new file mode 100644 index 000000000..59ceb94fd --- /dev/null +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterTestCase.java @@ -0,0 +1,43 @@ +/* + * Copyright 2011 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.MockMyTracksOverlay; +import com.google.android.maps.MapView; + +import android.graphics.Canvas; +import android.test.AndroidTestCase; + +/** + * Tests for the MyTracks track path descriptors and painters. + * + * @author Vangelis S. + */ +public class TrackPathPainterTestCase extends AndroidTestCase { + protected Canvas canvas; + protected MockMyTracksOverlay myTracksOverlay; + protected MapView mockView; + + @Override + protected void setUp() throws Exception { + super.setUp(); + canvas = new Canvas(); + myTracksOverlay = new MockMyTracksOverlay(getContext()); + // Enable drawing. + myTracksOverlay.setTrackDrawingEnabled(true); + mockView = null; + } +}