From c31049839a55ee94ede2ff9e4b5dbc2894e346c6 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sat, 10 Sep 2016 10:52:02 +0200 Subject: [PATCH] Make DeviceCommunicationServiceTestCase runnable with robolectric - enables the test for travis - tests operation when not connected - tests connecting - tests operation when connected --- .../service/AbstractDeviceSupport.java | 7 +- .../service/DeviceCommunicationService.java | 32 +- .../service/AbstractServiceTestCase.java | 74 +--- .../DeviceCommunicationServiceTestCase.java | 48 ++- .../service/TestDeviceService.java | 40 +- .../gadgetbridge/test/GBMockApplication.java | 43 -- .../gadgetbridge/test/GBMockContext.java | 24 -- .../gadgetbridge/test/GBMockIntent.java | 387 ------------------ .../test/GBMockPackageManager.java | 11 - .../gadgetbridge/test/LoggingTest.java | 27 +- .../gadgetbridge/test/MockHelper.java | 12 +- .../gadgetbridge/test/TestBase.java | 32 +- 12 files changed, 135 insertions(+), 602 deletions(-) delete mode 100644 app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockApplication.java delete mode 100644 app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockContext.java delete mode 100644 app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockIntent.java delete mode 100644 app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockPackageManager.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java index 36b12e7c8..ef37a75b7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java @@ -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); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 1d225cec6..118ca2761 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -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). diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractServiceTestCase.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractServiceTestCase.java index 802c5607e..8176d7354 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractServiceTestCase.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractServiceTestCase.java @@ -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 { - private static final int ID = -1; // currently not supported - private final Class mServiceClass; - private T mServiceInstance; +public abstract class AbstractServiceTestCase 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 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 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; } diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java index cd9ff019c..d85e6de41 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java @@ -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 { @@ -40,41 +37,51 @@ public class DeviceCommunicationServiceTestCase extends AbstractServiceTestCase< private TestDeviceSupport mockSupport; public DeviceCommunicationServiceTestCase() { - super(DeviceCommunicationService.class); + super(); } @Override - protected DeviceCommunicationService createService(Class 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(); } + } diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java index ad4a11bef..9db2157a1 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java @@ -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 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(); } } diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockApplication.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockApplication.java deleted file mode 100644 index f833453ff..000000000 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockApplication.java +++ /dev/null @@ -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; - } -} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockContext.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockContext.java deleted file mode 100644 index ce7d53326..000000000 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockContext.java +++ /dev/null @@ -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(); - } -} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockIntent.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockIntent.java deleted file mode 100644 index 080361e30..000000000 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockIntent.java +++ /dev/null @@ -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 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 value) { - extras.put(name, value); - return this; - } - - @NonNull - @Override - public Intent putIntegerArrayListExtra(String name, ArrayList value) { - extras.put(name, value); - return this; - } - - @NonNull - @Override - public Intent putStringArrayListExtra(String name, ArrayList value) { - extras.put(name, value); - return this; - } - - @NonNull - @Override - public Intent putCharSequenceArrayListExtra(String name, ArrayList 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 getParcelableExtra(String name) { - return (T) extras.get(name); - } - - @Override - public Parcelable[] getParcelableArrayExtra(String name) { - return (Parcelable[]) extras.get(name); - } - - @Override - public ArrayList getParcelableArrayListExtra(String name) { - return (ArrayList) extras.get(name); - } - - @Override - public Serializable getSerializableExtra(String name) { - return (Serializable) extras.get(name); - } - - @Override - public ArrayList getIntegerArrayListExtra(String name) { - return (ArrayList) extras.get(name); - } - - @Override - public ArrayList getStringArrayListExtra(String name) { - return (ArrayList) extras.get(name); - } - - @Override - public ArrayList getCharSequenceArrayListExtra(String name) { - return (ArrayList) 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; - } -} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockPackageManager.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockPackageManager.java deleted file mode 100644 index 7799532d8..000000000 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockPackageManager.java +++ /dev/null @@ -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 - } -} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/LoggingTest.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/LoggingTest.java index 92315b67e..5bc17f7f7 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/LoggingTest.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/LoggingTest.java @@ -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 { diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/MockHelper.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/MockHelper.java index 00947b989..a6d24d174 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/MockHelper.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/MockHelper.java @@ -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 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 createService(Class serviceClass, Application application) throws Exception { @@ -31,10 +29,10 @@ public class MockHelper { return mockedService; } - public T createDeviceCommunicationService(Class serviceClass, GBMockApplication application) throws Exception { + public T createDeviceCommunicationService(Class 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; } diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/TestBase.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/TestBase.java index 4f788f536..79875f54a 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/TestBase.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/TestBase.java @@ -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();