Add unit test cases about MyTracksProviderUtilsImpl. Change some code style of MyTracksProviderUtilsImplTest.

This commit is contained in:
youtaol
2012-07-24 18:01:51 +08:00
parent b1116ff90b
commit 737045bdde
@@ -33,6 +33,7 @@ import android.test.mock.MockContentResolver;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;
/**
@@ -44,13 +45,14 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
private Context context;
private MyTracksProviderUtils providerUtils;
private static final String tracksNamePrefix = "test name";
private static final String tracksCategory = "test category";
private static final String NAME_PREFIX = "test name";
private static final String TRACK_CATEGORY = "test category";
private static final String MOCK_DESC = "Mock Next Waypoint Desc!";
private static final String TEST_DESC = "Test Desc!";
private static final String TEST_DESC_NEW = "Test Desc new!";
@Override
protected void setUp() throws Exception {
super.setUp();
MockContentResolver mockContentResolver = new MockContentResolver();
@@ -63,6 +65,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
setContext(context);
providerUtils = MyTracksProviderUtils.Factory.get(context);
providerUtils.deleteAllTracks();
}
public void testLocationIterator_noPoints() {
@@ -186,7 +189,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
public void testGetAllTracks() {
int initialTrackNumber = providerUtils.getAllTracks().size();
long trackId = System.currentTimeMillis();
providerUtils.insertTrack(simulaTrack(trackId, 0));
providerUtils.insertTrack(getTrack(trackId, 0));
List<Track> allTracks = providerUtils.getAllTracks();
assertEquals(initialTrackNumber + 1, allTracks.size());
assertEquals(trackId, allTracks.get(allTracks.size() - 1).getId());
@@ -197,7 +200,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
*/
public void testGetTrack() {
long trackId = System.currentTimeMillis();
providerUtils.insertTrack(simulaTrack(trackId, 0));
providerUtils.insertTrack(getTrack(trackId, 0));
assertNotNull(providerUtils.getTrack(trackId));
}
@@ -206,7 +209,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
*/
public void testGetLastTrack() {
long trackId = System.currentTimeMillis();
providerUtils.insertTrack(simulaTrack(trackId, 0));
providerUtils.insertTrack(getTrack(trackId, 0));
assertEquals(trackId, providerUtils.getLastTrack().getId());
}
@@ -215,7 +218,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
*/
public void testGetLastTrackId() {
long trackId = System.currentTimeMillis();
providerUtils.insertTrack(simulaTrack(trackId, 0));
providerUtils.insertTrack(getTrack(trackId, 0));
assertEquals(trackId, providerUtils.getLastTrackId());
}
@@ -224,16 +227,16 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
*/
public void testTrackExists() {
long trackId = System.currentTimeMillis();
providerUtils.insertTrack(simulaTrack(trackId, 0));
providerUtils.insertTrack(getTrack(trackId, 0));
assertTrue(providerUtils.trackExists(trackId));
}
/**
* Tests the method {@link MyTracksProviderUtilsImpl#updateTrack(Track)}
*/
public void testUpdateTrack() {
long trackId = System.currentTimeMillis();
Track track = simulaTrack(trackId, 0);
Track track = getTrack(trackId, 0);
String nameOld = "name1";
String nameNew = "name2";
track.setName(nameOld);
@@ -248,24 +251,22 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
* Tests the method {@link MyTracksProviderUtilsImpl#deleteAllTracks()}
*/
public void testDeleteAllTracks() {
// Insert track, point and way point at first.
// Insert track, points and waypoint at first.
long trackId = System.currentTimeMillis();
Track track = simulaTrack(trackId, 10);
providerUtils.insertTrack(track);
providerUtils.bulkInsertTrackPoints(track.getLocations().toArray(new Location[0]), track
.getLocations().size(), trackId);
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
Waypoint waypoint = new Waypoint();
providerUtils.insertWaypoint(waypoint );
providerUtils.insertWaypoint(waypoint);
ContentResolver contentResolver = context.getContentResolver();
Cursor tracksCursor = contentResolver.query(TracksColumns.CONTENT_URI, null, null, null,
TracksColumns._ID);
assertTrue(tracksCursor.getCount() > 0);
assertEquals(1, tracksCursor.getCount());
Cursor tracksPointsCursor = contentResolver.query(TrackPointsColumns.CONTENT_URI, null, null,
null, TrackPointsColumns._ID);
assertTrue(tracksPointsCursor.getCount() > 0);
Cursor waypointCursor = contentResolver.query(TrackPointsColumns.CONTENT_URI, null, null,
assertEquals(10, tracksPointsCursor.getCount());
Cursor waypointCursor = contentResolver.query(WaypointsColumns.CONTENT_URI, null, null,
null, WaypointsColumns._ID);
assertTrue(waypointCursor.getCount() > 0);
assertEquals(1, waypointCursor.getCount());
// Delete all.
providerUtils.deleteAllTracks();
// Check whether all have been deleted.
@@ -284,21 +285,13 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
* Tests the method {@link MyTracksProviderUtilsImpl#deleteTrack(long)}.
*/
public void testDeleteTrack() {
// Delete all data at first.
providerUtils.deleteAllTracks();
//assertEquals(0, waypointCursor.getCount());
// Insert three tracks, points of two tracks and way point of one track.
long trackId = System.currentTimeMillis();
Track track = simulaTrack(trackId, 10);
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
providerUtils.insertTrack(simulaTrack(trackId + 1, 10));
providerUtils.insertTrack(simulaTrack(trackId + 2, 10));
providerUtils.bulkInsertTrackPoints(track.getLocations().toArray(new Location[0]), track
.getLocations().size(), trackId);
providerUtils.bulkInsertTrackPoints(track.getLocations().toArray(new Location[0]), track
.getLocations().size(), trackId + 1);
providerUtils.insertTrack(track);
insertTrackWithLocations(getTrack(trackId + 1, 10));
insertTrackWithLocations(getTrack(trackId + 2, 10));
Waypoint waypoint = new Waypoint();
waypoint.setTrackId(trackId);
@@ -334,23 +327,26 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
@UsesMocks(Cursor.class)
public void testCreateTrack() {
Cursor cursorMock = AndroidMock.createNiceMock(Cursor.class);
int startIndex = 2;
int index = startIndex;
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(TracksColumns._ID)).andReturn(index++);
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(TracksColumns.NAME)).andReturn(index++);
index = startIndex;
// The startColumnIndex can be any value, just make it to 2 in case it's
// hard coded to 0 or 1 in some where.
int startColumnIndex = 2;
int columnIndex = startColumnIndex;
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(TracksColumns._ID))
.andReturn(columnIndex++);
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(TracksColumns.NAME)).andReturn(
columnIndex++);
columnIndex = startColumnIndex;
// Id
AndroidMock.expect(cursorMock.isNull(index++)).andReturn(false);
System.out.println("index:"+index);
AndroidMock.expect(cursorMock.isNull(columnIndex++)).andReturn(false);
// Name
AndroidMock.expect(cursorMock.isNull(index++)).andReturn(false);
AndroidMock.expect(cursorMock.isNull(columnIndex++)).andReturn(false);
long trackId = System.currentTimeMillis();
index = startIndex;
columnIndex = startColumnIndex;
// Id
AndroidMock.expect(cursorMock.getLong(index++)).andReturn(trackId);
AndroidMock.expect(cursorMock.getLong(columnIndex++)).andReturn(trackId);
// Name
String name = tracksNamePrefix + Long.toString(trackId);
AndroidMock.expect(cursorMock.getString(index++)).andReturn(name);
String name = NAME_PREFIX + Long.toString(trackId);
AndroidMock.expect(cursorMock.getString(columnIndex++)).andReturn(name);
AndroidMock.replay(cursorMock);
Track track = providerUtils.createTrack(cursorMock);
assertEquals(trackId, track.getId());
@@ -362,14 +358,14 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
* Tests the method {@link MyTracksProviderUtilsImpl#createContentValues(Track)}.
*/
@UsesMocks(TripStatistics.class)
public void testCreateContentValues() {
public void testCreateContentValues_Track() {
// ID
long trackId = System.currentTimeMillis();
// Name
String name = tracksNamePrefix + Long.toString(trackId);
Track track = simulaTrack(trackId, 10);
String name = NAME_PREFIX + Long.toString(trackId);
Track track = getTrack(trackId, 10);
track.setName(name);
track.setCategory(tracksCategory);
track.setCategory(TRACK_CATEGORY);
TripStatistics tripStatistics = AndroidMock.createNiceMock(TripStatistics.class);
// Bottom
int bottom = 22;
@@ -393,7 +389,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
*/
public void testGetFirstWaypoint() {
long trackId = System.currentTimeMillis();
Track track = simulaTrack(trackId, 10);
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
Waypoint waypoint1 = new Waypoint();
@@ -415,14 +411,12 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
*/
public void testGetFirstWaypointId() {
long trackId = System.currentTimeMillis();
Track track = simulaTrack(trackId, 10);
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
Waypoint waypoint1 = new Waypoint();
waypoint1.setId(11L);
waypoint1.setTrackId(trackId);
Waypoint waypoint2 = new Waypoint();
waypoint2.setId(22L);
waypoint2.setTrackId(trackId);
providerUtils.insertWaypoint(waypoint1);
providerUtils.insertWaypoint(waypoint2);
@@ -436,17 +430,14 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
*/
public void testGetKLastWaypointId() {
long trackId = System.currentTimeMillis();
Track track = simulaTrack(trackId, 10);
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
Waypoint waypoint1 = new Waypoint();
waypoint1.setId(11L);
waypoint1.setTrackId(trackId);
Waypoint waypoint2 = new Waypoint();
waypoint2.setId(22L);
waypoint2.setTrackId(trackId);
Waypoint waypoint3 = new Waypoint();
waypoint3.setId(33L);
waypoint3.setTrackId(trackId);
providerUtils.insertWaypoint(waypoint1);
providerUtils.insertWaypoint(waypoint2);
@@ -461,7 +452,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
*/
public void testGetNextMarkerNumber() {
long trackId = System.currentTimeMillis();
Track track = simulaTrack(trackId, 10);
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
Waypoint waypoint1 = new Waypoint();
@@ -490,7 +481,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
*/
public void testGetNextStatisticsWaypointAfter() {
long trackId = System.currentTimeMillis();
Track track = simulaTrack(trackId, 10);
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
Waypoint waypoint1 = new Waypoint();
@@ -521,15 +512,463 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
*/
public void testInsertAndGetWaypoint() {
long trackId = System.currentTimeMillis();
Track track = simulaTrack(trackId, 10);
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
Waypoint waypoint = new Waypoint();
waypoint.setDescription(Long.toString(trackId));
waypoint.setDescription(TEST_DESC);
waypoint.setTrackId(trackId);
providerUtils.insertWaypoint(waypoint);
assertEquals(Long.toString(trackId), providerUtils.getWaypoint(1).getDescription());
assertEquals(TEST_DESC, providerUtils.getWaypoint(1).getDescription());
}
/**
* Tests the method {@link MyTracksProviderUtilsImpl#updateWaypoint(Waypoint)}.
*/
public void testUpdateWaypoint() {
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
// Insert at first.
Waypoint waypoint = new Waypoint();
waypoint.setDescription(TEST_DESC);
waypoint.setTrackId(trackId);
providerUtils.insertWaypoint(waypoint);
// Update
waypoint = providerUtils.getWaypoint(1);
waypoint.setDescription(TEST_DESC_NEW);
providerUtils.updateWaypoint(waypoint);
assertEquals(TEST_DESC_NEW, providerUtils.getWaypoint(1).getDescription());
}
/**
* Tests the method
* {@link MyTracksProviderUtilsImpl#deleteWaypoint(long, DescriptionGenerator)}
* when there is only one waypoint in the track.
*/
public void testDeleteWaypoint_onlyOneWayPoint() {
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
// Insert at first.
Waypoint waypoint1 = new Waypoint();
waypoint1.setDescription(TEST_DESC);
waypoint1.setTrackId(trackId);
waypoint1.setType(Waypoint.TYPE_STATISTICS);
providerUtils.insertWaypoint(waypoint1);
// Delete
DescriptionGenerator descriptionGenerator = new DescriptionGenerator() {
@Override
public String generateWaypointDescription(Waypoint waypoint) {
return MyTracksProviderUtilsImplTest.MOCK_DESC;
}
@SuppressWarnings("hiding")
@Override
public String generateTrackDescription(Track track, Vector<Double> distances,
Vector<Double> elevations, boolean html) {
return null;
}
};
providerUtils.deleteWaypoint(1, descriptionGenerator);
assertNull(providerUtils.getWaypoint(1));
}
/**
* Tests the method
* {@link MyTracksProviderUtilsImpl#deleteWaypoint(long, DescriptionGenerator)}
* when there is more than one waypoint in the track.
*/
public void testDeleteWaypoint_hasNextWayPoint() {
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
TripStatistics statistics = new TripStatistics();
statistics.setStartTime(1000L);
statistics.setStopTime(2500L);
statistics.setTotalTime(1500L);
statistics.setMovingTime(700L);
statistics.setTotalDistance(750.0);
statistics.setTotalElevationGain(50.0);
statistics.setMaxSpeed(60.0);
statistics.setMaxElevation(1250.0);
statistics.setMinElevation(1200.0);
statistics.setMaxGrade(15.0);
statistics.setMinGrade(-25.0);
statistics.setBounds(-10000, 20000, 30000, -40000);
// Insert at first.
Waypoint waypoint1 = new Waypoint();
waypoint1.setDescription(Long.toString(trackId));
waypoint1.setTrackId(trackId);
waypoint1.setType(Waypoint.TYPE_STATISTICS);
waypoint1.setTripStatistics(statistics);
providerUtils.insertWaypoint(waypoint1);
Waypoint waypoint2 = new Waypoint();
waypoint2.setDescription(Long.toString(trackId));
waypoint2.setTrackId(trackId);
waypoint2.setType(Waypoint.TYPE_STATISTICS);
waypoint2.setTripStatistics(statistics);
providerUtils.insertWaypoint(waypoint2);
// Delete
DescriptionGenerator descriptionGenerator = new DescriptionGenerator() {
@Override
public String generateWaypointDescription(Waypoint waypoint) {
return MyTracksProviderUtilsImplTest.MOCK_DESC;
}
@SuppressWarnings("hiding")
@Override
public String generateTrackDescription(Track track, Vector<Double> distances,
Vector<Double> elevations, boolean html) {
return null;
}
};
providerUtils.deleteWaypoint(1, descriptionGenerator);
assertNull(providerUtils.getWaypoint(1));
assertEquals(MyTracksProviderUtilsImplTest.MOCK_DESC, providerUtils.getWaypoint(2)
.getDescription());
}
/**
* Tests the method {@link MyTracksProviderUtilsImpl#createWaypoint(Cursor)}.
*/
@UsesMocks(Cursor.class)
public void testCreateWaypoint() {
Cursor cursorMock = AndroidMock.createNiceMock(Cursor.class);
// The startColumnIndex can be any value, just make it to 2 in case it's
// hard coded to 0 or 1 in some where.
int startColumnIndex = 2;
int columnIndex = startColumnIndex;
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(WaypointsColumns._ID))
.andReturn(columnIndex++);
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(WaypointsColumns.NAME)).andReturn(
columnIndex++);
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(WaypointsColumns.TRACKID)).andReturn(
columnIndex++);
columnIndex = startColumnIndex;
// Id
AndroidMock.expect(cursorMock.isNull(columnIndex++)).andReturn(false);
// Name
AndroidMock.expect(cursorMock.isNull(columnIndex++)).andReturn(false);
// trackIdIndex
AndroidMock.expect(cursorMock.isNull(columnIndex++)).andReturn(false);
long id = System.currentTimeMillis();
columnIndex = startColumnIndex;
// Id
AndroidMock.expect(cursorMock.getLong(columnIndex++)).andReturn(id);
// Name
String name = NAME_PREFIX + Long.toString(id);
AndroidMock.expect(cursorMock.getString(columnIndex++)).andReturn(name);
// trackIdIndex
long trackId = 11L;
AndroidMock.expect(cursorMock.getLong(columnIndex++)).andReturn(trackId);
AndroidMock.replay(cursorMock);
Waypoint waypoint = providerUtils.createWaypoint(cursorMock);
assertEquals(id, waypoint.getId());
assertEquals(name, waypoint.getName());
assertEquals(trackId, waypoint.getTrackId());
AndroidMock.verify(cursorMock);
}
/**
* Tests the method {@link MyTracksProviderUtilsImpl#createContentValues(Waypoint)}.
*/
public void testCreateContentValues_Waypoint() {
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
// Bottom
long startTime = 1000L;
// AverageSpeed
double minGrade = -20.11;
TripStatistics statistics = new TripStatistics();
statistics.setStartTime(startTime);
statistics.setStopTime(2500L);
statistics.setTotalTime(1500L);
statistics.setMovingTime(700L);
statistics.setTotalDistance(750.0);
statistics.setTotalElevationGain(50.0);
statistics.setMaxSpeed(60.0);
statistics.setMaxElevation(1250.0);
statistics.setMinElevation(1200.0);
statistics.setMaxGrade(15.0);
statistics.setMinGrade(minGrade);
statistics.setBounds(-10000, 20000, 30000, -40000);
// Insert at first.
Waypoint waypoint = new Waypoint();
waypoint.setDescription(TEST_DESC);
waypoint.setType(Waypoint.TYPE_STATISTICS);
waypoint.setTripStatistics(statistics);
Location loc = new Location("test");
loc.setLatitude(22);
loc.setLongitude(22);
loc.setAccuracy((float) 1 / 100.0f);
loc.setAltitude(2.5);
waypoint.setLocation(loc);
providerUtils.insertWaypoint(waypoint);
MyTracksProviderUtilsImpl myTracksProviderUtilsImpl = new MyTracksProviderUtilsImpl(
new MockContentResolver());
long waypointId = System.currentTimeMillis();
waypoint.setId(waypointId);
ContentValues contentValues = myTracksProviderUtilsImpl.createContentValues(waypoint);
assertEquals(waypointId, contentValues.get(WaypointsColumns._ID));
assertEquals(22 * 1000000, contentValues.get(WaypointsColumns.LONGITUDE));
assertEquals(TEST_DESC, contentValues.get(WaypointsColumns.DESCRIPTION));
assertEquals(startTime, contentValues.get(WaypointsColumns.STARTTIME));
assertEquals(minGrade, contentValues.get(WaypointsColumns.MINGRADE));
}
/**
* Tests the method {@link MyTracksProviderUtilsImpl#getFirstLocation()}.
*/
public void testGetFirstLocation() {
// Insert track, points at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
Location firstLocation = providerUtils.getFirstLocation();
checkLocation(0, firstLocation);
}
/**
* Tests the method {@link MyTracksProviderUtilsImpl#getLastLocation()}.
*/
public void testGetLastLocation() {
// Insert track, points at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
Location lastLocation = providerUtils.getLastLocation();
checkLocation(9, lastLocation);
}
/**
* Tests the method {@link MyTracksProviderUtilsImpl#getLocation(long)}.
*/
public void testGetLocation() {
// Insert track, points at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
Location location1 = providerUtils.getLocation(1L);
checkLocation(0, location1);
Location location5 = providerUtils.getLocation(5L);
checkLocation(4, location5);
Location location10 = providerUtils.getLocation(10L);
checkLocation(9, location10);
}
/**
* Tests the method
* {@link MyTracksProviderUtilsImpl#getLocationsCursor(long, long, int, boolean)}
* in descending.
*/
public void testGetLocationCursor_desc() {
// Insert track, points at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
Cursor cursor = providerUtils.getLocationsCursor(trackId, 2L, 5, true);
assertEquals(2, cursor.getCount());
}
/**
* Tests the method
* {@link MyTracksProviderUtilsImpl#getLocationsCursor(long, long, int, boolean)}
* in ascending.
*/
public void testGetLocationCursor_asc() {
// Insert track, points at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
Cursor cursor = providerUtils.getLocationsCursor(trackId, 2L, 5, false);
assertEquals(5, cursor.getCount());
}
/**
* Tests the method
* {@link MyTracksProviderUtilsImpl#getLocationIterator(long, long, boolean, LocationFactory)}
* in descending.
*/
public void testGeLocationIterator_desc() {
// Insert track, points at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
long startTrackPointId = 2L;
LocationIterator locationIterator = providerUtils.getLocationIterator(trackId,
startTrackPointId, true, MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY);
assertEquals(startTrackPointId, locationIterator.getLocationId());
for (int i = 1; i >= 0; i--) {
assertTrue(locationIterator.hasNext());
Location location = locationIterator.next();
checkLocation(i, location);
}
assertFalse(locationIterator.hasNext());
}
/**
* Tests the method
* {@link MyTracksProviderUtilsImpl#getLocationIterator(long, long, boolean, LocationFactory)}
* in ascending.
*/
public void testGeLocationIterator_asc() {
// Insert track, point at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
long startTrackPointId = 2L;
LocationIterator locationIterator = providerUtils.getLocationIterator(trackId,
startTrackPointId, false, MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY);
assertEquals(startTrackPointId, locationIterator.getLocationId());
for (int i = 1; i < 10; i++) {
assertTrue(locationIterator.hasNext());
Location location = locationIterator.next();
checkLocation(i, location);
}
assertFalse(locationIterator.hasNext());
}
/**
* Tests the method
* {@link MyTracksProviderUtilsImpl#getLastLocationId(long)}.
*/
public void testGetLastLocationId() {
// Insert track, point at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
assertEquals(10, providerUtils.getLastLocationId(trackId));
}
/**
* Tests the method
* {@link MyTracksProviderUtilsImpl#insertTrackPoint(Location, long)}. This
* test also covers the method
* {@link MyTracksProviderUtilsImpl#createContentValues(Location, long)}.
*/
public void testInsertTrackPoint() {
// Insert track, point at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
providerUtils.insertTrackPoint(createLocation(22), trackId);
assertEquals(11, providerUtils.getLocationsCursor(trackId, 0, 1000, false).getCount());
}
/**
* Tests the method
* {@link MyTracksProviderUtilsImpl#bulkInsertTrackPoints(Location[], int, long)}
* . This test also covers the method
* {@link MyTracksProviderUtilsImpl#createContentValues(Location, long)} and
* the inner class {@link MyTracksProviderUtilsImpl.CachedTrackPointsIndexes}.
*/
public void testBulkInsertTrackPoints() {
// Insert track, point at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
providerUtils.bulkInsertTrackPoints(track.getLocations().toArray(new Location[0]), -1, trackId);
assertEquals(20, providerUtils.getLocationsCursor(trackId, 0, 1000, false).getCount());
providerUtils.bulkInsertTrackPoints(track.getLocations().toArray(new Location[0]), 8, trackId);
assertEquals(28, providerUtils.getLocationsCursor(trackId, 0, 1000, false).getCount());
}
/**
* Tests the method {@link MyTracksProviderUtilsImpl#createLocation(Cursor)}.
* This test also covers the method
* {@link MyTracksProviderUtilsImpl#fillLocation(Cursor, Location)} and
* {@link MyTracksProviderUtilsImpl#fillLocation(Cursor, MyTracksProviderUtilsImpl.CachedTrackPointsIndexes, Location)}
* and the inner class
* {@link MyTracksProviderUtilsImpl.CachedTrackPointsIndexes}.
*/
@UsesMocks(Cursor.class)
public void testCreateLocation() {
Cursor cursorMock = AndroidMock.createNiceMock(Cursor.class);
// Set index.
int index = 1;
// Id
AndroidMock.expect(cursorMock.getColumnIndex(TrackPointsColumns._ID)).andReturn(index++);
// Longitude
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(TrackPointsColumns.LONGITUDE)).andReturn(
index++);
// Latitude
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(TrackPointsColumns.LATITUDE)).andReturn(
index++);
// Time
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(TrackPointsColumns.TIME))
.andReturn(index++);
// Speed
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(TrackPointsColumns.SPEED)).andReturn(
index++);
// Sensor
AndroidMock.expect(cursorMock.getColumnIndexOrThrow(TrackPointsColumns.SENSOR)).andReturn(
index++);
// Set return value of isNull().
index = 2;
// Longitude
AndroidMock.expect(cursorMock.isNull(index++)).andReturn(false);
// Latitude
AndroidMock.expect(cursorMock.isNull(index++)).andReturn(false);
// Time
AndroidMock.expect(cursorMock.isNull(index++)).andReturn(false);
// Speed
AndroidMock.expect(cursorMock.isNull(index++)).andReturn(false);
// Sensor
AndroidMock.expect(cursorMock.isNull(index++)).andReturn(false);
// Set return value of isNull().
index = 2;
// Longitude
int longitude = 11;
AndroidMock.expect(cursorMock.getInt(index++)).andReturn(longitude * 1000000);
// Latitude.
int latitude = 22;
AndroidMock.expect(cursorMock.getInt(index++)).andReturn(latitude * 1000000);
// Time
long time = System.currentTimeMillis();
AndroidMock.expect(cursorMock.getLong(index++)).andReturn(time);
// Speed
float speed = 2.2f;
AndroidMock.expect(cursorMock.getFloat(index++)).andReturn(speed);
// Sensor
byte[] sensor = "Sensor state".getBytes();
AndroidMock.expect(cursorMock.getBlob(index++)).andReturn(sensor);
AndroidMock.replay(cursorMock);
Location location = providerUtils.createLocation(cursorMock);
assertEquals((double) longitude, location.getLongitude());
assertEquals((double) latitude, location.getLatitude());
assertEquals(time, location.getTime());
assertEquals(speed, location.getSpeed());
AndroidMock.verify(cursorMock);
}
/**
@@ -539,19 +978,53 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
* @param numPoints the location number in the track
* @return the simulated track
*/
private Track simulaTrack(long id,int numPoints) {
private Track getTrack(long id,int numPoints) {
Track track = new Track();
track.setId(id);
track.setName("Test: " + id);
track.setNumberOfPoints(numPoints);
for(int i=0; i < numPoints; i++) {
Location loc = new Location("test");
loc.setLatitude(37.0 + (double) i / 10000.0);
loc.setLongitude(57.0 - (double) i / 10000.0);
loc.setAccuracy((float) i / 100.0f);
loc.setAltitude(i * 2.5);
track.addLocation(loc);
track.addLocation(createLocation(i));
}
return track;
}
/**
* Creates a location.
* @param i the index to set the value of location.
* @return created location
*/
private Location createLocation(int i) {
Location loc = new Location("test");
loc.setLatitude(37.0 + (double) i / 10000.0);
loc.setLongitude(57.0 - (double) i / 10000.0);
loc.setAccuracy((float) i / 100.0f);
loc.setAltitude(i * 2.5);
return loc;
}
/**
* Checks the value of a location.
*
* @param i the index of this location which created in the method {@link
* this#getTrack(long, int)}
* @param location the location to be checked
*/
private void checkLocation(int i, Location location) {
assertEquals(37.0 + (double) i / 10000.0, location.getLatitude());
assertEquals(57.0 - (double) i / 10000.0, location.getLongitude());
assertEquals((float) i / 100.0f, location.getAccuracy());
assertEquals(i * 2.5, location.getAltitude());
}
/**
* Inserts a track with locations into the database.
*
* @param track track to be inserted
*/
private void insertTrackWithLocations(Track track) {
providerUtils.insertTrack(track);
providerUtils.bulkInsertTrackPoints(track.getLocations().toArray(new Location[0]), track
.getLocations().size(), track.getId());
}
}