testing and SP logic

This commit is contained in:
AdrianLxM 2019-04-15 08:04:56 +02:00
parent 04618156cc
commit f739980988
3 changed files with 86 additions and 27 deletions

View file

@ -8,6 +8,7 @@ import info.nightscout.androidaps.R
import info.nightscout.androidaps.logging.L import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification import info.nightscout.androidaps.plugins.general.overview.notifications.Notification
import info.nightscout.androidaps.utils.SP
import org.apache.http.HttpResponse import org.apache.http.HttpResponse
import org.apache.http.client.methods.HttpGet import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient import org.apache.http.impl.client.DefaultHttpClient
@ -50,22 +51,34 @@ fun checkVersion() = if (isConnected()) {
log.debug("Github master version no checked. No connectivity") log.debug("Github master version no checked. No connectivity")
fun compareWithCurrentVersion(newVersion: String?, currentVersion: String) { fun compareWithCurrentVersion(newVersion: String?, currentVersion: String) {
val comparison = newVersion?.versionStrip()?.compareTo(currentVersion.versionStrip()) ?: 0 val comparison: Int? = newVersion?.versionStrip()?.compareTo(currentVersion.versionStrip())
when { when {
comparison == 0 -> log.debug("Version equal to master of fetch failed") comparison == null -> onVersionNotDetectable()
comparison > 0 -> { comparison == 0 -> onSameVersionDetected()
log.debug("Version ${currentVersion} outdated. Found $newVersion") comparison > 0 -> onNewVersionDetected(currentVersion = currentVersion, newVersion = newVersion)
val notification = Notification(Notification.NEWVERSIONDETECTED, String.format(MainApp.gs(R.string.versionavailable), newVersion.toString()), Notification.LOW)
MainApp.bus().post(EventNewNotification(notification))
}
else -> log.debug("Version newer than master. Are you developer?") else -> log.debug("Version newer than master. Are you developer?")
} }
} }
fun String.versionStrip() = this.mapNotNull { fun onSameVersionDetected() {
when (it) { SP.remove(R.string.key_new_version_available_since)
in '0'..'9' -> it }
'.' -> it
else -> null fun onVersionNotDetectable() {
} log.debug("fetch failed, ignore and smartcast to non-null")
}.joinToString (separator = "") }
fun onNewVersionDetected(currentVersion: String, newVersion: String?) {
log.debug("Version ${currentVersion} outdated. Found $newVersion")
val notification = Notification(Notification.NEWVERSIONDETECTED, String.format(MainApp.gs(R.string.versionavailable), newVersion.toString()), Notification.LOW)
MainApp.bus().post(EventNewNotification(notification))
SP.putLong(R.string.key_new_version_available_since, System.currentTimeMillis())
}
fun String.versionStrip() = this.mapNotNull {
when (it) {
in '0'..'9' -> it
'.' -> it
else -> null
}
}.joinToString(separator = "")

View file

@ -1327,6 +1327,7 @@
<string name="notconfigured">Not configured</string> <string name="notconfigured">Not configured</string>
<string name="profileswitchcreated">Profile switch created</string> <string name="profileswitchcreated">Profile switch created</string>
<string name="versionChecker">Version Checker</string> <string name="versionChecker">Version Checker</string>
<string name="key_new_version_available_since" translatable="false">new_version_available_since</string>
<plurals name="objective_days"> <plurals name="objective_days">
<item quantity="one">%1$d day</item> <item quantity="one">%1$d day</item>
<item quantity="other">%1$d days</item> <item quantity="other">%1$d days</item>

View file

@ -2,7 +2,9 @@ package info.nightscout.androidaps.plugins.general.versionChecker
import com.squareup.otto.Bus import com.squareup.otto.Bus
import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.logging.L import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.utils.SP
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
@ -58,33 +60,33 @@ class VersionCheckerUtilsKtTest {
} }
@Test @Test
@PrepareForTest(MainApp::class, L::class) @PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update1`() { fun `should find update1`() {
val bus = prepareCompareTests() val bus = prepareBus()
compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.2.1") compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.2.1")
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
} }
@Test @Test
@PrepareForTest(MainApp::class, L::class) @PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update2`() { fun `should find update2`() {
val bus = prepareCompareTests() val bus = prepareBus()
compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.2.1-dev") compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.2.1-dev")
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
} }
@Test @Test
@PrepareForTest(MainApp::class, L::class) @PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update3`() { fun `should find update3`() {
val bus = prepareCompareTests() val bus = prepareBus()
compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.1") compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.1")
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
} }
@Test @Test
@PrepareForTest(MainApp::class, L::class) @PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update4`() { fun `should find update4`() {
val bus = prepareCompareTests() val bus = prepareBus()
compareWithCurrentVersion(newVersion = "2.2", currentVersion = "2.1.1") compareWithCurrentVersion(newVersion = "2.2", currentVersion = "2.1.1")
@ -92,29 +94,72 @@ class VersionCheckerUtilsKtTest {
} }
@Test @Test
@PrepareForTest(MainApp::class, L::class) @PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update5`() { fun `should find update5`() {
val bus = prepareCompareTests() val bus = prepareBus()
compareWithCurrentVersion(newVersion = "2.2.1", currentVersion = "2.2-dev") compareWithCurrentVersion(newVersion = "2.2.1", currentVersion = "2.2-dev")
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
} }
@Test @Test
@PrepareForTest(MainApp::class, L::class) @PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update6`() { fun `should find update6`() {
val bus = prepareCompareTests() val bus = prepareBus()
val sp = prepareSP()
compareWithCurrentVersion(newVersion = "2.2.1", currentVersion = "2.2dev") compareWithCurrentVersion(newVersion = "2.2.1", currentVersion = "2.2dev")
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
} }
@Test
@PrepareForTest(MainApp::class, L::class, SP::class)
fun `find same version`() {
val buildGradle = """blabla
| android {
| aosenuthoae
| }
| version = "2.2.2"
| appName = "Aaoeu"
""".trimMargin()
val bus = prepareBus()
compareWithCurrentVersion(buildGradle.byteInputStream().findVersion(), currentVersion = "2.2.2")
verify(bus, times(0)).post(any())
verify(SP, times(1)).remove(R.string.key_new_version_available_since)
}
private fun prepareCompareTests(): Bus { @Test
@PrepareForTest(MainApp::class, L::class, SP::class)
fun `find higher version`() {
val buildGradle = """blabla
| android {
| aosenuthoae
| }
| version = "3.0"
| appName = "Aaoeu"
""".trimMargin()
val bus = prepareBus()
compareWithCurrentVersion(buildGradle.byteInputStream().findVersion(), currentVersion = "2.2.2")
verify(bus, times(1)).post(any())
}
private fun prepareBus(): Bus {
PowerMockito.mockStatic(MainApp::class.java) PowerMockito.mockStatic(MainApp::class.java)
val mainApp = mock<MainApp>(MainApp::class.java) val mainApp = mock<MainApp>(MainApp::class.java)
`when`(MainApp.instance()).thenReturn(mainApp) `when`(MainApp.instance()).thenReturn(mainApp)
val bus = mock(Bus::class.java) val bus = mock(Bus::class.java)
`when`(MainApp.bus()).thenReturn(bus) `when`(MainApp.bus()).thenReturn(bus)
`when`(MainApp.gs(ArgumentMatchers.anyInt())).thenReturn("some dummy string") `when`(MainApp.gs(ArgumentMatchers.anyInt())).thenReturn("some dummy string")
prepareSP()
return bus return bus
} }
private fun prepareSP() {
PowerMockito.mockStatic(SP::class.java)
}
private fun prepareLogging() {
PowerMockito.mockStatic(L::class.java)
`when`(L.isEnabled(any())).thenReturn(true)
}
} }