Merge heads

This commit is contained in:
youtaol
2013-07-25 20:16:22 +08:00
6 changed files with 249 additions and 274 deletions
@@ -49,12 +49,6 @@ public class TripStatisticsUpdater {
@VisibleForTesting
static final int RUN_SMOOTHING_FACTOR = 25;
/**
* The number of rise readings to smooth for calculating grade.
*/
@VisibleForTesting
static final int RISE_SMOOTHING_FACTOR = 25;
/**
* The number of grade readings to smooth to get a somewhat accurate signal.
*/
@@ -92,9 +86,6 @@ public class TripStatisticsUpdater {
// A buffer of the recent run readings (m) for calculating grade
private final DoubleBuffer runBuffer = new DoubleBuffer(RUN_SMOOTHING_FACTOR);
// A buffer of the recent rise readings (m) for calculating grade
private final DoubleBuffer riseBuffer = new DoubleBuffer(RISE_SMOOTHING_FACTOR);
// A buffer of the recent grade calculations (%)
private final DoubleBuffer gradeBuffer = new DoubleBuffer(GRADE_SMOOTHING_FACTOR);
@@ -150,7 +141,6 @@ public class TripStatisticsUpdater {
lastMovingLocation = null;
elevationBuffer.reset();
runBuffer.reset();
riseBuffer.reset();
gradeBuffer.reset();
speedBuffer.reset();
return;
@@ -158,8 +148,9 @@ public class TripStatisticsUpdater {
currentSegment.updateLatitudeExtremities(location.getLatitude());
currentSegment.updateLongitudeExtremities(location.getLongitude());
Double elevationDifference = null;
if (location.hasAltitude()) {
updateElevation(location.getAltitude());
elevationDifference = updateElevation(location.getAltitude());
}
if (lastLocation == null || lastMovingLocation == null) {
@@ -188,11 +179,8 @@ public class TripStatisticsUpdater {
currentSegment.addMovingTime(movingTime);
// Update grade
if (location.hasAltitude() && lastLocation.hasAltitude()) {
float run = lastLocation.distanceTo(location);
double rise = location.getAltitude() - lastLocation.getAltitude();
updateGrade(run, rise);
}
double run = lastLocation.distanceTo(location);
updateGrade(run, elevationDifference);
// Update max speed
if (location.hasSpeed() && lastLocation.hasSpeed()) {
@@ -231,8 +219,8 @@ public class TripStatisticsUpdater {
speedBuffer.reset();
} else if (isValidSpeed(time, speed, lastLocationTime, lastLocationSpeed)) {
speedBuffer.setNext(speed);
if (speed > currentSegment.getMaxSpeed()) {
currentSegment.setMaxSpeed(speed);
if (speedBuffer.isFull() && speedBuffer.getAverage() > currentSegment.getMaxSpeed()) {
currentSegment.setMaxSpeed(speedBuffer.getAverage());
}
} else {
Log.d(TAG, "Invalid speed. speed: " + speed + " lastLocationSpeed: " + lastLocationSpeed);
@@ -240,24 +228,25 @@ public class TripStatisticsUpdater {
}
/**
* Updates an elevation reading.
* Updates an elevation reading. Returns the difference.
*
* @param elevation the elevation
*/
@VisibleForTesting
void updateElevation(double elevation) {
currentSegment.updateElevationExtremities(elevation);
// update elevation gain
Double updateElevation(double elevation) {
// Update elevation using the smoothed average after the buffer is full
double oldAverage = elevationBuffer.getAverage();
elevationBuffer.setNext(elevation);
double newAverage = elevationBuffer.getAverage();
if (elevationBuffer.isFull()) {
double difference = newAverage - oldAverage;
currentSegment.updateElevationExtremities(newAverage);
Double difference = newAverage - oldAverage;
if (difference > 0) {
currentSegment.addTotalElevationGain(difference);
}
return difference;
}
return null;
}
/**
@@ -267,11 +256,10 @@ public class TripStatisticsUpdater {
* @param rise the rise
*/
@VisibleForTesting
void updateGrade(double run, double rise) {
void updateGrade(double run, Double rise) {
runBuffer.setNext(run);
riseBuffer.setNext(rise);
if (!runBuffer.isFull() || !riseBuffer.isFull()) {
if (!runBuffer.isFull() || rise == null) {
return;
}
@@ -284,8 +272,10 @@ public class TripStatisticsUpdater {
if (smoothedRun < 5.0) {
return;
}
gradeBuffer.setNext(riseBuffer.getAverage() / smoothedRun);
currentSegment.updateGradeExtremities(gradeBuffer.getAverage());
gradeBuffer.setNext(rise / smoothedRun);
if (gradeBuffer.isFull()) {
currentSegment.updateGradeExtremities(gradeBuffer.getAverage());
}
}
private TripStatistics init(long time) {
@@ -337,11 +327,12 @@ public class TripStatisticsUpdater {
* 10X the smoothed average and the speed difference doesn't imply 2g
* acceleration.
*/
if (!speedBuffer.isFull()) {
if (speedBuffer.isFull()) {
double average = speedBuffer.getAverage();
double diff = Math.abs(average - speed);
return (speed < average * 10) && (diff < MAX_ACCELERATION * timeDifference);
} else {
return true;
}
double average = speedBuffer.getAverage();
double diff = Math.abs(average - speed);
return (speed < average * 10) && (diff < MAX_ACCELERATION * timeDifference);
}
}
@@ -16,9 +16,6 @@
package com.google.android.apps.mytracks.services;
import static com.google.android.testing.mocking.AndroidMock.expect;
import com.google.android.testing.mocking.AndroidMock;
import com.google.android.testing.mocking.UsesMocks;
import android.test.AndroidTestCase;
@@ -42,62 +39,62 @@ public class RemoveTempFilesServiceTest extends AndroidTestCase {
service = new RemoveTempFilesService();
};
/**
* Tests when the directory doesn't exists.
*/
public void test_noDir() {
File dir = AndroidMock.createMock(File.class, DIR_NAME);
expect(dir.exists()).andStubReturn(false);
AndroidMock.replay(dir);
assertEquals(0, service.cleanTempDirectory(dir));
AndroidMock.verify(dir);
}
/**
* Tests when the directory is empty.
*/
public void test_emptyDir() {
File dir = AndroidMock.createMock(File.class, DIR_NAME);
expect(dir.exists()).andStubReturn(true);
expect(dir.listFiles()).andStubReturn(new File[0]);
AndroidMock.replay(dir);
assertEquals(0, service.cleanTempDirectory(dir));
AndroidMock.verify(dir);
}
/**
* Tests when there is a new file and it shouldn't get deleted.
*/
public void test_newFile() {
File file = AndroidMock.createMock(File.class, DIR_NAME + FILE_NAME);
expect(file.lastModified()).andStubReturn(System.currentTimeMillis());
File dir = AndroidMock.createMock(File.class, DIR_NAME);
expect(dir.exists()).andStubReturn(true);
expect(dir.listFiles()).andStubReturn(new File[] { file });
AndroidMock.replay(dir, file);
assertEquals(0, service.cleanTempDirectory(dir));
AndroidMock.verify(dir, file);
}
/**
* Tests when there is an old file and it should get deleted.
*/
public void test_oldFile() {
File file = AndroidMock.createMock(File.class, DIR_NAME + FILE_NAME);
// qSet to one hour and 1 millisecond later than the current time
expect(file.lastModified()).andStubReturn(System.currentTimeMillis() - 3600001);
expect(file.delete()).andStubReturn(true);
File dir = AndroidMock.createMock(File.class, DIR_NAME);
expect(dir.exists()).andStubReturn(true);
expect(dir.listFiles()).andStubReturn(new File[] { file });
AndroidMock.replay(dir, file);
assertEquals(1, service.cleanTempDirectory(dir));
AndroidMock.verify(dir, file);
}
// /**
// * Tests when the directory doesn't exists.
// */
// public void test_noDir() {
// File dir = AndroidMock.createMock(File.class, DIR_NAME);
// expect(dir.exists()).andStubReturn(false);
// AndroidMock.replay(dir);
//
// assertEquals(0, service.cleanTempDirectory(dir));
// AndroidMock.verify(dir);
// }
//
// /**
// * Tests when the directory is empty.
// */
// public void test_emptyDir() {
// File dir = AndroidMock.createMock(File.class, DIR_NAME);
// expect(dir.exists()).andStubReturn(true);
// expect(dir.listFiles()).andStubReturn(new File[0]);
// AndroidMock.replay(dir);
//
// assertEquals(0, service.cleanTempDirectory(dir));
// AndroidMock.verify(dir);
// }
//
// /**
// * Tests when there is a new file and it shouldn't get deleted.
// */
// public void test_newFile() {
// File file = AndroidMock.createMock(File.class, DIR_NAME + FILE_NAME);
// expect(file.lastModified()).andStubReturn(System.currentTimeMillis());
//
// File dir = AndroidMock.createMock(File.class, DIR_NAME);
// expect(dir.exists()).andStubReturn(true);
// expect(dir.listFiles()).andStubReturn(new File[] { file });
// AndroidMock.replay(dir, file);
//
// assertEquals(0, service.cleanTempDirectory(dir));
// AndroidMock.verify(dir, file);
// }
//
// /**
// * Tests when there is an old file and it should get deleted.
// */
// public void test_oldFile() {
// File file = AndroidMock.createMock(File.class, DIR_NAME + FILE_NAME);
// // qSet to one hour and 1 millisecond later than the current time
// expect(file.lastModified()).andStubReturn(System.currentTimeMillis() - 3600001);
// expect(file.delete()).andStubReturn(true);
//
// File dir = AndroidMock.createMock(File.class, DIR_NAME);
// expect(dir.exists()).andStubReturn(true);
// expect(dir.listFiles()).andStubReturn(new File[] { file });
// AndroidMock.replay(dir, file);
//
// assertEquals(1, service.cleanTempDirectory(dir));
// AndroidMock.verify(dir, file);
// }
}
@@ -80,7 +80,7 @@ public class TripStatisticsTest extends TestCase {
assertEquals(1300L, statistics.getMovingTime());
assertEquals(1100.0, statistics.getTotalDistance());
assertEquals(900.0, statistics.getTotalElevationGain());
assertEquals(60.0, statistics.getMaxSpeed());
assertEquals(statistics.getTotalDistance()/ (statistics.getMovingTime() / 1000.0), statistics.getMaxSpeed());
assertEquals(-10000, statistics.getLeft());
assertEquals(30000, statistics.getRight());
assertEquals(35000, statistics.getTop());
@@ -87,16 +87,18 @@ public class TripStatisticsUpdaterTest extends TestCase {
tripStatisticsUpdater.updateElevation(elevation);
assertEquals(elevation, tripStatisticsUpdater.getSmoothedElevation());
TripStatistics tripStatistics = tripStatisticsUpdater.getTripStatistics();
assertEquals(elevation, tripStatistics.getMinElevation());
assertEquals(elevation, tripStatistics.getMaxElevation());
assertEquals(0.0, tripStatistics.getTotalElevationGain());
if (i >= TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR) {
TripStatistics tripStatistics = tripStatisticsUpdater.getTripStatistics();
assertEquals(elevation, tripStatistics.getMinElevation());
assertEquals(elevation, tripStatistics.getMaxElevation());
assertEquals(0.0, tripStatistics.getTotalElevationGain());
}
}
}
}
/**
* Tests {@link TripStatisticsUpdater#updateGrade(double, double)} with
* Tests {@link TripStatisticsUpdater#updateGrade(double, Double)} with
* elevation gain.
*/
public void testElevationGain() throws Exception {
@@ -105,32 +107,33 @@ public class TripStatisticsUpdaterTest extends TestCase {
assertEquals(i, tripStatisticsUpdater.getSmoothedElevation(),
TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR / 2);
TripStatistics data = tripStatisticsUpdater.getTripStatistics();
assertEquals(0.0, data.getMinElevation());
assertEquals(i, data.getMaxElevation(), TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR / 2);
assertEquals(
i, data.getTotalElevationGain(), TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR);
if (i >= TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR) {
TripStatistics data = tripStatisticsUpdater.getTripStatistics();
assertEquals(12.0, data.getMinElevation());
assertEquals(
i, data.getMaxElevation(), TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR / 2);
assertEquals(
i, data.getTotalElevationGain(), TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR);
}
}
}
/**
* Tests {@link TripStatisticsUpdater#updateGrade(double, double)} with grade
* Tests {@link TripStatisticsUpdater#updateGrade(double, Double)} with grade
* of 1 and -1.
*/
public void testGradeSimple() throws Exception {
for (double i = 0; i < 1000; i++) {
tripStatisticsUpdater.updateGrade(100, 100);
if (i >= TripStatisticsUpdater.GRADE_SMOOTHING_FACTOR
&& i >= TripStatisticsUpdater.RISE_SMOOTHING_FACTOR
&& i >= TripStatisticsUpdater.RUN_SMOOTHING_FACTOR) {
tripStatisticsUpdater.updateGrade(100, Double.valueOf(100));
if (i >= TripStatisticsUpdater.RUN_SMOOTHING_FACTOR
+ TripStatisticsUpdater.GRADE_SMOOTHING_FACTOR) {
assertEquals(1.0, tripStatisticsUpdater.getTripStatistics().getMaxGrade());
assertEquals(1.0, tripStatisticsUpdater.getTripStatistics().getMinGrade());
}
}
for (double i = 0; i < 1000; i++) {
tripStatisticsUpdater.updateGrade(100, -100);
tripStatisticsUpdater.updateGrade(100, Double.valueOf(-100));
if (i >= TripStatisticsUpdater.GRADE_SMOOTHING_FACTOR
&& i >= TripStatisticsUpdater.RISE_SMOOTHING_FACTOR
&& i >= TripStatisticsUpdater.RUN_SMOOTHING_FACTOR) {
assertEquals(1.0, tripStatisticsUpdater.getTripStatistics().getMaxGrade());
// add 0.1 delta since changing min grade from 1 to -1
@@ -140,7 +143,7 @@ public class TripStatisticsUpdaterTest extends TestCase {
}
/**
* Tests {@link TripStatisticsUpdater#updateGrade(double, double)} with
* Tests {@link TripStatisticsUpdater#updateGrade(double, Double)} with
* distance of 1. The grade should get ignored.
*/
public void testGradeIgnoreShort() throws Exception {
@@ -150,7 +153,7 @@ public class TripStatisticsUpdaterTest extends TestCase {
* elevation buffer.
*/
tripStatisticsUpdater.updateElevation(i);
tripStatisticsUpdater.updateGrade(1, 100);
tripStatisticsUpdater.updateGrade(1, Double.valueOf(100));
assertEquals(
Double.NEGATIVE_INFINITY, tripStatisticsUpdater.getTripStatistics().getMaxGrade());
assertEquals(
@@ -228,20 +231,21 @@ public class TripStatisticsUpdaterTest extends TestCase {
assertEquals((locationOffset + i) * TEN_SECONDS, tripStatistics.getMovingTime());
assertEquals(i + locationOffset, tripStatisticsUpdater.getSmoothedElevation(),
TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR / 2);
assertEquals(0.0, tripStatistics.getMinElevation());
assertEquals(i + locationOffset, tripStatistics.getMaxElevation(),
TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR / 2);
assertEquals(i + locationOffset, tripStatistics.getTotalElevationGain(),
TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR);
if (i + locationOffset >= TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR) {
assertEquals(12.0, tripStatistics.getMinElevation());
assertEquals(i + locationOffset, tripStatistics.getMaxElevation(),
TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR / 2);
assertEquals(i + locationOffset, tripStatistics.getTotalElevationGain(),
TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR);
}
if (i + locationOffset >= TripStatisticsUpdater.SPEED_SMOOTHING_FACTOR) {
assertEquals(MOVING_SPEED, tripStatistics.getMaxSpeed(), 0.1);
}
// If there are only moving locations in the track.
if (locationOffset == 0
&& (i + locationOffset) >= TripStatisticsUpdater.RUN_SMOOTHING_FACTOR
&& (i + locationOffset) >= TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR) {
if (locationOffset == 0 && (i + locationOffset) >= TripStatisticsUpdater.RUN_SMOOTHING_FACTOR
+ TripStatisticsUpdater.GRADE_SMOOTHING_FACTOR) {
// 1 m / 111 m = .009
assertEquals(0.009, tripStatistics.getMinGrade(), 0.0001);
assertEquals(0.009, tripStatistics.getMaxGrade(), 0.0001);
@@ -273,7 +277,7 @@ public class TripStatisticsUpdaterTest extends TestCase {
assertEquals((locationOffset) * TEN_SECONDS, tripStatistics.getMovingTime());
assertEquals(locationOffset, tripStatisticsUpdater.getSmoothedElevation(),
TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR / 2);
assertEquals(0.0, tripStatistics.getMinElevation());
assertEquals(12.0, tripStatistics.getMinElevation());
assertEquals(locationOffset, tripStatistics.getMaxElevation(),
TripStatisticsUpdater.ELEVATION_SMOOTHING_FACTOR / 2);
assertEquals(locationOffset, tripStatistics.getTotalElevationGain(),
@@ -181,6 +181,133 @@ public class ExportAllAndImportAllTest extends ActivityInstrumentationTestCase2<
checkExportAndImport();
}
/**
* Tests export and import tracks.
* <ul>
* <li>Create two tracks, one of them is empty(Have no Gps data).</li>
* <li>Tests import when there is no track file.</li>
* <li>Tests export tracks to Gpx files.</li>
* <li>Tests import Gpx files to tracks.</li>
* <li>Tests export tracks to Kml files.</li>
* </ul>
*/
private void checkExportAndImport() {
// Delete all exported gpx and kml tracks.
EndToEndTestUtils.deleteExportedFiles(EndToEndTestUtils.GPX);
EndToEndTestUtils.deleteExportedFiles(EndToEndTestUtils.KML);
int gpxFilesNumber = 0;
File[] allGpxFiles = EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX);
// For the first export, there is no MyTracks folder.
if (allGpxFiles != null) {
gpxFilesNumber = EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX).length;
}
// Get track number in current track list of MyTracks.
trackNumber = EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount();
// No Gpx file to imported.
importTracks(EndToEndTestUtils.GPX);
EndToEndTestUtils.SOLO.waitForText(getImportErrorMessage(0, 0, EndToEndTestUtils.GPX));
EndToEndTestUtils
.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true);
// Click to export tracks(At least one track) to Gpx files.
exportTracks(EndToEndTestUtils.GPX);
// Check export Gpx file.
assertEquals(gpxFilesNumber + trackNumber,
EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX).length);
instrumentation.waitForIdleSync();
// Click to import Gpx track.
gpxFilesNumber = EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX).length;
trackNumber = EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount();
importTracks(EndToEndTestUtils.GPX);
EndToEndTestUtils.rotateCurrentActivity();
checkImport();
assertTrue(EndToEndTestUtils.SOLO.waitForText(EndToEndTestUtils.trackName));
assertEquals(trackNumber + gpxFilesNumber,
EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount());
// Click to export tracks(At least two tracks) to KML files.
gpxFilesNumber = EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX).length;
trackNumber = EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount();
exportTracks(EndToEndTestUtils.KML);
// Check export files.
assertEquals(gpxFilesNumber, EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX).length);
assertEquals(trackNumber, EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.KML).length);
// Click to import KML track.
int KMLFilesNumber = EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.KML).length;
instrumentation.waitForIdleSync();
trackNumber = EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount();
importTracks(EndToEndTestUtils.KML);
checkImport();
EndToEndTestUtils.SOLO.waitForActivity(TrackListActivity.class);
assertEquals(trackNumber + KMLFilesNumber,
EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount());
}
@Override
protected void tearDown() throws Exception {
EndToEndTestUtils.SOLO.finishOpenedActivities();
super.tearDown();
}
private String getSaveSuccessMessage(int count, String type) {
String tracks = activityMyTracks.getResources().getQuantityString(R.plurals.tracks, count,
count);
String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(R.string.export_external_storage_success, tracks,
directoryDisplayName);
}
private String getImportSuccessMessage(int count, String type) {
String files = activityMyTracks.getResources().getQuantityString(R.plurals.files, count, count);
String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(R.string.import_success, files, directoryDisplayName);
}
private String getImportErrorMessage(int count, int total, String type) {
String files = activityMyTracks.getResources().getQuantityString(R.plurals.files, total, total);
String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(R.string.import_error, count, files, directoryDisplayName);
}
/**
* Imports tracks from external storage.
*
* @param fileType the file type, can be GPX and KML currently
*/
private void importTracks(String fileType) {
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_import_all), true);
EndToEndTestUtils.SOLO.clickOnText(fileType.toUpperCase());
EndToEndTestUtils.getButtonOnScreen(
EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), true, true);
}
/**
* Exports tracks to external storage.
*
* @param fileType the file type, can be GPX/KML/TCX/CSV currently
*/
private void exportTracks(String fileType) {
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_export_all), true);
EndToEndTestUtils.SOLO.clickOnText(fileType.toUpperCase());
EndToEndTestUtils.getButtonOnScreen(
EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), true, true);
EndToEndTestUtils.SOLO.waitForText(getSaveSuccessMessage(2, fileType));
EndToEndTestUtils
.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true);
}
/**
* Test export/import one track and checks the detail of this track.
*/
@@ -371,131 +498,4 @@ public class ExportAllAndImportAllTest extends ActivityInstrumentationTestCase2<
fail();
}
}
/**
* Tests export and import tracks.
* <ul>
* <li>Create two tracks, one of them is empty(Have no Gps data).</li>
* <li>Tests import when there is no track file.</li>
* <li>Tests export tracks to Gpx files.</li>
* <li>Tests import Gpx files to tracks.</li>
* <li>Tests export tracks to Kml files.</li>
* </ul>
*/
private void checkExportAndImport() {
// Delete all exported gpx and kml tracks.
EndToEndTestUtils.deleteExportedFiles(EndToEndTestUtils.GPX);
EndToEndTestUtils.deleteExportedFiles(EndToEndTestUtils.KML);
int gpxFilesNumber = 0;
File[] allGpxFiles = EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX);
// For the first export, there is no MyTracks folder.
if (allGpxFiles != null) {
gpxFilesNumber = EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX).length;
}
// Get track number in current track list of MyTracks.
trackNumber = EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount();
// No Gpx file to imported.
importTracks(EndToEndTestUtils.GPX);
EndToEndTestUtils.SOLO.waitForText(getImportErrorMessage(0, 0, EndToEndTestUtils.GPX));
EndToEndTestUtils
.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true);
// Click to export tracks(At least one track) to Gpx files.
exportTracks(EndToEndTestUtils.GPX);
// Check export Gpx file.
assertEquals(gpxFilesNumber + trackNumber,
EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX).length);
instrumentation.waitForIdleSync();
// Click to import Gpx track.
gpxFilesNumber = EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX).length;
trackNumber = EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount();
importTracks(EndToEndTestUtils.GPX);
EndToEndTestUtils.rotateCurrentActivity();
checkImport();
assertTrue(EndToEndTestUtils.SOLO.waitForText(EndToEndTestUtils.trackName));
assertEquals(trackNumber + gpxFilesNumber,
EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount());
// Click to export tracks(At least two tracks) to KML files.
gpxFilesNumber = EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX).length;
trackNumber = EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount();
exportTracks(EndToEndTestUtils.KML);
// Check export files.
assertEquals(gpxFilesNumber, EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.GPX).length);
assertEquals(trackNumber, EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.KML).length);
// Click to import KML track.
int KMLFilesNumber = EndToEndTestUtils.getExportedFiles(EndToEndTestUtils.KML).length;
instrumentation.waitForIdleSync();
trackNumber = EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount();
importTracks(EndToEndTestUtils.KML);
checkImport();
EndToEndTestUtils.SOLO.waitForActivity(TrackListActivity.class);
assertEquals(trackNumber + KMLFilesNumber,
EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount());
}
private String getSaveSuccessMessage(int count, String type) {
String tracks = activityMyTracks.getResources().getQuantityString(R.plurals.tracks, count,
count);
String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(R.string.export_external_storage_success, tracks,
directoryDisplayName);
}
private String getImportSuccessMessage(int count, String type) {
String files = activityMyTracks.getResources().getQuantityString(R.plurals.files, count, count);
String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(R.string.import_success, files, directoryDisplayName);
}
private String getImportErrorMessage(int count, int total, String type) {
String files = activityMyTracks.getResources().getQuantityString(R.plurals.files, total, total);
String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(R.string.import_error, count, files, directoryDisplayName);
}
/**
* Imports tracks from external storage.
*
* @param fileType the file type, can be GPX and KML currently
*/
private void importTracks(String fileType) {
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_import_all), true);
EndToEndTestUtils.SOLO.clickOnText(fileType.toUpperCase());
EndToEndTestUtils.getButtonOnScreen(
EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), true, true);
}
/**
* Exports tracks to external storage.
*
* @param fileType the file type, can be GPX/KML/TCX/CSV currently
*/
private void exportTracks(String fileType) {
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_export_all), true);
EndToEndTestUtils.SOLO.clickOnText(fileType.toUpperCase());
EndToEndTestUtils.getButtonOnScreen(
EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), true, true);
EndToEndTestUtils.SOLO.waitForText(getSaveSuccessMessage(2, fileType));
EndToEndTestUtils
.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true);
}
@Override
protected void tearDown() throws Exception {
EndToEndTestUtils.SOLO.finishOpenedActivities();
super.tearDown();
}
}
@@ -155,35 +155,18 @@ public class MenuItemsTest extends ActivityInstrumentationTestCase2<TrackListAct
EndToEndTestUtils.findMenuItem(GPSStatus ? activityMyTracks.getString(R.string.menu_stop_gps)
: activityMyTracks.getString(R.string.menu_start_gps), true);
GPSStatus = !GPSStatus;
EndToEndTestUtils.SOLO.waitForText(
GPSStatus ? activityMyTracks.getString(R.string.menu_start_gps) : activityMyTracks
.getString(R.string.menu_stop_gps), 1, EndToEndTestUtils.SHORT_WAIT_TIME);
instrumentation.waitForIdleSync();
EndToEndTestUtils.sleep(EndToEndTestUtils.SHORT_WAIT_TIME);
if (GPSStatus != EndToEndTestUtils.findMenuItem(
activityMyTracks.getString(R.string.menu_stop_gps), false)) {
EndToEndTestUtils.sleep(EndToEndTestUtils.SHORT_WAIT_TIME);
// Retry once
assertEquals(GPSStatus,
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_stop_gps), false));
}
EndToEndTestUtils.waitTextToDisappear(GPSStatus ? activityMyTracks
.getString(R.string.menu_start_gps) : activityMyTracks.getString(R.string.menu_stop_gps));
assertEquals(GPSStatus,
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_stop_gps), false));
EndToEndTestUtils.findMenuItem(GPSStatus ? activityMyTracks.getString(R.string.menu_stop_gps)
: activityMyTracks.getString(R.string.menu_start_gps), true);
GPSStatus = !GPSStatus;
EndToEndTestUtils.SOLO.waitForText(GPSStatus ? activityMyTracks
EndToEndTestUtils.waitTextToDisappear(GPSStatus ? activityMyTracks
.getString(R.string.menu_start_gps) : activityMyTracks.getString(R.string.menu_stop_gps));
EndToEndTestUtils.sleep(EndToEndTestUtils.SHORT_WAIT_TIME);
if (GPSStatus != EndToEndTestUtils.findMenuItem(
activityMyTracks.getString(R.string.menu_stop_gps), false)) {
EndToEndTestUtils.sleep(EndToEndTestUtils.SHORT_WAIT_TIME);
// Retry once
assertEquals(GPSStatus,
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_stop_gps), false));
}
assertEquals(GPSStatus,
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_stop_gps), false));
}
@Override