Even more service testcase cleanup
This commit is contained in:
parent
c31049839a
commit
bfffd64b65
|
@ -1,47 +0,0 @@
|
||||||
package nodomain.freeyourgadget.gadgetbridge.service;
|
|
||||||
|
|
||||||
import android.app.Application;
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.app.Service;
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
import org.robolectric.RuntimeEnvironment;
|
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.test.MockHelper;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.test.TestBase;
|
|
||||||
|
|
||||||
public abstract class AbstractServiceTestCase<T extends Service> extends TestBase {
|
|
||||||
private Context mContext;
|
|
||||||
private GBApplication mApplication;
|
|
||||||
private NotificationManager mNotificationManager;
|
|
||||||
private MockHelper mMockHelper;
|
|
||||||
|
|
||||||
protected AbstractServiceTestCase() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Context getContext() {
|
|
||||||
return mContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected MockHelper getmMockHelper() {
|
|
||||||
return mMockHelper;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setUp() throws Exception {
|
|
||||||
super.setUp();
|
|
||||||
mMockHelper = new MockHelper();
|
|
||||||
mApplication = (GBApplication) RuntimeEnvironment.application;
|
|
||||||
mContext = mApplication;
|
|
||||||
mNotificationManager = mMockHelper.createNotificationManager(mContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Application getApplication() {
|
|
||||||
return mApplication;
|
|
||||||
}
|
|
||||||
|
|
||||||
private NotificationManager getNotificationService() {
|
|
||||||
return mNotificationManager;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,11 +10,12 @@ import org.mockito.Mockito;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.test.TestBase;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class DeviceCommunicationServiceTestCase extends AbstractServiceTestCase<DeviceCommunicationService> {
|
public class DeviceCommunicationServiceTestCase extends TestBase {
|
||||||
private static final java.lang.String TEST_DEVICE_ADDRESS = TestDeviceSupport.class.getName();
|
private static final java.lang.String TEST_DEVICE_ADDRESS = TestDeviceSupport.class.getName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,7 +50,7 @@ public class DeviceCommunicationServiceTestCase extends AbstractServiceTestCase<
|
||||||
mockSupport = Mockito.spy(realSupport);
|
mockSupport = Mockito.spy(realSupport);
|
||||||
DeviceCommunicationService.setDeviceSupportFactory(new TestDeviceSupportFactory(getContext()));
|
DeviceCommunicationService.setDeviceSupportFactory(new TestDeviceSupportFactory(getContext()));
|
||||||
|
|
||||||
mDeviceService = new TestDeviceService(this);
|
mDeviceService = new TestDeviceService(getContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected GBDevice getDevice() {
|
protected GBDevice getDevice() {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package nodomain.freeyourgadget.gadgetbridge.service;
|
package nodomain.freeyourgadget.gadgetbridge.service;
|
||||||
|
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
|
||||||
import org.robolectric.Robolectric;
|
import org.robolectric.Robolectric;
|
||||||
|
@ -13,13 +14,11 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
|
||||||
* with Robolectric.
|
* with Robolectric.
|
||||||
*/
|
*/
|
||||||
public class TestDeviceService extends GBDeviceService {
|
public class TestDeviceService extends GBDeviceService {
|
||||||
private final AbstractServiceTestCase<?> mTestCase;
|
|
||||||
private final ServiceController<DeviceCommunicationService> serviceController;
|
private final ServiceController<DeviceCommunicationService> serviceController;
|
||||||
private final DeviceCommunicationService service;
|
private final DeviceCommunicationService service;
|
||||||
|
|
||||||
public TestDeviceService(AbstractServiceTestCase<?> testCase) throws Exception {
|
public TestDeviceService(Context context) throws Exception {
|
||||||
super(testCase.getContext());
|
super(context);
|
||||||
mTestCase = testCase;
|
|
||||||
|
|
||||||
serviceController = Robolectric.buildService(DeviceCommunicationService.class, createIntent());
|
serviceController = Robolectric.buildService(DeviceCommunicationService.class, createIntent());
|
||||||
service = serviceController.create().get();
|
service = serviceController.create().get();
|
||||||
|
|
|
@ -1,42 +0,0 @@
|
||||||
package nodomain.freeyourgadget.gadgetbridge.test;
|
|
||||||
|
|
||||||
import android.app.Application;
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.app.Service;
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
import junit.framework.Assert;
|
|
||||||
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
|
|
||||||
|
|
||||||
public class MockHelper {
|
|
||||||
public <T extends Service> NotificationManager createNotificationManager(Context mContext) throws Exception {
|
|
||||||
return (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T extends Service> T createService(Class<T> serviceClass, Application application) throws Exception {
|
|
||||||
Constructor<T> constructor = serviceClass.getConstructor();
|
|
||||||
Assert.assertNotNull(constructor);
|
|
||||||
T realService = constructor.newInstance();
|
|
||||||
T mockedService = Mockito.spy(realService);
|
|
||||||
Mockito.when(mockedService.getApplicationContext()).thenReturn(application);
|
|
||||||
Mockito.when(mockedService.getPackageManager()).thenReturn(application.getPackageManager());
|
|
||||||
return mockedService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T extends DeviceCommunicationService> T createDeviceCommunicationService(Class<T> serviceClass, GBApplication application) throws Exception {
|
|
||||||
T mockedService = createService(serviceClass, application);
|
|
||||||
Mockito.when(mockedService.getPrefs()).thenReturn(GBApplication.getPrefs());
|
|
||||||
Mockito.when(mockedService.getGBPrefs()).thenReturn(GBApplication.getGBPrefs());
|
|
||||||
return mockedService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addSystemServiceTo(Context context, String serviceName, Object service) {
|
|
||||||
Mockito.when(context.getSystemService(serviceName)).thenReturn(service);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,6 @@
|
||||||
package nodomain.freeyourgadget.gadgetbridge.test;
|
package nodomain.freeyourgadget.gadgetbridge.test;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
|
@ -63,6 +64,7 @@ public abstract class TestBase {
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
assertNotNull(app);
|
assertNotNull(app);
|
||||||
|
assertNotNull(getContext());
|
||||||
// doesn't work with Robolectric yet
|
// doesn't work with Robolectric yet
|
||||||
// dbHandler = GBApplication.acquireDB();
|
// dbHandler = GBApplication.acquireDB();
|
||||||
// daoSession = dbHandler.getDaoSession();
|
// daoSession = dbHandler.getDaoSession();
|
||||||
|
@ -84,4 +86,7 @@ public abstract class TestBase {
|
||||||
return dummyGBDevice;
|
return dummyGBDevice;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Context getContext() {
|
||||||
|
return app;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue