mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-28 04:46:51 +01:00
Make DeviceCommunicationServiceTestCase runnable with robolectric
- enables the test for travis - tests operation when not connected - tests connecting - tests operation when connected
This commit is contained in:
parent
ee0f1f23c0
commit
c31049839a
@ -61,6 +61,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
|
||||
private Context context;
|
||||
private boolean autoReconnect;
|
||||
|
||||
@Override
|
||||
public void setContext(GBDevice gbDevice, BluetoothAdapter btAdapter, Context context) {
|
||||
this.gbDevice = gbDevice;
|
||||
this.btAdapter = btAdapter;
|
||||
@ -281,11 +282,11 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
|
||||
(BatteryState.BATTERY_LOW.equals(deviceEvent.state) ||
|
||||
BatteryState.BATTERY_NORMAL.equals(deviceEvent.state))
|
||||
) {
|
||||
GB.updateBatteryNotification(context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), deviceEvent.level),
|
||||
GB.updateBatteryNotification(context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), String.valueOf(deviceEvent.level)),
|
||||
deviceEvent.extendedInfoAvailable() ?
|
||||
context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), deviceEvent.level) + "\n" +
|
||||
context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), String.valueOf(deviceEvent.level)) + "\n" +
|
||||
context.getString(R.string.notif_battery_low_bigtext_last_charge_time, DateFormat.getDateTimeInstance().format(deviceEvent.lastChargeTime.getTime())) +
|
||||
context.getString(R.string.notif_battery_low_bigtext_number_of_charges, deviceEvent.numCharges)
|
||||
context.getString(R.string.notif_battery_low_bigtext_number_of_charges, String.valueOf(deviceEvent.numCharges))
|
||||
: ""
|
||||
, context);
|
||||
}
|
||||
|
@ -120,6 +120,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_URI
|
||||
|
||||
public class DeviceCommunicationService extends Service implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(DeviceCommunicationService.class);
|
||||
private static DeviceSupportFactory DEVICE_SUPPORT_FACTORY = null;
|
||||
|
||||
private boolean mStarted = false;
|
||||
|
||||
@ -138,6 +139,19 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||
|
||||
private Random mRandom = new Random();
|
||||
|
||||
/**
|
||||
* For testing!
|
||||
*
|
||||
* @param factory
|
||||
*/
|
||||
public static void setDeviceSupportFactory(DeviceSupportFactory factory) {
|
||||
DEVICE_SUPPORT_FACTORY = factory;
|
||||
}
|
||||
|
||||
public DeviceCommunicationService() {
|
||||
|
||||
}
|
||||
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
@ -178,13 +192,20 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||
LOG.debug("DeviceCommunicationService is being created");
|
||||
super.onCreate();
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED));
|
||||
mFactory = new DeviceSupportFactory(this);
|
||||
mFactory = getDeviceSupportFactory();
|
||||
|
||||
if (hasPrefs()) {
|
||||
getPrefs().getPreferences().registerOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
private DeviceSupportFactory getDeviceSupportFactory() {
|
||||
if (DEVICE_SUPPORT_FACTORY != null) {
|
||||
return DEVICE_SUPPORT_FACTORY;
|
||||
}
|
||||
return new DeviceSupportFactory(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int onStartCommand(Intent intent, int flags, int startId) {
|
||||
|
||||
@ -461,15 +482,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
/**
|
||||
* For testing!
|
||||
*
|
||||
* @param factory
|
||||
*/
|
||||
public void setDeviceSupportFactory(DeviceSupportFactory factory) {
|
||||
mFactory = factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disposes the current DeviceSupport instance (if any) and sets a new device support instance
|
||||
* (if not null).
|
||||
|
@ -4,97 +4,43 @@ import android.app.Application;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.test.GBMockApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.test.GBMockContext;
|
||||
import nodomain.freeyourgadget.gadgetbridge.test.GBMockPackageManager;
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.test.MockHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.test.TestBase;
|
||||
|
||||
public abstract class AbstractServiceTestCase<T extends Service> {
|
||||
private static final int ID = -1; // currently not supported
|
||||
private final Class<T> mServiceClass;
|
||||
private T mServiceInstance;
|
||||
public abstract class AbstractServiceTestCase<T extends Service> extends TestBase {
|
||||
private Context mContext;
|
||||
private GBMockApplication mApplication;
|
||||
private boolean wasStarted;
|
||||
private PackageManager mPackageManager;
|
||||
private GBApplication mApplication;
|
||||
private NotificationManager mNotificationManager;
|
||||
private MockHelper mMockHelper;
|
||||
|
||||
protected AbstractServiceTestCase(Class<T> serviceClass) {
|
||||
mServiceClass = serviceClass;
|
||||
Assert.assertNotNull(serviceClass);
|
||||
protected AbstractServiceTestCase() {
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
return mContext;
|
||||
}
|
||||
|
||||
public T getServiceInstance() {
|
||||
return mServiceInstance;
|
||||
}
|
||||
|
||||
protected MockHelper getmMockHelper() {
|
||||
return mMockHelper;
|
||||
}
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
mMockHelper = new MockHelper();
|
||||
mPackageManager = createPackageManager();
|
||||
mApplication = createApplication(mPackageManager);
|
||||
mContext = createContext(mApplication);
|
||||
mApplication = (GBApplication) RuntimeEnvironment.application;
|
||||
mContext = mApplication;
|
||||
mNotificationManager = mMockHelper.createNotificationManager(mContext);
|
||||
mServiceInstance = createService(mServiceClass, mApplication, mNotificationManager);
|
||||
mServiceInstance.onCreate();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
if (mServiceInstance != null) {
|
||||
stopService();
|
||||
}
|
||||
}
|
||||
|
||||
public void startService(Intent intent) {
|
||||
wasStarted = true;
|
||||
mServiceInstance.onStartCommand(intent, Service.START_FLAG_REDELIVERY, ID);
|
||||
}
|
||||
|
||||
public void stopService() {
|
||||
mServiceInstance.onDestroy();
|
||||
mServiceInstance = null;
|
||||
}
|
||||
|
||||
protected GBMockApplication createApplication(PackageManager packageManager) {
|
||||
return new GBMockApplication(packageManager);
|
||||
}
|
||||
|
||||
protected PackageManager createPackageManager() {
|
||||
return new GBMockPackageManager();
|
||||
}
|
||||
|
||||
protected Application getApplication() {
|
||||
return mApplication;
|
||||
}
|
||||
|
||||
protected Context createContext(final Application application) {
|
||||
return new GBMockContext(application);
|
||||
}
|
||||
|
||||
protected T createService(Class<T> serviceClass, GBMockApplication application, NotificationManager notificationManager) throws Exception {
|
||||
T service = mMockHelper.createService(serviceClass, application);
|
||||
mMockHelper.addSystemServiceTo(service, Context.NOTIFICATION_SERVICE, notificationManager);
|
||||
return service;
|
||||
}
|
||||
|
||||
private NotificationManager getNotificationService() {
|
||||
return mNotificationManager;
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.service;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.mockito.Mock;
|
||||
@ -12,9 +10,8 @@ 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.GBMockApplication;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class DeviceCommunicationServiceTestCase extends AbstractServiceTestCase<DeviceCommunicationService> {
|
||||
@ -40,41 +37,51 @@ public class DeviceCommunicationServiceTestCase extends AbstractServiceTestCase<
|
||||
private TestDeviceSupport mockSupport;
|
||||
|
||||
public DeviceCommunicationServiceTestCase() {
|
||||
super(DeviceCommunicationService.class);
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DeviceCommunicationService createService(Class<DeviceCommunicationService> serviceClass, GBMockApplication application, NotificationManager notificationManager) throws Exception {
|
||||
DeviceCommunicationService service = getmMockHelper().createDeviceCommunicationService(serviceClass, application);
|
||||
getmMockHelper().addSystemServiceTo(service, Context.NOTIFICATION_SERVICE, notificationManager);
|
||||
return service;
|
||||
}
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
mockSupport = null;
|
||||
realSupport = new TestDeviceSupport();
|
||||
realSupport.setContext(new GBDevice(TEST_DEVICE_ADDRESS, "Test Device", DeviceType.TEST), null, getContext());
|
||||
mockSupport = Mockito.spy(realSupport);
|
||||
getServiceInstance().setDeviceSupportFactory(new TestDeviceSupportFactory(getContext()));
|
||||
DeviceCommunicationService.setDeviceSupportFactory(new TestDeviceSupportFactory(getContext()));
|
||||
|
||||
mDeviceService = new TestDeviceService(this);
|
||||
}
|
||||
|
||||
protected GBDevice getDevice() {
|
||||
return realSupport.getDevice();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tearDown() throws Exception {
|
||||
mDeviceService.stopService(mDeviceService.createIntent());
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStart() {
|
||||
assertFalse("Service was already", getServiceInstance().isStarted());
|
||||
mDeviceService.start();
|
||||
assertTrue("Service should be started", getServiceInstance().isStarted());
|
||||
public void testNotConnected() {
|
||||
GBDevice device = getDevice();
|
||||
assertEquals(GBDevice.State.NOT_CONNECTED, device.getState());
|
||||
|
||||
// verify that the events like onFindDevice do not reach the DeviceSupport instance,
|
||||
// because not connected
|
||||
InOrder inOrder = Mockito.inOrder(mockSupport);
|
||||
mDeviceService.onFindDevice(true);
|
||||
inOrder.verify(mockSupport, Mockito.times(0)).onFindDevice(true);
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ensureConnected() {
|
||||
mDeviceService.connect(realSupport.getDevice());
|
||||
mDeviceService.start();
|
||||
// connection goes synchronously here
|
||||
mDeviceService.connect(getDevice());
|
||||
Mockito.verify(mockSupport, Mockito.times(1)).connect();
|
||||
assertTrue(realSupport.getDevice().isInitialized());
|
||||
assertTrue(getDevice().isInitialized());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -88,4 +95,5 @@ public class DeviceCommunicationServiceTestCase extends AbstractServiceTestCase<
|
||||
inOrder.verify(mockSupport, Mockito.times(1)).onFindDevice(false);
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,25 +1,51 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
|
||||
import nodomain.freeyourgadget.gadgetbridge.test.GBMockIntent;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.util.ServiceController;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
|
||||
|
||||
/**
|
||||
* Extends GBDeviceServer so that communication with the service works
|
||||
* 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Intent createIntent() {
|
||||
return new GBMockIntent();
|
||||
serviceController = Robolectric.buildService(DeviceCommunicationService.class, createIntent());
|
||||
service = serviceController.create().get();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invokeService(Intent intent) {
|
||||
mTestCase.startService(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));
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
@ -1,43 +0,0 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.test;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.test.mock.MockApplication;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBEnvironment;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||
|
||||
public class GBMockApplication extends MockApplication {
|
||||
private static final String PREF_NAME = "testprefs";
|
||||
private final PackageManager mPackageManager;
|
||||
private Prefs prefs;
|
||||
private GBPrefs gbPrefs;
|
||||
|
||||
public GBMockApplication(PackageManager packageManager) {
|
||||
GB.environment = GBEnvironment.createDeviceEnvironment().createLocalTestEnvironment();
|
||||
mPackageManager = packageManager;
|
||||
prefs = new Prefs(PreferenceManager.getDefaultSharedPreferences(this));
|
||||
gbPrefs = new GBPrefs(prefs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context getApplicationContext() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PackageManager getPackageManager() {
|
||||
return mPackageManager;
|
||||
}
|
||||
|
||||
public Prefs getPrefs() {
|
||||
return prefs;
|
||||
}
|
||||
|
||||
public GBPrefs getGBPrefs() {
|
||||
return gbPrefs;
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.test;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.test.mock.MockContext;
|
||||
|
||||
public class GBMockContext extends MockContext {
|
||||
private final Application mApplication;
|
||||
|
||||
public GBMockContext(Application application) {
|
||||
mApplication = application;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context getApplicationContext() {
|
||||
return mApplication;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PackageManager getPackageManager() {
|
||||
return mApplication.getPackageManager();
|
||||
}
|
||||
}
|
@ -1,387 +0,0 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.test;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class GBMockIntent extends Intent {
|
||||
private String mAction;
|
||||
private final Map<String, Object> extras = new HashMap<>();
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent setAction(String action) {
|
||||
mAction = action;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAction() {
|
||||
return mAction;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, boolean value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, byte value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, char value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, short value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, int value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, long value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, float value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, double value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, String value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, CharSequence value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, Parcelable value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, Parcelable[] value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putIntegerArrayListExtra(String name, ArrayList<Integer> value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putStringArrayListExtra(String name, ArrayList<String> value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putCharSequenceArrayListExtra(String name, ArrayList<CharSequence> value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, Serializable value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, boolean[] value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, byte[] value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, short[] value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, char[] value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, int[] value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, long[] value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, float[] value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, double[] value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, String[] value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, CharSequence[] value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent putExtra(String name, Bundle value) {
|
||||
extras.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBooleanExtra(String name, boolean defaultValue) {
|
||||
if (extras.containsKey(name)) {
|
||||
return (boolean) extras.get(name);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getByteExtra(String name, byte defaultValue) {
|
||||
if (extras.containsKey(name)) {
|
||||
return (byte) extras.get(name);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getShortExtra(String name, short defaultValue) {
|
||||
if (extras.containsKey(name)) {
|
||||
return (short) extras.get(name);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getCharExtra(String name, char defaultValue) {
|
||||
if (extras.containsKey(name)) {
|
||||
return (char) extras.get(name);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntExtra(String name, int defaultValue) {
|
||||
if (extras.containsKey(name)) {
|
||||
return (int) extras.get(name);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLongExtra(String name, long defaultValue) {
|
||||
if (extras.containsKey(name)) {
|
||||
return (long) extras.get(name);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFloatExtra(String name, float defaultValue) {
|
||||
if (extras.containsKey(name)) {
|
||||
return (float) extras.get(name);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDoubleExtra(String name, double defaultValue) {
|
||||
if (extras.containsKey(name)) {
|
||||
return (double) extras.get(name);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getCharSequenceExtra(String name) {
|
||||
return (CharSequence) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Parcelable> T getParcelableExtra(String name) {
|
||||
return (T) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parcelable[] getParcelableArrayExtra(String name) {
|
||||
return (Parcelable[]) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Parcelable> ArrayList<T> getParcelableArrayListExtra(String name) {
|
||||
return (ArrayList<T>) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Serializable getSerializableExtra(String name) {
|
||||
return (Serializable) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Integer> getIntegerArrayListExtra(String name) {
|
||||
return (ArrayList<Integer>) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<String> getStringArrayListExtra(String name) {
|
||||
return (ArrayList<String>) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<CharSequence> getCharSequenceArrayListExtra(String name) {
|
||||
return (ArrayList<CharSequence>) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] getBooleanArrayExtra(String name) {
|
||||
return (boolean[]) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getByteArrayExtra(String name) {
|
||||
return (byte[]) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public short[] getShortArrayExtra(String name) {
|
||||
return (short[]) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public char[] getCharArrayExtra(String name) {
|
||||
return (char[]) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getIntArrayExtra(String name) {
|
||||
return (int[]) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] getLongArrayExtra(String name) {
|
||||
return (long[]) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float[] getFloatArrayExtra(String name) {
|
||||
return (float[]) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double[] getDoubleArrayExtra(String name) {
|
||||
return (double[]) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getStringArrayExtra(String name) {
|
||||
return (String[]) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence[] getCharSequenceArrayExtra(String name) {
|
||||
return (CharSequence[]) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStringExtra(String name) {
|
||||
return (String) extras.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "GBMockIntent: " + mAction;
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.test;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.test.mock.MockPackageManager;
|
||||
|
||||
public class GBMockPackageManager extends MockPackageManager {
|
||||
@Override
|
||||
public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ import org.robolectric.annotation.Config;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import ch.qos.logback.classic.util.ContextInitializer;
|
||||
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
|
||||
import nodomain.freeyourgadget.gadgetbridge.Logging;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
||||
@ -27,35 +28,11 @@ import static org.junit.Assert.fail;
|
||||
* Test is currently disabled because logback-android does not work
|
||||
* inside a plain junit test.
|
||||
*/
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(constants = BuildConfig.class, sdk = 19)
|
||||
// need sdk 19 because "WITHOUT ROWID" is not supported in robolectric/sqlite4java
|
||||
public class LoggingTest {
|
||||
|
||||
private static File logFilesDir;
|
||||
public class LoggingTest extends TestBase {
|
||||
|
||||
public LoggingTest() throws Exception {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setupSuite() throws Exception {
|
||||
// properties might be preconfigured in build.gradle because of test ordering problems
|
||||
String logDir = System.getProperty(Logging.PROP_LOGFILES_DIR);
|
||||
if (logDir != null) {
|
||||
logFilesDir = new File(logDir);
|
||||
} else {
|
||||
logFilesDir = FileUtils.createTempDir("logfiles");
|
||||
System.setProperty(Logging.PROP_LOGFILES_DIR, logFilesDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
if (System.getProperty("logback.configurationFile") == null) {
|
||||
File workingDir = new File(System.getProperty("user.dir"));
|
||||
File configFile = new File(workingDir, "src/main/assets/logback.xml");
|
||||
System.out.println(configFile.getAbsolutePath());
|
||||
System.setProperty("logback.configurationFile", configFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
private Logging logging = new Logging() {
|
||||
@Override
|
||||
protected String createLogDirectory() throws IOException {
|
||||
|
@ -11,14 +11,12 @@ 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 {
|
||||
Constructor<?>[] constructors = NotificationManager.class.getDeclaredConstructors();
|
||||
constructors[0].setAccessible(true);
|
||||
Class<?>[] parameterTypes = constructors[0].getParameterTypes();
|
||||
return (NotificationManager) constructors[0].newInstance();
|
||||
return (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
}
|
||||
|
||||
public <T extends Service> T createService(Class<T> serviceClass, Application application) throws Exception {
|
||||
@ -31,10 +29,10 @@ public class MockHelper {
|
||||
return mockedService;
|
||||
}
|
||||
|
||||
public <T extends DeviceCommunicationService> T createDeviceCommunicationService(Class<T> serviceClass, GBMockApplication application) throws Exception {
|
||||
public <T extends DeviceCommunicationService> T createDeviceCommunicationService(Class<T> serviceClass, GBApplication application) throws Exception {
|
||||
T mockedService = createService(serviceClass, application);
|
||||
Mockito.when(mockedService.getPrefs()).thenReturn(application.getPrefs());
|
||||
Mockito.when(mockedService.getGBPrefs()).thenReturn(application.getGBPrefs());
|
||||
Mockito.when(mockedService.getPrefs()).thenReturn(GBApplication.getPrefs());
|
||||
Mockito.when(mockedService.getGBPrefs()).thenReturn(GBApplication.getGBPrefs());
|
||||
return mockedService;
|
||||
}
|
||||
|
||||
|
@ -4,19 +4,26 @@ import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowLog;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import ch.qos.logback.classic.util.ContextInitializer;
|
||||
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.Logging;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
||||
import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
|
||||
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@ -24,15 +31,38 @@ import static org.junit.Assert.assertNotNull;
|
||||
@Config(constants = BuildConfig.class, sdk = 19)
|
||||
// need sdk 19 because "WITHOUT ROWID" is not supported in robolectric/sqlite4java
|
||||
public abstract class TestBase {
|
||||
protected static File logFilesDir;
|
||||
|
||||
protected GBApplication app = (GBApplication) RuntimeEnvironment.application;
|
||||
protected DaoSession daoSession;
|
||||
protected DBHandler dbHandler;
|
||||
|
||||
// Make sure logging is set up for all testcases, so that we can debug problems
|
||||
@BeforeClass
|
||||
public static void setupSuite() throws Exception {
|
||||
// print everything going to android.util.Log to System.out
|
||||
ShadowLog.stream = System.out;
|
||||
|
||||
// properties might be preconfigured in build.gradle because of test ordering problems
|
||||
String logDir = System.getProperty(Logging.PROP_LOGFILES_DIR);
|
||||
if (logDir != null) {
|
||||
logFilesDir = new File(logDir);
|
||||
} else {
|
||||
logFilesDir = FileUtils.createTempDir("logfiles");
|
||||
System.setProperty(Logging.PROP_LOGFILES_DIR, logFilesDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
if (System.getProperty(ContextInitializer.CONFIG_FILE_PROPERTY) == null) {
|
||||
File workingDir = new File(System.getProperty("user.dir"));
|
||||
File configFile = new File(workingDir, "src/main/assets/logback.xml");
|
||||
System.out.println(configFile.getAbsolutePath());
|
||||
System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, configFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
assertNotNull(app);
|
||||
|
||||
// doesn't work with Robolectric yet
|
||||
// dbHandler = GBApplication.acquireDB();
|
||||
// daoSession = dbHandler.getDaoSession();
|
||||
|
Loading…
Reference in New Issue
Block a user