From 68f50ef6de60616de2354bd391410ec8a1604a5b Mon Sep 17 00:00:00 2001 From: youtaol Date: Thu, 5 Apr 2012 14:25:30 +0800 Subject: [PATCH] Change the code style of DynamicSpeedTrackPathPainterTest,SingleColorTrackPathPainterTest, SingleColorTrackPathPainter and TrackPathPainterTestCase. --- .../maps/SingleColorTrackPathPainter.java | 56 +++++---- .../DynamicSpeedTrackPathPainterTest.java | 112 ++++++++++-------- .../maps/SingleColorTrackPathPainterTest.java | 52 ++++---- .../maps/TrackPathPainterTestCase.java | 16 ++- 4 files changed, 122 insertions(+), 114 deletions(-) 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 e0e7237d3..cd9ee85a7 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainter.java @@ -30,16 +30,15 @@ import android.graphics.Rect; import java.util.List; - /** * A path painter that not variates the path colors. - * - * @author Vangelis S. + * + * @author Vangelis S. */ public class SingleColorTrackPathPainter implements TrackPathPainter { private final Paint selectedTrackPaint; private Path path; - + public SingleColorTrackPathPainter(Context context) { selectedTrackPaint = TrackPathUtilities.getPaint(R.color.red, context); } @@ -48,53 +47,53 @@ public class SingleColorTrackPathPainter implements TrackPathPainter { public void drawTrack(Canvas canvas) { canvas.drawPath(path, selectedTrackPaint); } - + @Override public void updatePath(Projection projection, Rect viewRect, int startLocationIdx, Boolean alwaysVisible, List points) { Path newPath = new Path(); newPath.incReserve(points.size()); - updatePath(projection, viewRect, startLocationIdx, alwaysVisible, points, newPath); - + path = updatePath(projection, viewRect, startLocationIdx, alwaysVisible, points, newPath); + } - + /** - * Updates path. + * Updates the path. * - * @param projection - * @param viewRect - * @param startLocationIdx - * @param alwaysVisible - * @param points - * @param newPath + * @param projection The Canvas to draw upon. + * @param viewRect The Path to be drawn. + * @param startLocationIdx The start point from where update the path. + * @param alwaysVisible Flag for always visible. + * @param points The list of points used to update the path. + * @param newPath The path to be created. */ @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; + Path 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. - for (int i = startLocationIdx; i < points.size() ; ++i) { + for (int i = startLocationIdx; i < points.size(); ++i) { CachedLocation loc = points.get(i); - + // Check if valid, if not then indicate a new segment. if (!loc.valid) { newSegment = true; continue; } - + final GeoPoint geoPoint = loc.geoPoint; // Check if this breaks the existing segment. - boolean visible = alwaysVisible + boolean visible = alwaysVisible || viewRect.contains(geoPoint.getLongitudeE6(), geoPoint.getLatitudeE6()); if (!visible && !lastVisible) { // This is a point outside view not connected to a visible one. newSegment = true; } lastVisible = visible; - + // Either move to beginning of a new segment or continue the old one. projection.toPixels(geoPoint, pt); if (newSegment) { @@ -104,10 +103,9 @@ public class SingleColorTrackPathPainter implements TrackPathPainter { newPath.lineTo(pt.x, pt.y); } } - - path = newPath; + return newPath; } - + @Override public void clear() { path = null; @@ -117,12 +115,12 @@ public class SingleColorTrackPathPainter implements TrackPathPainter { public boolean needsRedraw() { return false; } - + @Override public Path getLastPath() { return path; } - + // Visible for testing public Path newPath() { return new Path(); 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 61cd52d09..d6044e59f 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathPainterTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathPainterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Google Inc. + * 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 @@ -23,8 +23,6 @@ import com.google.android.maps.mytracks.R; import com.google.android.testing.mocking.AndroidMock; import com.google.android.testing.mocking.UsesMocks; -import android.location.Location; - import java.util.List; /** @@ -33,32 +31,38 @@ import java.util.List; * @author Youtao Liu */ public class DynamicSpeedTrackPathPainterTest extends TrackPathPainterTestCase { + private DynamicSpeedTrackPathPainter dynamicSpeedTrackPathPainter; private TrackPathDescriptor trackPathDescriptor; + // This number must bigger than 10 to meets the requirement of test. private static final int NUMBER_OF_LOCATIONS = 100; + private static final int NUMBER_OF_SEGMENTS = 4; + private static final int LOCATIONS_PER_SEGMENT = 25; + // The maximum speed which is considered slow. private static final int SLOW_SPEED = 30; + // The maximum speed which is considered normal. private static final int NORMAL_SPEED = 50; + @Override + protected void setUp() throws Exception { + super.setUp(); + initialTrackPathDescriptorMock(); + dynamicSpeedTrackPathPainter = new DynamicSpeedTrackPathPainter(getContext(), + trackPathDescriptor); + } + /** * Tests the method * {@link DynamicSpeedTrackPathPainter#updatePath(com.google.android.maps.Projection, android.graphics.Rect, int, Boolean, java.util.List)} * when all locations are invalid. */ public void testUpdatePath_AllInvalidLocation() { - initialTrackPathDescriptorMock(); - List points = createCachedLocations(NUMBER_OF_LOCATIONS, false, -1); - for (int i = 0; i < 100; ++i) { - Location location = TrackStubUtils.createMyTracksLocation(INVALID_LATITUDE, - TrackStubUtils.INITIAL_LONGITUDE, TrackStubUtils.INITIAL_ALTITUDE); - CachedLocation cachedLocation = new CachedLocation(location); - points.add(cachedLocation); - } + List points = createCachedLocations(NUMBER_OF_LOCATIONS, INVALID_LATITUDE, -1); dynamicSpeedTrackPathPainter.updatePath(myTracksOverlay.getMapProjection(mockView), myTracksOverlay.getMapViewRect(mockView), 1, true, points); AndroidMock.verify(trackPathDescriptor); // Should be zero for there is no valid locations. assertEquals(0, dynamicSpeedTrackPathPainter.getColoredPaths().size()); - } /** @@ -67,44 +71,50 @@ public class DynamicSpeedTrackPathPainterTest extends TrackPathPainterTestCase { * 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()); + List points = createCachedLocations(NUMBER_OF_LOCATIONS, + TrackStubUtils.INITIAL_LATITUDE, -1); + // Gets a number as the start index of points. + int startLocationIdx = NUMBER_OF_LOCATIONS / 2; dynamicSpeedTrackPathPainter.updatePath(myTracksOverlay.getMapProjection(mockView), myTracksOverlay.getMapViewRect(mockView), startLocationIdx, true, points); AndroidMock.verify(trackPathDescriptor); assertEquals(NUMBER_OF_LOCATIONS - startLocationIdx, dynamicSpeedTrackPathPainter .getColoredPaths().size()); - } /** * Tests the * {@link DynamicSpeedTrackPathPainter#updatePath(com.google.android.maps.Projection, android.graphics.Rect, int, Boolean, java.util.List)} - * when all locations are valid. + * when all locations are valid. This test setups 4 segments with 25 points + * each. The first segment has slow speed, the second segment has normal + * speed, the third segment has fast speed, and the fourth segment has slow + * speed. */ public void testUpdatePath_CheckColoredPath() { - initialTrackPathDescriptorMock(); - // Gets the slow speed for location. + // Gets the slow speed. Divide SLOW_SPEED by 2 to make it smaller than + // SLOW_SPEED. int slowSpeed = (int) (SLOW_SPEED / (2 * UnitConversions.MS_TO_KMH)); - // Gets the normal speed for location. + // Gets the normal speed. Makes it smaller than SLOW_SPEED and bigger than + // NORMAL_SPEED. int normalSpeed = (int) ((SLOW_SPEED + NORMAL_SPEED) / (2 * UnitConversions.MS_TO_KMH)); - // Gets the fast speed for location. - int fastSpeed = (int) (NORMAL_SPEED * 3 / UnitConversions.MS_TO_KMH); + // Gets the fast speed. Multiply it by 2 to make it bigger than + // NORMAL_SPEED. + int fastSpeed = (int) (NORMAL_SPEED * 2 / UnitConversions.MS_TO_KMH); - // 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; + // Get a number of startLocationIdx. And divide NUMBER_OF_LOCATIONS by 8 to + // make sure it is less than numberOfFirstThreeSegments. + int startLocationIdx = NUMBER_OF_LOCATIONS / NUMBER_OF_SEGMENTS / 2; - List points = createCachedLocations(numberOfFirstThreeSegments, true, slowSpeed); - points.addAll(createCachedLocations(numberOfFirstThreeSegments, true, normalSpeed)); - points.addAll(createCachedLocations(numberOfFirstThreeSegments, true, fastSpeed)); - points.addAll(createCachedLocations(numberOfLastSegment, true, slowSpeed)); + List points = createCachedLocations(LOCATIONS_PER_SEGMENT, + TrackStubUtils.INITIAL_LATITUDE, slowSpeed); + points.addAll(createCachedLocations(LOCATIONS_PER_SEGMENT, TrackStubUtils.INITIAL_LATITUDE, + normalSpeed)); + points.addAll(createCachedLocations(LOCATIONS_PER_SEGMENT, TrackStubUtils.INITIAL_LATITUDE, + fastSpeed)); + points.addAll(createCachedLocations(LOCATIONS_PER_SEGMENT, TrackStubUtils.INITIAL_LATITUDE, + slowSpeed)); dynamicSpeedTrackPathPainter.updatePath(myTracksOverlay.getMapProjection(mockView), myTracksOverlay.getMapViewRect(mockView), startLocationIdx, true, points); @@ -114,22 +124,24 @@ public class DynamicSpeedTrackPathPainterTest extends TrackPathPainterTestCase { assertEquals(NUMBER_OF_LOCATIONS - startLocationIdx, coloredPath.size()); // Checks different speeds with different color in the coloredPath. - int i = 0; - for (; i < numberOfFirstThreeSegments - startLocationIdx; i++) { - assertEquals(getContext().getResources().getColor(R.color.slow_path), coloredPath.get(i) - .getPathPaint().getColor()); - } - for (; i < numberOfFirstThreeSegments; i++) { - assertEquals(getContext().getResources().getColor(R.color.normal_path), coloredPath.get(i) - .getPathPaint().getColor()); - } - for (; i < numberOfFirstThreeSegments; i++) { - assertEquals(getContext().getResources().getColor(R.color.fast_path), coloredPath.get(i) - .getPathPaint().getColor()); - } - for (; i < numberOfLastSegment; i++) { - assertEquals(getContext().getResources().getColor(R.color.slow_path), coloredPath.get(i) - .getPathPaint().getColor()); + for (int i = 0; i < NUMBER_OF_LOCATIONS - startLocationIdx; i++) { + if (i < LOCATIONS_PER_SEGMENT - startLocationIdx) { + // Slow. + assertEquals(getContext().getResources().getColor(R.color.slow_path), coloredPath.get(i) + .getPathPaint().getColor()); + } else if (i < LOCATIONS_PER_SEGMENT * 2 - startLocationIdx) { + // Normal. + assertEquals(getContext().getResources().getColor(R.color.normal_path), coloredPath.get(i) + .getPathPaint().getColor()); + } else if (i < LOCATIONS_PER_SEGMENT * 3 - startLocationIdx) { + // Fast. + assertEquals(getContext().getResources().getColor(R.color.fast_path), coloredPath.get(i) + .getPathPaint().getColor()); + } else { + // Slow. + assertEquals(getContext().getResources().getColor(R.color.slow_path), coloredPath.get(i) + .getPathPaint().getColor()); + } } } @@ -142,10 +154,6 @@ public class DynamicSpeedTrackPathPainterTest extends TrackPathPainterTestCase { AndroidMock.expect(trackPathDescriptor.getSlowSpeed()).andReturn(SLOW_SPEED); AndroidMock.expect(trackPathDescriptor.getNormalSpeed()).andReturn(NORMAL_SPEED); AndroidMock.replay(trackPathDescriptor); - - dynamicSpeedTrackPathPainter = new DynamicSpeedTrackPathPainter(getContext(), - trackPathDescriptor); } - } diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainterTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainterTest.java index d86fec916..3654cf396 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainterTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/SingleColorTrackPathPainterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Google Inc. + * 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 @@ -16,6 +16,7 @@ package com.google.android.apps.mytracks.maps; import com.google.android.apps.mytracks.MapOverlay.CachedLocation; +import com.google.android.apps.mytracks.TrackStubUtils; import com.google.android.testing.mocking.AndroidMock; import com.google.android.testing.mocking.UsesMocks; @@ -29,20 +30,34 @@ import java.util.List; * @author Youtao Liu */ public class SingleColorTrackPathPainterTest extends TrackPathPainterTestCase { + private SingleColorTrackPathPainter singleColorTrackPathPainter; private Path pathMock; private static final int NUMBER_OF_LOCATIONS = 100; + /** + * Initials a mocked TrackPathDescriptor object and + * singleColorTrackPathPainter. + */ + @Override + @UsesMocks(Path.class) + protected void setUp() throws Exception { + super.setUp(); + + pathMock = AndroidMock.createStrictMock(Path.class); + singleColorTrackPathPainter = new SingleColorTrackPathPainter(getContext()); + } + /** * 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()); + List points = createCachedLocations(NUMBER_OF_LOCATIONS, + TrackStubUtils.INITIAL_LATITUDE, -1); + // Gets a number as the start index of points. + int startLocationIdx = NUMBER_OF_LOCATIONS / 2; for (int i = 0; i < NUMBER_OF_LOCATIONS - startLocationIdx; i++) { pathMock.lineTo(0, 0); @@ -60,8 +75,7 @@ public class SingleColorTrackPathPainterTest extends TrackPathPainterTestCase { * method when all locations are invalid. */ public void testUpdatePath_AllInvalidLocation() { - initialTrackPathDescriptorMock(); - List points = createCachedLocations(NUMBER_OF_LOCATIONS, false, -1); + List points = createCachedLocations(NUMBER_OF_LOCATIONS, INVALID_LATITUDE, -1); // Gets a random number from 1 to numberOfLocations. int startLocationIdx = (int) (1 + (NUMBER_OF_LOCATIONS - 1) * Math.random()); AndroidMock.replay(pathMock); @@ -73,18 +87,18 @@ public class SingleColorTrackPathPainterTest extends TrackPathPainterTestCase { /** * Tests the * {@link SingleColorTrackPathPainter#updatePath(com.google.android.maps.Projection, android.graphics.Rect, int, Boolean, List, Path)} - * method when there are three segment. + * method when there are three segments. */ public void testUpdatePath_ThreeSegments() { - initialTrackPathDescriptorMock(); // First segment. - List points = createCachedLocations(NUMBER_OF_LOCATIONS, true, -1); - points.addAll(createCachedLocations(1, false, -1)); + List points = createCachedLocations(NUMBER_OF_LOCATIONS, + TrackStubUtils.INITIAL_LATITUDE, -1); + points.addAll(createCachedLocations(1, INVALID_LATITUDE, -1)); // Second segment. - points.addAll(createCachedLocations(NUMBER_OF_LOCATIONS, true, -1)); - points.addAll(createCachedLocations(1, false, -1)); + points.addAll(createCachedLocations(NUMBER_OF_LOCATIONS, TrackStubUtils.INITIAL_LATITUDE, -1)); + points.addAll(createCachedLocations(1, INVALID_LATITUDE, -1)); // Third segment. - points.addAll(createCachedLocations(NUMBER_OF_LOCATIONS, true, -1)); + points.addAll(createCachedLocations(NUMBER_OF_LOCATIONS, TrackStubUtils.INITIAL_LATITUDE, -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++) { @@ -104,14 +118,4 @@ public class SingleColorTrackPathPainterTest extends TrackPathPainterTestCase { 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 763c89a9b..3069e7cf6 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterTestCase.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/TrackPathPainterTestCase.java @@ -34,10 +34,11 @@ import java.util.List; * @author Vangelis S. */ public class TrackPathPainterTestCase extends AndroidTestCase { + protected Canvas canvas; protected MockMyTracksOverlay myTracksOverlay; protected MapView mockView; - + final int INVALID_LATITUDE = 100; @Override @@ -49,21 +50,18 @@ 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 + * @param latitude the latitude value of 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 createCachedLocations(int number, double latitude, 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);