|
| 1 | +package net.thunderbird.app.common.account.data |
| 2 | + |
| 3 | +import assertk.assertThat |
| 4 | +import assertk.assertions.containsExactly |
| 5 | +import assertk.assertions.isEqualTo |
| 6 | +import assertk.assertions.isNotNull |
| 7 | +import assertk.assertions.isNull |
| 8 | +import assertk.assertions.prop |
| 9 | +import com.fsck.k9.mail.AuthType |
| 10 | +import com.fsck.k9.mail.ConnectionSecurity |
| 11 | +import com.fsck.k9.mail.ServerSettings |
| 12 | +import com.fsck.k9.preferences.K9StoragePersister |
| 13 | +import net.thunderbird.components.core.logging.testing.TestLogger |
| 14 | +import net.thunderbird.core.android.account.AccountDefaultsProvider |
| 15 | +import net.thunderbird.core.android.account.LegacyAccount |
| 16 | +import net.thunderbird.core.android.testing.RobolectricTest |
| 17 | +import net.thunderbird.core.preference.storage.DefaultStorageProvider |
| 18 | +import net.thunderbird.core.preference.storage.Storage |
| 19 | +import net.thunderbird.core.preference.storage.StorageProvider |
| 20 | +import net.thunderbird.feature.account.AccountId |
| 21 | +import net.thunderbird.feature.account.AccountIdFactory |
| 22 | +import net.thunderbird.feature.account.storage.legacy.LegacyAccountStorageHandler |
| 23 | +import net.thunderbird.feature.account.storage.legacy.LegacyAvatarDtoStorageHandler |
| 24 | +import net.thunderbird.feature.account.storage.legacy.LegacyProfileDtoStorageHandler |
| 25 | +import net.thunderbird.feature.account.storage.legacy.serializer.ServerSettingsDtoSerializer |
| 26 | +import net.thunderbird.feature.account.storage.profile.AvatarDto |
| 27 | +import net.thunderbird.feature.account.storage.profile.AvatarTypeDto |
| 28 | +import net.thunderbird.feature.account.storage.profile.ProfileDto |
| 29 | +import org.junit.Test |
| 30 | +import org.robolectric.RuntimeEnvironment |
| 31 | + |
| 32 | +/** |
| 33 | + * Runs [LegacyAccountLocalDataSource] against the real preferences database to verify that reading from the cached |
| 34 | + * storage snapshot returns the latest values and that nothing is lost on disk. |
| 35 | + */ |
| 36 | +class LegacyAccountLocalDataSourceStorageTest : RobolectricTest() { |
| 37 | + |
| 38 | + private val logger = TestLogger() |
| 39 | + |
| 40 | + private val storageProvider = createStorageProvider() |
| 41 | + private val testSubject = createTestSubject(storageProvider) |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `getById should return the latest saved values`() { |
| 45 | + val account = createAccount() |
| 46 | + |
| 47 | + testSubject.save(account.copy(lastSyncTime = 1L)) |
| 48 | + testSubject.save(account.copy(lastSyncTime = 2L)) |
| 49 | + |
| 50 | + assertThat(testSubject.getById(account.id)).isNotNull() |
| 51 | + .prop(LegacyAccount::lastSyncTime).isEqualTo(2L) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `getById should return account values written by another editor`() { |
| 56 | + val account = createAccount() |
| 57 | + testSubject.save(account) |
| 58 | + |
| 59 | + storageProvider.createStorageEditor() |
| 60 | + .putLong("${account.id}.lastSyncTime", 42L) |
| 61 | + .commit() |
| 62 | + |
| 63 | + assertThat(testSubject.getById(account.id)).isNotNull() |
| 64 | + .prop(LegacyAccount::lastSyncTime).isEqualTo(42L) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun `saved values should be loaded after a restart`() { |
| 69 | + val account = createAccount() |
| 70 | + testSubject.save(account.copy(lastSyncTime = 1L)) |
| 71 | + testSubject.save(account.copy(lastSyncTime = 2L)) |
| 72 | + |
| 73 | + val restartedTestSubject = createTestSubject(createStorageProvider()) |
| 74 | + |
| 75 | + assertThat(restartedTestSubject.loadAll().map { it.id }).containsExactly(account.id) |
| 76 | + assertThat(restartedTestSubject.getById(account.id)).isNotNull() |
| 77 | + .prop(LegacyAccount::lastSyncTime).isEqualTo(2L) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun `saving an account should keep values written by another editor`() { |
| 82 | + val account = createAccount() |
| 83 | + testSubject.save(account) |
| 84 | + |
| 85 | + storageProvider.createStorageEditor() |
| 86 | + .putString("otherKey", "otherValue") |
| 87 | + .commit() |
| 88 | + testSubject.save(account.copy(lastSyncTime = 1L)) |
| 89 | + |
| 90 | + val restartedStorageProvider = createStorageProvider() |
| 91 | + assertThat(restartedStorageProvider.storage.getStringOrNull("otherKey")).isEqualTo("otherValue") |
| 92 | + assertThat(createTestSubject(restartedStorageProvider).getById(account.id)).isNotNull() |
| 93 | + .prop(LegacyAccount::lastSyncTime).isEqualTo(1L) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun `deleted account should be gone after a restart`() { |
| 98 | + val account = createAccount() |
| 99 | + val otherAccount = createAccount() |
| 100 | + testSubject.save(account) |
| 101 | + testSubject.save(otherAccount) |
| 102 | + |
| 103 | + testSubject.delete(account.id) |
| 104 | + |
| 105 | + val restartedTestSubject = createTestSubject(createStorageProvider()) |
| 106 | + assertThat(restartedTestSubject.getById(account.id)).isNull() |
| 107 | + assertThat(restartedTestSubject.loadAll().map { it.id }).containsExactly(otherAccount.id) |
| 108 | + } |
| 109 | + |
| 110 | + private fun createStorageProvider(): StorageProvider { |
| 111 | + return DefaultStorageProvider(K9StoragePersister(RuntimeEnvironment.getApplication(), logger)) |
| 112 | + } |
| 113 | + |
| 114 | + private fun createTestSubject(storageProvider: StorageProvider): LegacyAccountLocalDataSource { |
| 115 | + return LegacyAccountLocalDataSource( |
| 116 | + storageProvider = storageProvider, |
| 117 | + accountStorageHandler = LegacyAccountStorageHandler( |
| 118 | + serverSettingsDtoSerializer = ServerSettingsDtoSerializer(), |
| 119 | + profileDtoStorageHandler = LegacyProfileDtoStorageHandler(LegacyAvatarDtoStorageHandler()), |
| 120 | + logger = logger, |
| 121 | + ), |
| 122 | + accountDefaultsProvider = PassThroughAccountDefaultsProvider(), |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + private class PassThroughAccountDefaultsProvider : AccountDefaultsProvider { |
| 127 | + override fun applyDefaults(account: LegacyAccount): LegacyAccount = account |
| 128 | + override fun applyOverwrites(account: LegacyAccount, storage: Storage): LegacyAccount = account |
| 129 | + } |
| 130 | + |
| 131 | + private companion object { |
| 132 | + fun createAccount(id: AccountId = AccountIdFactory.create()): LegacyAccount { |
| 133 | + return LegacyAccount( |
| 134 | + id = id, |
| 135 | + name = "Account", |
| 136 | + email = "user@example.com", |
| 137 | + profile = ProfileDto( |
| 138 | + id = id, |
| 139 | + name = "Account", |
| 140 | + color = -1, |
| 141 | + avatar = AvatarDto( |
| 142 | + accountId = id, |
| 143 | + avatarType = AvatarTypeDto.MONOGRAM, |
| 144 | + avatarMonogram = "AC", |
| 145 | + avatarImageUri = null, |
| 146 | + avatarIconName = null, |
| 147 | + ), |
| 148 | + ), |
| 149 | + incomingServerSettings = createServerSettings("imap", 993), |
| 150 | + outgoingServerSettings = createServerSettings("smtp", 465), |
| 151 | + identities = emptyList(), |
| 152 | + accountNumber = 1, |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + fun createServerSettings(type: String, port: Int) = ServerSettings( |
| 157 | + type = type, |
| 158 | + host = "$type.example.com", |
| 159 | + port = port, |
| 160 | + connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED, |
| 161 | + authenticationType = AuthType.PLAIN, |
| 162 | + username = "user", |
| 163 | + password = "password", |
| 164 | + clientCertificateAlias = null, |
| 165 | + ) |
| 166 | + } |
| 167 | +} |
0 commit comments