Fix testcases (all this should be scrapped and redone with e.g. robolectric)

here
cpfeiffer 2016-04-29 21:49:17 +02:00
parent 5e02724c4c
commit 10d7274aa1
5 changed files with 67 additions and 11 deletions

View File

@ -133,8 +133,9 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED));
mFactory = new DeviceSupportFactory(this);
Prefs prefs = GBApplication.getPrefs();
prefs.getPreferences().registerOnSharedPreferenceChangeListener(this);
if (hasPrefs()) {
getPrefs().getPreferences().registerOnSharedPreferenceChangeListener(this);
}
}
@Override
@ -174,7 +175,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
// when we get past this, we should have valid mDeviceSupport and mGBDevice instances
Prefs prefs = GBApplication.getPrefs();
Prefs prefs = getPrefs();
switch (action) {
case ACTION_START:
start();
@ -196,9 +197,9 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
}
boolean autoReconnect = GBPrefs.AUTO_RECONNECT_DEFAULT;
if (prefs != null) {
if (prefs != null && prefs.getPreferences() != null) {
prefs.getPreferences().edit().putString("last_device_address", btDeviceAddress).apply();
autoReconnect = prefs.getPreferences().getBoolean(GBPrefs.AUTO_RECONNECT, GBPrefs.AUTO_RECONNECT_DEFAULT);
autoReconnect = getGBPrefs().getAutoReconnect();
}
if (gbDevice != null && !isConnecting() && !isConnected()) {
@ -486,7 +487,9 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
@Override
public void onDestroy() {
GBApplication.getPrefs().getPreferences().unregisterOnSharedPreferenceChangeListener(this);
if (hasPrefs()) {
getPrefs().getPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
LOG.debug("DeviceCommunicationService is being destroyed");
super.onDestroy();
@ -528,10 +531,22 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (GBPrefs.AUTO_RECONNECT.equals(key)) {
boolean autoReconnect = GBApplication.getGBPrefs().getAutoReconnect();
boolean autoReconnect = getGBPrefs().getAutoReconnect();
if (mDeviceSupport != null) {
mDeviceSupport.setAutoReconnect(autoReconnect);
}
}
}
protected boolean hasPrefs() {
return getPrefs().getPreferences() != null;
}
public Prefs getPrefs() {
return GBApplication.getPrefs();
}
public GBPrefs getGBPrefs() {
return GBApplication.getGBPrefs();
}
}

View File

@ -5,7 +5,9 @@ import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import junit.framework.Assert;
@ -22,7 +24,7 @@ public abstract class AbstractServiceTestCase<T extends Service> {
private final Class<T> mServiceClass;
private T mServiceInstance;
private Context mContext;
private Application mApplication;
private GBMockApplication mApplication;
private boolean wasStarted;
private PackageManager mPackageManager;
private NotificationManager mNotificationManager;
@ -41,6 +43,10 @@ public abstract class AbstractServiceTestCase<T extends Service> {
return mServiceInstance;
}
protected MockHelper getmMockHelper() {
return mMockHelper;
}
@Before
public void setUp() throws Exception {
mMockHelper = new MockHelper();
@ -69,7 +75,7 @@ public abstract class AbstractServiceTestCase<T extends Service> {
mServiceInstance = null;
}
protected Application createApplication(PackageManager packageManager) {
protected GBMockApplication createApplication(PackageManager packageManager) {
return new GBMockApplication(packageManager);
}
@ -85,9 +91,9 @@ public abstract class AbstractServiceTestCase<T extends Service> {
return new GBMockContext(application);
}
private T createService(Class<T> serviceClass, Application application, NotificationManager notificationManager) throws Exception {
protected T createService(Class<T> serviceClass, GBMockApplication application, NotificationManager notificationManager) throws Exception {
T service = mMockHelper.createService(serviceClass, application);
mMockHelper.addSystemServiceTo(service, Context.NOTIFICATION_SERVICE, getNotificationService());
mMockHelper.addSystemServiceTo(service, Context.NOTIFICATION_SERVICE, notificationManager);
return service;
}

View File

@ -1,5 +1,7 @@
package nodomain.freeyourgadget.gadgetbridge.service;
import android.app.Application;
import android.app.NotificationManager;
import android.content.Context;
import org.junit.Before;
@ -11,6 +13,7 @@ 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.assertTrue;
@ -41,6 +44,14 @@ public class DeviceCommunicationServiceTestCase extends AbstractServiceTestCase<
super(DeviceCommunicationService.class);
}
@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();

View File

@ -1,18 +1,27 @@
package nodomain.freeyourgadget.gadgetbridge.test;
import android.content.Context;
import android.content.SharedPreferences;
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
@ -25,4 +34,10 @@ public class GBMockApplication extends MockApplication {
return mPackageManager;
}
public Prefs getPrefs() {
return prefs;
}
public GBPrefs getGBPrefs() {
return gbPrefs;
}
}

View File

@ -11,6 +11,8 @@ import org.mockito.Mockito;
import java.lang.reflect.Constructor;
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
public class MockHelper {
public <T extends Service> NotificationManager createNotificationManager(Context mContext) throws Exception {
Constructor<?>[] constructors = NotificationManager.class.getDeclaredConstructors();
@ -29,6 +31,13 @@ public class MockHelper {
return mockedService;
}
public <T extends DeviceCommunicationService> T createDeviceCommunicationService(Class<T> serviceClass, GBMockApplication application) throws Exception {
T mockedService = createService(serviceClass, application);
Mockito.when(mockedService.getPrefs()).thenReturn(application.getPrefs());
Mockito.when(mockedService.getGBPrefs()).thenReturn(application.getGBPrefs());
return mockedService;
}
public void addSystemServiceTo(Context context, String serviceName, Object service) {
Mockito.when(context.getSystemService(serviceName)).thenReturn(service);
}