ConfigBuilderTest

This commit is contained in:
Milos Kozak 2018-03-31 11:31:09 +02:00
parent 1c6c5f85f7
commit 364364f205
7 changed files with 82 additions and 36 deletions

View file

@ -106,13 +106,13 @@ public class ConfigBuilderPlugin extends PluginBase implements TreatmentsInterfa
} }
@Override @Override
public void onStart() { protected void onStart() {
super.onStart(); super.onStart();
MainApp.bus().register(this); MainApp.bus().register(this);
} }
@Override @Override
public void onStop() { protected void onStop() {
super.onStop(); super.onStop();
MainApp.bus().unregister(this); MainApp.bus().unregister(this);
} }

View file

@ -76,13 +76,13 @@ public class IobCobCalculatorPlugin extends PluginBase {
} }
@Override @Override
public void onStart() { protected void onStart() {
super.onStart(); super.onStart();
MainApp.bus().register(this); MainApp.bus().register(this);
} }
@Override @Override
public void onStop() { protected void onStop() {
super.onStop(); super.onStop();
MainApp.bus().unregister(this); MainApp.bus().unregister(this);
} }

View file

@ -61,12 +61,12 @@ public class OverviewPlugin extends PluginBase {
} }
@Override @Override
public void onStart() { protected void onStart() {
MainApp.bus().register(this); MainApp.bus().register(this);
} }
@Override @Override
public void onStop() { protected void onStop() {
MainApp.bus().unregister(this); MainApp.bus().unregister(this);
} }

View file

@ -79,14 +79,14 @@ public class StatuslinePlugin extends PluginBase {
} }
@Override @Override
public void onStart() { protected void onStart() {
super.onStart(); super.onStart();
MainApp.bus().register(this); MainApp.bus().register(this);
sendStatus(); sendStatus();
} }
@Override @Override
public void onStop() { protected void onStop() {
super.onStop(); super.onStop();
MainApp.bus().unregister(this); MainApp.bus().unregister(this);
sendStatus(); sendStatus();

View file

@ -76,6 +76,8 @@ public class AAPSMocker {
when(MainApp.gs(R.string.absolute)).thenReturn("Absolute"); when(MainApp.gs(R.string.absolute)).thenReturn("Absolute");
when(MainApp.gs(R.string.waitingforpumpresult)).thenReturn("Waiting for result"); 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.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() { public static MainApp mockMainApp() {
@ -125,4 +127,31 @@ public class AAPSMocker {
} catch (JSONException ignored) {} } catch (JSONException ignored) {}
return profile; 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;
}
}
} }

View file

@ -5,6 +5,7 @@ import com.squareup.otto.Bus;
import junit.framework.Assert; import junit.framework.Assert;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
@ -16,6 +17,7 @@ import java.util.Calendar;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import info.AAPSMocker;
import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
@ -44,12 +46,8 @@ public class ProfileTest {
//String profileStore = "{\"defaultProfile\":\"Default\",\"store\":{\"Default\":" + validProfile + "}}"; //String profileStore = "{\"defaultProfile\":\"Default\",\"store\":{\"Default\":" + validProfile + "}}";
boolean notificationSent = false;
@Test @Test
public void doTests() throws Exception { public void doTests() throws Exception {
prepareMock();
Profile p = new Profile(); Profile p = new Profile();
// Test valid profile // Test valid profile
@ -105,7 +103,7 @@ public class ProfileTest {
//Test basal profile below limit //Test basal profile below limit
p = new Profile(new JSONObject(belowLimitValidProfile), 100, 0); p = new Profile(new JSONObject(belowLimitValidProfile), 100, 0);
p.isValid("Test"); p.isValid("Test");
Assert.assertEquals(true, notificationSent); Assert.assertEquals(true, ((AAPSMocker.MockedBus) MainApp.bus()).notificationSent);
// Test profile w/o units // Test profile w/o units
p = new Profile(new JSONObject(noUnitsValidProfile), 100, 0); p = new Profile(new JSONObject(noUnitsValidProfile), 100, 0);
@ -140,37 +138,24 @@ public class ProfileTest {
// Test hour alignment // Test hour alignment
MainApp.getConfigBuilder().getActivePump().getPumpDescription().is30minBasalRatesCapable = false; MainApp.getConfigBuilder().getActivePump().getPumpDescription().is30minBasalRatesCapable = false;
notificationSent = false; ((AAPSMocker.MockedBus) MainApp.bus()).notificationSent = false;
p = new Profile(new JSONObject(notAllignedBasalValidProfile), 100, 0); p = new Profile(new JSONObject(notAllignedBasalValidProfile), 100, 0);
p.isValid("Test"); p.isValid("Test");
Assert.assertEquals(true, notificationSent); Assert.assertEquals(true, ((AAPSMocker.MockedBus) MainApp.bus()).notificationSent);
} }
private void prepareMock() throws Exception { @Before
Locale.setDefault(new Locale("en", "US")); public void prepareMock() throws Exception {
ConfigBuilderPlugin configBuilderPlugin = mock(ConfigBuilderPlugin.class); AAPSMocker.mockMainApp();
PowerMockito.mockStatic(ConfigBuilderPlugin.class); 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.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.mockStatic(FabricPrivacy.class);
// PowerMockito.doNothing().when(FabricPrivacy.log("")); // 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;
}
}
}

View file

@ -2,13 +2,21 @@ package info.nightscout.androidaps.plugins.ConfigBuilder;
import junit.framework.Assert; import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; 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 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) @RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class})
public class ConfigBuilderPluginTest { public class ConfigBuilderPluginTest {
@Test @Test
@ -16,4 +24,28 @@ public class ConfigBuilderPluginTest {
ConfigBuilderPlugin configBuilderPlugin = ConfigBuilderPlugin.getPlugin(); ConfigBuilderPlugin configBuilderPlugin = ConfigBuilderPlugin.getPlugin();
Assert.assertNotNull(configBuilderPlugin); 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();
}
} }