From 156d71da0a0f2db9e17d5a104750abfbe253fa2d Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 19 Mar 2020 21:43:41 +0100 Subject: [PATCH] NSClientReceiverDelegateTest --- .../NsClientReceiverDelegateTest.java | 126 ------------------ .../nsclient/NsClientReceiverDelegateTest.kt | 126 ++++++++++++++++++ 2 files changed, 126 insertions(+), 126 deletions(-) delete mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.java deleted file mode 100644 index b0fde44b08..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.java +++ /dev/null @@ -1,126 +0,0 @@ -package info.nightscout.androidaps.plugins.general.nsclient; - -import android.content.Context; - -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 info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.events.EventChargingState; -import info.nightscout.androidaps.events.EventNetworkChange; -import info.nightscout.androidaps.utils.SP; - -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, SP.class, Context.class}) -public class NsClientReceiverDelegateTest { - - private NsClientReceiverDelegate sut; - - @Before - public void prepare() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockApplicationContext(); - - sut = new NsClientReceiverDelegate(); - } - - @Test - public void testCalculateStatusChargingState() { - PowerMockito.mockStatic(SP.class); - when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(false); - EventChargingState ev = new EventChargingState(true); - assertTrue(sut.calculateStatus(ev)); - ev = new EventChargingState(false); - assertTrue(sut.calculateStatus(ev)); - - when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(true); - ev = new EventChargingState(true); - assertTrue(sut.calculateStatus(ev)); - ev = new EventChargingState(false); - assertTrue(!sut.calculateStatus(ev)); - } - - @Test - public void testCalculateStatusNetworkState() { - PowerMockito.mockStatic(SP.class); - // wifiOnly = false - // allowRoaming = false as well - when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(false); - when(SP.getString(anyInt(), anyString())).thenReturn(""); - EventNetworkChange ev = new EventNetworkChange(); - ev.setSsid(""); - - ev.setMobileConnected(true); - ev.setWifiConnected(true); - assertTrue(sut.calculateStatus(ev)); - - ev.setSsid("test"); - when(SP.getString(anyInt(), anyString())).thenReturn("\"test\""); - assertTrue(sut.calculateStatus(ev)); - - ev.setSsid("\"test\""); - assertTrue(sut.calculateStatus(ev)); - - ev.setWifiConnected(false); - assertTrue(sut.calculateStatus(ev)); - - // wifiOnly = true - // allowRoaming = true as well - when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(true); - ev.setWifiConnected(true); - assertTrue(sut.calculateStatus(ev)); - ev.setWifiConnected(false); - assertTrue(!sut.calculateStatus(ev)); - - // wifiOnly = false - // allowRoaming = false as well - when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(false); - ev.setWifiConnected(false); - ev.setRoaming(true); - assertTrue(!sut.calculateStatus(ev)); - - // wifiOnly = false - // allowRoaming = true - when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(false); - when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true); - ev.setWifiConnected(false); - ev.setRoaming(true); - assertTrue(sut.calculateStatus(ev)); - - // wifiOnly = true - // allowRoaming = true - when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(true); - when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true); - ev.setWifiConnected(false); - ev.setRoaming(true); - assertTrue(!sut.calculateStatus(ev)); - - // wifiOnly = true - // allowRoaming = true - when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(true); - when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true); - ev.setWifiConnected(true); - ev.setRoaming(true); - assertTrue(sut.calculateStatus(ev)); - - // wifiOnly = false - // allowRoaming = false - when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(false); - when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(false); - ev.setWifiConnected(true); - ev.setRoaming(true); - assertTrue(sut.calculateStatus(ev)); - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt new file mode 100644 index 0000000000..de55bcaaec --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt @@ -0,0 +1,126 @@ +package info.nightscout.androidaps.plugins.general.nsclient + +import android.content.Context +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.TestBase +import info.nightscout.androidaps.events.EventChargingState +import info.nightscout.androidaps.events.EventNetworkChange +import info.nightscout.androidaps.utils.SP +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentMatchers +import org.mockito.ArgumentMatchers.anyBoolean +import org.mockito.ArgumentMatchers.anyInt +import org.mockito.ArgumentMatchers.anyLong +import org.mockito.ArgumentMatchers.anyString +import org.mockito.Mockito +import org.mockito.Mockito.`when` +import org.mockito.Mockito.mock +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(MainApp::class, SP::class, Context::class) +class NsClientReceiverDelegateTest : TestBase() { + + private var sut: NsClientReceiverDelegate? = null + + @Before fun prepare() { + System.setProperty("disableFirebase", "true") + PowerMockito.mockStatic(MainApp::class.java) + val mainApp: MainApp = mock(MainApp::class.java) + `when`(MainApp.instance()).thenReturn(mainApp) + PowerMockito.mockStatic(SP::class.java) + `when`(SP.getLong(anyInt(), anyLong())).thenReturn(0L) + `when`(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(false) + `when`(SP.getInt(anyInt(), anyInt())).thenReturn(0) + `when`(SP.getString(anyInt(), anyString())).thenReturn("") + + sut = NsClientReceiverDelegate() + } + + @Test fun testCalculateStatusChargingState() { + PowerMockito.mockStatic(SP::class.java) + Mockito.`when`(SP.getBoolean(ArgumentMatchers.anyInt(), ArgumentMatchers.anyBoolean())).thenReturn(false) + var ev = EventChargingState(true) + Assert.assertTrue(sut!!.calculateStatus(ev)) + ev = EventChargingState(false) + Assert.assertTrue(sut!!.calculateStatus(ev)) + Mockito.`when`(SP.getBoolean(ArgumentMatchers.anyInt(), ArgumentMatchers.anyBoolean())).thenReturn(true) + ev = EventChargingState(true) + Assert.assertTrue(sut!!.calculateStatus(ev)) + ev = EventChargingState(false) + Assert.assertTrue(!sut!!.calculateStatus(ev)) + } + + @Test fun testCalculateStatusNetworkState() { + PowerMockito.mockStatic(SP::class.java) + // wifiOnly = false + // allowRoaming = false as well + Mockito.`when`(SP.getBoolean(ArgumentMatchers.anyInt(), ArgumentMatchers.anyBoolean())).thenReturn(false) + Mockito.`when`(SP.getString(ArgumentMatchers.anyInt(), ArgumentMatchers.anyString())).thenReturn("") + val ev = EventNetworkChange() + ev.ssid = "" + ev.mobileConnected = true + ev.wifiConnected = true + Assert.assertTrue(sut!!.calculateStatus(ev)) + ev.ssid = "test" + Mockito.`when`(SP.getString(ArgumentMatchers.anyInt(), ArgumentMatchers.anyString())).thenReturn("\"test\"") + Assert.assertTrue(sut!!.calculateStatus(ev)) + ev.ssid = "\"test\"" + Assert.assertTrue(sut!!.calculateStatus(ev)) + ev.wifiConnected = false + Assert.assertTrue(sut!!.calculateStatus(ev)) + + // wifiOnly = true + // allowRoaming = true as well + Mockito.`when`(SP.getBoolean(ArgumentMatchers.anyInt(), ArgumentMatchers.anyBoolean())).thenReturn(true) + ev.wifiConnected = true + Assert.assertTrue(sut!!.calculateStatus(ev)) + ev.wifiConnected = false + Assert.assertTrue(!sut!!.calculateStatus(ev)) + + // wifiOnly = false + // allowRoaming = false as well + Mockito.`when`(SP.getBoolean(ArgumentMatchers.anyInt(), ArgumentMatchers.anyBoolean())).thenReturn(false) + ev.wifiConnected = false + ev.roaming = true + Assert.assertTrue(!sut!!.calculateStatus(ev)) + + // wifiOnly = false + // allowRoaming = true + Mockito.`when`(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(false) + Mockito.`when`(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true) + ev.wifiConnected = false + ev.roaming = true + Assert.assertTrue(sut!!.calculateStatus(ev)) + + // wifiOnly = true + // allowRoaming = true + Mockito.`when`(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(true) + Mockito.`when`(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true) + ev.wifiConnected = false + ev.roaming = true + Assert.assertTrue(!sut!!.calculateStatus(ev)) + + // wifiOnly = true + // allowRoaming = true + Mockito.`when`(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(true) + Mockito.`when`(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true) + ev.wifiConnected = true + ev.roaming = true + Assert.assertTrue(sut!!.calculateStatus(ev)) + + // wifiOnly = false + // allowRoaming = false + Mockito.`when`(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(false) + Mockito.`when`(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(false) + ev.wifiConnected = true + ev.roaming = true + Assert.assertTrue(sut!!.calculateStatus(ev)) + } +} \ No newline at end of file