tests -> kt

This commit is contained in:
Milos Kozak 2022-05-03 20:46:41 +02:00
parent 787bd4a074
commit 37cee04926
11 changed files with 544 additions and 712 deletions

View file

@ -1,206 +0,0 @@
package info.nightscout.androidaps.interaction.utils;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import info.nightscout.androidaps.TestBase;
import info.nightscout.androidaps.data.RawDisplayData;
import info.nightscout.androidaps.testing.mockers.RawDataMocker;
import info.nightscout.androidaps.testing.mockers.WearUtilMocker;
import info.nightscout.shared.sharedPreferences.SP;
/**
* This test covers DisplayFormat class (directly)
* but also SmallestDoubleString - due to carefully chosen input data to format
*/
public class DisplayFormatTest extends TestBase {
private DisplayFormat displayFormat;
private RawDataMocker rawDataMocker;
private WearUtil wearUtil;
private WearUtilMocker wearUtilMocker;
@Mock SP sp;
@Before
public void mock() {
wearUtil = spy(new WearUtil());
rawDataMocker = new RawDataMocker(wearUtil);
wearUtilMocker = new WearUtilMocker(wearUtil);
wearUtilMocker.prepareMock();
displayFormat = new DisplayFormat();
displayFormat.wearUtil = wearUtil;
displayFormat.sp = sp;
when(sp.getBoolean("complication_unicode", true)).thenReturn(true);
}
@Test
public void shortTimeSinceTest() {
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 0, 0)), "0'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 0, 5)), "0'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 0, 55)), "0'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 1, 0)), "1'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 1, 59)), "1'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 2, 0)), "2'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 3, 0)), "3'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 4, 0)), "4'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 10, 0)), "10'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 30, 0)), "30'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 59, 0)), "59'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 59, 59)), "59'");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 1, 0, 0)), "1h");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 1, 30, 0)), "1h");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 1, 59, 59)), "1h");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 2, 0, 0)), "2h");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 3, 0, 0)), "3h");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 4, 0, 0)), "4h");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 5, 0, 0)), "5h");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 12, 0, 0)), "12h");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 18, 0, 0)), "18h");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 23, 59, 59)), "23h");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(1, 0, 0, 0)), "1d");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(1, 12, 0, 0)), "1d");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(1, 23, 59, 59)), "1d");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(2, 0, 0, 0)), "2d");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(3, 0, 0, 0)), "3d");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(4, 0, 0, 0)), "4d");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(5, 0, 0, 0)), "5d");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(6, 0, 0, 0)), "6d");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(6, 23, 59, 59)), "6d");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(7, 0, 0, 0)), "1w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(8, 0, 0, 0)), "1w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(9, 0, 0, 0)), "1w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(13, 23, 59, 59)), "1w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(14, 0, 0, 0)), "2w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(21, 0, 0, 0)), "3w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(28, 0, 0, 0)), "4w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(31, 0, 0, 0)), "4w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(32, 0, 0, 0)), "4w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(35, 0, 0, 0)), "5w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(100, 0, 0, 0)), "14w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(200, 0, 0, 0)), "28w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(365, 0, 0, 0)), "52w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(366, 0, 0, 0)), "52w");
assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(367, 0, 0, 0)), "52w");
}
@Test
public void shortTrendTest() {
RawDisplayData raw = new RawDisplayData();
assertEquals(displayFormat.shortTrend(raw), "-- Δ--");
raw.getSingleBg().setTimeStamp(wearUtilMocker.backInTime(0, 0, 2, 0));
assertEquals(displayFormat.shortTrend(raw), "2' Δ--");
when(sp.getBoolean("complication_unicode", true)).thenReturn(true);
// shortening
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(2, "1.2")), "2' Δ1.2");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(11, "1.2")), "11' 1.2");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(12, "0.7")), "12' Δ.7");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(10, "1.0")), "10' Δ1");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(14, "-5.0")), "14' Δ-5");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(13, "-5.1")), "13' -5");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(15, "0.87")), "15' .87");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(10, "-1.78")), "10' -2");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(3, "2.549")), "3' 2.55");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(1, "-1.563")), "1' -1.6");
// preserving separator
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(2, "1,2")), "2' Δ1,2");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(15, "0,87")), "15' ,87");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(3, "+2,549")), "3' 2,55");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(1, "-1,563")), "1' -1,6");
// UTF-off mode - without delta symbol
when(sp.getBoolean("complication_unicode", true)).thenReturn(false);
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(2, "1.2")), "2' 1.2");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(12, "0.7")), "12' 0.7");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(10, "1.0")), "10' 1.0");
assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(14, "-5.0")), "14' -5");
}
@Test
public void longGlucoseLine() {
assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("125", 2, "1.2")), "125→ Δ1.2 (2')");
assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("97", 11, "5.2")), "97↗ Δ5.2 (11')");
assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("110", 12, "0.7")), "110→ Δ.7 (12')");
assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("65", 10, "7.0")), "65↗ Δ7 (10')");
assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("215", 14, "-5.0")), "215↘ Δ-5 (14')");
assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("8.3", 13, "-5.1")), "8.3↘ Δ-5.1 (13')");
assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("6.8", 15, "10.83")), "6.8↑ Δ10.83 (15')");
assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("13.2", 10, "-11.78")), "13.2↓ Δ-11.78 (10')");
assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("3.9", 3, "2.549")), "3.9→ Δ2.549 (3')");
assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("11.1", 1, "-15.563")), "11.1↓ Δ-15.563 (1')");
}
@Test
public void longDetailsLineTest() {
when(sp.getBoolean("complication_unicode", true)).thenReturn(true);
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("0g", "0U", "3.5U/h")), "0g ⁞ 0U ⁞ ⎍3.5U/h");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("50g", "7.56U", "0%")), "50g ⁞ 7.56U ⁞ ⎍0%");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("12g", "3.23U", "120%")), "12g ⁞ 3.23U ⁞ 120%");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("2(40)g", "-1.5U", "0.55U/h")), "2(40)g ⁞ -2U ⁞ 0.55U/h");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("0(24)g", "0.05U", "160%")), "0(24)g ⁞ 0.05U ⁞ 160%");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("47g", "13.87U", "220%")), "47g ⁞ 13.87U ⁞ 220%");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("13(5)g", "5.90U", "300%")), "13(5)g ⁞ 5.90U ⁞ 300%");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("11(50)g", "0U", "70%")), "11(50)g ⁞ 0U ⁞ 70%");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("7g", "0.54U", "30%")), "7g ⁞ 0.54U ⁞ ⎍30%");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("19(38)g", "35.545U", "12.9U/h")), "19g ⁞ 36U ⁞ 12.9U/h");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("100(1)g", "12.345U", "6.98647U/h")), "100g 12U 6.98647U/h");
when(sp.getBoolean("complication_unicode", true)).thenReturn(false);
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("0g", "0U", "3.5U/h")), "0g | 0U | 3.5U/h");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("50g", "7.56U", "0%")), "50g | 7.56U | 0%");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("12g", "3.23U", "120%")), "12g | 3.23U | 120%");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("7g", "0.54U", "30%")), "7g | 0.54U | 30%");
assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("19(38)g", "35.545U", "12.9U/h")), "19g | 36U | 12.9U/h");
}
@Test
public void detailedIobTest() {
assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("-1.29U", "(0,910|-2,20)")), Pair.create("-1.29U", ",91 -2"));
assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("3.50U", "")), Pair.create("3.50U", ""));
assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("12.5U", "(+1,4|-4.78)")), Pair.create("12.5U", "1,4 -5"));
assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("0.67U", "some junks")), Pair.create(".67U", ""));
assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("-11.0U", "(broken|data)")), Pair.create("-11U", "-- --"));
assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("5.52U", "(0,5439|wrong)")), Pair.create("5.52U", ",54 --"));
assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("-8.1U", "(|-8,1)")), Pair.create("-8.1U", "-- -8"));
assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("-8.1U", "(|-8,1)")), Pair.create("-8.1U", "-- -8"));
assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("7.6U", "(malformed)")), Pair.create("7.6U", ""));
assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("-4.26U", "(6,97|1,3422|too much)")), Pair.create("-4.26U", "7 1,3"));
}
@Test
public void detailedCobTest() {
assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("0g")), Pair.create("0g", ""));
assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("50g")), Pair.create("50g", ""));
assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("2(40)g")), Pair.create("2g", "40g"));
assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("0(24)g")), Pair.create("0g", "24g"));
assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("13(5)g")), Pair.create("13g", "5g"));
assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("11(50)g")), Pair.create("11g", "50g"));
assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("19(38)g")), Pair.create("19g", "38g"));
assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("100(1)g")), Pair.create("100g", "1g"));
}
}

