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.

This commit is contained in:
youtaol
2012-03-31 17:46:11 +08:00
parent 314c3a76a6
commit b3688b043e
4 changed files with 180 additions and 37 deletions
@@ -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<CachedLocation> createCachedLocations(int number, boolean isValid, float speed) {
List<CachedLocation> points = new ArrayList<MapOverlay.CachedLocation>();
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;
}
}
@@ -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<CachedLocation> 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<CachedLocation> 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<CachedLocation> 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());
}
}
@@ -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<CachedLocation> createCachedLocations(int number, boolean isValid, float speed) {
List<CachedLocation> points = new ArrayList<MapOverlay.CachedLocation>();
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;
}
}