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 ee681cad4..7738f40c7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -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(); + } } 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 fe9e03e3a..b3b426bfd 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractServiceTestCase.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractServiceTestCase.java @@ -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 { private final Class 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 { return mServiceInstance; } + protected MockHelper getmMockHelper() { + return mMockHelper; + } + @Before public void setUp() throws Exception { mMockHelper = new MockHelper(); @@ -69,7 +75,7 @@ public abstract class AbstractServiceTestCase { mServiceInstance = null; } - protected Application createApplication(PackageManager packageManager) { + protected GBMockApplication createApplication(PackageManager packageManager) { return new GBMockApplication(packageManager); } @@ -85,9 +91,9 @@ public abstract class AbstractServiceTestCase { return new GBMockContext(application); } - private T createService(Class serviceClass, Application application, NotificationManager notificationManager) throws Exception { + protected T createService(Class 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; } 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 e4d1fe5b5..8fa5ea5f7 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java @@ -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 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(); diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockApplication.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockApplication.java index 805716fa7..2fb2ffbff 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockApplication.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockApplication.java @@ -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; + } } 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 9461cab1d..00947b989 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/MockHelper.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/MockHelper.java @@ -11,6 +11,8 @@ import org.mockito.Mockito; import java.lang.reflect.Constructor; +import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService; + public class MockHelper { public NotificationManager createNotificationManager(Context mContext) throws Exception { Constructor[] constructors = NotificationManager.class.getDeclaredConstructors(); @@ -29,6 +31,13 @@ public class MockHelper { return mockedService; } + public T createDeviceCommunicationService(Class 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); }