View file

@ -0,0 +1,168 @@
package info.nightscout.androidaps.interaction.utils
import info.nightscout.androidaps.TestBase
import info.nightscout.androidaps.data.RawDisplayData
import info.nightscout.androidaps.testing.mockers.RawDataMocker
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito
/**
* This test covers DisplayFormat class (directly)
* but also SmallestDoubleString - due to carefully chosen input data to format
*/
class DisplayFormatTest : TestBase() {
private lateinit var displayFormat: DisplayFormat
private lateinit var rawDataMocker: RawDataMocker
@Before fun mock() {
rawDataMocker = RawDataMocker(wearUtil)
wearUtilMocker.prepareMock()
displayFormat = DisplayFormat()
displayFormat.wearUtil = wearUtil
displayFormat.sp = sp
Mockito.`when`(sp.getBoolean("complication_unicode", true)).thenReturn(true)
}
@Test fun shortTimeSinceTest() {
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 0, 0)), "0'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 0, 5)), "0'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 0, 55)), "0'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 1, 0)), "1'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 1, 59)), "1'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 2, 0)), "2'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 3, 0)), "3'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 4, 0)), "4'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 10, 0)), "10'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 30, 0)), "30'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 59, 0)), "59'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 59, 59)), "59'")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 1, 0, 0)), "1h")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 1, 30, 0)), "1h")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 1, 59, 59)), "1h")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 2, 0, 0)), "2h")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 3, 0, 0)), "3h")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 4, 0, 0)), "4h")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 5, 0, 0)), "5h")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 12, 0, 0)), "12h")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 18, 0, 0)), "18h")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 23, 59, 59)), "23h")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(1, 0, 0, 0)), "1d")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(1, 12, 0, 0)), "1d")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(1, 23, 59, 59)), "1d")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(2, 0, 0, 0)), "2d")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(3, 0, 0, 0)), "3d")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(4, 0, 0, 0)), "4d")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(5, 0, 0, 0)), "5d")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(6, 0, 0, 0)), "6d")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(6, 23, 59, 59)), "6d")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(7, 0, 0, 0)), "1w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(8, 0, 0, 0)), "1w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(9, 0, 0, 0)), "1w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(13, 23, 59, 59)), "1w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(14, 0, 0, 0)), "2w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(21, 0, 0, 0)), "3w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(28, 0, 0, 0)), "4w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(31, 0, 0, 0)), "4w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(32, 0, 0, 0)), "4w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(35, 0, 0, 0)), "5w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(100, 0, 0, 0)), "14w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(200, 0, 0, 0)), "28w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(365, 0, 0, 0)), "52w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(366, 0, 0, 0)), "52w")
Assert.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(367, 0, 0, 0)), "52w")
}
@Test fun shortTrendTest() {
val raw = RawDisplayData()
Assert.assertEquals(displayFormat.shortTrend(raw), "-- Δ--")
raw.singleBg.timeStamp = wearUtilMocker.backInTime(0, 0, 2, 0)
Assert.assertEquals(displayFormat.shortTrend(raw), "2' Δ--")
Mockito.`when`(sp.getBoolean("complication_unicode", true)).thenReturn(true)
// shortening
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(2, "1.2")), "2' Δ1.2")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(11, "1.2")), "11' 1.2")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(12, "0.7")), "12' Δ.7")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(10, "1.0")), "10' Δ1")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(14, "-5.0")), "14' Δ-5")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(13, "-5.1")), "13' -5")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(15, "0.87")), "15' .87")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(10, "-1.78")), "10' -2")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(3, "2.549")), "3' 2.55")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(1, "-1.563")), "1' -1.6")
// preserving separator
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(2, "1,2")), "2' Δ1,2")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(15, "0,87")), "15' ,87")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(3, "+2,549")), "3' 2,55")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(1, "-1,563")), "1' -1,6")
// UTF-off mode - without delta symbol
Mockito.`when`(sp.getBoolean("complication_unicode", true)).thenReturn(false)
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(2, "1.2")), "2' 1.2")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(12, "0.7")), "12' 0.7")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(10, "1.0")), "10' 1.0")
Assert.assertEquals(displayFormat.shortTrend(rawDataMocker.rawDelta(14, "-5.0")), "14' -5")
}
@Test fun longGlucoseLine() {
Assert.assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("125", 2, "1.2")), "125→ Δ1.2 (2')")
Assert.assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("97", 11, "5.2")), "97↗ Δ5.2 (11')")
Assert.assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("110", 12, "0.7")), "110→ Δ.7 (12')")
Assert.assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("65", 10, "7.0")), "65↗ Δ7 (10')")
Assert.assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("215", 14, "-5.0")), "215↘ Δ-5 (14')")
Assert.assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("8.3", 13, "-5.1")), "8.3↘ Δ-5.1 (13')")
Assert.assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("6.8", 15, "10.83")), "6.8↑ Δ10.83 (15')")
Assert.assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("13.2", 10, "-11.78")), "13.2↓ Δ-11.78 (10')")
Assert.assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("3.9", 3, "2.549")), "3.9→ Δ2.549 (3')")
Assert.assertEquals(displayFormat.longGlucoseLine(rawDataMocker.rawSgv("11.1", 1, "-15.563")), "11.1↓ Δ-15.563 (1')")
}
@Test fun longDetailsLineTest() {
Mockito.`when`(sp.getBoolean("complication_unicode", true)).thenReturn(true)
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("0g", "0U", "3.5U/h")), "0g ⁞ 0U ⁞ ⎍3.5U/h")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("50g", "7.56U", "0%")), "50g ⁞ 7.56U ⁞ ⎍0%")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("12g", "3.23U", "120%")), "12g ⁞ 3.23U ⁞ 120%")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("2(40)g", "-1.5U", "0.55U/h")), "2(40)g ⁞ -2U ⁞ 0.55U/h")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("0(24)g", "0.05U", "160%")), "0(24)g ⁞ 0.05U ⁞ 160%")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("47g", "13.87U", "220%")), "47g ⁞ 13.87U ⁞ 220%")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("13(5)g", "5.90U", "300%")), "13(5)g ⁞ 5.90U ⁞ 300%")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("11(50)g", "0U", "70%")), "11(50)g ⁞ 0U ⁞ 70%")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("7g", "0.54U", "30%")), "7g ⁞ 0.54U ⁞ ⎍30%")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("19(38)g", "35.545U", "12.9U/h")), "19g ⁞ 36U ⁞ 12.9U/h")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("100(1)g", "12.345U", "6.98647U/h")), "100g 12U 6.98647U/h")
Mockito.`when`(sp.getBoolean("complication_unicode", true)).thenReturn(false)
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("0g", "0U", "3.5U/h")), "0g | 0U | 3.5U/h")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("50g", "7.56U", "0%")), "50g | 7.56U | 0%")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("12g", "3.23U", "120%")), "12g | 3.23U | 120%")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("7g", "0.54U", "30%")), "7g | 0.54U | 30%")
Assert.assertEquals(displayFormat.longDetailsLine(rawDataMocker.rawCobIobBr("19(38)g", "35.545U", "12.9U/h")), "19g | 36U | 12.9U/h")
}
@Test fun detailedIobTest() {
Assert.assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("-1.29U", "(0,910|-2,20)")), Pair.create("-1.29U", ",91 -2"))
Assert.assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("3.50U", "")), Pair.create("3.50U", ""))
Assert.assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("12.5U", "(+1,4|-4.78)")), Pair.create("12.5U", "1,4 -5"))
Assert.assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("0.67U", "some junks")), Pair.create(".67U", ""))
Assert.assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("-11.0U", "(broken|data)")), Pair.create("-11U", "-- --"))
Assert.assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("5.52U", "(0,5439|wrong)")), Pair.create("5.52U", ",54 --"))
Assert.assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("-8.1U", "(|-8,1)")), Pair.create("-8.1U", "-- -8"))
Assert.assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("-8.1U", "(|-8,1)")), Pair.create("-8.1U", "-- -8"))
Assert.assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("7.6U", "(malformed)")), Pair.create("7.6U", ""))
Assert.assertEquals(displayFormat.detailedIob(rawDataMocker.rawIob("-4.26U", "(6,97|1,3422|too much)")), Pair.create("-4.26U", "7 1,3"))
}
@Test fun detailedCobTest() {
Assert.assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("0g")), Pair.create("0g", ""))
Assert.assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("50g")), Pair.create("50g", ""))
Assert.assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("2(40)g")), Pair.create("2g", "40g"))
Assert.assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("0(24)g")), Pair.create("0g", "24g"))
Assert.assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("13(5)g")), Pair.create("13g", "5g"))
Assert.assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("11(50)g")), Pair.create("11g", "50g"))
Assert.assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("19(38)g")), Pair.create("19g", "38g"))
Assert.assertEquals(displayFormat.detailedCob(rawDataMocker.rawCob("100(1)g")), Pair.create("100g", "1g"))
}
}

View file

@ -1,64 +0,0 @@
package info.nightscout.androidaps.interaction.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings({"rawtypes", "SpellCheckingInspection"})
public class PairTest {
@Test
public void pairEqualsTest() {
// GIVEN
Pair left = Pair.create("aa", "bbb");
Pair right = Pair.create("ccc", "dd");
Pair another = Pair.create("aa", "bbb");
Pair samePos1 = Pair.create("aa", "d");
Pair samePos2 = Pair.create("zzzzz", "bbb");
Pair no1 = Pair.create(12, 345L);
Pair no2 = Pair.create(-943, 42);
Pair no3 = Pair.create(12L, 345);
Pair no4 = Pair.create(12, 345L);
// THEN
assertNotEquals(left, right);
assertEquals(left, another);
assertNotEquals(left, samePos1);
assertNotEquals(left, samePos2);
assertNotEquals(no1, no2);
assertNotEquals(no1, no3);
assertEquals(no1, no4);
assertNotEquals("aa bbb", left.toString());
}
@Test
public void pairHashTest() {
// GIVEN
Pair inserted = Pair.create("aa", "bbb");
Set<Pair> set = new HashSet<>();
// THEN
//noinspection ConstantConditions
assertFalse(set.contains(inserted));
set.add(inserted);
assertTrue(set.contains(inserted));
}
@Test
public void toStringTest() {
// GIVEN
Pair pair = Pair.create("the-first", "2nd");
assertTrue(pair.toString().contains("the-first"));
assertTrue(pair.toString().contains("2nd"));
}
}

View file

@ -0,0 +1,48 @@
package info.nightscout.androidaps.interaction.utils
import org.junit.Assert
import org.junit.Test
class PairTest {
@Test fun pairEqualsTest() {
// GIVEN
val left: Pair<*, *> = Pair.create("aa", "bbb")
val right: Pair<*, *> = Pair.create("ccc", "dd")
val another: Pair<*, *> = Pair.create("aa", "bbb")
val samePos1: Pair<*, *> = Pair.create("aa", "d")
val samePos2: Pair<*, *> = Pair.create("zzzzz", "bbb")
val no1: Pair<*, *> = Pair.create(12, 345L)
val no2: Pair<*, *> = Pair.create(-943, 42)
val no3: Pair<*, *> = Pair.create(12L, 345)
val no4: Pair<*, *> = Pair.create(12, 345L)
// THEN
Assert.assertNotEquals(left, right)
Assert.assertEquals(left, another)
Assert.assertNotEquals(left, samePos1)
Assert.assertNotEquals(left, samePos2)
Assert.assertNotEquals(no1, no2)
Assert.assertNotEquals(no1, no3)
Assert.assertEquals(no1, no4)
Assert.assertNotEquals("aa bbb", left.toString())
}
@Test fun pairHashTest() {
// GIVEN
val inserted: Pair<*, *> = Pair.create("aa", "bbb")
val set: MutableSet<Pair<*, *>> = HashSet()
// THEN
Assert.assertFalse(set.contains(inserted))
set.add(inserted)
Assert.assertTrue(set.contains(inserted))
}
@Test fun toStringTest() {
// GIVEN
val pair: Pair<*, *> = Pair.create("the-first", "2nd")
Assert.assertTrue(pair.toString().contains("the-first"))
Assert.assertTrue(pair.toString().contains("2nd"))
}
}

