diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index c45b97d50c..3ca653f8ca 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -65,7 +65,4 @@ #2E2E2E - #FFFFFF - #BBBBBB - diff --git a/app/src/test/java/info/nightscout/androidaps/utils/DecimalFormatterTest.java b/app/src/test/java/info/nightscout/androidaps/utils/DecimalFormatterTest.java deleted file mode 100644 index a7112fae48..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/utils/DecimalFormatterTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package info.nightscout.androidaps.utils; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.modules.junit4.PowerMockRunner; - -@RunWith(PowerMockRunner.class) -public class DecimalFormatterTest { - - @Test - public void to0DecimalTest() { - Assert.assertEquals("1", DecimalFormatter.to0Decimal(1.33d).replace(",", ".")); - Assert.assertEquals("1U", DecimalFormatter.to0Decimal(1.33d, "U").replace(",", ".")); - } - - @Test - public void to1DecimalTest() { - Assert.assertEquals("1.3", DecimalFormatter.to1Decimal(1.33d).replace(",", ".")); - Assert.assertEquals("1.3U", DecimalFormatter.to1Decimal(1.33d, "U").replace(",", ".")); - } - - @Test - public void to2DecimalTest() { - Assert.assertEquals("1.33", DecimalFormatter.to2Decimal(1.3333d).replace(",", ".")); - Assert.assertEquals("1.33U", DecimalFormatter.to2Decimal(1.3333d, "U").replace(",", ".")); - } - - @Test - public void to3DecimalTest() { - Assert.assertEquals("1.333", DecimalFormatter.to3Decimal(1.3333d).replace(",", ".")); - Assert.assertEquals("1.333U", DecimalFormatter.to3Decimal(1.3333d, "U").replace(",", ".")); - } - - @Test - public void toPumpSupportedBolus() { - } - - @Test - public void pumpSupportedBolusFormat() { - } -} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/utils/JsonHelperTest.java b/app/src/test/java/info/nightscout/androidaps/utils/JsonHelperTest.java deleted file mode 100644 index 359cc8de29..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/utils/JsonHelperTest.java +++ /dev/null @@ -1,93 +0,0 @@ -package info.nightscout.androidaps.utils; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -/** - * Created by mike on 12.03.2018. - */ - -public class JsonHelperTest { - - private String jsonString = "{\"d\":\"3.0\",\"i\":\"4\",\"s\":\"5\",\"b\":\"true\",\"j\":{\"a\": \"1\"}}"; - - @Test - public void safeGetObjectTest() throws JSONException { - JSONObject object = new JSONObject(jsonString); - Object o = new Object(); - assertEquals(o, JsonHelper.safeGetObject(null, "x", o)); - assertEquals(o, JsonHelper.safeGetObject(object, "x", o)); - assertNotEquals(o, JsonHelper.safeGetObject(object, "d", o)); - } - - @Test - public void safeGetJSONObjectTest() throws JSONException { - JSONObject object = new JSONObject(jsonString); - JSONObject o = new JSONObject(); - assertEquals(o, JsonHelper.safeGetJSONObject(null, "x", o)); - assertTrue(JsonHelper.safeGetJSONObject(object, "j", o).has("a")); - assertEquals(o, JsonHelper.safeGetJSONObject(object, "d", o)); - } - - @Test - public void safeGetStringTest() throws JSONException { - JSONObject object = new JSONObject(jsonString); - Object o = new Object(); - assertNull(JsonHelper.safeGetString(null, "s")); - assertNull(JsonHelper.safeGetString(object, "notexisting")); - assertEquals("5", JsonHelper.safeGetString(object, "s")); - - assertEquals("default", JsonHelper.safeGetString(null, "notexisting", "default")); - assertEquals("default", JsonHelper.safeGetString(object, "notexisting", "default")); - assertEquals("5", JsonHelper.safeGetString(object, "s", "default")); - - assertEquals("default", JsonHelper.safeGetStringAllowNull(null, "notexisting", "default")); - assertEquals("default", JsonHelper.safeGetStringAllowNull(object, "notexisting", "default")); - assertNull(JsonHelper.safeGetStringAllowNull(object, "notexisting", null)); - assertEquals("5", JsonHelper.safeGetStringAllowNull(object, "s", "default")); - } - - @Test - public void safeGetDoubleTest() throws JSONException { - JSONObject object = new JSONObject(jsonString); - - assertEquals(0.0d, JsonHelper.safeGetDouble(object, "notexisting"), 0.0d); - assertEquals(0.0d, JsonHelper.safeGetDouble(null, "notexisting"), 0.0d); - assertEquals(3.0d, JsonHelper.safeGetDouble(object, "d"), 0.000001d); - - assertEquals(6d, JsonHelper.safeGetDouble(null, "notexisting", 6d), 0.0d); - assertEquals(6d, JsonHelper.safeGetDouble(object, "notexisting", 6d), 0.0d); - assertEquals(3d, JsonHelper.safeGetDouble(object, "d", 6d), 0.0d); - } - - @Test - public void safeGetLntTest() throws JSONException { - JSONObject object = new JSONObject(jsonString); - assertEquals(0, JsonHelper.safeGetInt(null, "notexisting")); - assertEquals(0, JsonHelper.safeGetInt(object, "notexisting")); - assertEquals(4, JsonHelper.safeGetInt(object, "i")); - } - - @Test - public void safeGetLongTest() throws JSONException { - JSONObject object = new JSONObject(jsonString); - assertEquals(0, JsonHelper.safeGetInt(null, "notexisting")); - assertEquals(0, JsonHelper.safeGetInt(object, "notexisting")); - assertEquals(4, JsonHelper.safeGetInt(object, "i")); - } - - @Test - public void safeGetBooleanTest() throws JSONException { - JSONObject object = new JSONObject(jsonString); - assertFalse(JsonHelper.safeGetBoolean(null, "notexisting")); - assertFalse(JsonHelper.safeGetBoolean(object, "notexisting")); - assertTrue(JsonHelper.safeGetBoolean(object, "b")); - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/utils/StringUtilsTest.java b/app/src/test/java/info/nightscout/androidaps/utils/StringUtilsTest.java deleted file mode 100644 index dd95962964..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/utils/StringUtilsTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package info.nightscout.androidaps.utils; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class StringUtilsTest { - - @Test - public void removeSurroundingQuotesTest() { - String compareString = "test"; - - assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString)); - assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\"")); - assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString)); - - compareString = "te\"st"; - assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString)); - assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\"")); - assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString)); - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/utils/TTest.java b/app/src/test/java/info/nightscout/androidaps/utils/TTest.java deleted file mode 100644 index fa5bcbd8ad..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/utils/TTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package info.nightscout.androidaps.utils; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Created by mike on 26.03.2018. - */ - -//@RunWith(PowerMockRunner.class) -public class TTest { - - @Test - public void toUnits() { - Assert.assertEquals(1, T.msecs(1000).secs()); - Assert.assertEquals(1, T.secs(60).mins()); - Assert.assertEquals(1, T.mins(60).hours()); - Assert.assertEquals(1, T.hours(24).days()); - Assert.assertEquals(24, T.days(1).hours()); - Assert.assertEquals(60000, T.mins(1).msecs()); - } - - @Test - public void now() { - Assert.assertTrue(Math.abs(T.now().msecs() - System.currentTimeMillis()) < 5000); - } - - @Test - public void additions() { - long nowMsecs = System.currentTimeMillis(); - T now = T.msecs(nowMsecs); - - Assert.assertEquals(now.plus(T.secs(5)).msecs(), nowMsecs + 5 * 1000); - Assert.assertEquals(now.plus(T.mins(5)).msecs(), nowMsecs + 5 * 60 * 1000); - Assert.assertEquals(now.plus(T.hours(5)).msecs(), nowMsecs + 5 * 60 * 60 * 1000); - Assert.assertEquals(now.plus(T.days(5)).msecs(), nowMsecs + 5 * 24 * 60 * 60 * 1000); - } - - @Test - public void subtractions() { - long nowMsecs = System.currentTimeMillis(); - T now = T.msecs(nowMsecs); - - Assert.assertEquals(now.minus(T.secs(5)).msecs(), nowMsecs - 5 * 1000); - Assert.assertEquals(now.minus(T.mins(5)).msecs(), nowMsecs - 5 * 60 * 1000); - Assert.assertEquals(now.minus(T.hours(5)).msecs(), nowMsecs - 5 * 60 * 60 * 1000); - Assert.assertEquals(now.minus(T.days(5)).msecs(), nowMsecs - 5 * 24 * 60 * 60 * 1000); - } -} diff --git a/core/build.gradle b/core/build.gradle index c9a18bd8ce..a539c9528d 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -46,6 +46,17 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + testOptions { + unitTests { + returnDefaultValues = true + includeAndroidResources = true + + all { + maxParallelForks = 10 + forkEvery = 20 + } + } + } } diff --git a/core/core_dependencies.gradle b/core/core_dependencies.gradle index 283e90adab..dbbb926ca6 100644 --- a/core/core_dependencies.gradle +++ b/core/core_dependencies.gradle @@ -109,6 +109,12 @@ dependencies { api 'com.scottyab:rootbeer-lib:0.0.8' testImplementation "junit:junit:$junit_version" + testImplementation "org.mockito:mockito-core:${mockitoVersion}" + testImplementation "org.powermock:powermock-api-mockito2:${powermockVersion}" + testImplementation "org.powermock:powermock-module-junit4-rule-agent:${powermockVersion}" + testImplementation "org.powermock:powermock-module-junit4-rule:${powermockVersion}" + testImplementation "org.powermock:powermock-module-junit4:${powermockVersion}" androidTestImplementation "androidx.test.ext:junit:$androidx_junit" androidTestImplementation "androidx.test:rules:$androidx_rules" + testImplementation 'org.json:json:20201115' // Needed for JsonHelperTest } \ No newline at end of file diff --git a/core/src/main/res/layout/maintenance_import_list_item.xml b/core/src/main/res/layout/maintenance_import_list_item.xml index e36e36cd6f..f79758b19b 100644 --- a/core/src/main/res/layout/maintenance_import_list_item.xml +++ b/core/src/main/res/layout/maintenance_import_list_item.xml @@ -1,5 +1,6 @@ + app:tint="@color/importListFileName" /> + app:tint="@color/importListAdditionalInfo" /> + app:tint="@color/importListAdditionalInfo" /> #FF8C00 #FF5555 + + #BBBBBB + #FFFFFF + diff --git a/core/src/test/java/info/nightscout/androidaps/TestBase.kt b/core/src/test/java/info/nightscout/androidaps/TestBase.kt new file mode 100644 index 0000000000..573c9dcbcc --- /dev/null +++ b/core/src/test/java/info/nightscout/androidaps/TestBase.kt @@ -0,0 +1,40 @@ +package info.nightscout.androidaps + +import info.nightscout.androidaps.logging.AAPSLoggerTest +import info.nightscout.androidaps.utils.rx.AapsSchedulers +import info.nightscout.androidaps.utils.rx.TestAapsSchedulers +import org.junit.Before +import org.junit.Rule +import org.mockito.Mockito +import org.mockito.junit.MockitoJUnit +import org.mockito.junit.MockitoRule +import java.util.* + +@Suppress("SpellCheckingInspection") +open class TestBase { + + val aapsLogger = AAPSLoggerTest() + val aapsSchedulers: AapsSchedulers = TestAapsSchedulers() + + // Add a JUnit rule that will setup the @Mock annotated vars and log. + // Another possibility would be to add `MockitoAnnotations.initMocks(this) to the setup method. + @get:Rule + val mockitoRule: MockitoRule = MockitoJUnit.rule() + + @Before + fun setupLocale() { + Locale.setDefault(Locale.ENGLISH) + System.setProperty("disableFirebase", "true") + } + + // Workaround for Kotlin nullability. + // https://medium.com/@elye.project/befriending-kotlin-and-mockito-1c2e7b0ef791 + // https://stackoverflow.com/questions/30305217/is-it-possible-to-use-mockito-in-kotlin + fun anyObject(): T { + Mockito.any() + return uninitialized() + } + + @Suppress("Unchecked_Cast") + fun uninitialized(): T = null as T +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/utils/CryptoUtilTest.kt b/core/src/test/java/info/nightscout/androidaps/utils/CryptoUtilTest.kt similarity index 96% rename from app/src/test/java/info/nightscout/androidaps/utils/CryptoUtilTest.kt rename to core/src/test/java/info/nightscout/androidaps/utils/CryptoUtilTest.kt index 9d6b3ad9cd..f79dc0b95d 100644 --- a/app/src/test/java/info/nightscout/androidaps/utils/CryptoUtilTest.kt +++ b/core/src/test/java/info/nightscout/androidaps/utils/CryptoUtilTest.kt @@ -1,97 +1,98 @@ -package info.nightscout.androidaps.utils - -import info.nightscout.androidaps.TestBase -import org.hamcrest.CoreMatchers.containsString -import org.hamcrest.CoreMatchers.not -import org.junit.Assert -import org.junit.Assume.assumeThat -import org.junit.Test -import org.junit.runner.RunWith -import org.powermock.core.classloader.annotations.PowerMockIgnore -import org.powermock.modules.junit4.PowerMockRunner - -// https://stackoverflow.com/questions/52344522/joseexception-couldnt-create-aes-gcm-nopadding-cipher-illegal-key-size -// https://stackoverflow.com/questions/47708951/can-aes-256-work-on-android-devices-with-api-level-26 -// Java prior to Oracle Java 8u161 does not have policy for 256 bit AES - but Android support it -// when test is run in Vanilla JVM without policy - Invalid key size exception is thrown -fun assumeAES256isSupported(cryptoUtil: CryptoUtil) { - cryptoUtil.lastException?.message?.let { exceptionMessage -> - assumeThat("Upgrade your testing environment Java (OpenJDK or Java 8u161) and JAVA_HOME - AES 256 is supported by Android so this exception should not happen!", exceptionMessage, not(containsString("key size"))) - } -} - -@PowerMockIgnore("javax.crypto.*") -@RunWith(PowerMockRunner::class) -class CryptoUtilTest: TestBase() { - - var cryptoUtil: CryptoUtil = CryptoUtil(aapsLogger) - - @Test - fun testFixedSaltCrypto() { - val salt = byteArrayOf( - -33, -29, 16, -19, 99, -111, -3, 2, 116, 106, 47, 38, -54, 11, -77, 28, - 111, -15, -65, -110, 4, -32, -29, -70, -95, -88, -53, 19, 87, -103, 123, -15) - - val password = "thisIsFixedPassword" - val payload = "FIXED-PAYLOAD" - - val encrypted = cryptoUtil.encrypt(password, salt, payload) - assumeAES256isSupported(cryptoUtil) - Assert.assertNotNull(encrypted) - - val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!) - assumeAES256isSupported(cryptoUtil) - Assert.assertEquals(decrypted, payload) - } - - @Test - fun testStandardCrypto() { - val salt = cryptoUtil.mineSalt() - - val password = "topSikret" - val payload = "{what:payloadYouWantToProtect}" - - val encrypted = cryptoUtil.encrypt(password, salt, payload) - assumeAES256isSupported(cryptoUtil) - Assert.assertNotNull(encrypted) - - val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!) - assumeAES256isSupported(cryptoUtil) - Assert.assertEquals(decrypted, payload) - } - - @Test - fun testHashVector() { - val payload = "{what:payloadYouWantToProtect}" - val hash = cryptoUtil.sha256(payload) - Assert.assertEquals(hash, "a1aafe3ed6cc127e6d102ddbc40a205147230e9cfd178daf108c83543bbdcd13") - } - - @Test - fun testHmac() { - val payload = "{what:payloadYouWantToProtect}" - val password = "topSikret" - val expectedHmac = "ea2213953d0f2e55047cae2d23fb4f0de1b805d55e6271efa70d6b85fb692bea" // generated using other HMAC tool - val hash = cryptoUtil.hmac256(payload, password) - Assert.assertEquals(hash, expectedHmac) - } - - @Test - fun testPlainPasswordCheck() { - Assert.assertTrue(cryptoUtil.checkPassword("same", "same")) - Assert.assertFalse(cryptoUtil.checkPassword("same", "other")) - } - - @Test - fun testHashedPasswordCheck() { - Assert.assertTrue(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("givenSecret"))) - Assert.assertFalse(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("otherSecret"))) - - Assert.assertTrue(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1")) - Assert.assertFalse(cryptoUtil.checkPassword("givenMashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1")) - Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:0fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1")) - Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:b0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1")) - } - -} - +package info.nightscout.androidaps.utils + +import info.nightscout.androidaps.TestBase +import org.hamcrest.CoreMatchers.containsString +import org.hamcrest.CoreMatchers.not +import org.junit.Assert +import org.junit.Assume.assumeThat +import org.junit.Test +import org.junit.runner.RunWith +import org.powermock.core.classloader.annotations.PowerMockIgnore +import org.powermock.modules.junit4.PowerMockRunner + +// https://stackoverflow.com/questions/52344522/joseexception-couldnt-create-aes-gcm-nopadding-cipher-illegal-key-size +// https://stackoverflow.com/questions/47708951/can-aes-256-work-on-android-devices-with-api-level-26 +// Java prior to Oracle Java 8u161 does not have policy for 256 bit AES - but Android support it +// when test is run in Vanilla JVM without policy - Invalid key size exception is thrown +fun assumeAES256isSupported(cryptoUtil: CryptoUtil) { + cryptoUtil.lastException?.message?.let { exceptionMessage -> + assumeThat("Upgrade your testing environment Java (OpenJDK or Java 8u161) and JAVA_HOME - AES 256 is supported by Android so this exception should not happen!", exceptionMessage, not(containsString("key size"))) + } +} + +@Suppress("SpellCheckingInspection") +@PowerMockIgnore("javax.crypto.*") +@RunWith(PowerMockRunner::class) +class CryptoUtilTest: TestBase() { + + private var cryptoUtil: CryptoUtil = CryptoUtil(aapsLogger) + + @Test + fun testFixedSaltCrypto() { + val salt = byteArrayOf( + -33, -29, 16, -19, 99, -111, -3, 2, 116, 106, 47, 38, -54, 11, -77, 28, + 111, -15, -65, -110, 4, -32, -29, -70, -95, -88, -53, 19, 87, -103, 123, -15) + + val password = "thisIsFixedPassword" + val payload = "FIXED-PAYLOAD" + + val encrypted = cryptoUtil.encrypt(password, salt, payload) + assumeAES256isSupported(cryptoUtil) + Assert.assertNotNull(encrypted) + + val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!) + assumeAES256isSupported(cryptoUtil) + Assert.assertEquals(decrypted, payload) + } + + @Test + fun testStandardCrypto() { + val salt = cryptoUtil.mineSalt() + + val password = "topSikret" + val payload = "{what:payloadYouWantToProtect}" + + val encrypted = cryptoUtil.encrypt(password, salt, payload) + assumeAES256isSupported(cryptoUtil) + Assert.assertNotNull(encrypted) + + val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!) + assumeAES256isSupported(cryptoUtil) + Assert.assertEquals(decrypted, payload) + } + + @Test + fun testHashVector() { + val payload = "{what:payloadYouWantToProtect}" + val hash = cryptoUtil.sha256(payload) + Assert.assertEquals(hash, "a1aafe3ed6cc127e6d102ddbc40a205147230e9cfd178daf108c83543bbdcd13") + } + + @Test + fun testHmac() { + val payload = "{what:payloadYouWantToProtect}" + val password = "topSikret" + val expectedHmac = "ea2213953d0f2e55047cae2d23fb4f0de1b805d55e6271efa70d6b85fb692bea" // generated using other HMAC tool + val hash = cryptoUtil.hmac256(payload, password) + Assert.assertEquals(hash, expectedHmac) + } + + @Test + fun testPlainPasswordCheck() { + Assert.assertTrue(cryptoUtil.checkPassword("same", "same")) + Assert.assertFalse(cryptoUtil.checkPassword("same", "other")) + } + + @Test + fun testHashedPasswordCheck() { + Assert.assertTrue(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("givenSecret"))) + Assert.assertFalse(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("otherSecret"))) + + Assert.assertTrue(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1")) + Assert.assertFalse(cryptoUtil.checkPassword("givenMashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1")) + Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:0fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1")) + Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:b0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1")) + } + +} + diff --git a/app/src/test/java/info/nightscout/androidaps/utils/DateUtilTest.kt b/core/src/test/java/info/nightscout/androidaps/utils/DateUtilTest.kt similarity index 98% rename from app/src/test/java/info/nightscout/androidaps/utils/DateUtilTest.kt rename to core/src/test/java/info/nightscout/androidaps/utils/DateUtilTest.kt index 064b843234..8df098789e 100644 --- a/app/src/test/java/info/nightscout/androidaps/utils/DateUtilTest.kt +++ b/core/src/test/java/info/nightscout/androidaps/utils/DateUtilTest.kt @@ -2,7 +2,7 @@ package info.nightscout.androidaps.utils import android.content.Context import info.nightscout.androidaps.TestBase -import info.nightscout.androidaps.R +import info.nightscout.androidaps.core.R import info.nightscout.androidaps.utils.resources.ResourceHelper import org.junit.Assert import org.junit.Test diff --git a/core/src/test/java/info/nightscout/androidaps/utils/DecimalFormatterTest.kt b/core/src/test/java/info/nightscout/androidaps/utils/DecimalFormatterTest.kt new file mode 100644 index 0000000000..35049e7fb8 --- /dev/null +++ b/core/src/test/java/info/nightscout/androidaps/utils/DecimalFormatterTest.kt @@ -0,0 +1,30 @@ +package info.nightscout.androidaps.utils + +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +class DecimalFormatterTest { + + @Test fun to0DecimalTest() { + Assert.assertEquals("1", DecimalFormatter.to0Decimal(1.33).replace(",", ".")) + Assert.assertEquals("1U", DecimalFormatter.to0Decimal(1.33, "U").replace(",", ".")) + } + + @Test fun to1DecimalTest() { + Assert.assertEquals("1.3", DecimalFormatter.to1Decimal(1.33).replace(",", ".")) + Assert.assertEquals("1.3U", DecimalFormatter.to1Decimal(1.33, "U").replace(",", ".")) + } + + @Test fun to2DecimalTest() { + Assert.assertEquals("1.33", DecimalFormatter.to2Decimal(1.3333).replace(",", ".")) + Assert.assertEquals("1.33U", DecimalFormatter.to2Decimal(1.3333, "U").replace(",", ".")) + } + + @Test fun to3DecimalTest() { + Assert.assertEquals("1.333", DecimalFormatter.to3Decimal(1.3333).replace(",", ".")) + Assert.assertEquals("1.333U", DecimalFormatter.to3Decimal(1.3333, "U").replace(",", ".")) + } +} \ No newline at end of file diff --git a/core/src/test/java/info/nightscout/androidaps/utils/JsonHelperTest.kt b/core/src/test/java/info/nightscout/androidaps/utils/JsonHelperTest.kt new file mode 100644 index 0000000000..c2863ff4a3 --- /dev/null +++ b/core/src/test/java/info/nightscout/androidaps/utils/JsonHelperTest.kt @@ -0,0 +1,82 @@ +package info.nightscout.androidaps.utils + +import org.json.JSONObject +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.powermock.modules.junit4.PowerMockRunner + +@Suppress("SpellCheckingInspection") +@RunWith(PowerMockRunner::class) +class JsonHelperTest { + + private val jsonString = "{\"d\":\"3.0\",\"i\":\"4\",\"s\":\"5\",\"b\":\"true\",\"j\":{\"a\": \"1\"}}" + + @Test + fun safeGetObjectTest() { + val json = JSONObject(jsonString) + val o = Any() + Assert.assertEquals(o, JsonHelper.safeGetObject(null, "x", o)) + Assert.assertEquals(o, JsonHelper.safeGetObject(json, "x", o)) + Assert.assertNotEquals(o, JsonHelper.safeGetObject(json, "d", o)) + } + + @Test + fun safeGetJSONObjectTest() { + val json = JSONObject(jsonString) + val o = JSONObject() + Assert.assertEquals(o, JsonHelper.safeGetJSONObject(null, "x", o)) + Assert.assertTrue(JsonHelper.safeGetJSONObject(json, "j", o)!!.has("a")) + Assert.assertEquals(o, JsonHelper.safeGetJSONObject(json, "d", o)) + } + + @Test + fun safeGetStringTest() { + val json = JSONObject(jsonString) + Assert.assertNull(JsonHelper.safeGetString(null, "s")) + Assert.assertNull(JsonHelper.safeGetString(json, "notexisting")) + Assert.assertEquals("5", JsonHelper.safeGetString(json, "s")) + Assert.assertEquals("default", JsonHelper.safeGetString(null, "notexisting", "default")) + Assert.assertEquals("default", JsonHelper.safeGetString(json, "notexisting", "default")) + Assert.assertEquals("5", JsonHelper.safeGetString(json, "s", "default")) + Assert.assertEquals("default", JsonHelper.safeGetStringAllowNull(null, "notexisting", "default")) + Assert.assertEquals("default", JsonHelper.safeGetStringAllowNull(json, "notexisting", "default")) + Assert.assertNull(JsonHelper.safeGetStringAllowNull(json, "notexisting", null)) + Assert.assertEquals("5", JsonHelper.safeGetStringAllowNull(json, "s", "default")) + } + + @Test + fun safeGetDoubleTest() { + val json = JSONObject(jsonString) + Assert.assertEquals(0.0, JsonHelper.safeGetDouble(json, "notexisting"), 0.0) + Assert.assertEquals(0.0, JsonHelper.safeGetDouble(null, "notexisting"), 0.0) + Assert.assertEquals(3.0, JsonHelper.safeGetDouble(json, "d"), 0.000001) + Assert.assertEquals(6.0, JsonHelper.safeGetDouble(null, "notexisting", 6.0), 0.0) + Assert.assertEquals(6.0, JsonHelper.safeGetDouble(json, "notexisting", 6.0), 0.0) + Assert.assertEquals(3.0, JsonHelper.safeGetDouble(json, "d", 6.0), 0.0) + } + + @Test + fun safeGetLntTest() { + val json = JSONObject(jsonString) + Assert.assertEquals(0, JsonHelper.safeGetInt(null, "notexisting").toLong()) + Assert.assertEquals(0, JsonHelper.safeGetInt(json, "notexisting").toLong()) + Assert.assertEquals(4, JsonHelper.safeGetInt(json, "i").toLong()) + } + + @Test + fun safeGetLongTest() { + val json = JSONObject(jsonString) + Assert.assertEquals(0, JsonHelper.safeGetInt(null, "notexisting").toLong()) + Assert.assertEquals(0, JsonHelper.safeGetInt(json, "notexisting").toLong()) + Assert.assertEquals(4, JsonHelper.safeGetInt(json, "i").toLong()) + } + + @Test + fun safeGetBooleanTest() { + val json = JSONObject(jsonString) + Assert.assertFalse(JsonHelper.safeGetBoolean(null, "notexisting")) + Assert.assertFalse(JsonHelper.safeGetBoolean(json, "notexisting")) + Assert.assertTrue(JsonHelper.safeGetBoolean(json, "b")) + } +} \ No newline at end of file diff --git a/core/src/test/java/info/nightscout/androidaps/utils/RoundTest.kt b/core/src/test/java/info/nightscout/androidaps/utils/RoundTest.kt index ad86a4755b..e798b28e35 100644 --- a/core/src/test/java/info/nightscout/androidaps/utils/RoundTest.kt +++ b/core/src/test/java/info/nightscout/androidaps/utils/RoundTest.kt @@ -32,4 +32,9 @@ class RoundTest { Assert.assertEquals(2.0, Round.ceilTo(1.49999, 1.0), 0.00000001) Assert.assertEquals(0.0, Round.ceilTo(0.0, 1.0), 0.00000001) } + + @Test + fun isSameTest() { + Assert.assertTrue(Round.isSame(0.54, 0.54)) + } } \ No newline at end of file diff --git a/core/src/test/java/info/nightscout/androidaps/utils/StringUtilsTest.kt b/core/src/test/java/info/nightscout/androidaps/utils/StringUtilsTest.kt new file mode 100644 index 0000000000..3bf95fa75e --- /dev/null +++ b/core/src/test/java/info/nightscout/androidaps/utils/StringUtilsTest.kt @@ -0,0 +1,18 @@ +package info.nightscout.androidaps.utils + +import org.junit.Assert +import org.junit.Test + +class StringUtilsTest { + + @Test fun removeSurroundingQuotesTest() { + var compareString = "test" + Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString)) + Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\"")) + Assert.assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString)) + compareString = "te\"st" + Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString)) + Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\"")) + Assert.assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString)) + } +} \ No newline at end of file diff --git a/core/src/test/java/info/nightscout/androidaps/utils/TTest.kt b/core/src/test/java/info/nightscout/androidaps/utils/TTest.kt new file mode 100644 index 0000000000..7893ffda67 --- /dev/null +++ b/core/src/test/java/info/nightscout/androidaps/utils/TTest.kt @@ -0,0 +1,40 @@ +package info.nightscout.androidaps.utils + +import org.junit.Assert +import org.junit.Test +import kotlin.math.abs + +@Suppress("SpellCheckingInspection") +class TTest { + + @Test fun toUnits() { + Assert.assertEquals(1, T.msecs(1000).secs()) + Assert.assertEquals(1, T.secs(60).mins()) + Assert.assertEquals(1, T.mins(60).hours()) + Assert.assertEquals(1, T.hours(24).days()) + Assert.assertEquals(24, T.days(1).hours()) + Assert.assertEquals(60000, T.mins(1).msecs()) + } + + @Test fun now() { + Assert.assertTrue(abs(T.now().msecs() - System.currentTimeMillis()) < 5000) + } + + @Test fun additions() { + val nowMsecs = System.currentTimeMillis() + val now = T.msecs(nowMsecs) + Assert.assertEquals(now.plus(T.secs(5)).msecs(), nowMsecs + 5 * 1000) + Assert.assertEquals(now.plus(T.mins(5)).msecs(), nowMsecs + 5 * 60 * 1000) + Assert.assertEquals(now.plus(T.hours(5)).msecs(), nowMsecs + 5 * 60 * 60 * 1000) + Assert.assertEquals(now.plus(T.days(5)).msecs(), nowMsecs + 5 * 24 * 60 * 60 * 1000) + } + + @Test fun subtractions() { + val nowMsecs = System.currentTimeMillis() + val now = T.msecs(nowMsecs) + Assert.assertEquals(now.minus(T.secs(5)).msecs(), nowMsecs - 5 * 1000) + Assert.assertEquals(now.minus(T.mins(5)).msecs(), nowMsecs - 5 * 60 * 1000) + Assert.assertEquals(now.minus(T.hours(5)).msecs(), nowMsecs - 5 * 60 * 60 * 1000) + Assert.assertEquals(now.minus(T.days(5)).msecs(), nowMsecs - 5 * 24 * 60 * 60 * 1000) + } +} \ No newline at end of file