Fix testcases (all this should be scrapped and redone with e.g. robolectric)
This commit is contained in:
parent
5e02724c4c
commit
10d7274aa1
|
@ -133,8 +133,9 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||||
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED));
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED));
|
||||||
mFactory = new DeviceSupportFactory(this);
|
mFactory = new DeviceSupportFactory(this);
|
||||||
|
|
||||||
Prefs prefs = GBApplication.getPrefs();
|
if (hasPrefs()) {
|
||||||
prefs.getPreferences().registerOnSharedPreferenceChangeListener(this);
|
getPrefs().getPreferences().registerOnSharedPreferenceChangeListener(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
// when we get past this, we should have valid mDeviceSupport and mGBDevice instances
|
||||||
|
|
||||||
Prefs prefs = GBApplication.getPrefs();
|
Prefs prefs = getPrefs();
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case ACTION_START:
|
case ACTION_START:
|
||||||
start();
|
start();
|
||||||
|
@ -196,9 +197,9 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean autoReconnect = GBPrefs.AUTO_RECONNECT_DEFAULT;
|
boolean autoReconnect = GBPrefs.AUTO_RECONNECT_DEFAULT;
|
||||||
if (prefs != null) {
|
if (prefs != null && prefs.getPreferences() != null) {
|
||||||
prefs.getPreferences().edit().putString("last_device_address", btDeviceAddress).apply();
|
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()) {
|
if (gbDevice != null && !isConnecting() && !isConnected()) {
|
||||||
|
@ -486,7 +487,9 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
GBApplication.getPrefs().getPreferences().unregisterOnSharedPreferenceChangeListener(this);
|
if (hasPrefs()) {
|
||||||
|
getPrefs().getPreferences().unregisterOnSharedPreferenceChangeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
LOG.debug("DeviceCommunicationService is being destroyed");
|
LOG.debug("DeviceCommunicationService is being destroyed");
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
@ -528,10 +531,22 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||||
if (GBPrefs.AUTO_RECONNECT.equals(key)) {
|
if (GBPrefs.AUTO_RECONNECT.equals(key)) {
|
||||||
boolean autoReconnect = GBApplication.getGBPrefs().getAutoReconnect();
|
boolean autoReconnect = getGBPrefs().getAutoReconnect();
|
||||||
if (mDeviceSupport != null) {
|
if (mDeviceSupport != null) {
|
||||||
mDeviceSupport.setAutoReconnect(autoReconnect);
|
mDeviceSupport.setAutoReconnect(autoReconnect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean hasPrefs() {
|
||||||
|
return getPrefs().getPreferences() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Prefs getPrefs() {
|
||||||
|
return GBApplication.getPrefs();
|
||||||
|
}
|
||||||
|
|
||||||
|
public GBPrefs getGBPrefs() {
|
||||||
|
return GBApplication.getGBPrefs();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,9 @@ import android.app.NotificationManager;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
@ -22,7 +24,7 @@ public abstract class AbstractServiceTestCase<T extends Service> {
|
||||||
private final Class<T> mServiceClass;
|
private final Class<T> mServiceClass;
|
||||||
private T mServiceInstance;
|
private T mServiceInstance;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private Application mApplication;
|
private GBMockApplication mApplication;
|
||||||
private boolean wasStarted;
|
private boolean wasStarted;
|
||||||
private PackageManager mPackageManager;
|
private PackageManager mPackageManager;
|
||||||
private NotificationManager mNotificationManager;
|
private NotificationManager mNotificationManager;
|
||||||
|
@ -41,6 +43,10 @@ public abstract class AbstractServiceTestCase<T extends Service> {
|
||||||
return mServiceInstance;
|
return mServiceInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected MockHelper getmMockHelper() {
|
||||||
|
return mMockHelper;
|
||||||
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
mMockHelper = new MockHelper();
|
mMockHelper = new MockHelper();
|
||||||
|
@ -69,7 +75,7 @@ public abstract class AbstractServiceTestCase<T extends Service> {
|
||||||
mServiceInstance = null;
|
mServiceInstance = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Application createApplication(PackageManager packageManager) {
|
protected GBMockApplication createApplication(PackageManager packageManager) {
|
||||||
return new GBMockApplication(packageManager);
|
return new GBMockApplication(packageManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,9 +91,9 @@ public abstract class AbstractServiceTestCase<T extends Service> {
|
||||||
return new GBMockContext(application);
|
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);
|
T service = mMockHelper.createService(serviceClass, application);
|
||||||
mMockHelper.addSystemServiceTo(service, Context.NOTIFICATION_SERVICE, getNotificationService());
|
mMockHelper.addSystemServiceTo(service, Context.NOTIFICATION_SERVICE, notificationManager);
|
||||||
return service;
|
return service;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package nodomain.freeyourgadget.gadgetbridge.service;
|
package nodomain.freeyourgadget.gadgetbridge.service;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
|
import android.app.NotificationManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
@ -11,6 +13,7 @@ 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.GBMockApplication;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
@ -41,6 +44,14 @@ public class DeviceCommunicationServiceTestCase extends AbstractServiceTestCase<
|
||||||
super(DeviceCommunicationService.class);
|
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
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
|
|
@ -1,18 +1,27 @@
|
||||||
package nodomain.freeyourgadget.gadgetbridge.test;
|
package nodomain.freeyourgadget.gadgetbridge.test;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
import android.test.mock.MockApplication;
|
import android.test.mock.MockApplication;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBEnvironment;
|
import nodomain.freeyourgadget.gadgetbridge.GBEnvironment;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||||
|
|
||||||
public class GBMockApplication extends MockApplication {
|
public class GBMockApplication extends MockApplication {
|
||||||
|
private static final String PREF_NAME = "testprefs";
|
||||||
private final PackageManager mPackageManager;
|
private final PackageManager mPackageManager;
|
||||||
|
private Prefs prefs;
|
||||||
|
private GBPrefs gbPrefs;
|
||||||
|
|
||||||
public GBMockApplication(PackageManager packageManager) {
|
public GBMockApplication(PackageManager packageManager) {
|
||||||
GB.environment = GBEnvironment.createDeviceEnvironment().createLocalTestEnvironment();
|
GB.environment = GBEnvironment.createDeviceEnvironment().createLocalTestEnvironment();
|
||||||
mPackageManager = packageManager;
|
mPackageManager = packageManager;
|
||||||
|
prefs = new Prefs(PreferenceManager.getDefaultSharedPreferences(this));
|
||||||
|
gbPrefs = new GBPrefs(prefs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -25,4 +34,10 @@ public class GBMockApplication extends MockApplication {
|
||||||
return mPackageManager;
|
return mPackageManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Prefs getPrefs() {
|
||||||
|
return prefs;
|
||||||
|
}
|
||||||
|
public GBPrefs getGBPrefs() {
|
||||||
|
return gbPrefs;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@ import org.mockito.Mockito;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
|
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
|
||||||
|
|
||||||
public class MockHelper {
|
public class MockHelper {
|
||||||
public <T extends Service> NotificationManager createNotificationManager(Context mContext) throws Exception {
|
public <T extends Service> NotificationManager createNotificationManager(Context mContext) throws Exception {
|
||||||
Constructor<?>[] constructors = NotificationManager.class.getDeclaredConstructors();
|
Constructor<?>[] constructors = NotificationManager.class.getDeclaredConstructors();
|
||||||
|
@ -29,6 +31,13 @@ public class MockHelper {
|
||||||
return mockedService;
|
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) {
|
public void addSystemServiceTo(Context context, String serviceName, Object service) {
|
||||||
Mockito.when(context.getSystemService(serviceName)).thenReturn(service);
|
Mockito.when(context.getSystemService(serviceName)).thenReturn(service);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue