From cedc3b7fd94fc333cf342b3f018c87f75ceae956 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Thu, 24 Nov 2022 20:34:57 +0200 Subject: [PATCH 01/11] Check if WordPress app exists --- .../android/ui/utils/JetpackAppMigrationFlowUtils.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt b/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt index de4b64920c52..c4706fb5b30f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt @@ -14,13 +14,18 @@ class JetpackAppMigrationFlowUtils @Inject constructor( private val contextProvider: ContextProvider, private val appPrefsWrapper: AppPrefsWrapper, private val accountStore: AccountStore, + private val appStatus: AppStatus, + private val wordPressPublicData: WordPressPublicData, ) { fun shouldShowMigrationFlow() = buildConfigWrapper.isJetpackApp && jetpackMigrationFlowFeatureConfig.isEnabled() && appPrefsWrapper.getIsFirstTrySharedLoginJetpack() && !accountStore.hasAccessToken() + && isWordPressInstalled() fun startJetpackMigrationFlow() { ActivityLauncher.startJetpackMigrationFlow(contextProvider.getContext()) } + + private fun isWordPressInstalled() = appStatus.isAppInstalled(wordPressPublicData.currentPackageId()) } From f884a3296f6fe47844d530233e75ba3ebe80d79f Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Thu, 24 Nov 2022 20:36:06 +0200 Subject: [PATCH 02/11] Check if WordPress app is compatible --- .../ui/utils/JetpackAppMigrationFlowUtils.kt | 9 +++++++++ .../util/publicdata/WordPressPublicData.kt | 13 ++++++++++++- .../sharedlogin/WordPressPublicDataTest.kt | 18 +++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt b/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt index c4706fb5b30f..df0a373acf99 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt @@ -5,6 +5,9 @@ import org.wordpress.android.ui.ActivityLauncher import org.wordpress.android.ui.prefs.AppPrefsWrapper import org.wordpress.android.util.BuildConfigWrapper import org.wordpress.android.util.config.JetpackMigrationFlowFeatureConfig +import org.wordpress.android.util.helpers.Version +import org.wordpress.android.util.publicdata.AppStatus +import org.wordpress.android.util.publicdata.WordPressPublicData import org.wordpress.android.viewmodel.ContextProvider import javax.inject.Inject @@ -17,15 +20,21 @@ class JetpackAppMigrationFlowUtils @Inject constructor( private val appStatus: AppStatus, private val wordPressPublicData: WordPressPublicData, ) { + private val minimumSupportedVersion = "21.3" + fun shouldShowMigrationFlow() = buildConfigWrapper.isJetpackApp && jetpackMigrationFlowFeatureConfig.isEnabled() && appPrefsWrapper.getIsFirstTrySharedLoginJetpack() && !accountStore.hasAccessToken() && isWordPressInstalled() + && isWordPressCompatible() fun startJetpackMigrationFlow() { ActivityLauncher.startJetpackMigrationFlow(contextProvider.getContext()) } private fun isWordPressInstalled() = appStatus.isAppInstalled(wordPressPublicData.currentPackageId()) + + private fun isWordPressCompatible() = + Version(wordPressPublicData.nonSemanticPackageVersion()) >= Version(minimumSupportedVersion) } diff --git a/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt b/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt index 0e54a2711702..8c00ea9bb3f5 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt @@ -6,7 +6,7 @@ import org.wordpress.android.util.publicdata.WordPressPublicData.PackageName.Van import org.wordpress.android.util.publicdata.WordPressPublicData.PackageName.Wasabi import javax.inject.Inject -class WordPressPublicData @Inject constructor() { +class WordPressPublicData @Inject constructor(private val packageManagerWrapper: PackageManagerWrapper) { private sealed class PackageName(val type: String, val value: String) { object Jalapeno : PackageName("jalapeno", "org.wordpress.android.prealpha") @@ -21,4 +21,15 @@ class WordPressPublicData @Inject constructor() { Wasabi.type -> Wasabi.value else -> throw IllegalArgumentException("Failed to get Jetpack package ID: build flavor not found.") } + + fun currentPackageVersion() = packageManagerWrapper.getPackageInfo(currentPackageId())?.versionName + + fun nonSemanticPackageVersion(): String? { + val rawVersion = currentPackageVersion() + + // Clean app semantic versioning info. E.g 21.2-rc-3 turns to 21.2 + val wordPressVersion = rawVersion?.split("-")?.getOrNull(0) ?: rawVersion ?: "" + + return if(Regex("^\\d+(\\.\\d+)*\$").matchEntire(wordPressVersion) != null) wordPressVersion else null + } } diff --git a/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt b/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt index 593e0b4a14a2..3e444fe41278 100644 --- a/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt @@ -1,11 +1,20 @@ package org.wordpress.android.sharedlogin +import android.content.pm.PackageInfo import org.assertj.core.api.Assertions import org.junit.Test +import org.mockito.kotlin.any +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever +import org.wordpress.android.BuildConfig +import org.wordpress.android.util.publicdata.PackageManagerWrapper import org.wordpress.android.util.publicdata.WordPressPublicData class WordPressPublicDataTest { - private val classToTest = WordPressPublicData() + private val packageManagerWrapper: PackageManagerWrapper = mock() + private val packageInfo: PackageInfo = mock() + + private val classToTest = WordPressPublicData(packageManagerWrapper) @Test fun `Should return correct current package ID`() { @@ -13,4 +22,11 @@ class WordPressPublicDataTest { val expected = "org.wordpress.android" Assertions.assertThat(actual).isEqualTo(expected) } + + @Test + fun `Should return correct current package version`() { + val actual = classToTest.currentPackageVersion() + val expected = BuildConfig.VERSION_NAME + Assertions.assertThat(actual).isEqualTo(expected) + } } From f031cdfff60ff9dcc21cd6085dec99c4d3fda16f Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Thu, 24 Nov 2022 21:14:32 +0200 Subject: [PATCH 03/11] Adds tests for the semantic version stripping mechanism --- .../sharedlogin/WordPressPublicDataTest.kt | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt b/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt index 3e444fe41278..07cf0c4021b9 100644 --- a/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt @@ -12,7 +12,6 @@ import org.wordpress.android.util.publicdata.WordPressPublicData class WordPressPublicDataTest { private val packageManagerWrapper: PackageManagerWrapper = mock() - private val packageInfo: PackageInfo = mock() private val classToTest = WordPressPublicData(packageManagerWrapper) @@ -29,4 +28,41 @@ class WordPressPublicDataTest { val expected = BuildConfig.VERSION_NAME Assertions.assertThat(actual).isEqualTo(expected) } + + @Test + fun `Versions without semantic information should be equal to the non semantic version`() { + mockVersion("21.2") + val actual = classToTest.nonSemanticPackageVersion() + val expected = "21.2" + Assertions.assertThat(actual).isEqualTo(expected) + } + + @Test + fun `Release candidate versions should be stripped from the non semantic version`() { + mockVersion("21.2-rc-3") + val actual = classToTest.nonSemanticPackageVersion() + val expected = "21.2" + Assertions.assertThat(actual).isEqualTo(expected) + } + + @Test + fun `Alpha versions should be stripped from the non semantic version`() { + mockVersion("21.2-alpha-3") + val actual = classToTest.nonSemanticPackageVersion() + val expected = "21.2" + Assertions.assertThat(actual).isEqualTo(expected) + } + + @Test + fun `Invalid versions should return a null non semantic version`() { + mockVersion("21.2...-rc2") + val actual = classToTest.nonSemanticPackageVersion() + val expected = null + Assertions.assertThat(actual).isEqualTo(expected) + } + + private fun mockVersion(version: String) { + val packageInfo = PackageInfo().apply { versionName = version } + whenever(packageManagerWrapper.getPackageInfo(any(), any())).thenReturn(packageInfo) + } } From 45d6148ac7a7dfc3e8ffdf6be504a6d38c12cafb Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Thu, 24 Nov 2022 21:56:10 +0200 Subject: [PATCH 04/11] Fixes package version unit test --- .../wordpress/android/sharedlogin/WordPressPublicDataTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt b/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt index 07cf0c4021b9..5076059aa902 100644 --- a/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt @@ -6,7 +6,6 @@ import org.junit.Test import org.mockito.kotlin.any import org.mockito.kotlin.mock import org.mockito.kotlin.whenever -import org.wordpress.android.BuildConfig import org.wordpress.android.util.publicdata.PackageManagerWrapper import org.wordpress.android.util.publicdata.WordPressPublicData @@ -24,8 +23,9 @@ class WordPressPublicDataTest { @Test fun `Should return correct current package version`() { + mockVersion("21.2-rc-3") val actual = classToTest.currentPackageVersion() - val expected = BuildConfig.VERSION_NAME + val expected = "21.2-rc-3" Assertions.assertThat(actual).isEqualTo(expected) } From 9e4b7dbeca6c6b3dc3c69ffc5b7184b1f34896ca Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Thu, 24 Nov 2022 21:56:48 +0200 Subject: [PATCH 05/11] Adds unit tests for Jetpack migration flow eligibility criteria --- .../utils/JetpackAppMigrationFlowUtilsTest.kt | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 WordPress/src/test/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtilsTest.kt diff --git a/WordPress/src/test/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtilsTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtilsTest.kt new file mode 100644 index 000000000000..9cdec0656fff --- /dev/null +++ b/WordPress/src/test/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtilsTest.kt @@ -0,0 +1,104 @@ +package org.wordpress.android.ui.utils + +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.junit.MockitoJUnitRunner +import org.mockito.kotlin.any +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever +import org.wordpress.android.fluxc.store.AccountStore +import org.wordpress.android.ui.prefs.AppPrefsWrapper +import org.wordpress.android.util.BuildConfigWrapper +import org.wordpress.android.util.config.JetpackMigrationFlowFeatureConfig +import org.wordpress.android.util.publicdata.AppStatus +import org.wordpress.android.util.publicdata.WordPressPublicData +import org.wordpress.android.viewmodel.ContextProvider + +@RunWith(MockitoJUnitRunner::class) +class JetpackAppMigrationFlowUtilsTest { + private val buildConfigWrapper: BuildConfigWrapper = mock() + private val jetpackMigrationFlowFeatureConfig: JetpackMigrationFlowFeatureConfig = mock() + private val contextProvider: ContextProvider = mock() + private val appPrefsWrapper: AppPrefsWrapper = mock() + private val accountStore: AccountStore = mock() + private val appStatus: AppStatus = mock() + private val wordPressPublicData: WordPressPublicData = mock() + + private val jetpackAppMigrationFlowUtils = JetpackAppMigrationFlowUtils( + buildConfigWrapper, + jetpackMigrationFlowFeatureConfig, + contextProvider, + appPrefsWrapper, + accountStore, + appStatus, + wordPressPublicData + ) + + @Before + fun setUp() { + whenever(buildConfigWrapper.isJetpackApp).thenReturn(true) + whenever(jetpackMigrationFlowFeatureConfig.isEnabled()).thenReturn(true) + whenever(appPrefsWrapper.getIsFirstTrySharedLoginJetpack()).thenReturn(true) + whenever(accountStore.hasAccessToken()).thenReturn(false) + whenever(wordPressPublicData.currentPackageId()).thenReturn("package") + whenever(appStatus.isAppInstalled(any())).thenReturn(true) + whenever(wordPressPublicData.nonSemanticPackageVersion()).thenReturn("21.3") + } + + @Test + fun `When all conditions are met the migration flow should be shown`() { + val expected = true + val actual = jetpackAppMigrationFlowUtils.shouldShowMigrationFlow() + Assert.assertEquals(expected, actual) + } + + @Test + fun `When not in the Jetpack app the migration flow should not be shown`() { + whenever(buildConfigWrapper.isJetpackApp).thenReturn(false) + val expected = false + val actual = jetpackAppMigrationFlowUtils.shouldShowMigrationFlow() + Assert.assertEquals(expected, actual) + } + + @Test + fun `When the jetpackMigrationFlowFeature is not enableed the Jetpack app the migration flow should not be shown`() { + whenever(jetpackMigrationFlowFeatureConfig.isEnabled()).thenReturn(false) + val expected = false + val actual = jetpackAppMigrationFlowUtils.shouldShowMigrationFlow() + Assert.assertEquals(expected, actual) + } + + @Test + fun `When the it is not the first migration attempt the Jetpack app the migration flow should not be shown`() { + whenever(appPrefsWrapper.getIsFirstTrySharedLoginJetpack()).thenReturn(false) + val expected = false + val actual = jetpackAppMigrationFlowUtils.shouldShowMigrationFlow() + Assert.assertEquals(expected, actual) + } + + @Test + fun `If the user is logged in the Jetpack app the migration flow should not be shown`() { + whenever(accountStore.hasAccessToken()).thenReturn(true) + val expected = false + val actual = jetpackAppMigrationFlowUtils.shouldShowMigrationFlow() + Assert.assertEquals(expected, actual) + } + + @Test + fun `When the WordPress app is not installed the Jetpack app the migration flow should not be shown`() { + whenever(appStatus.isAppInstalled(any())).thenReturn(false) + val expected = false + val actual = jetpackAppMigrationFlowUtils.shouldShowMigrationFlow() + Assert.assertEquals(expected, actual) + } + + @Test + fun `When the WordPress app is not compatible the Jetpack app the migration flow should not be shown`() { + whenever(wordPressPublicData.nonSemanticPackageVersion()).thenReturn("21.2") + val expected = false + val actual = jetpackAppMigrationFlowUtils.shouldShowMigrationFlow() + Assert.assertEquals(expected, actual) + } +} From 5be5a6cc1d475b2b1d2abe9b4676e26fb9e1bd3c Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Thu, 24 Nov 2022 22:10:39 +0200 Subject: [PATCH 06/11] Adds check for null WordPress version --- .../android/ui/utils/JetpackAppMigrationFlowUtils.kt | 8 +++++--- .../android/util/publicdata/WordPressPublicData.kt | 5 ++++- .../ui/utils/JetpackAppMigrationFlowUtilsTest.kt | 10 +++++++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt b/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt index df0a373acf99..c04fc3c5a402 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt @@ -20,7 +20,7 @@ class JetpackAppMigrationFlowUtils @Inject constructor( private val appStatus: AppStatus, private val wordPressPublicData: WordPressPublicData, ) { - private val minimumSupportedVersion = "21.3" + private val minimumSupportedVersion = "21.3" //non semantic minimum supported version fun shouldShowMigrationFlow() = buildConfigWrapper.isJetpackApp && jetpackMigrationFlowFeatureConfig.isEnabled() @@ -35,6 +35,8 @@ class JetpackAppMigrationFlowUtils @Inject constructor( private fun isWordPressInstalled() = appStatus.isAppInstalled(wordPressPublicData.currentPackageId()) - private fun isWordPressCompatible() = - Version(wordPressPublicData.nonSemanticPackageVersion()) >= Version(minimumSupportedVersion) + private fun isWordPressCompatible(): Boolean { + val wordPressVersion = wordPressPublicData.nonSemanticPackageVersion() + return wordPressVersion != null && Version(wordPressVersion) >= Version(minimumSupportedVersion) + } } diff --git a/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt b/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt index 8c00ea9bb3f5..1a24b06f6505 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt @@ -30,6 +30,9 @@ class WordPressPublicData @Inject constructor(private val packageManagerWrapper: // Clean app semantic versioning info. E.g 21.2-rc-3 turns to 21.2 val wordPressVersion = rawVersion?.split("-")?.getOrNull(0) ?: rawVersion ?: "" - return if(Regex("^\\d+(\\.\\d+)*\$").matchEntire(wordPressVersion) != null) wordPressVersion else null + // Version is supported by org.wordpress.android.util.helpers.Version.Version + val versionIsSupportedForComparison = Regex("[0-9]+(\\.[0-9]+)*").matchEntire(wordPressVersion) != null + + return if(versionIsSupportedForComparison) wordPressVersion else null } } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtilsTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtilsTest.kt index 9cdec0656fff..2dc59614e1d6 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtilsTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtilsTest.kt @@ -63,7 +63,7 @@ class JetpackAppMigrationFlowUtilsTest { } @Test - fun `When the jetpackMigrationFlowFeature is not enableed the Jetpack app the migration flow should not be shown`() { + fun `When the jetpackMigrationFlow is not enableed the Jetpack app the migration flow should not be shown`() { whenever(jetpackMigrationFlowFeatureConfig.isEnabled()).thenReturn(false) val expected = false val actual = jetpackAppMigrationFlowUtils.shouldShowMigrationFlow() @@ -101,4 +101,12 @@ class JetpackAppMigrationFlowUtilsTest { val actual = jetpackAppMigrationFlowUtils.shouldShowMigrationFlow() Assert.assertEquals(expected, actual) } + + @Test + fun `When the WordPress app version is null the Jetpack app the migration flow should not be shown`() { + whenever(wordPressPublicData.nonSemanticPackageVersion()).thenReturn(null) + val expected = false + val actual = jetpackAppMigrationFlowUtils.shouldShowMigrationFlow() + Assert.assertEquals(expected, actual) + } } From 398ff12fea92a290a91fc01995b04d101a892ef1 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Fri, 25 Nov 2022 11:15:26 +0200 Subject: [PATCH 07/11] Adds explicit return type --- .../wordpress/android/util/publicdata/WordPressPublicData.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt b/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt index 1a24b06f6505..aa7af2574263 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt @@ -22,7 +22,7 @@ class WordPressPublicData @Inject constructor(private val packageManagerWrapper: else -> throw IllegalArgumentException("Failed to get Jetpack package ID: build flavor not found.") } - fun currentPackageVersion() = packageManagerWrapper.getPackageInfo(currentPackageId())?.versionName + fun currentPackageVersion(): String? = packageManagerWrapper.getPackageInfo(currentPackageId())?.versionName fun nonSemanticPackageVersion(): String? { val rawVersion = currentPackageVersion() From 02fec9c586c50847ce4a71db76869562f2a707e4 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Fri, 25 Nov 2022 11:31:59 +0200 Subject: [PATCH 08/11] Use regex to clean up semantic version string --- .../util/publicdata/WordPressPublicData.kt | 11 +++++---- .../sharedlogin/WordPressPublicDataTest.kt | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt b/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt index aa7af2574263..4f55c39bb3c4 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt @@ -25,14 +25,15 @@ class WordPressPublicData @Inject constructor(private val packageManagerWrapper: fun currentPackageVersion(): String? = packageManagerWrapper.getPackageInfo(currentPackageId())?.versionName fun nonSemanticPackageVersion(): String? { - val rawVersion = currentPackageVersion() + val rawVersion = currentPackageVersion() ?: return null - // Clean app semantic versioning info. E.g 21.2-rc-3 turns to 21.2 - val wordPressVersion = rawVersion?.split("-")?.getOrNull(0) ?: rawVersion ?: "" + // Clean app semantic versioning and keep ony major-minor version info. E.g 21.2-rc-3 turns to 21.2 + val majorMinorRegex = "^(\\d*)(\\.(\\d*))".toRegex() + val wordPressVersion = majorMinorRegex.find(rawVersion)?.value ?: return null - // Version is supported by org.wordpress.android.util.helpers.Version.Version + // Verify that the resulting version is supported by org.wordpress.android.util.helpers.Version.Version val versionIsSupportedForComparison = Regex("[0-9]+(\\.[0-9]+)*").matchEntire(wordPressVersion) != null - return if(versionIsSupportedForComparison) wordPressVersion else null + return if (versionIsSupportedForComparison) wordPressVersion else null } } diff --git a/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt b/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt index 5076059aa902..a2dae26faafa 100644 --- a/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt @@ -61,6 +61,30 @@ class WordPressPublicDataTest { Assertions.assertThat(actual).isEqualTo(expected) } + @Test + fun `Empty versions should return a null non semantic version`() { + mockVersion("") + val actual = classToTest.nonSemanticPackageVersion() + val expected = null + Assertions.assertThat(actual).isEqualTo(expected) + } + + @Test + fun `Only the major-minor version information is returned`() { + mockVersion("21.3.1") + val actual = classToTest.nonSemanticPackageVersion() + val expected = "21.3" + Assertions.assertThat(actual).isEqualTo(expected) + } + + @Test + fun `When only the major is provided a null non semantic version is returned`() { + mockVersion("21") + val actual = classToTest.nonSemanticPackageVersion() + val expected = null + Assertions.assertThat(actual).isEqualTo(expected) + } + private fun mockVersion(version: String) { val packageInfo = PackageInfo().apply { versionName = version } whenever(packageManagerWrapper.getPackageInfo(any(), any())).thenReturn(packageInfo) From 3b8c9a7ec121ddac820a922416039bc1f1b6ed59 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Fri, 25 Nov 2022 11:42:31 +0200 Subject: [PATCH 09/11] Fixes detekt issus with many return statemenents --- .../wordpress/android/util/publicdata/WordPressPublicData.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt b/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt index 4f55c39bb3c4..bac327671ced 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/publicdata/WordPressPublicData.kt @@ -29,10 +29,11 @@ class WordPressPublicData @Inject constructor(private val packageManagerWrapper: // Clean app semantic versioning and keep ony major-minor version info. E.g 21.2-rc-3 turns to 21.2 val majorMinorRegex = "^(\\d*)(\\.(\\d*))".toRegex() - val wordPressVersion = majorMinorRegex.find(rawVersion)?.value ?: return null + val wordPressVersion = majorMinorRegex.find(rawVersion)?.value // Verify that the resulting version is supported by org.wordpress.android.util.helpers.Version.Version - val versionIsSupportedForComparison = Regex("[0-9]+(\\.[0-9]+)*").matchEntire(wordPressVersion) != null + val versionIsSupportedForComparison = wordPressVersion !=null + && Regex("[0-9]+(\\.[0-9]+)*").matchEntire(wordPressVersion) != null return if (versionIsSupportedForComparison) wordPressVersion else null } From a532babbfa57ae0df3bfd60cb0858136385c3b80 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Fri, 25 Nov 2022 12:04:35 +0200 Subject: [PATCH 10/11] Fixes checkstyle issue --- .../wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt b/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt index c04fc3c5a402..ec110d1619aa 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt @@ -20,7 +20,7 @@ class JetpackAppMigrationFlowUtils @Inject constructor( private val appStatus: AppStatus, private val wordPressPublicData: WordPressPublicData, ) { - private val minimumSupportedVersion = "21.3" //non semantic minimum supported version + private val minimumSupportedVersion = "21.3" // non semantic minimum supported version fun shouldShowMigrationFlow() = buildConfigWrapper.isJetpackApp && jetpackMigrationFlowFeatureConfig.isEnabled() From 6dd469907e469b2f0ceec88f7af36cb5a978b890 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Fri, 25 Nov 2022 12:31:28 +0200 Subject: [PATCH 11/11] Fixes broken test due to the validation change --- .../wordpress/android/sharedlogin/WordPressPublicDataTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt b/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt index a2dae26faafa..58a3ef52b221 100644 --- a/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/sharedlogin/WordPressPublicDataTest.kt @@ -55,7 +55,7 @@ class WordPressPublicDataTest { @Test fun `Invalid versions should return a null non semantic version`() { - mockVersion("21.2...-rc2") + mockVersion("21.a2...-rc2") val actual = classToTest.nonSemanticPackageVersion() val expected = null Assertions.assertThat(actual).isEqualTo(expected)