diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 0ad9bedab0..dace12d1ba 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -106,13 +106,13 @@ public class ConfigBuilderPlugin extends PluginBase implements TreatmentsInterfa } @Override - public void onStart() { + protected void onStart() { super.onStart(); MainApp.bus().register(this); } @Override - public void onStop() { + protected void onStop() { super.onStop(); MainApp.bus().unregister(this); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java index 21dcefdb1b..fe2b834d6a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java @@ -76,13 +76,13 @@ public class IobCobCalculatorPlugin extends PluginBase { } @Override - public void onStart() { + protected void onStart() { super.onStart(); MainApp.bus().register(this); } @Override - public void onStop() { + protected void onStop() { super.onStop(); MainApp.bus().unregister(this); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java index bc219d6f32..5e9ff75e10 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java @@ -61,12 +61,12 @@ public class OverviewPlugin extends PluginBase { } @Override - public void onStart() { + protected void onStart() { MainApp.bus().register(this); } @Override - public void onStop() { + protected void onStop() { MainApp.bus().unregister(this); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java index b7cd680859..95b6b449d1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java @@ -79,14 +79,14 @@ public class StatuslinePlugin extends PluginBase { } @Override - public void onStart() { + protected void onStart() { super.onStart(); MainApp.bus().register(this); sendStatus(); } @Override - public void onStop() { + protected void onStop() { super.onStop(); MainApp.bus().unregister(this); sendStatus(); diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java index 9ec0019ae5..199a87a4c5 100644 --- a/app/src/test/java/info/AAPSMocker.java +++ b/app/src/test/java/info/AAPSMocker.java @@ -76,6 +76,8 @@ public class AAPSMocker { when(MainApp.gs(R.string.absolute)).thenReturn("Absolute"); when(MainApp.gs(R.string.waitingforpumpresult)).thenReturn("Waiting for result"); when(MainApp.gs(R.string.insulin_unit_shortname)).thenReturn("U"); + when(MainApp.gs(R.string.minimalbasalvaluereplaced)).thenReturn("Basal value replaced by minimal supported value"); + when(MainApp.gs(R.string.basalprofilenotaligned)).thenReturn("Basal values not aligned to hours: %s"); } public static MainApp mockMainApp() { @@ -125,4 +127,31 @@ public class AAPSMocker { } catch (JSONException ignored) {} return profile; } + + private static MockedBus bus = new MockedBus(); + + public static void prepareMockedBus() { + when(MainApp.bus()).thenReturn(bus); + } + + public static class MockedBus extends Bus { + public boolean registered = false; + public boolean notificationSent = false; + + @Override + public void register(Object event) { + registered = true; + } + + @Override + public void unregister(Object event) { + registered = false; + } + + @Override + public void post(Object event) { + notificationSent = true; + } + } + } diff --git a/app/src/test/java/info/nightscout/androidaps/data/ProfileTest.java b/app/src/test/java/info/nightscout/androidaps/data/ProfileTest.java index 664515d45d..78a5a338aa 100644 --- a/app/src/test/java/info/nightscout/androidaps/data/ProfileTest.java +++ b/app/src/test/java/info/nightscout/androidaps/data/ProfileTest.java @@ -5,6 +5,7 @@ import com.squareup.otto.Bus; import junit.framework.Assert; import org.json.JSONObject; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; @@ -16,6 +17,7 @@ import java.util.Calendar; import java.util.Locale; import java.util.TimeZone; +import info.AAPSMocker; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -44,12 +46,8 @@ public class ProfileTest { //String profileStore = "{\"defaultProfile\":\"Default\",\"store\":{\"Default\":" + validProfile + "}}"; - boolean notificationSent = false; - @Test public void doTests() throws Exception { - prepareMock(); - Profile p = new Profile(); // Test valid profile @@ -105,7 +103,7 @@ public class ProfileTest { //Test basal profile below limit p = new Profile(new JSONObject(belowLimitValidProfile), 100, 0); p.isValid("Test"); - Assert.assertEquals(true, notificationSent); + Assert.assertEquals(true, ((AAPSMocker.MockedBus) MainApp.bus()).notificationSent); // Test profile w/o units p = new Profile(new JSONObject(noUnitsValidProfile), 100, 0); @@ -140,37 +138,24 @@ public class ProfileTest { // Test hour alignment MainApp.getConfigBuilder().getActivePump().getPumpDescription().is30minBasalRatesCapable = false; - notificationSent = false; + ((AAPSMocker.MockedBus) MainApp.bus()).notificationSent = false; p = new Profile(new JSONObject(notAllignedBasalValidProfile), 100, 0); p.isValid("Test"); - Assert.assertEquals(true, notificationSent); + Assert.assertEquals(true, ((AAPSMocker.MockedBus) MainApp.bus()).notificationSent); } - private void prepareMock() throws Exception { - Locale.setDefault(new Locale("en", "US")); - ConfigBuilderPlugin configBuilderPlugin = mock(ConfigBuilderPlugin.class); - PowerMockito.mockStatic(ConfigBuilderPlugin.class); + @Before + public void prepareMock() throws Exception { + AAPSMocker.mockMainApp(); + AAPSMocker.mockConfigBuilder(); + AAPSMocker.mockStrings(); + AAPSMocker.prepareMockedBus(); - MainApp mainApp = mock(MainApp.class); - PowerMockito.mockStatic(MainApp.class); - when(MainApp.instance()).thenReturn(mainApp); - when(MainApp.getConfigBuilder()).thenReturn(configBuilderPlugin); when(MainApp.getConfigBuilder().getActivePump()).thenReturn(pump); - when(MainApp.gs(R.string.minimalbasalvaluereplaced)).thenReturn("AnyString"); - when(MainApp.gs(R.string.basalprofilenotaligned)).thenReturn("AnyString"); PowerMockito.mockStatic(FabricPrivacy.class); // PowerMockito.doNothing().when(FabricPrivacy.log("")); - MockedBus bus = new MockedBus(); - when(MainApp.bus()).thenReturn(bus); } - class MockedBus extends Bus { - @Override - public void post(Object event) { - notificationSent = true; - } - } - -} + } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPluginTest.java index 68193f7b94..59628a8dea 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPluginTest.java @@ -2,13 +2,21 @@ package info.nightscout.androidaps.plugins.ConfigBuilder; import junit.framework.Assert; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import static org.junit.Assert.*; +import info.AAPSMocker; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.interfaces.PluginType; + +import static org.powermock.api.mockito.PowerMockito.mock; @RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class}) public class ConfigBuilderPluginTest { @Test @@ -16,4 +24,28 @@ public class ConfigBuilderPluginTest { ConfigBuilderPlugin configBuilderPlugin = ConfigBuilderPlugin.getPlugin(); Assert.assertNotNull(configBuilderPlugin); } + + @Test + public void onStartTest() { + ConfigBuilderPlugin configBuilderPlugin = ConfigBuilderPlugin.getPlugin(); + configBuilderPlugin.setPluginEnabled(PluginType.GENERAL, true); + Assert.assertEquals(true, ((AAPSMocker.MockedBus) MainApp.bus()).registered); + } + + @Test + public void onStopTest() { + ConfigBuilderPlugin configBuilderPlugin = ConfigBuilderPlugin.getPlugin(); + configBuilderPlugin.setPluginEnabled(PluginType.GENERAL, true); + configBuilderPlugin.setPluginEnabled(PluginType.GENERAL, false); + Assert.assertEquals(false, ((AAPSMocker.MockedBus) MainApp.bus()).registered); + } + + @Before + public void prepareMock() throws Exception { + MainApp mainApp = mock(MainApp.class); + PowerMockito.mockStatic(MainApp.class); + AAPSMocker.prepareMockedBus(); + } + + } \ No newline at end of file