View file

@ -1,141 +0,0 @@
package info.nightscout.androidaps.testing.mockers;
import info.nightscout.androidaps.data.RawDisplayData;
import info.nightscout.androidaps.interaction.utils.WearUtil;
import info.nightscout.shared.SafeParse;
import info.nightscout.shared.weardata.EventData;
@SuppressWarnings("PointlessArithmeticExpression")
public class RawDataMocker {
private final WearUtilMocker wearUtilMocker;
public RawDataMocker(WearUtil wearUtil) {
wearUtilMocker = new WearUtilMocker(wearUtil);
}
public RawDisplayData rawSgv(String sgv, int m, String deltaString) {
RawDisplayData raw = new RawDisplayData();
double delta = SafeParse.stringToDouble(deltaString);
String d;
if (delta <= (-3.5 * 5)) {
d = "\u21ca";
} else if (delta <= (-2 * 5)) {
d = "\u2193";
} else if (delta <= (-1 * 5)) {
d = "\u2198";
} else if (delta <= (1 * 5)) {
d = "\u2192";
} else if (delta <= (2 * 5)) {
d = "\u2197";
} else if (delta <= (3.5 * 5)) {
d = "\u2191";
} else {
d = "\u21c8";
}
raw.setSingleBg(
new EventData.SingleBg(
wearUtilMocker.backInTime(0, 0, m, 0),
sgv,
"",
d,
deltaString,
"",
0,
0.0,
0.0,
0.0,
0
)
);
return raw;
}
public RawDisplayData rawDelta(int m, String delta) {
RawDisplayData raw = new RawDisplayData();
raw.setSingleBg(
new EventData.SingleBg(
wearUtilMocker.backInTime(0, 0, m, 0),
"",
"",
"",
delta,
"",
0,
0.0,
0.0,
0.0,
0
)
);
return raw;
}
public RawDisplayData rawCobIobBr(String cob, String iob, String br) {
RawDisplayData raw = new RawDisplayData();
raw.setStatus(
new EventData.Status(
"",
iob,
"",
true,
cob,
br,
"",
"",
0L,
"",
true,
0
)
);
return raw;
}
public RawDisplayData rawIob(String iob, String iob2) {
RawDisplayData raw = new RawDisplayData();
raw.setStatus(
new EventData.Status(
"",
iob,
iob2,
true,
"",
"",
"",
"",
0L,
"",
true,
0
)
);
return raw;
}
public RawDisplayData rawCob(String cob) {
RawDisplayData raw = new RawDisplayData();
raw.setStatus(
new EventData.Status(
"",
"",
"",
true,
cob,
"",
"",
"",
0L,
"",
true,
0
)
);
return raw;
}
}

View file

