diff options
| author | test@dangofactory.cos <you@example.com> | 2026-08-20 16:57:18 +0000 |
|---|---|---|
| committer | test@dangofactory.cos <you@example.com> | 2026-08-20 16:57:18 +0000 |
| commit | 50ce5d5e6cd0d720ff437d35eace545816848428 (patch) | |
| tree | b59af206a36265cf081945e434900b32b0c43edd /app/testing/src/main/java/com | |
| download | monocles_mail-50ce5d5e6cd0d720ff437d35eace545816848428.tar.gz monocles_mail-50ce5d5e6cd0d720ff437d35eace545816848428.tar.bz2 monocles_mail-50ce5d5e6cd0d720ff437d35eace545816848428.zip | |
Inital
Diffstat (limited to 'app/testing/src/main/java/com')
4 files changed, 104 insertions, 0 deletions
diff --git a/app/testing/src/main/java/com/fsck/k9/RobolectricTest.kt b/app/testing/src/main/java/com/fsck/k9/RobolectricTest.kt new file mode 100644 index 0000000..8dde7f5 --- /dev/null +++ b/app/testing/src/main/java/com/fsck/k9/RobolectricTest.kt @@ -0,0 +1,17 @@ +package com.fsck.k9 + +import android.app.Application +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * A Robolectric test that does not create an instance of our [Application] class [K9]. + * + * See also [K9RobolectricTest]. + */ +@RunWith(RobolectricTestRunner::class) +@Config(application = EmptyApplication::class) +abstract class RobolectricTest + +class EmptyApplication : Application() diff --git a/app/testing/src/main/java/com/fsck/k9/preferences/InMemoryStoragePersister.kt b/app/testing/src/main/java/com/fsck/k9/preferences/InMemoryStoragePersister.kt new file mode 100644 index 0000000..6b3d3e7 --- /dev/null +++ b/app/testing/src/main/java/com/fsck/k9/preferences/InMemoryStoragePersister.kt @@ -0,0 +1,61 @@ +package com.fsck.k9.preferences + +class InMemoryStoragePersister : StoragePersister { + private val values = mutableMapOf<String, Any?>() + + override fun loadValues(): Storage { + return Storage(values.mapValues { (_, value) -> value?.toString() ?: "" }) + } + + override fun createStorageEditor(storageUpdater: StorageUpdater): StorageEditor { + return InMemoryStorageEditor(storageUpdater) + } + + private inner class InMemoryStorageEditor(private val storageUpdater: StorageUpdater) : StorageEditor { + private val removals = mutableSetOf<String>() + private val changes = mutableMapOf<String, String>() + private var alreadyCommitted = false + + override fun putBoolean(key: String, value: Boolean) = apply { + changes[key] = value.toString() + removals.remove(key) + } + + override fun putInt(key: String, value: Int) = apply { + changes[key] = value.toString() + removals.remove(key) + } + + override fun putLong(key: String, value: Long) = apply { + changes[key] = value.toString() + removals.remove(key) + } + + override fun putString(key: String, value: String?) = apply { + if (value == null) { + remove(key) + } else { + changes[key] = value + removals.remove(key) + } + } + + override fun remove(key: String) = apply { + removals.add(key) + changes.remove(key) + } + + override fun commit(): Boolean { + if (alreadyCommitted) throw AssertionError("StorageEditor.commit() called more than once") + alreadyCommitted = true + + storageUpdater.updateStorage(::writeValues) + + return true + } + + private fun writeValues(currentStorage: Storage): Storage { + return Storage(currentStorage.all - removals + changes) + } + } +} diff --git a/app/testing/src/main/java/com/fsck/k9/testing/MockHelper.kt b/app/testing/src/main/java/com/fsck/k9/testing/MockHelper.kt new file mode 100644 index 0000000..17e9e69 --- /dev/null +++ b/app/testing/src/main/java/com/fsck/k9/testing/MockHelper.kt @@ -0,0 +1,23 @@ +package com.fsck.k9.testing + +import org.mockito.Mockito +import org.mockito.Mockito.mock +import org.mockito.kotlin.KStubbing + +object MockHelper { + @JvmStatic + fun <T> mockBuilder(classToMock: Class<T>): T { + return mock(classToMock) { invocation -> + val mock = invocation.mock + if (invocation.method.returnType.isInstance(mock)) { + mock + } else { + Mockito.RETURNS_DEFAULTS.answer(invocation) + } + } + } + + inline fun <reified T> mockBuilder(stubbing: KStubbing<T>.(T) -> Unit = {}): T { + return mockBuilder(T::class.java).apply { KStubbing(this).stubbing(this) } + } +} diff --git a/app/testing/src/main/java/com/fsck/k9/testing/StringExtensions.kt b/app/testing/src/main/java/com/fsck/k9/testing/StringExtensions.kt new file mode 100644 index 0000000..2e5a461 --- /dev/null +++ b/app/testing/src/main/java/com/fsck/k9/testing/StringExtensions.kt @@ -0,0 +1,3 @@ +package com.fsck.k9.testing + +fun String.removeNewlines(): String = replace("([\\r\\n])".toRegex(), "") |
