Change the code style of DynamicSpeedTrackPathPainterTest and SingleColorTrackPathPainterTest, and make the code easier to read.

This commit is contained in:
youtaol
2012-04-10 17:54:49 +08:00
parent 28750142a9
commit e5d86a708e
3 changed files with 20 additions and 13 deletions
@@ -52,7 +52,6 @@ public class SingleColorTrackPathPainter implements TrackPathPainter {
public void updatePath(Projection projection, Rect viewRect, int startLocationIdx, public void updatePath(Projection projection, Rect viewRect, int startLocationIdx,
Boolean alwaysVisible, List<CachedLocation> points) { Boolean alwaysVisible, List<CachedLocation> points) {
path = new Path(); path = new Path();
path.incReserve(points.size());
updatePath(projection, viewRect, startLocationIdx, alwaysVisible, points, path); updatePath(projection, viewRect, startLocationIdx, alwaysVisible, points, path);
} }
@@ -65,11 +64,12 @@ public class SingleColorTrackPathPainter implements TrackPathPainter {
* @param startLocationIdx The start point from where update the path. * @param startLocationIdx The start point from where update the path.
* @param alwaysVisible Flag for always visible. * @param alwaysVisible Flag for always visible.
* @param points The list of points used to update the path. * @param points The list of points used to update the path.
* @param newPath The path to be created. * @param pathToUpdate The path to be created.
*/ */
@VisibleForTesting @VisibleForTesting
void updatePath(Projection projection, Rect viewRect, int startLocationIdx, void updatePath(Projection projection, Rect viewRect, int startLocationIdx,
Boolean alwaysVisible, List<CachedLocation> points, Path newPath) { Boolean alwaysVisible, List<CachedLocation> points, Path pathToUpdate) {
pathToUpdate.incReserve(points.size());
// Whether to start a new segment on new valid and visible point. // Whether to start a new segment on new valid and visible point.
boolean newSegment = startLocationIdx <= 0 || !points.get(startLocationIdx - 1).valid; boolean newSegment = startLocationIdx <= 0 || !points.get(startLocationIdx - 1).valid;
boolean lastVisible = !newSegment; boolean lastVisible = !newSegment;
@@ -97,10 +97,10 @@ public class SingleColorTrackPathPainter implements TrackPathPainter {
// Either move to beginning of a new segment or continue the old one. // Either move to beginning of a new segment or continue the old one.
projection.toPixels(geoPoint, pt); projection.toPixels(geoPoint, pt);
if (newSegment) { if (newSegment) {
newPath.moveTo(pt.x, pt.y); pathToUpdate.moveTo(pt.x, pt.y);
newSegment = false; newSegment = false;
} else { } else {
newPath.lineTo(pt.x, pt.y); pathToUpdate.lineTo(pt.x, pt.y);
} }
} }
} }
@@ -18,7 +18,6 @@ package com.google.android.apps.mytracks.maps;
import com.google.android.apps.mytracks.ColoredPath; import com.google.android.apps.mytracks.ColoredPath;
import com.google.android.apps.mytracks.MapOverlay.CachedLocation; import com.google.android.apps.mytracks.MapOverlay.CachedLocation;
import com.google.android.apps.mytracks.TrackStubUtils; import com.google.android.apps.mytracks.TrackStubUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import com.google.android.testing.mocking.AndroidMock; import com.google.android.testing.mocking.AndroidMock;
import com.google.android.testing.mocking.UsesMocks; import com.google.android.testing.mocking.UsesMocks;
@@ -38,9 +37,14 @@ public class DynamicSpeedTrackPathPainterTest extends TrackPathPainterTestCase {
private static final int NUMBER_OF_LOCATIONS = 100; private static final int NUMBER_OF_LOCATIONS = 100;
private static final int LOCATIONS_PER_SEGMENT = 25; private static final int LOCATIONS_PER_SEGMENT = 25;
// The maximum speed(KM/H) which is considered slow. // The maximum speed(KM/H) which is considered slow.
private static final int SLOW_SPEED = 30; private static final int SLOW_SPEED_KMH = 30;
// The maximum speed(KM/H) which is considered normal. // The maximum speed(KM/H) which is considered normal.
private static final int NORMAL_SPEED = 50; private static final int NORMAL_SPEED_KMH = 50;
// Convert from kilometers per hour to meters per second
private static final double KMH_TO_MS = 1 / 3.6;
private static final int SLOW_SPEED_MS = (int) (SLOW_SPEED_KMH * KMH_TO_MS);
private static final int NORMAL_SPEED_MS = (int) (NORMAL_SPEED_KMH * KMH_TO_MS);
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
@@ -95,15 +99,15 @@ public class DynamicSpeedTrackPathPainterTest extends TrackPathPainterTestCase {
// Gets the slow speed. Divide SLOW_SPEED by 2 to make it smaller than // Gets the slow speed. Divide SLOW_SPEED by 2 to make it smaller than
// SLOW_SPEED. Speed in MyTracksLocation use MS, but speed in CachedLocation // SLOW_SPEED. Speed in MyTracksLocation use MS, but speed in CachedLocation
// use KMH. // use KMH.
int slowSpeed = (int) (SLOW_SPEED / (2 * UnitConversions.MS_TO_KMH)); int slowSpeed = SLOW_SPEED_MS / 2;
// Gets the normal speed. Makes it smaller than SLOW_SPEED and bigger than // Gets the normal speed. Makes it smaller than SLOW_SPEED and bigger than
// NORMAL_SPEED. Speed in MyTracksLocation use MS, but speed in // NORMAL_SPEED. Speed in MyTracksLocation use MS, but speed in
// CachedLocation use KMH. // CachedLocation use KMH.
int normalSpeed = (int) ((SLOW_SPEED + NORMAL_SPEED) / (2 * UnitConversions.MS_TO_KMH)); int normalSpeed = (SLOW_SPEED_MS + NORMAL_SPEED_MS) / 2;
// Gets the fast speed. Multiply it by 2 to make it bigger than // Gets the fast speed. Multiply it by 2 to make it bigger than
// NORMAL_SPEED. Speed in MyTracksLocation use MS, but speed in // NORMAL_SPEED. Speed in MyTracksLocation use MS, but speed in
// CachedLocation use KMH. // CachedLocation use KMH.
int fastSpeed = (int) (NORMAL_SPEED * 2 / UnitConversions.MS_TO_KMH); int fastSpeed = NORMAL_SPEED_MS * 2;
// Get a number of startLocationIdx. And divide NUMBER_OF_LOCATIONS by 8 to // Get a number of startLocationIdx. And divide NUMBER_OF_LOCATIONS by 8 to
// make sure it is less than numberOfFirstThreeSegments. // make sure it is less than numberOfFirstThreeSegments.
@@ -153,8 +157,8 @@ public class DynamicSpeedTrackPathPainterTest extends TrackPathPainterTestCase {
@UsesMocks(TrackPathDescriptor.class) @UsesMocks(TrackPathDescriptor.class)
private void initialTrackPathDescriptorMock() { private void initialTrackPathDescriptorMock() {
trackPathDescriptor = AndroidMock.createMock(TrackPathDescriptor.class); trackPathDescriptor = AndroidMock.createMock(TrackPathDescriptor.class);
AndroidMock.expect(trackPathDescriptor.getSlowSpeed()).andReturn(SLOW_SPEED); AndroidMock.expect(trackPathDescriptor.getSlowSpeed()).andReturn(SLOW_SPEED_KMH);
AndroidMock.expect(trackPathDescriptor.getNormalSpeed()).andReturn(NORMAL_SPEED); AndroidMock.expect(trackPathDescriptor.getNormalSpeed()).andReturn(NORMAL_SPEED_KMH);
AndroidMock.replay(trackPathDescriptor); AndroidMock.replay(trackPathDescriptor);
} }
@@ -54,6 +54,7 @@ public class SingleColorTrackPathPainterTest extends TrackPathPainterTestCase {
* method when all locations are valid. * method when all locations are valid.
*/ */
public void testUpdatePath_AllValidLocation() { public void testUpdatePath_AllValidLocation() {
pathMock.incReserve(NUMBER_OF_LOCATIONS);
List<CachedLocation> points = createCachedLocations(NUMBER_OF_LOCATIONS, List<CachedLocation> points = createCachedLocations(NUMBER_OF_LOCATIONS,
TrackStubUtils.INITIAL_LATITUDE, -1); TrackStubUtils.INITIAL_LATITUDE, -1);
// Gets a number as the start index of points. // Gets a number as the start index of points.
@@ -75,6 +76,7 @@ public class SingleColorTrackPathPainterTest extends TrackPathPainterTestCase {
* method when all locations are invalid. * method when all locations are invalid.
*/ */
public void testUpdatePath_AllInvalidLocation() { public void testUpdatePath_AllInvalidLocation() {
pathMock.incReserve(NUMBER_OF_LOCATIONS);
List<CachedLocation> points = createCachedLocations(NUMBER_OF_LOCATIONS, INVALID_LATITUDE, -1); List<CachedLocation> points = createCachedLocations(NUMBER_OF_LOCATIONS, INVALID_LATITUDE, -1);
// Gets a random number from 1 to numberOfLocations. // Gets a random number from 1 to numberOfLocations.
int startLocationIdx = NUMBER_OF_LOCATIONS / 2; int startLocationIdx = NUMBER_OF_LOCATIONS / 2;
@@ -101,6 +103,7 @@ public class SingleColorTrackPathPainterTest extends TrackPathPainterTestCase {
points.addAll(createCachedLocations(NUMBER_OF_LOCATIONS, TrackStubUtils.INITIAL_LATITUDE, -1)); points.addAll(createCachedLocations(NUMBER_OF_LOCATIONS, TrackStubUtils.INITIAL_LATITUDE, -1));
// Gets a random number from 1 to numberOfLocations. // Gets a random number from 1 to numberOfLocations.
int startLocationIdx = NUMBER_OF_LOCATIONS / 2; int startLocationIdx = NUMBER_OF_LOCATIONS / 2;
pathMock.incReserve(NUMBER_OF_LOCATIONS *3 + 1 +1);
for (int i = 0; i < NUMBER_OF_LOCATIONS - startLocationIdx; i++) { for (int i = 0; i < NUMBER_OF_LOCATIONS - startLocationIdx; i++) {
pathMock.lineTo(0, 0); pathMock.lineTo(0, 0);
} }