|
| 1 | +package com.fsck.k9.preferences.migration |
| 2 | + |
| 3 | +import assertk.assertThat |
| 4 | +import assertk.assertions.doesNotContainKey |
| 5 | +import assertk.assertions.isEqualTo |
| 6 | +import assertk.assertions.key |
| 7 | +import com.fsck.k9.preferences.createPreferencesDatabase |
| 8 | +import java.util.UUID |
| 9 | +import kotlin.test.Test |
| 10 | +import net.thunderbird.core.logging.legacy.Log |
| 11 | +import net.thunderbird.core.logging.testing.TestLogger |
| 12 | +import net.thunderbird.feature.account.storage.profile.AvatarTypeDto |
| 13 | +import org.junit.After |
| 14 | +import org.junit.Before |
| 15 | +import org.junit.runner.RunWith |
| 16 | +import org.robolectric.RobolectricTestRunner |
| 17 | + |
| 18 | +@RunWith(RobolectricTestRunner::class) |
| 19 | +class StorageMigrationTo28Test { |
| 20 | + private val database = createPreferencesDatabase() |
| 21 | + private val migrationHelper = DefaultStorageMigrationHelper() |
| 22 | + private val migration = StorageMigrationTo28(database, migrationHelper) |
| 23 | + |
| 24 | + @Before |
| 25 | + fun setUp() { |
| 26 | + Log.logger = TestLogger() |
| 27 | + } |
| 28 | + |
| 29 | + @After |
| 30 | + fun tearDown() { |
| 31 | + database.close() |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `avatar type should be set for accounts with no avatar type`() { |
| 36 | + val accountUuid = createAccount( |
| 37 | + "avatarType" to null, |
| 38 | + "avatarMonogram" to "AB", |
| 39 | + ) |
| 40 | + writeAccountUuids(accountUuid) |
| 41 | + |
| 42 | + migration.ensureAvatarSet() |
| 43 | + |
| 44 | + assertAvatarType(accountUuid, AvatarTypeDto.MONOGRAM) |
| 45 | + assertAvatarMonogram(accountUuid, "AB") |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `avatar type should not be set for accounts with existing avatar type`() { |
| 50 | + val accountUuid = createAccount( |
| 51 | + "avatarType" to AvatarTypeDto.IMAGE.name, |
| 52 | + ) |
| 53 | + writeAccountUuids(accountUuid) |
| 54 | + |
| 55 | + migration.ensureAvatarSet() |
| 56 | + |
| 57 | + assertAvatarType(accountUuid, AvatarTypeDto.IMAGE) |
| 58 | + assertThat(migrationHelper.readAllValues(database)) |
| 59 | + .doesNotContainKey("$accountUuid.avatarMonogram") |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `avatar monogram should be set for accounts with MONOGRAM avatar type and no monogram`() { |
| 64 | + val accountUuid = createAccount( |
| 65 | + "avatarType" to AvatarTypeDto.MONOGRAM.name, |
| 66 | + "avatarMonogram" to null, |
| 67 | + "name.0" to "John Doe", |
| 68 | + "email.0" to "test@example.com", |
| 69 | + ) |
| 70 | + writeAccountUuids(accountUuid) |
| 71 | + |
| 72 | + migration.ensureAvatarSet() |
| 73 | + |
| 74 | + assertAvatarType(accountUuid, AvatarTypeDto.MONOGRAM) |
| 75 | + assertAvatarMonogram(accountUuid, "JO") |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun `avatar monogram should be added for accounts with no monogram and name but email`() { |
| 80 | + val accountUuid = createAccount( |
| 81 | + "avatarType" to AvatarTypeDto.MONOGRAM.name, |
| 82 | + "avatarMonogram" to null, |
| 83 | + "name.0" to null, |
| 84 | + "email.0" to "test@example.com", |
| 85 | + ) |
| 86 | + writeAccountUuids(accountUuid) |
| 87 | + |
| 88 | + migration.ensureAvatarSet() |
| 89 | + |
| 90 | + assertAvatarType(accountUuid, AvatarTypeDto.MONOGRAM) |
| 91 | + assertAvatarMonogram(accountUuid, "TE") |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun `avatar monogram should be added for accounts with MONOGRAM avatar type and no monogram, name and email`() { |
| 96 | + val accountUuid = createAccount( |
| 97 | + "avatarType" to AvatarTypeDto.MONOGRAM.name, |
| 98 | + "avatarMonogram" to null, |
| 99 | + "name.0" to null, |
| 100 | + "email.0" to null, |
| 101 | + ) |
| 102 | + writeAccountUuids(accountUuid) |
| 103 | + |
| 104 | + migration.ensureAvatarSet() |
| 105 | + |
| 106 | + assertAvatarType(accountUuid, AvatarTypeDto.MONOGRAM) |
| 107 | + assertAvatarMonogram(accountUuid, "XX") |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun `avatar type and monogram should be set for accounts with missing avatar type and monogram`() { |
| 112 | + val accountUuid = createAccount( |
| 113 | + "avatarType" to null, |
| 114 | + "avatarMonogram" to null, |
| 115 | + "name.0" to "John Doe", |
| 116 | + "email.0" to "test@example.com", |
| 117 | + ) |
| 118 | + |
| 119 | + writeAccountUuids(accountUuid) |
| 120 | + |
| 121 | + migration.ensureAvatarSet() |
| 122 | + |
| 123 | + assertAvatarType(accountUuid, AvatarTypeDto.MONOGRAM) |
| 124 | + assertAvatarMonogram(accountUuid, "JO") |
| 125 | + } |
| 126 | + |
| 127 | + private fun assertAvatarType( |
| 128 | + accountUuid: String, |
| 129 | + expectedAvatarType: AvatarTypeDto, |
| 130 | + ) { |
| 131 | + assertThat(migrationHelper.readAllValues(database)) |
| 132 | + .key("$accountUuid.avatarType").isEqualTo(expectedAvatarType.name) |
| 133 | + } |
| 134 | + |
| 135 | + private fun assertAvatarMonogram( |
| 136 | + accountUuid: String, |
| 137 | + expectedMonogram: String, |
| 138 | + ) { |
| 139 | + assertThat(migrationHelper.readAllValues(database)) |
| 140 | + .key("$accountUuid.avatarMonogram").isEqualTo(expectedMonogram) |
| 141 | + } |
| 142 | + |
| 143 | + private fun writeAccountUuids(vararg accounts: String) { |
| 144 | + val accountUuids = accounts.joinToString(separator = ",") |
| 145 | + migrationHelper.insertValue(database, "accountUuids", accountUuids) |
| 146 | + } |
| 147 | + |
| 148 | + private fun createAccount(vararg pairs: Pair<String, String?>): String { |
| 149 | + val accountUuid = UUID.randomUUID().toString() |
| 150 | + |
| 151 | + for ((key, value) in pairs) { |
| 152 | + migrationHelper.insertValue(database, "$accountUuid.$key", value) |
| 153 | + } |
| 154 | + |
| 155 | + return accountUuid |
| 156 | + } |
| 157 | +} |
0 commit comments