@ -0,0 +1,118 @@
package info.nightscout.androidaps.testing.mockers
import info.nightscout.androidaps.data.RawDisplayData
import info.nightscout.androidaps.interaction.utils.WearUtil
import info.nightscout.shared.SafeParse.stringToDouble
import info.nightscout.shared.weardata.EventData
import info.nightscout.shared.weardata.EventData.SingleBg
class RawDataMocker(wearUtil: WearUtil) {
private val wearUtilMocker: WearUtilMocker = WearUtilMocker(wearUtil)
fun rawSgv(sgv: String?, m: Int, deltaString: String): RawDisplayData {
val raw = RawDisplayData()
val delta = stringToDouble(deltaString)
val d: String =
when {
delta <= -3.5 * 5 -> "\u21ca"
delta <= -2 * 5 -> "\u2193"
delta <= -1 * 5 -> "\u2198"
delta <= 1 * 5 -> "\u2192"
delta <= 2 * 5 -> "\u2197"
delta <= 3.5 * 5 -> "\u2191"
else -> "\u21c8"
}
raw.singleBg = SingleBg(
wearUtilMocker.backInTime(0, 0, m, 0),
sgv!!,
"",
d,
deltaString,
"",
0,
0.0,
0.0,
0.0,
0
)
return raw
}
fun rawDelta(m: Int, delta: String): RawDisplayData {
val raw = RawDisplayData()
raw.singleBg = SingleBg(
wearUtilMocker.backInTime(0, 0, m, 0),
"",
"",
"",
delta,
"",
0,
0.0,
0.0,
0.0,
0
)
return raw
}
fun rawCobIobBr(cob: String, iob: String, br: String): RawDisplayData {
val raw = RawDisplayData()
raw.status = EventData.Status(
"",
iob,
"",
true,
cob,
br,
"",
"",
0L,
"",
true,
0
)
return raw
}
fun rawIob(iob: String, iob2: String): RawDisplayData {
val raw = RawDisplayData()
raw.status = EventData.Status(
"",
iob,
iob2,
true,
"",
"",
"",
"",
0L,
"",
true,
0
)
return raw
}
fun rawCob(cob: String?): RawDisplayData {
val raw = RawDisplayData()
raw.status = EventData.Status(
"",
"",
"",
true,
cob!!,
"",
"",
"",
0L,
"",
true,
0
)
return raw
}
}

View file

@ -1,102 +0,0 @@
package info.nightscout.androidaps.testing.mockers;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import android.os.Bundle;
import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.DataMap;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import java.util.ArrayList;
import info.nightscout.androidaps.interaction.utils.Constants;
import info.nightscout.androidaps.interaction.utils.WearUtil;
@SuppressWarnings({"rawtypes", "unchecked"})
public class WearUtilMocker {
private final WearUtil wearUtil;
public WearUtilMocker(WearUtil wearUtil) {
this.wearUtil = wearUtil;
}
public static final long REF_NOW = 1572610530000L;
private long clockMsDiff = 0L;
public void prepareMock() {
resetClock();
// because we cleverly used timestamp() by implementation, we can mock it
// and control the time in tests
when(wearUtil.timestamp()).thenReturn(REF_NOW + clockMsDiff);
}
public void prepareMockNoReal() {
resetClock();
Mockito.doAnswer(invocation -> REF_NOW + clockMsDiff).when(wearUtil).timestamp();
Mockito.doReturn(null).when(wearUtil).getWakeLock(anyString(), anyInt());
Mockito.doAnswer(bundleToDataMapMock).when(wearUtil).bundleToDataMap(any());
}
public void resetClock() {
clockMsDiff = 0L;
}
public void progressClock(long byMilliseconds) {
clockMsDiff = clockMsDiff + byMilliseconds;
}
@SuppressWarnings("unused")
public void setClock(long atMillisecondsSinceEpoch) {
clockMsDiff = atMillisecondsSinceEpoch - REF_NOW;
}
public long backInTime(int d, int h, int m, int s) {
return REF_NOW - (Constants.DAY_IN_MS * d + Constants.HOUR_IN_MS * h + Constants.MINUTE_IN_MS * m + Constants.SECOND_IN_MS * s);
}
private final Answer bundleToDataMapMock = invocation -> {
DataMap map = new DataMap();
Bundle bundle = invocation.getArgument(0);
for (String key : bundle.keySet()) {
Object v = bundle.get(key);
if (v instanceof Asset) map.putAsset(key, (Asset) v);
if (v instanceof Boolean) map.putBoolean(key, (Boolean) v);
if (v instanceof Byte) map.putByte(key, (Byte) v);
if (v instanceof byte[]) map.putByteArray(key, (byte[]) v);
if (v instanceof DataMap) map.putDataMap(key, (DataMap) v);
if (v instanceof Double) map.putDouble(key, (Double) v);
if (v instanceof Float) map.putFloat(key, (Float) v);
if (v instanceof float[]) map.putFloatArray(key, (float[]) v);
if (v instanceof Integer) map.putInt(key, (Integer) v);
if (v instanceof Long) map.putLong(key, (Long) v);
if (v instanceof long[]) map.putLongArray(key, (long[]) v);
if (v instanceof String) map.putString(key, (String) v);
if (v instanceof String[]) map.putStringArray(key, (String[]) v);
if (v instanceof ArrayList) {
if (!((ArrayList) v).isEmpty()) {
if (((ArrayList) v).get(0) instanceof Integer) {
map.putIntegerArrayList(key, (ArrayList<Integer>) v);
}
if (((ArrayList) v).get(0) instanceof String) {
map.putStringArrayList(key, (ArrayList<String>) v);
}
if (((ArrayList) v).get(0) instanceof DataMap) {
map.putDataMapArrayList(key, (ArrayList<DataMap>) v);
}
}
}
}
return map;
};
}

