Repo created
This commit is contained in:
parent
a629de6271
commit
3cef7c5092
2161 changed files with 246605 additions and 2 deletions
17
app/testing/build.gradle.kts
Normal file
17
app/testing/build.gradle.kts
Normal 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"
|
||||
}
|
||||
17
app/testing/src/main/java/com/fsck/k9/RobolectricTest.kt
Normal file
17
app/testing/src/main/java/com/fsck/k9/RobolectricTest.kt
Normal 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()
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
23
app/testing/src/main/java/com/fsck/k9/testing/MockHelper.kt
Normal file
23
app/testing/src/main/java/com/fsck/k9/testing/MockHelper.kt
Normal 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) }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
package com.fsck.k9.testing
|
||||
|
||||
fun String.removeNewlines(): String = replace("([\\r\\n])".toRegex(), "")
|
||||
Loading…
Add table
Add a link
Reference in a new issue