diff --git a/app/build.gradle b/app/build.gradle index b398132004..c33a3259db 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -66,6 +66,7 @@ android { version "1.60c-dev" buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "BUILDVERSION", generateGitBuild() + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" ndk { moduleName "BleCommandUtil" @@ -153,8 +154,9 @@ android { testOptions { unitTests.returnDefaultValues = true + unitTests.includeAndroidResources = true } -} + } allprojects { repositories { @@ -228,6 +230,7 @@ dependencies { testCompile "joda-time:joda-time:2.9.4.2" testCompile "com.google.truth:truth:0.39" testCompile "org.skyscreamer:jsonassert:1.5.0" + testCompile 'org.robolectric:robolectric:3.8' androidTestCompile "org.mockito:mockito-core:2.7.22" androidTestCompile "com.google.dexmaker:dexmaker:${dexmakerVersion}" diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 60989800b6..50d555c493 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -282,9 +282,6 @@ public class MainApp extends Application { } public static DatabaseHelper getDbHelper() { - if (sDatabaseHelper == null) { - sDatabaseHelper = OpenHelperManager.getHelper(sInstance, DatabaseHelper.class); - } return sDatabaseHelper; } @@ -321,21 +318,6 @@ public class MainApp extends Application { return newList; } - /* - @Nullable - public static InsulinInterface getInsulinIterfaceById(int id) { - if (pluginsList != null) { - for (PluginBase p : pluginsList) { - if (p.getType() == PluginType.INSULIN && ((InsulinInterface) p).getId() == id) - return (InsulinInterface) p; - } - } else { - log.error("InsulinInterface not found"); - } - return null; - } - */ - public static ArrayList getSpecificPluginsVisibleInList(PluginType type) { ArrayList newList = new ArrayList<>(); @@ -399,7 +381,7 @@ public class MainApp extends Application { return engineeringMode || !devBranch; } - private String getLogDirectory() { + public String getLogDirectory() { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); return lc.getProperty("EXT_FILES_DIR"); } @@ -407,6 +389,9 @@ public class MainApp extends Application { @Override public void onTerminate() { super.onTerminate(); - sDatabaseHelper.close(); + if (sDatabaseHelper != null) { + sDatabaseHelper.close(); + sDatabaseHelper = null; + } } } diff --git a/app/src/test/java/info/nightscout/MainAppTest.java b/app/src/test/java/info/nightscout/MainAppTest.java new file mode 100644 index 0000000000..301c2e79f5 --- /dev/null +++ b/app/src/test/java/info/nightscout/MainAppTest.java @@ -0,0 +1,111 @@ +package info.nightscout; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.PluginType; +import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.Overview.OverviewPlugin; + + +/** + * Created by mike on 28.03.2018. + */ + +@RunWith(RobolectricTestRunner.class) +public class MainAppTest { + MainApp mainApp = new MainApp(); + + @Test + public void busTest() { + Assert.assertNotNull(mainApp.bus()); + } + + @Test + public void gsTest() { + Assert.assertEquals("AndroidAPS", mainApp.gs(R.string.app_name)); + Assert.assertEquals("AndroidAPS", mainApp.gs(R.string.app_name, "")); + } + + @Test + public void gcTest() { + Assert.assertEquals(-16711681, mainApp.gc(R.color.basal)); + } + + @Test + public void instanceTest() { + Assert.assertNotNull(mainApp.instance()); + } + + @Test + public void getDbHelperTest() { + Assert.assertNotNull(mainApp.getDbHelper()); + } + + @Test + public void closeDbHelperTest() { + mainApp.closeDbHelper(); + Assert.assertNull(mainApp.getDbHelper()); + } + + @Test + public void getConfigBuilderTest() { + Assert.assertNotNull(mainApp.getConfigBuilder()); + } + + @Test + public void getConstraintCheckerTest() { + Assert.assertNotNull(mainApp.getConstraintChecker()); + } + + @Test + public void getPluginsListTest() { + Assert.assertNotNull(mainApp.getPluginsList()); + } + + @Test + public void getSpecificPluginsListTest() { + // currently MDI, VP, R, Rv2, KoreanR, RS + Assert.assertEquals(6, mainApp.getSpecificPluginsList(PluginType.PUMP).size()); + } + + @Test + public void getSpecificPluginsVisibleInListTest() { + // currently MDI, VP, R, Rv2, KoreanR, RS + Assert.assertEquals(6, mainApp.getSpecificPluginsVisibleInList(PluginType.PUMP).size()); + } + + @Test + public void getSpecificPluginsListByInterfaceTest() { + // currently MDI, VP, R, Rv2, KoreanR, RS + Assert.assertEquals(6, mainApp.getSpecificPluginsListByInterface(PumpInterface.class).size()); + } + + @Test + public void getSpecificPluginsVisibleInListByInterfaceTest() { + // currently MDI, VP, R, Rv2, KoreanR, RS + Assert.assertEquals(6, mainApp.getSpecificPluginsVisibleInListByInterface(PumpInterface.class, PluginType.PUMP).size()); + } + + @Test + public void getSpecificPluginTest() { + // currently MDI, VP, R, Rv2, KoreanR, RS + Assert.assertEquals("Overview", mainApp.getSpecificPlugin(OverviewPlugin.class).getName()); + } + + @Test + public void isEngineeringModeOrReleaseTest() { + Assert.assertEquals(false, mainApp.isEngineeringModeOrRelease()); + } + + @Test + public void getLogDirectoryTest() { + // logger not initialized in Roboelectric + Assert.assertNull(mainApp.getLogDirectory()); + } + +} diff --git a/app/src/test/java/info/nightscout/androidaps/PumpDanaR/DanaRPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPluginTest.java similarity index 94% rename from app/src/test/java/info/nightscout/androidaps/PumpDanaR/DanaRPluginTest.java rename to app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPluginTest.java index 86681bd3ec..f892262494 100644 --- a/app/src/test/java/info/nightscout/androidaps/PumpDanaR/DanaRPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPluginTest.java @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.PumpDanaR; +package info.nightscout.androidaps.plugins.PumpDanaR; import android.content.Context; @@ -17,8 +17,6 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; -import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; import info.nightscout.utils.SP; import info.nightscout.utils.ToastUtils;