From f739980988cf54571733cf14291af0e39d359bdd Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Mon, 15 Apr 2019 08:04:56 +0200 Subject: [PATCH] testing and SP logic --- .../versionChecker/VersionCheckerUtils.kt | 41 +++++++---- app/src/main/res/values/strings.xml | 1 + .../VersionCheckerUtilsKtTest.kt | 71 +++++++++++++++---- 3 files changed, 86 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt index c1378451df..b8cfa7fffc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt @@ -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 = "") \ No newline at end of file +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 = "") \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3d7d2a3c60..feaab74c41 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1327,6 +1327,7 @@ Not configured Profile switch created Version Checker + new_version_available_since %1$d day %1$d days diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtilsKtTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtilsKtTest.kt index 503c8d46be..b1b62b589d 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtilsKtTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtilsKtTest.kt @@ -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::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) + } + } \ No newline at end of file