55
55
import de .dennisguse .opentracks .io .file .TrackFileFormat ;
56
56
import de .dennisguse .opentracks .io .file .exporter .TrackExporter ;
57
57
import de .dennisguse .opentracks .services .TrackRecordingService ;
58
+ import de .dennisguse .opentracks .services .TrackRecordingServiceTest ;
58
59
import de .dennisguse .opentracks .services .handlers .TrackPointCreator ;
59
60
import de .dennisguse .opentracks .services .sensors .AltitudeSumManager ;
60
61
import de .dennisguse .opentracks .services .sensors .BluetoothRemoteSensorManager ;
@@ -98,38 +99,51 @@ public static void preSetUp() {
98
99
private TrackImporter trackImporter ;
99
100
100
101
@ Before
101
- public void fileSetup () throws IOException {
102
+ public void fileSetup () throws IOException , TimeoutException {
103
+ TrackRecordingServiceTest .resetService (mServiceRule , context );
104
+
102
105
tmpFile = File .createTempFile ("test" , "test" , context .getFilesDir ());
103
106
tmpFileUri = Uri .fromFile (tmpFile );
104
107
105
108
trackImporter = new TrackImporter (context , contentProviderUtils , Distance .of (10 ), Distance .of (200 ), true );
109
+
110
+ TrackRecordingServiceTest .resetService (mServiceRule , context );
106
111
}
107
112
108
113
@ After
109
- public void FileTearDown () throws TimeoutException {
114
+ public void tearDown () throws TimeoutException {
110
115
tmpFile .deleteOnExit ();
111
116
tmpFileUri = null ;
112
117
113
- TrackRecordingService service = ((TrackRecordingService .Binder ) mServiceRule .bindService (new Intent (context , TrackRecordingService .class )))
114
- .getService ();
115
- service .getTrackPointCreator ().setClock (Clock .systemUTC ());
118
+ // Ensure that the database is empty after every test
119
+ contentProviderUtils .deleteAllTracks (context );
120
+
121
+ TrackRecordingServiceTest .resetService (mServiceRule , context );
116
122
}
117
123
118
- public void setUp (boolean hasSensorDistance ) throws TimeoutException {
124
+ public void setUp () throws TimeoutException {
119
125
TrackRecordingService service = ((TrackRecordingService .Binder ) mServiceRule .bindService (new Intent (context , TrackRecordingService .class )))
120
126
.getService ();
121
127
122
128
TrackPointCreator trackPointCreator = service .getTrackPointCreator ();
123
129
124
130
trackPointCreator .setClock (Clock .fixed (Instant .parse ("2020-02-02T02:02:02Z" ), ZoneId .of ("CET" )));
125
131
trackId = service .startNewTrack ();
132
+ service .stopUpdateRecordingData ();
126
133
127
- Distance sensorDistance = hasSensorDistance ? Distance .of (10 ) : null ; // recording distance interval
134
+ Distance sensorDistance = Distance .of (10 ); // recording distance interval
128
135
129
- sendLocation (trackPointCreator , Instant .parse ("2020-02-02T02:02:03Z" ), 3 , 14 , 10 , 15 , 10 , 1 , 66 , 3 , 50 , sensorDistance );
136
+ mockBLESensorData (trackPointCreator , 15f , sensorDistance , 66f , 3f , 50f );
137
+ sendLocation (trackPointCreator , Instant .parse ("2020-02-02T02:02:03Z" ), 3 , 14 , 10 , 15 , 10 , 1 );
130
138
service .insertMarker ("Marker 1" , "Marker 1 category" , "Marker 1 desc" , null );
131
- sendLocation (trackPointCreator , Instant .parse ("2020-02-02T02:02:04Z" ), 3 , 14.001 , 10 , 15 , 10 , 0 , 66 , 3 , 50 , sensorDistance );
132
- sendLocation (trackPointCreator , Instant .parse ("2020-02-02T02:02:05Z" ), 3 , 14.002 , 10 , 15 , 10 , 0 , 66 , 3 , 50 , sensorDistance );
139
+
140
+ // A sensor-only TrackPoint
141
+ mockBLESensorData (trackPointCreator , 15f , sensorDistance , 66f , 3f , 50f );
142
+ mockAltitudeChange (trackPointCreator , 1 );
143
+ sendSensor (trackPointCreator , Instant .parse ("2020-02-02T02:02:04Z" ));
144
+
145
+ mockBLESensorData (trackPointCreator , 15f , sensorDistance , 66f , 3f , 50f );
146
+ sendLocation (trackPointCreator , Instant .parse ("2020-02-02T02:02:05Z" ), 3 , 14.002 , 10 , 15 , 10 , 0 );
133
147
service .insertMarker ("Marker 2" , "Marker 2 category" , "Marker 2 desc" , null );
134
148
135
149
trackPointCreator .setClock (Clock .fixed (Instant .parse ("2020-02-02T02:02:06Z" ), ZoneId .of ("CET" )));
@@ -138,12 +152,17 @@ public void setUp(boolean hasSensorDistance) throws TimeoutException {
138
152
139
153
trackPointCreator .setClock (Clock .fixed (Instant .parse ("2020-02-02T02:02:20Z" ), ZoneId .of ("CET" )));
140
154
service .resumeCurrentTrack ();
155
+ service .stopUpdateRecordingData ();
141
156
142
157
trackPointCreator .setClock (Clock .fixed (Instant .parse ("2020-02-02T02:02:21Z" ), ZoneId .of ("CET" )));
143
- sendLocation (trackPointCreator , Instant .parse ("2020-02-02T02:02:21Z" ), 3 , 14.003 , 10 , 15 , 10 , 0 , 66 , 3 , 50 , sensorDistance );
158
+ mockBLESensorData (trackPointCreator , 15f , sensorDistance , 66f , 3f , 50f );
159
+ sendLocation (trackPointCreator , Instant .parse ("2020-02-02T02:02:21Z" ), 3 , 14.003 , 10 , 15 , 10 , 0 );
160
+
161
+ mockBLESensorData (trackPointCreator , 15f , sensorDistance , 66f , 3f , 50f );
162
+ sendLocation (trackPointCreator , Instant .parse ("2020-02-02T02:02:22Z" ), 3 , 16 , 10 , 15 , 10 , 0 );
144
163
145
- sendLocation (trackPointCreator , Instant . parse ( "2020-02-02T02:02:22Z" ), 3 , 16 , 10 , 15 , 10 , 0 , 66 , 3 , 50 , sensorDistance );
146
- sendLocation (trackPointCreator , Instant .parse ("2020-02-02T02:02:23Z" ), 3 , 16.001 , 10 , 15 , 10 , 0 , 66 , 3 , 50 , sensorDistance );
164
+ mockBLESensorData (trackPointCreator , 15f , sensorDistance , 66f , 3f , 50f );
165
+ sendLocation (trackPointCreator , Instant .parse ("2020-02-02T02:02:23Z" ), 3 , 16.001 , 10 , 15 , 10 , 0 );
147
166
148
167
trackPointCreator .setClock (Clock .fixed (Instant .parse ("2020-02-02T02:02:24Z" ), ZoneId .of ("CET" )));
149
168
trackPointCreator .setRemoteSensorManager (new BluetoothRemoteSensorManager (context ));
@@ -162,21 +181,11 @@ public void setUp(boolean hasSensorDistance) throws TimeoutException {
162
181
assertEquals (2 , markers .size ());
163
182
}
164
183
165
- @ After
166
- public void tearDown () {
167
- if (trackId != null ) {
168
- contentProviderUtils .deleteTrack (context , trackId );
169
- }
170
- if (importTrackId != null ) {
171
- contentProviderUtils .deleteTrack (context , importTrackId );
172
- }
173
- }
174
-
175
184
//TODO Does not test images
176
185
@ LargeTest
177
186
@ Test
178
187
public void kmz_with_trackdetail_and_sensordata () throws TimeoutException , IOException {
179
- setUp (true );
188
+ setUp ();
180
189
181
190
// given
182
191
Track track = contentProviderUtils .getTrack (trackId );
@@ -213,23 +222,40 @@ public void kmz_with_trackdetail_and_sensordata() throws TimeoutException, IOExc
213
222
assertEquals (Instant .parse ("2020-02-02T02:02:02Z" ), importedTrackStatistics .getStartTime ());
214
223
assertEquals (Instant .parse ("2020-02-02T02:02:24Z" ), importedTrackStatistics .getStopTime ());
215
224
216
- assertEquals (track .getTrackStatistics ().getTotalTime (), importedTrackStatistics .getTotalTime ());
225
+ TrackStatistics originalTrackStatistics = track .getTrackStatistics ();
226
+
227
+ assertEquals (originalTrackStatistics .getTotalTime (), importedTrackStatistics .getTotalTime ());
217
228
assertEquals (Duration .ofSeconds (8 ), importedTrackStatistics .getTotalTime ());
229
+
230
+ assertEquals (originalTrackStatistics .getMovingTime (), importedTrackStatistics .getMovingTime ());
218
231
assertEquals (Duration .ofSeconds (4 ), importedTrackStatistics .getMovingTime ());
219
232
220
233
// Distance
234
+ assertEquals (originalTrackStatistics .getTotalDistance (), importedTrackStatistics .getTotalDistance ());
221
235
assertEquals (Distance .of (60 ), importedTrackStatistics .getTotalDistance ());
222
236
223
237
// Speed
238
+ assertEquals (originalTrackStatistics .getMaxSpeed (), importedTrackStatistics .getMaxSpeed ());
224
239
assertEquals (Speed .of (15 ), importedTrackStatistics .getMaxSpeed ());
240
+
241
+ assertEquals (originalTrackStatistics .getAverageSpeed (), importedTrackStatistics .getAverageSpeed ());
225
242
assertEquals (Speed .of (7.5 ), importedTrackStatistics .getAverageSpeed ());
243
+
244
+ assertEquals (originalTrackStatistics .getAverageMovingSpeed (), importedTrackStatistics .getAverageMovingSpeed ());
226
245
assertEquals (Speed .of (15 ), importedTrackStatistics .getAverageMovingSpeed ());
227
246
228
247
// Altitude
248
+ assertEquals (originalTrackStatistics .getMinAltitude (), importedTrackStatistics .getMinAltitude (), 0.01 );
229
249
assertEquals (10 , importedTrackStatistics .getMinAltitude (), 0.01 );
250
+
251
+ assertEquals (originalTrackStatistics .getMaxAltitude (), importedTrackStatistics .getMaxAltitude (), 0.01 );
230
252
assertEquals (10 , importedTrackStatistics .getMaxAltitude (), 0.01 );
231
- assertEquals (1 , importedTrackStatistics .getTotalAltitudeGain (), 0.01 );
232
- assertEquals (1 , importedTrackStatistics .getTotalAltitudeLoss (), 0.01 );
253
+
254
+ assertEquals (originalTrackStatistics .getTotalAltitudeGain (), importedTrackStatistics .getTotalAltitudeGain (), 0.01 );
255
+ assertEquals (2 , importedTrackStatistics .getTotalAltitudeGain (), 0.01 );
256
+
257
+ assertEquals (originalTrackStatistics .getTotalAltitudeLoss (), importedTrackStatistics .getTotalAltitudeLoss (), 0.01 );
258
+ assertEquals (2 , importedTrackStatistics .getTotalAltitudeLoss (), 0.01 );
233
259
234
260
// 4. markers
235
261
assertMarkers ();
@@ -238,7 +264,7 @@ public void kmz_with_trackdetail_and_sensordata() throws TimeoutException, IOExc
238
264
@ LargeTest
239
265
@ Test (expected = ImportAlreadyExistsException .class )
240
266
public void kml_with_trackdetail_and_sensordata_duplicate_trackUUID () throws TimeoutException , IOException {
241
- setUp (false );
267
+ setUp ();
242
268
243
269
// given
244
270
SharedPreferences .Editor editor = PreferenceManager .getDefaultSharedPreferences (context ).edit ();
@@ -264,7 +290,7 @@ public void kml_with_trackdetail_and_sensordata_duplicate_trackUUID() throws Tim
264
290
@ LargeTest
265
291
@ Test
266
292
public void gpx () throws TimeoutException , IOException {
267
- setUp (true );
293
+ setUp ();
268
294
269
295
// given
270
296
Track track = contentProviderUtils .getTrack (trackId );
@@ -297,7 +323,10 @@ public void gpx() throws TimeoutException, IOException {
297
323
// Therefore, the track segmentation is changes.
298
324
List <TrackPoint > trackPointsWithCoordinates = trackPoints .stream ().filter (it -> TrackPoint .Type .SEGMENT_START_AUTOMATIC .equals (it .getType ()) || TrackPoint .Type .TRACKPOINT .equals (it .getType ())).collect (Collectors .toList ());
299
325
trackPointsWithCoordinates .get (0 ).setType (TrackPoint .Type .SEGMENT_START_AUTOMATIC );
300
- trackPointsWithCoordinates .get (3 ).setType (TrackPoint .Type .SEGMENT_START_AUTOMATIC );
326
+ trackPointsWithCoordinates .get (1 ).setSensorDistance (Distance .of (20 ));
327
+ trackPointsWithCoordinates .get (1 ).setAltitudeGain (1f );
328
+ trackPointsWithCoordinates .get (1 ).setAltitudeLoss (1f );
329
+ trackPointsWithCoordinates .get (2 ).setType (TrackPoint .Type .SEGMENT_START_AUTOMATIC );
301
330
302
331
TrackPointAssert a = new TrackPointAssert ()
303
332
.setDelta (0.05 )
@@ -326,8 +355,8 @@ public void gpx() throws TimeoutException, IOException {
326
355
// Altitude
327
356
assertEquals (10 , importedTrackStatistics .getMinAltitude (), 0.01 );
328
357
assertEquals (10 , importedTrackStatistics .getMaxAltitude (), 0.01 );
329
- assertEquals (1 , importedTrackStatistics .getTotalAltitudeGain (), 0.01 );
330
- assertEquals (1 , importedTrackStatistics .getTotalAltitudeLoss (), 0.01 );
358
+ assertEquals (2 , importedTrackStatistics .getTotalAltitudeGain (), 0.01 );
359
+ assertEquals (2 , importedTrackStatistics .getTotalAltitudeLoss (), 0.01 );
331
360
332
361
// 4. markers
333
362
assertMarkers ();
@@ -336,7 +365,7 @@ public void gpx() throws TimeoutException, IOException {
336
365
@ LargeTest
337
366
@ Test (expected = ImportAlreadyExistsException .class )
338
367
public void gpx_duplicate_trackUUID () throws TimeoutException , IOException {
339
- setUp (false );
368
+ setUp ();
340
369
341
370
// given
342
371
SharedPreferences .Editor editor = PreferenceManager .getDefaultSharedPreferences (context ).edit ();
@@ -380,22 +409,7 @@ private void assertMarkers() {
380
409
}
381
410
}
382
411
383
- private void sendLocation (TrackPointCreator trackPointCreator , Instant time , double latitude , double longitude , float accuracy , float speed , float altitude , float altitudeGain , float heartRate , float cyclingCadence , float power , Distance distance ) {
384
- Location location = new Location ("mock" );
385
- location .setLatitude (latitude );
386
- location .setLongitude (longitude );
387
- location .setAccuracy (accuracy );
388
- location .setSpeed (speed );
389
- location .setAltitude (altitude );
390
-
391
- trackPointCreator .setAltitudeSumManager (new AltitudeSumManager () {
392
- @ Override
393
- public void fill (@ NonNull TrackPoint trackPoint ) {
394
- trackPoint .setAltitudeGain (altitudeGain );
395
- trackPoint .setAltitudeLoss (altitudeGain );
396
- }
397
- });
398
-
412
+ private void mockBLESensorData (TrackPointCreator trackPointCreator , Float speed , Distance distance , Float heartRate , Float cyclingCadence , Float power ) {
399
413
trackPointCreator .setRemoteSensorManager (new BluetoothRemoteSensorManager (context ) {
400
414
@ Override
401
415
public SensorDataSet fill (@ NonNull TrackPoint trackPoint ) {
@@ -421,8 +435,30 @@ public SensorDataSet fill(@NonNull TrackPoint trackPoint) {
421
435
return sensorDataSet ;
422
436
}
423
437
});
438
+ }
439
+
440
+ private void mockAltitudeChange (TrackPointCreator trackPointCreator , float altitudeGain ) {
441
+ AltitudeSumManager altitudeSumManager = trackPointCreator .getAltitudeSumManager ();
442
+ altitudeSumManager .setAltitudeGain_m (altitudeGain );
443
+ altitudeSumManager .setAltitudeLoss_m (altitudeGain );
444
+ }
445
+
446
+ private void sendSensor (TrackPointCreator trackPointCreator , Instant time ) {
447
+ trackPointCreator .setClock (Clock .fixed (time , ZoneId .of ("CET" )));
448
+ trackPointCreator .onNewTrackPointWithoutGPS ();
449
+ }
450
+
451
+ private void sendLocation (TrackPointCreator trackPointCreator , Instant time , double latitude , double longitude , float accuracy , float speed , float altitude , float altitudeGain ) {
452
+ Location location = new Location ("mock" );
453
+ location .setLatitude (latitude );
454
+ location .setLongitude (longitude );
455
+ location .setAccuracy (accuracy );
456
+ location .setSpeed (speed );
457
+ location .setAltitude (altitude );
458
+
459
+ mockAltitudeChange (trackPointCreator , altitudeGain );
424
460
425
461
trackPointCreator .setClock (Clock .fixed (time , ZoneId .of ("CET" )));
426
- trackPointCreator .getLocationHandler ().onLocationChanged (location );
462
+ trackPointCreator .getGpsHandler ().onLocationChanged (location );
427
463
}
428
464
}
0 commit comments