forked from upstream-mirrors/OpenTracks
Fixed ControlRecordingService: only unbind if connected.
This commit is contained in:
+8
-6
@@ -25,7 +25,9 @@ import androidx.test.rule.ServiceTestRule;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
@@ -40,16 +42,17 @@ import static org.mockito.Mockito.when;
|
|||||||
*
|
*
|
||||||
* @author Youtao Liu
|
* @author Youtao Liu
|
||||||
*/
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class ControlRecordingServiceTest {
|
public class ControlRecordingServiceTest {
|
||||||
|
|
||||||
|
private Context context = ApplicationProvider.getApplicationContext();
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public final ServiceTestRule mServiceRule = new ServiceTestRule();
|
public final ServiceTestRule mServiceRule = new ServiceTestRule();
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);
|
public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);
|
||||||
|
|
||||||
private Context context = ApplicationProvider.getApplicationContext();
|
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private ITrackRecordingService iTrackRecordingServiceMock;
|
private ITrackRecordingService iTrackRecordingServiceMock;
|
||||||
|
|
||||||
@@ -68,7 +71,7 @@ public class ControlRecordingServiceTest {
|
|||||||
|
|
||||||
when(iTrackRecordingServiceMock.startNewTrack()).thenReturn(1L);
|
when(iTrackRecordingServiceMock.startNewTrack()).thenReturn(1L);
|
||||||
subject.onHandleIntent(intent, iTrackRecordingServiceMock);
|
subject.onHandleIntent(intent, iTrackRecordingServiceMock);
|
||||||
verify(iTrackRecordingServiceMock);
|
verify(iTrackRecordingServiceMock).startNewTrack();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -79,10 +82,9 @@ public class ControlRecordingServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testStopRecording() throws TimeoutException {
|
public void testStopRecording() throws TimeoutException {
|
||||||
Intent intent = startControlRecordingService(context.getString(R.string.track_action_end));
|
Intent intent = startControlRecordingService(context.getString(R.string.track_action_end));
|
||||||
iTrackRecordingServiceMock.endCurrentTrack();
|
|
||||||
|
|
||||||
subject.onHandleIntent(intent, iTrackRecordingServiceMock);
|
subject.onHandleIntent(intent, iTrackRecordingServiceMock);
|
||||||
verify(iTrackRecordingServiceMock);
|
verify(iTrackRecordingServiceMock).endCurrentTrack();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -94,7 +96,7 @@ public class ControlRecordingServiceTest {
|
|||||||
Intent intent = new Intent(context, ControlRecordingService.class);
|
Intent intent = new Intent(context, ControlRecordingService.class);
|
||||||
intent.setAction(action);
|
intent.setAction(action);
|
||||||
mServiceRule.startService(intent);
|
mServiceRule.startService(intent);
|
||||||
subject = ((ControlRecordingService) mServiceRule.bindService(intent));
|
subject = ((ControlRecordingService.LocalBinder) mServiceRule.bindService(intent)).getService();
|
||||||
return intent;
|
return intent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,12 +20,14 @@ import android.app.IntentService;
|
|||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
|
import android.os.Binder;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
import de.dennisguse.opentracks.widgets.TrackWidgetProvider;
|
|
||||||
import de.dennisguse.opentracks.R;
|
import de.dennisguse.opentracks.R;
|
||||||
|
import de.dennisguse.opentracks.widgets.TrackWidgetProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A service to control starting and stopping of a recording. This service,
|
* A service to control starting and stopping of a recording. This service,
|
||||||
@@ -57,6 +59,39 @@ public class ControlRecordingService extends IntentService implements ServiceCon
|
|||||||
bindService(newIntent, this, 0);
|
bindService(newIntent, this, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public IBinder onBind(Intent intent) {
|
||||||
|
return new LocalBinder();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the intent to start or stop a recording.
|
||||||
|
*
|
||||||
|
* @param intent to be handled
|
||||||
|
* @param service the trackRecordingService
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
void onHandleIntent(Intent intent, ITrackRecordingService service) {
|
||||||
|
String action = intent.getAction();
|
||||||
|
if (action != null) {
|
||||||
|
if (action.equals(getString(R.string.track_action_start))) {
|
||||||
|
service.startNewTrack();
|
||||||
|
} else if (action.equals(getString(R.string.track_action_end))) {
|
||||||
|
service.endCurrentTrack();
|
||||||
|
} else if (action.equals(getString(R.string.track_action_pause))) {
|
||||||
|
service.pauseCurrentTrack();
|
||||||
|
} else if (action.equals(getString(R.string.track_action_resume))) {
|
||||||
|
service.resumeCurrentTrack();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (connected) {
|
||||||
|
unbindService(this);
|
||||||
|
connected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||||
trackRecordingService = (ITrackRecordingService) service;
|
trackRecordingService = (ITrackRecordingService) service;
|
||||||
@@ -97,31 +132,14 @@ public class ControlRecordingService extends IntentService implements ServiceCon
|
|||||||
onHandleIntent(intent, trackRecordingService);
|
onHandleIntent(intent, trackRecordingService);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the intent to start or stop a recording.
|
|
||||||
*
|
|
||||||
* @param intent to be handled
|
|
||||||
* @param service the trackRecordingService
|
|
||||||
*/
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void onHandleIntent(Intent intent, ITrackRecordingService service) {
|
public class LocalBinder extends Binder {
|
||||||
String action = intent.getAction();
|
ControlRecordingService getService() {
|
||||||
if (action != null) {
|
return ControlRecordingService.this;
|
||||||
if (action.equals(getString(R.string.track_action_start))) {
|
|
||||||
service.startNewTrack();
|
|
||||||
} else if (action.equals(getString(R.string.track_action_end))) {
|
|
||||||
service.endCurrentTrack();
|
|
||||||
} else if (action.equals(getString(R.string.track_action_pause))) {
|
|
||||||
service.pauseCurrentTrack();
|
|
||||||
} else if (action.equals(getString(R.string.track_action_resume))) {
|
|
||||||
service.resumeCurrentTrack();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
unbindService(this);
|
|
||||||
connected = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|||||||
Reference in New Issue
Block a user