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.plugins.general.overview.events.EventNewNotification
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification
import info.nightscout.androidaps.utils.SP
import org.apache.http.HttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
@ -50,22 +51,34 @@ fun checkVersion() = if (isConnected()) {
log.debug("Github master version no checked. No connectivity")
fun compareWithCurrentVersion(newVersion: String?, currentVersion: String) {
val comparison = newVersion?.versionStrip()?.compareTo(currentVersion.versionStrip()) ?: 0
val comparison: Int? = newVersion?.versionStrip()?.compareTo(currentVersion.versionStrip())
when {
comparison == 0 -> log.debug("Version equal to master of fetch failed")
comparison > 0 -> {
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))
}
comparison == null -> onVersionNotDetectable()
comparison == 0 -> onSameVersionDetected()
comparison > 0 -> onNewVersionDetected(currentVersion = currentVersion, newVersion = newVersion)
else -> log.debug("Version newer than master. Are you developer?")
}
}
fun String.versionStrip() = this.mapNotNull {
when (it) {
in '0'..'9' -> it
'.' -> it
else -> null
}
}.joinToString (separator = "")
fun onSameVersionDetected() {
SP.remove(R.string.key_new_version_available_since)
}
fun onVersionNotDetectable() {
log.debug("fetch failed, ignore and smartcast to non-null")
}
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="profileswitchcreated">Profile switch created</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">
<item quantity="one">%1$d day</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 info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.utils.SP
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@ -58,33 +60,33 @@ class VersionCheckerUtilsKtTest {
}
@Test
@PrepareForTest(MainApp::class, L::class)
@PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update1`() {
val bus = prepareCompareTests()
val bus = prepareBus()
compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.2.1")
verify(bus, times(1)).post(any())
}
@Test
@PrepareForTest(MainApp::class, L::class)
@PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update2`() {
val bus = prepareCompareTests()
val bus = prepareBus()
compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.2.1-dev")
verify(bus, times(1)).post(any())
}
@Test
@PrepareForTest(MainApp::class, L::class)
@PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update3`() {
val bus = prepareCompareTests()
val bus = prepareBus()
compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.1")
verify(bus, times(1)).post(any())
}
@Test
@PrepareForTest(MainApp::class, L::class)
@PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update4`() {
val bus = prepareCompareTests()
val bus = prepareBus()
compareWithCurrentVersion(newVersion = "2.2", currentVersion = "2.1.1")
@ -92,29 +94,72 @@ class VersionCheckerUtilsKtTest {
}
@Test
@PrepareForTest(MainApp::class, L::class)
@PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update5`() {
val bus = prepareCompareTests()
val bus = prepareBus()
compareWithCurrentVersion(newVersion = "2.2.1", currentVersion = "2.2-dev")
verify(bus, times(1)).post(any())
}
@Test
@PrepareForTest(MainApp::class, L::class)
@PrepareForTest(MainApp::class, L::class, SP::class)
fun `should find update6`() {
val bus = prepareCompareTests()
val bus = prepareBus()
val sp = prepareSP()
compareWithCurrentVersion(newVersion = "2.2.1", currentVersion = "2.2dev")
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)
val mainApp = mock<MainApp>(MainApp::class.java)
`when`(MainApp.instance()).thenReturn(mainApp)
val bus = mock(Bus::class.java)
`when`(MainApp.bus()).thenReturn(bus)
`when`(MainApp.gs(ArgumentMatchers.anyInt())).thenReturn("some dummy string")
prepareSP()
return bus
}
private fun prepareSP() {
PowerMockito.mockStatic(SP::class.java)
}
private fun prepareLogging() {
PowerMockito.mockStatic(L::class.java)
`when`(L.isEnabled(any())).thenReturn(true)
}
}