2015-08-22 01:08:46 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service;
|
|
|
|
|
2016-09-10 10:52:02 +02:00
|
|
|
import android.app.Service;
|
2016-09-10 11:22:26 +02:00
|
|
|
import android.content.Context;
|
2015-08-22 01:08:46 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
|
2016-09-10 10:52:02 +02:00
|
|
|
import org.robolectric.Robolectric;
|
2017-04-21 22:30:29 +02:00
|
|
|
import org.robolectric.android.controller.ServiceController;
|
2016-09-10 10:52:02 +02:00
|
|
|
|
2015-08-22 01:08:46 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
|
|
|
|
|
2016-09-10 10:52:02 +02:00
|
|
|
/**
|
|
|
|
* Extends GBDeviceServer so that communication with the service works
|
|
|
|
* with Robolectric.
|
|
|
|
*/
|
2016-12-31 18:56:24 +01:00
|
|
|
class TestDeviceService extends GBDeviceService {
|
2016-09-10 10:52:02 +02:00
|
|
|
private final ServiceController<DeviceCommunicationService> serviceController;
|
|
|
|
private final DeviceCommunicationService service;
|
2015-08-22 01:08:46 +02:00
|
|
|
|
2016-12-31 18:56:24 +01:00
|
|
|
TestDeviceService(Context context) throws Exception {
|
2016-09-10 11:22:26 +02:00
|
|
|
super(context);
|
2016-09-10 10:52:02 +02:00
|
|
|
|
|
|
|
serviceController = Robolectric.buildService(DeviceCommunicationService.class, createIntent());
|
|
|
|
service = serviceController.create().get();
|
2015-08-22 01:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-09-10 10:52:02 +02:00
|
|
|
protected void invokeService(Intent intent) {
|
|
|
|
// calling though to the service natively does not work with robolectric,
|
|
|
|
// we have to use the ServiceController to do that
|
|
|
|
service.onStartCommand(intent, Service.START_FLAG_REDELIVERY, (int) (Math.random() * 10000));
|
2017-01-24 22:04:05 +01:00
|
|
|
super.invokeService(intent);
|
2015-08-22 01:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-09-10 10:52:02 +02:00
|
|
|
public void start() {
|
|
|
|
super.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void stopService(Intent intent) {
|
|
|
|
super.stopService(intent);
|
|
|
|
serviceController.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Intent createIntent() {
|
|
|
|
return super.createIntent();
|
2015-08-22 01:08:46 +02:00
|
|
|
}
|
|
|
|
}
|