From b3688b043e47bf8a40fc408eff145b6a5c0e0705 Mon Sep 17 00:00:00 2001 From: youtaol Date: Sat, 31 Mar 2012 17:46:11 +0800 Subject: [PATCH] Add test case for SingleColorTrackPathPainter. Move createCachedLocations method and INVALID_LATITUDE field from DynamicSpeedTrackPathPainterTest to TrackPathPainterTestCase to make them can be invoked by both test cases. --- .../maps/SingleColorTrackPathPainter.java | 34 +++-- .../DynamicSpeedTrackPathPainterTest.java | 31 +---- .../maps/SingleColorTrackPathPainterTest.java | 117 ++++++++++++++++++ .../maps/TrackPathPainterTestCase.java | 35 ++++++ 4 files changed, 180 insertions(+), 37 deletions(-) create mode 100644 MyTracksTest/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainterTest.java diff --git a/MyTracks/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainter.java b/MyTracks/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainter.java index c73927cd6..e0e7237d3 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainter.java @@ -19,6 +19,7 @@ import com.google.android.apps.mytracks.MapOverlay.CachedLocation; import com.google.android.maps.GeoPoint; import com.google.android.maps.Projection; import com.google.android.maps.mytracks.R; +import com.google.common.annotations.VisibleForTesting; import android.content.Context; import android.graphics.Canvas; @@ -51,16 +52,31 @@ public class SingleColorTrackPathPainter implements TrackPathPainter { @Override public void updatePath(Projection projection, Rect viewRect, int startLocationIdx, Boolean alwaysVisible, List points) { - // Whether to start a new segment on new valid and visible point. + Path newPath = new Path(); + newPath.incReserve(points.size()); + updatePath(projection, viewRect, startLocationIdx, alwaysVisible, points, newPath); + + } + + /** + * Updates path. + * + * @param projection + * @param viewRect + * @param startLocationIdx + * @param alwaysVisible + * @param points + * @param newPath + */ + @VisibleForTesting + void updatePath(Projection projection, Rect viewRect, int startLocationIdx, + Boolean alwaysVisible, List points, Path newPath) { + // Whether to start a new segment on new valid and visible point. boolean newSegment = startLocationIdx <= 0 || !points.get(startLocationIdx - 1).valid; boolean lastVisible = !newSegment; final Point pt = new Point(); // Loop over track points. - int numPoints = points.size(); - path = newPath(); - path.incReserve(numPoints); - - for (int i = startLocationIdx; i < numPoints ; ++i) { + for (int i = startLocationIdx; i < points.size() ; ++i) { CachedLocation loc = points.get(i); // Check if valid, if not then indicate a new segment. @@ -82,12 +98,14 @@ public class SingleColorTrackPathPainter implements TrackPathPainter { // Either move to beginning of a new segment or continue the old one. projection.toPixels(geoPoint, pt); if (newSegment) { - path.moveTo(pt.x, pt.y); + newPath.moveTo(pt.x, pt.y); newSegment = false; } else { - path.lineTo(pt.x, pt.y); + newPath.lineTo(pt.x, pt.y); } } + + path = newPath; } @Override diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathPainterTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathPainterTest.java index c822f74cc..61cd52d09 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathPainterTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathPainterTest.java @@ -16,7 +16,6 @@ package com.google.android.apps.mytracks.maps; import com.google.android.apps.mytracks.ColoredPath; -import com.google.android.apps.mytracks.MapOverlay; import com.google.android.apps.mytracks.MapOverlay.CachedLocation; import com.google.android.apps.mytracks.TrackStubUtils; import com.google.android.apps.mytracks.util.UnitConversions; @@ -26,7 +25,6 @@ import com.google.android.testing.mocking.UsesMocks; import android.location.Location; -import java.util.ArrayList; import java.util.List; /** @@ -37,7 +35,6 @@ import java.util.List; public class DynamicSpeedTrackPathPainterTest extends TrackPathPainterTestCase { private DynamicSpeedTrackPathPainter dynamicSpeedTrackPathPainter; private TrackPathDescriptor trackPathDescriptor; - private static final int INVALID_LATITUDE = 100; private static final int NUMBER_OF_LOCATIONS = 100; private static final int SLOW_SPEED = 30; private static final int NORMAL_SPEED = 50; @@ -99,8 +96,7 @@ public class DynamicSpeedTrackPathPainterTest extends TrackPathPainterTestCase { // Gets the fast speed for location. int fastSpeed = (int) (NORMAL_SPEED * 3 / UnitConversions.MS_TO_KMH); - // Get a number of startLocationIdx. And makes sure is less than - // numberOfFirstThreeSegments. + // Get a number of startLocationIdx. And makes sure is less than numberOfFirstThreeSegments. int startLocationIdx = NUMBER_OF_LOCATIONS / 8; int numberOfFirstThreeSegments = NUMBER_OF_LOCATIONS / 4; int numberOfLastSegment = NUMBER_OF_LOCATIONS - numberOfFirstThreeSegments * 3; @@ -151,28 +147,5 @@ public class DynamicSpeedTrackPathPainterTest extends TrackPathPainterTestCase { trackPathDescriptor); } - /** - * Creates a list of CachedLocations. - * - * @param number the number of locations - * @param isValid flag whether creates valid locations - * @param speed the speed of locations, and will be default value if less than - * zero - * @return the simulated locations - */ - private List createCachedLocations(int number, boolean isValid, float speed) { - List points = new ArrayList(); - double latitude = TrackStubUtils.INITIAL_LATITUDE; - if (!isValid) latitude = INVALID_LATITUDE; - for (int i = 0; i < number; ++i) { - Location location = TrackStubUtils.createMyTracksLocation(latitude, - TrackStubUtils.INITIAL_LONGITUDE, TrackStubUtils.INITIAL_ALTITUDE); - if (speed > 0) { - location.setSpeed(speed); - } - CachedLocation cachedLocation = new CachedLocation(location); - points.add(cachedLocation); - } - return points; - } + } diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainterTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainterTest.java new file mode 100644 index 000000000..d86fec916 --- /dev/null +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainterTest.java @@ -0,0 +1,117 @@ +/* + * 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.MapOverlay.CachedLocation; +import com.google.android.testing.mocking.AndroidMock; +import com.google.android.testing.mocking.UsesMocks; + +import android.graphics.Path; + +import java.util.List; + +/** + * Tests for the {@link SingleColorTrackPathPainter}. + * + * @author Youtao Liu + */ +public class SingleColorTrackPathPainterTest extends TrackPathPainterTestCase { + private SingleColorTrackPathPainter singleColorTrackPathPainter; + private Path pathMock; + private static final int NUMBER_OF_LOCATIONS = 100; + + /** + * Tests the + * {@link SingleColorTrackPathPainter#updatePath(com.google.android.maps.Projection, android.graphics.Rect, int, Boolean, List, Path)} + * method when all locations are valid. + */ + public void testUpdatePath_AllValidLocation() { + initialTrackPathDescriptorMock(); + List points = createCachedLocations(NUMBER_OF_LOCATIONS, true, -1); + // Gets a random number from 1 to numberOfLocations. + int startLocationIdx = (int) (1 + (NUMBER_OF_LOCATIONS - 1) * Math.random()); + + for (int i = 0; i < NUMBER_OF_LOCATIONS - startLocationIdx; i++) { + pathMock.lineTo(0, 0); + } + + AndroidMock.replay(pathMock); + singleColorTrackPathPainter.updatePath(myTracksOverlay.getMapProjection(mockView), + myTracksOverlay.getMapViewRect(mockView), startLocationIdx, true, points, pathMock); + AndroidMock.verify(pathMock); + } + + /** + * Tests the + * {@link SingleColorTrackPathPainter#updatePath(com.google.android.maps.Projection, android.graphics.Rect, int, Boolean, List, Path)} + * method when all locations are invalid. + */ + public void testUpdatePath_AllInvalidLocation() { + initialTrackPathDescriptorMock(); + List points = createCachedLocations(NUMBER_OF_LOCATIONS, false, -1); + // Gets a random number from 1 to numberOfLocations. + int startLocationIdx = (int) (1 + (NUMBER_OF_LOCATIONS - 1) * Math.random()); + AndroidMock.replay(pathMock); + singleColorTrackPathPainter.updatePath(myTracksOverlay.getMapProjection(mockView), + myTracksOverlay.getMapViewRect(mockView), startLocationIdx, true, points, pathMock); + AndroidMock.verify(pathMock); + } + + /** + * Tests the + * {@link SingleColorTrackPathPainter#updatePath(com.google.android.maps.Projection, android.graphics.Rect, int, Boolean, List, Path)} + * method when there are three segment. + */ + public void testUpdatePath_ThreeSegments() { + initialTrackPathDescriptorMock(); + // First segment. + List points = createCachedLocations(NUMBER_OF_LOCATIONS, true, -1); + points.addAll(createCachedLocations(1, false, -1)); + // Second segment. + points.addAll(createCachedLocations(NUMBER_OF_LOCATIONS, true, -1)); + points.addAll(createCachedLocations(1, false, -1)); + // Third segment. + points.addAll(createCachedLocations(NUMBER_OF_LOCATIONS, true, -1)); + // Gets a random number from 1 to numberOfLocations. + int startLocationIdx = (int) (1 + (NUMBER_OF_LOCATIONS - 1) * Math.random()); + for (int i = 0; i < NUMBER_OF_LOCATIONS - startLocationIdx; i++) { + pathMock.lineTo(0, 0); + } + pathMock.moveTo(0, 0); + for (int i = 0; i < NUMBER_OF_LOCATIONS - 1; i++) { + pathMock.lineTo(0, 0); + } + pathMock.moveTo(0, 0); + for (int i = 0; i < NUMBER_OF_LOCATIONS - 1; i++) { + pathMock.lineTo(0, 0); + } + + AndroidMock.replay(pathMock); + singleColorTrackPathPainter.updatePath(myTracksOverlay.getMapProjection(mockView), + myTracksOverlay.getMapViewRect(mockView), startLocationIdx, true, points, pathMock); + AndroidMock.verify(pathMock); + } + + /** + * Initials a mocked TrackPathDescriptor object. + */ + @UsesMocks(Path.class) + private void initialTrackPathDescriptorMock() { + pathMock = AndroidMock.createStrictMock(Path.class); + singleColorTrackPathPainter = new SingleColorTrackPathPainter(getContext()); + } + +} diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterTestCase.java b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterTestCase.java index 59ceb94fd..763c89a9b 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterTestCase.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterTestCase.java @@ -15,12 +15,19 @@ */ package com.google.android.apps.mytracks.maps; +import com.google.android.apps.mytracks.MapOverlay; +import com.google.android.apps.mytracks.MapOverlay.CachedLocation; import com.google.android.apps.mytracks.MockMyTracksOverlay; +import com.google.android.apps.mytracks.TrackStubUtils; import com.google.android.maps.MapView; import android.graphics.Canvas; +import android.location.Location; import android.test.AndroidTestCase; +import java.util.ArrayList; +import java.util.List; + /** * Tests for the MyTracks track path descriptors and painters. * @@ -30,6 +37,8 @@ public class TrackPathPainterTestCase extends AndroidTestCase { protected Canvas canvas; protected MockMyTracksOverlay myTracksOverlay; protected MapView mockView; + + final int INVALID_LATITUDE = 100; @Override protected void setUp() throws Exception { @@ -40,4 +49,30 @@ public class TrackPathPainterTestCase extends AndroidTestCase { myTracksOverlay.setTrackDrawingEnabled(true); mockView = null; } + + /** + * Creates a list of CachedLocations. + * + * @param number the number of locations + * @param isValid flag whether creates valid locations + * @param speed the speed of locations, and will give a default valid value if less than + * zero + * @return the simulated locations + * @author Youtao Liu. + */ + List createCachedLocations(int number, boolean isValid, float speed) { + List points = new ArrayList(); + double latitude = TrackStubUtils.INITIAL_LATITUDE; + if (!isValid) latitude = INVALID_LATITUDE; + for (int i = 0; i < number; ++i) { + Location location = TrackStubUtils.createMyTracksLocation(latitude, + TrackStubUtils.INITIAL_LONGITUDE, TrackStubUtils.INITIAL_ALTITUDE); + if (speed > 0) { + location.setSpeed(speed); + } + CachedLocation cachedLocation = new CachedLocation(location); + points.add(cachedLocation); + } + return points; + } }