|
| 1 | +/* |
| 2 | + * Infomaniak Mail - Android |
| 3 | + * Copyright (C) 2024 Infomaniak Network SA |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package com.infomaniak.mail |
| 19 | + |
| 20 | +import com.infomaniak.lib.stores.updaterequired.data.models.AppPublishedVersion |
| 21 | +import com.infomaniak.lib.stores.updaterequired.data.models.AppVersion |
| 22 | +import com.infomaniak.lib.stores.updaterequired.data.models.AppVersion.Companion.compareVersionTo |
| 23 | +import com.infomaniak.lib.stores.updaterequired.data.models.AppVersion.Companion.toVersionNumbers |
| 24 | +import org.junit.Assert |
| 25 | +import org.junit.Test |
| 26 | + |
| 27 | +/** |
| 28 | + * Tests for the [com.infomaniak.lib.stores.updaterequired.data.models.AppVersion] methods to compare two App Versions |
| 29 | + */ |
| 30 | +class AppVersionCheckingTest { |
| 31 | + |
| 32 | + private val negativeVersion = "-1.0.2" |
| 33 | + private val smallVersion = "0.0.1" |
| 34 | + private val basicVersion = "1.0.1" |
| 35 | + private val basicBigDigitVersion = "1.0.9" |
| 36 | + private val basicNumberVersion = "1.0.10" |
| 37 | + private val mediumVersion = "1.2.0" |
| 38 | + private val mediumVersionShortFormat = "1.2" |
| 39 | + private val greatVersion = "1.2.2" |
| 40 | + private val greaterVersion = "1.3.0" |
| 41 | + private val invalidCommaVersion = "1.2,9" |
| 42 | + private val invalidParseVersion = "toto" |
| 43 | + private val invalidEmptyVersion = "" |
| 44 | + |
| 45 | + private val defaultAppVersion = AppVersion( |
| 46 | + mediumVersion, |
| 47 | + arrayOf(AppPublishedVersion(tag = greatVersion, _type = "production")) |
| 48 | + ) |
| 49 | + private val invalidMinimalAppVersion = AppVersion( |
| 50 | + mediumVersion, |
| 51 | + arrayOf( |
| 52 | + AppPublishedVersion(tag = basicVersion, _type = "production"), |
| 53 | + AppPublishedVersion(tag = greaterVersion, _type = "beta"), |
| 54 | + ) |
| 55 | + ) |
| 56 | + private val invalidFormatAppVersion = AppVersion( |
| 57 | + invalidCommaVersion, |
| 58 | + arrayOf( |
| 59 | + AppPublishedVersion(tag = basicVersion, _type = "production"), |
| 60 | + AppPublishedVersion(tag = greaterVersion, _type = "beta"), |
| 61 | + ) |
| 62 | + ) |
| 63 | + |
| 64 | + // region toVersionNumbers() |
| 65 | + @Test |
| 66 | + fun parseVersionNumber_defaultFormat() { |
| 67 | + Assert.assertEquals(listOf(1, 0, 1), basicVersion.toVersionNumbers()) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun parseVersionNumber_negativeFormat() { |
| 72 | + Assert.assertEquals(listOf(-1, 0, 2), negativeVersion.toVersionNumbers()) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun parseVersionNumber_numberFormat() { |
| 77 | + Assert.assertEquals(listOf(1, 0, 10), basicNumberVersion.toVersionNumbers()) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun parseVersionNumber_invalidCommaFormat() { |
| 82 | + Assert.assertThrows(NumberFormatException::class.java) { invalidCommaVersion.toVersionNumbers() } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun parseVersionNumber_invalidStringFormat() { |
| 87 | + Assert.assertThrows(NumberFormatException::class.java) { invalidParseVersion.toVersionNumbers() } |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun parseVersionNumber_invalidEmptyFormat() { |
| 92 | + Assert.assertThrows(NumberFormatException::class.java) { invalidEmptyVersion.toVersionNumbers() } |
| 93 | + } |
| 94 | + //endregion |
| 95 | + |
| 96 | + //region compareVersionTo |
| 97 | + @Test |
| 98 | + fun compareVersions_equalVersions() { |
| 99 | + compareVersion(0, basicVersion, basicVersion) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun compareVersions_olderVersions() { |
| 104 | + compareVersion(-1, basicVersion, greatVersion) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun compareVersions_newerVersions() { |
| 109 | + compareVersion(1, basicVersion, smallVersion) |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun compareVersions_numberVersions() { |
| 114 | + compareVersion(-1, basicBigDigitVersion, basicNumberVersion) |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + fun compareVersions_shortVersions() { |
| 119 | + compareVersion(-1, basicNumberVersion, mediumVersionShortFormat) |
| 120 | + compareVersion(0, mediumVersion, mediumVersionShortFormat) |
| 121 | + compareVersion(1, greatVersion, mediumVersionShortFormat) |
| 122 | + compareVersion(1, greaterVersion, mediumVersionShortFormat) |
| 123 | + |
| 124 | + compareVersion(1, mediumVersionShortFormat, basicNumberVersion) |
| 125 | + compareVersion(0, mediumVersionShortFormat, mediumVersion) |
| 126 | + compareVersion(-1, mediumVersionShortFormat, greatVersion) |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + fun compareVersions_negativeVersions() { |
| 131 | + compareVersion(1, smallVersion, negativeVersion) |
| 132 | + } |
| 133 | + |
| 134 | + private fun compareVersion(expectedResult: Int, caller: String, other: String) { |
| 135 | + Assert.assertEquals(expectedResult, caller.toVersionNumbers().compareVersionTo(other.toVersionNumbers())) |
| 136 | + } |
| 137 | + //endregion |
| 138 | + |
| 139 | + //region isMinimalVersionValid |
| 140 | + @Test |
| 141 | + fun isMinimalVersionValid_correct() { |
| 142 | + val minimalVersionNumbers = defaultAppVersion.minimalAcceptedVersion.toVersionNumbers() |
| 143 | + Assert.assertTrue(defaultAppVersion.isMinimalVersionValid(minimalVersionNumbers)) |
| 144 | + Assert.assertTrue(invalidMinimalAppVersion.isMinimalVersionValid(negativeVersion.toVersionNumbers())) |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + fun isMinimalVersionValid_wrongMinimalVersion() { |
| 149 | + val minimalVersionNumbers = invalidMinimalAppVersion.minimalAcceptedVersion.toVersionNumbers() |
| 150 | + Assert.assertFalse(invalidMinimalAppVersion.isMinimalVersionValid(minimalVersionNumbers)) |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + fun isMinimalVersionValid_invalidMinimalVersions() { |
| 155 | + val invalidVersions = listOf(invalidCommaVersion, invalidParseVersion, invalidEmptyVersion) |
| 156 | + |
| 157 | + invalidVersions.forEach { version -> |
| 158 | + Assert.assertThrows(NumberFormatException::class.java) { |
| 159 | + defaultAppVersion.isMinimalVersionValid(version.toVersionNumbers()) |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + //endregion |
| 164 | + |
| 165 | + //region mustRequireUpdate |
| 166 | + @Test |
| 167 | + fun mustRequireUpdate_newerVersion() { |
| 168 | + Assert.assertFalse(defaultAppVersion.mustRequireUpdate(greatVersion)) |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + fun mustRequireUpdate_sameVersion() { |
| 173 | + Assert.assertFalse(defaultAppVersion.mustRequireUpdate(mediumVersion)) |
| 174 | + Assert.assertFalse(defaultAppVersion.mustRequireUpdate(mediumVersionShortFormat)) |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + fun mustRequireUpdate_olderVersion() { |
| 179 | + Assert.assertTrue(defaultAppVersion.mustRequireUpdate(basicVersion)) |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + fun mustRequireUpdate_invalidMinimal() { |
| 184 | + Assert.assertFalse(invalidMinimalAppVersion.mustRequireUpdate(greaterVersion)) |
| 185 | + Assert.assertFalse(invalidMinimalAppVersion.mustRequireUpdate(mediumVersion)) |
| 186 | + Assert.assertFalse(invalidMinimalAppVersion.mustRequireUpdate(smallVersion)) |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + fun mustRequireUpdate_invalidFormat() { |
| 191 | + Assert.assertFalse(invalidFormatAppVersion.mustRequireUpdate(greaterVersion)) |
| 192 | + Assert.assertFalse(invalidFormatAppVersion.mustRequireUpdate(mediumVersion)) |
| 193 | + Assert.assertFalse(invalidFormatAppVersion.mustRequireUpdate(smallVersion)) |
| 194 | + } |
| 195 | + //endregion |
| 196 | +} |
0 commit comments