View file

@ -0,0 +1,87 @@
package info.nightscout.androidaps.testing.mockers
import android.os.Bundle
import com.google.android.gms.wearable.Asset
import com.google.android.gms.wearable.DataMap
import info.nightscout.androidaps.interaction.utils.Constants
import info.nightscout.androidaps.interaction.utils.WearUtil
import org.mockito.ArgumentMatchers
import org.mockito.Mockito
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer
class WearUtilMocker(private val wearUtil: WearUtil) {
private var clockMsDiff = 0L
fun prepareMock() {
resetClock()
// because we cleverly used timestamp() by implementation, we can mock it
// and control the time in tests
Mockito.`when`(wearUtil.timestamp()).thenReturn(REF_NOW + clockMsDiff)
}
fun prepareMockNoReal() {
resetClock()
Mockito.doAnswer { invocation: InvocationOnMock? -> REF_NOW + clockMsDiff }.`when`(wearUtil).timestamp()
Mockito.doReturn(null).`when`(wearUtil).getWakeLock(ArgumentMatchers.anyString(), ArgumentMatchers.anyInt())
Mockito.doAnswer(bundleToDataMapMock).`when`(wearUtil).bundleToDataMap(ArgumentMatchers.any())
}
fun resetClock() {
clockMsDiff = 0L
}
fun progressClock(byMilliseconds: Long) {
clockMsDiff += byMilliseconds
}
fun setClock(atMillisecondsSinceEpoch: Long) {
clockMsDiff = atMillisecondsSinceEpoch - REF_NOW
}
fun backInTime(d: Int, h: Int, m: Int, s: Int): Long {
return REF_NOW - (Constants.DAY_IN_MS * d + Constants.HOUR_IN_MS * h + Constants.MINUTE_IN_MS * m + Constants.SECOND_IN_MS * s)
}
@Suppress("UNCHECKED_CAST")
private val bundleToDataMapMock: Answer<*> = Answer { invocation: InvocationOnMock ->
val map = DataMap()
val bundle = invocation.getArgument<Bundle>(0)
for (key in bundle.keySet()) {
val v = bundle[key]
if (v is Asset) map.putAsset(key, v)
if (v is Boolean) map.putBoolean(key, v)
if (v is Byte) map.putByte(key, (v as Byte?)!!)
if (v is ByteArray) map.putByteArray(key, v)
if (v is DataMap) map.putDataMap(key, v)
if (v is Double) map.putDouble(key, v)
if (v is Float) map.putFloat(key, (v as Float))
if (v is FloatArray) map.putFloatArray(key, v)
if (v is Int) map.putInt(key, v)
if (v is Long) map.putLong(key, v)
if (v is LongArray) map.putLongArray(key, v)
if (v is String) map.putString(key, v)
if (v is Array<*>) map.putStringArray(key, v as Array<String>)
if (v is ArrayList<*>) {
if (!v.isEmpty()) {
if (v[0] is Int) {
map.putIntegerArrayList(key, v as ArrayList<Int>)
}
if (v[0] is String) {
map.putStringArrayList(key, v as ArrayList<String>)
}
if (v[0] is DataMap) {
map.putDataMapArrayList(key, v as ArrayList<DataMap>)
}
}
}
}
map
}
companion object {
const val REF_NOW = 1572610530000L
}
}

View file

@ -1,40 +0,0 @@
package info.nightscout.androidaps.testing.mocks;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
import android.content.Intent;
import android.os.Bundle;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import java.util.HashMap;
@SuppressWarnings({"SuspiciousMethodCalls", "rawtypes"})
public final class IntentMock {
public static Intent mock() {
return mock(new HashMap<>());
}
public static Intent mock(final HashMap<String, Object> map) {
Answer put = invocation -> {
map.put((String) invocation.getArguments()[0], invocation.getArguments()[1]);
return null;
};
Answer<Object> get = invocation -> map.get(invocation.getArguments()[0]);
Intent intent = Mockito.mock(Intent.class);
when(intent.putExtra(anyString(), any(Bundle.class))).thenAnswer(put);
when(intent.getBundleExtra(anyString())).thenAnswer(get);
doAnswer(invocation -> map.containsKey(invocation.getArguments()[0])).when(intent).hasExtra(anyString());
return intent;
}
}

View file

