Repo created

This commit is contained in:
Fr4nz D13trich 2025-11-24 18:55:42 +01:00
parent a629de6271
commit 3cef7c5092
2161 changed files with 246605 additions and 2 deletions

View file

@ -0,0 +1,17 @@
plugins {
id(ThunderbirdPlugins.Library.android)
}
dependencies {
implementation(projects.app.core)
api(libs.junit)
api(libs.robolectric)
api(libs.koin.core)
api(libs.mockito.core)
api(libs.mockito.kotlin)
}
android {
namespace = "com.fsck.k9.testing"
}

View file

@ -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()

View file

@ -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)
}
}
}

View file

@ -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) }
}
}

View file

@ -0,0 +1,3 @@
package com.fsck.k9.testing
fun String.removeNewlines(): String = replace("([\\r\\n])".toRegex(), "")