1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-24 10:56:50 +01:00

Even more service testcase cleanup

This commit is contained in:
cpfeiffer 2016-09-10 11:22:26 +02:00
parent c31049839a
commit bfffd64b65
5 changed files with 11 additions and 95 deletions

View File

@ -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;
}
}

View File

@ -10,11 +10,12 @@ import org.mockito.Mockito;
import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.test.TestBase;
import static org.junit.Assert.assertEquals;
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();
/**
@ -49,7 +50,7 @@ public class DeviceCommunicationServiceTestCase extends AbstractServiceTestCase<
mockSupport = Mockito.spy(realSupport);
DeviceCommunicationService.setDeviceSupportFactory(new TestDeviceSupportFactory(getContext()));
mDeviceService = new TestDeviceService(this);
mDeviceService = new TestDeviceService(getContext());
}
protected GBDevice getDevice() {

View File

@ -1,6 +1,7 @@
package nodomain.freeyourgadget.gadgetbridge.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import org.robolectric.Robolectric;
@ -13,13 +14,11 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
* with Robolectric.
*/
public class TestDeviceService extends GBDeviceService {
private final AbstractServiceTestCase<?> mTestCase;
private final ServiceController<DeviceCommunicationService> serviceController;
private final DeviceCommunicationService service;
public TestDeviceService(AbstractServiceTestCase<?> testCase) throws Exception {
super(testCase.getContext());
mTestCase = testCase;
public TestDeviceService(Context context) throws Exception {
super(context);
serviceController = Robolectric.buildService(DeviceCommunicationService.class, createIntent());
service = serviceController.create().get();

View File

@ -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);
}
}

View File

@ -1,5 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge.test;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import org.junit.After;
@ -63,6 +64,7 @@ public abstract class TestBase {
@Before
public void setUp() throws Exception {
assertNotNull(app);
assertNotNull(getContext());
// doesn't work with Robolectric yet
// dbHandler = GBApplication.acquireDB();
// daoSession = dbHandler.getDaoSession();
@ -84,4 +86,7 @@ public abstract class TestBase {
return dummyGBDevice;
}
protected Context getContext() {
return app;
}
}