Addressed Sandor's comments and added more unit tests and fixed 2 NPEs.

This commit is contained in:
Bartlomiej Niechwiej
2010-11-11 10:55:15 -08:00
parent 30f5d6dc85
commit 7e1eae6681
3 changed files with 69 additions and 4 deletions
@@ -52,6 +52,11 @@ public class PeriodicTaskExecuter {
* @param interval The interval in milliseconds * @param interval The interval in milliseconds
*/ */
public void scheduleTask(long interval) { public void scheduleTask(long interval) {
// TODO: Decouple service from this class once and forever.
if (!service.isRecording()) {
return;
}
timer.cancel(); timer.cancel();
timer.purge(); timer.purge();
timer = new Timer(); timer = new Timer();
@@ -73,6 +73,11 @@ public class SplitManager {
* Calculates the next distance that a split should be inserted at. * Calculates the next distance that a split should be inserted at.
*/ */
public void calculateNextSplit() { public void calculateNextSplit() {
// TODO: Decouple service from this class once and forever.
if (!service.isRecording()) {
return;
}
if (splitFrequency >= 0) { if (splitFrequency >= 0) {
nextSplitDistance = Double.MAX_VALUE; nextSplitDistance = Double.MAX_VALUE;
Log.d(MyTracksConstants.TAG, Log.d(MyTracksConstants.TAG,
@@ -121,6 +126,12 @@ public class SplitManager {
*/ */
public void setSplitFrequency(int splitFrequency) { public void setSplitFrequency(int splitFrequency) {
this.splitFrequency = splitFrequency; this.splitFrequency = splitFrequency;
// TODO: Decouple service from this class once and forever.
if (!service.isRecording()) {
return;
}
if (splitFrequency < 1) { if (splitFrequency < 1) {
if (splitExecuter != null) { if (splitExecuter != null) {
splitExecuter.shutdown(); splitExecuter.shutdown();
@@ -46,6 +46,11 @@ import java.util.List;
* Tests for the MyTracks track recording service. * Tests for the MyTracks track recording service.
* *
* @author Bartlomiej Niechwiej * @author Bartlomiej Niechwiej
*
* TODO: The original class, ServiceTestCase, has a few limitations, e.g.
* it's not possible to properly shutdown the service, unless tearDown()
* is called, which prevents from testing multiple scenarios in a single
* test (see runFunctionTest for more details).
*/ */
public class TrackRecordingServiceTest public class TrackRecordingServiceTest
extends ServiceTestCase<TrackRecordingService> { extends ServiceTestCase<TrackRecordingService> {
@@ -554,13 +559,50 @@ public class TrackRecordingServiceTest
public void testWithProperties_defaultMinRecordingDist() throws Exception { public void testWithProperties_defaultMinRecordingDist() throws Exception {
functionalTest(R.string.min_recording_distance_key, 2); functionalTest(R.string.min_recording_distance_key, 2);
} }
@MediumTest
public void testWithProperties_noSignalSamplingFreq() throws Exception {
functionalTest(R.string.signal_sampling_frequency_key, (Object) null);
}
@MediumTest
public void testWithProperties_defaultSignalSamplingFreq() throws Exception {
functionalTest(R.string.signal_sampling_frequency_key, 1);
}
@MediumTest
public void testWithProperties_noSplitFreq() throws Exception {
functionalTest(R.string.split_frequency_key, (Object) null);
}
@MediumTest
public void testWithProperties_defaultSplitFreqByDist() throws Exception {
functionalTest(R.string.split_frequency_key, 5);
}
@MediumTest
public void testWithProperties_defaultSplitFreqByTime() throws Exception {
functionalTest(R.string.split_frequency_key, -2);
}
@MediumTest
public void testWithProperties_noMetricUnits() throws Exception {
functionalTest(R.string.metric_units_key, (Object) null);
}
@MediumTest
public void testWithProperties_metricUnitsEnabled() throws Exception {
functionalTest(R.string.metric_units_key, true);
}
@MediumTest
public void testWithProperties_metricUnitsDisabled() throws Exception {
functionalTest(R.string.metric_units_key, false);
}
// TODO: Add the following tests: // TODO: Add the following tests:
// R.string.metric_units_key
// R.string.min_recording_interval_key // R.string.min_recording_interval_key
// R.string.min_required_accuracy_key // R.string.min_required_accuracy_key
// R.string.signal_sampling_frequency_key
// R.string.split_frequency_key
private ITrackRecordingService bindAndGetService(Intent intent) { private ITrackRecordingService bindAndGetService(Intent intent) {
ITrackRecordingService service = ITrackRecordingService.Stub.asInterface( ITrackRecordingService service = ITrackRecordingService.Stub.asInterface(
@@ -607,7 +649,10 @@ public class TrackRecordingServiceTest
editor.putLong(context.getString(R.string.recording_track_key), id); editor.putLong(context.getString(R.string.recording_track_key), id);
editor.commit(); editor.commit();
} }
// TODO: We support multiple values for readability, however this test's
// base class doesn't properly shutdown the service, so it's not possible
// to pass more than 1 value at a time.
private void functionalTest(int resourceId, Object ...values) private void functionalTest(int resourceId, Object ...values)
throws Exception { throws Exception {
final String key = context.getString(resourceId); final String key = context.getString(resourceId);
@@ -621,6 +666,8 @@ public class TrackRecordingServiceTest
editor.putLong(key, (Long) value); editor.putLong(key, (Long) value);
} else if (value instanceof Integer) { } else if (value instanceof Integer) {
editor.putInt(key, (Integer) value); editor.putInt(key, (Integer) value);
} else if (value instanceof Boolean) {
editor.putBoolean(key, (Boolean) value);
} else if (value == null) { } else if (value == null) {
// Do nothing, as clear above has already removed this property. // Do nothing, as clear above has already removed this property.
} }
@@ -645,6 +692,8 @@ public class TrackRecordingServiceTest
context.getString(R.string.recording_track_key), -1)); context.getString(R.string.recording_track_key), -1));
assertEquals(id, service.getRecordingTrackId()); assertEquals(id, service.getRecordingTrackId());
// TODO: Add a few locations, insert markers, etc.
// Stop the track. Validate if it has correct data. // Stop the track. Validate if it has correct data.
service.endCurrentTrack(); service.endCurrentTrack();
assertFalse(service.isRecording()); assertFalse(service.isRecording());