@ -1,159 +0,0 @@
package info.nightscout.androidaps.testing.mocks;
import android.content.SharedPreferences;
import androidx.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@SuppressWarnings({"ConstantConditions", "unchecked"})
public class SharedPreferencesMock implements SharedPreferences {
private final EditorInternals editor = new EditorInternals();
static class EditorInternals implements Editor {
Map<String, Object> innerMap = new HashMap<>();
@Override
public Editor putString(String k, @Nullable String v) {
innerMap.put(k, v);
return this;
}
@Override
public Editor putStringSet(String k, @Nullable Set<String> set) {
innerMap.put(k, set);
return this;
}
@Override
public Editor putInt(String k, int i) {
innerMap.put(k, i);
return this;
}
@Override
public Editor putLong(String k, long l) {
innerMap.put(k, l);
return this;
}
@Override
public Editor putFloat(String k, float v) {
innerMap.put(k, v);
return this;
}
@Override
public Editor putBoolean(String k, boolean b) {
innerMap.put(k, b);
return this;
}
@Override
public Editor remove(String k) {
innerMap.remove(k);
return this;
}
@Override
public Editor clear() {
innerMap.clear();
return this;
}
@Override
public boolean commit() {
return true;
}
@Override
public void apply() {
}
}
@Override
public Map<String, ?> getAll() {
return editor.innerMap;
}
@Nullable
@Override
public String getString(String k, @Nullable String s) {
if (editor.innerMap.containsKey(k)) {
return (String) editor.innerMap.get(k);
} else {
return s;
}
}
@Nullable
@Override
public Set<String> getStringSet(String k, @Nullable Set<String> set) {
if (editor.innerMap.containsKey(k)) {
return (Set<String>) editor.innerMap.get(k);
} else {
return set;
}
}
@Override
public int getInt(String k, int i) {
if (editor.innerMap.containsKey(k)) {
return (Integer) editor.innerMap.get(k);
} else {
return i;
}
}
@Override
public long getLong(String k, long l) {
if (editor.innerMap.containsKey(k)) {
return (Long) editor.innerMap.get(k);
} else {
return l;
}
}
@Override
public float getFloat(String k, float v) {
if (editor.innerMap.containsKey(k)) {
return (Float) editor.innerMap.get(k);
} else {
return v;
}
}
@Override
public boolean getBoolean(String k, boolean b) {
if (editor.innerMap.containsKey(k)) {
return (Boolean) editor.innerMap.get(k);
} else {
return b;
}
}
@Override
public boolean contains(String k) {
return editor.innerMap.containsKey(k);
}
@Override
public Editor edit() {
return editor;
}
@Override
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
}
@Override
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
}
}

View file

@ -0,0 +1,123 @@
package info.nightscout.androidaps.testing.mocks
import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
class SharedPreferencesMock : SharedPreferences {
private val editor = EditorInternals()
internal class EditorInternals : SharedPreferences.Editor {
var innerMap: MutableMap<String, Any?> = HashMap()
override fun putString(k: String, v: String?): SharedPreferences.Editor {
innerMap[k] = v
return this
}
override fun putStringSet(k: String, set: Set<String>?): SharedPreferences.Editor {
innerMap[k] = set
return this
}
override fun putInt(k: String, i: Int): SharedPreferences.Editor {
innerMap[k] = i
return this
}
override fun putLong(k: String, l: Long): SharedPreferences.Editor {
innerMap[k] = l
return this
}
override fun putFloat(k: String, v: Float): SharedPreferences.Editor {
innerMap[k] = v
return this
}
override fun putBoolean(k: String, b: Boolean): SharedPreferences.Editor {
innerMap[k] = b
return this
}
override fun remove(k: String): SharedPreferences.Editor {
innerMap.remove(k)
return this
}
override fun clear(): SharedPreferences.Editor {
innerMap.clear()
return this
}
override fun commit(): Boolean {
return true
}
override fun apply() {}
}
override fun getAll(): Map<String, *> {
return editor.innerMap
}
override fun getString(k: String, s: String?): String? {
return if (editor.innerMap.containsKey(k)) {
editor.innerMap[k] as String?
} else {
s
}
}
@Suppress("UNCHECKED_CAST")
override fun getStringSet(k: String, set: Set<String>?): Set<String>? {
return if (editor.innerMap.containsKey(k)) {
editor.innerMap[k] as Set<String>?
} else {
set
}
}
override fun getInt(k: String, i: Int): Int {
return if (editor.innerMap.containsKey(k)) {
editor.innerMap[k] as Int
} else {
i
}
}
override fun getLong(k: String, l: Long): Long {
return if (editor.innerMap.containsKey(k)) {
editor.innerMap[k] as Long
} else {
l
}
}
override fun getFloat(k: String, v: Float): Float {
return if (editor.innerMap.containsKey(k)) {
editor.innerMap[k] as Float
} else {
v
}
}
override fun getBoolean(k: String, b: Boolean): Boolean {
return if (editor.innerMap.containsKey(k)) {
editor.innerMap[k] as Boolean
} else {
b
}
}
override fun contains(k: String): Boolean {
return editor.innerMap.containsKey(k)
}
override fun edit(): SharedPreferences.Editor {
return editor
}
override fun registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener: OnSharedPreferenceChangeListener) {}
override fun unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener: OnSharedPreferenceChangeListener) {}
}