Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #46 from mapzen/dont-req-speed-for-mock-engine-gpx
Browse files Browse the repository at this point in the history
Set default speed if not in gpx file
  • Loading branch information
ecgreb committed Feb 19, 2016
2 parents 1d0387c + ae3e5a2 commit f85008d
Show file tree
Hide file tree
Showing 3 changed files with 730 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ private Location nodeToLocation(NodeList nodeList, NodeList speedList, int i) {
location.setLatitude(Double.parseDouble(lat));
location.setLongitude(Double.parseDouble(lng));
location.setTime(System.currentTimeMillis());
location.setSpeed(Float.parseFloat(speedList.item(i).getFirstChild().getNodeValue()));
if (speedList.item(i) != null && speedList.item(i).getFirstChild() != null) {
location.setSpeed(
Float.parseFloat(speedList.item(i).getFirstChild().getNodeValue()));
}

if (previous != null) {
location.setBearing(previous.bearingTo(location));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ public void setTrace_shouldRespectFastestInterval() throws Exception {
assertThat(callback.locations).hasSize(3);
}

@Test
public void setTrace_shouldNotRequireSpeed() throws Exception {
mockEngine.setTrace(getTestGpxTrace());
mockEngine.setRequest(LocationRequest.create().setFastestInterval(0));
Thread.sleep(100);
ShadowLooper.runUiThreadTasks();
assertThat(callback.locations.get(0).hasSpeed()).isTrue();
mockEngine.disable();
callback.reset();
Thread.sleep(100);
mockEngine.setTrace(getNoSpeedGpxTrace());
mockEngine.setRequest(LocationRequest.create().setFastestInterval(0));
Thread.sleep(100);
ShadowLooper.runUiThreadTasks();
assertThat(callback.locations.get(0).hasSpeed()).isFalse();
}

@Test
public void disable_shouldCancelTraceReplay() throws Exception {
mockEngine.setTrace(getTestGpxTrace());
Expand All @@ -127,17 +144,26 @@ public void disable_shouldCancelTraceReplay() throws Exception {
assertThat(callback.locations).hasSize(1);
}

public static File getTestGpxTrace() throws IOException {
String contents = Files.toString(new File("src/test/resources/lost.gpx"), Charsets.UTF_8);
public static File getGpxFile(String filename) throws IOException {
String contents = Files.toString(
new File("src/test/resources/" + filename), Charsets.UTF_8);
ShadowEnvironment.setExternalStorageState(Environment.MEDIA_MOUNTED);
File directory = Environment.getExternalStorageDirectory();
File file = new File(directory, "lost.gpx");
File file = new File(directory, filename);
FileWriter fileWriter = new FileWriter(file, false);
fileWriter.write(contents);
fileWriter.close();
return file;
}

public static File getTestGpxTrace() throws IOException {
return getGpxFile("lost.gpx");
}

public static File getNoSpeedGpxTrace() throws IOException {
return getGpxFile("lost-no-speed.gpx");
}

class TestCallback implements LocationEngine.Callback {
private Location lastLocation;
private ArrayList<Location> locations = new ArrayList<>();
Expand All @@ -147,5 +173,10 @@ public void reportLocation(Location location) {
lastLocation = location;
locations.add(location);
}

public void reset() {
lastLocation = null;
locations.clear();
}
}
}
Loading

0 comments on commit f85008d

Please sign in to comment.