1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-10 20:19:32 +01:00
Gadgetbridge/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java

51 lines
1.5 KiB
Java
Raw Normal View History

package nodomain.freeyourgadget.gadgetbridge.service;
import android.app.Service;
2016-09-10 11:22:26 +02:00
import android.content.Context;
import android.content.Intent;
import org.robolectric.Robolectric;
2017-04-21 22:30:29 +02:00
import org.robolectric.android.controller.ServiceController;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
/**
* Extends GBDeviceServer so that communication with the service works
* with Robolectric.
*/
class TestDeviceService extends GBDeviceService {
private final ServiceController<DeviceCommunicationService> serviceController;
private final DeviceCommunicationService service;
TestDeviceService(Context context) throws Exception {
2016-09-10 11:22:26 +02:00
super(context);
serviceController = Robolectric.buildService(DeviceCommunicationService.class, createIntent());
service = serviceController.create().get();
}
@Override
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);
}
@Override
public void start() {
super.start();
}
@Override
protected void stopService(Intent intent) {
super.stopService(intent);
serviceController.destroy();
}
@Override
public Intent createIntent() {
return super.createIntent();
}
}