Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: test(KMP): use mockative instead of mockito #5706

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ plugins {
id("com.android.application")
kotlin("android")
kotlin("plugin.serialization") version "1.9.22"
kotlin("plugin.allopen") version "1.9.22"
id("com.google.devtools.ksp") version "1.9.22-1.0.16"
}

android {
Expand Down Expand Up @@ -83,6 +85,19 @@ android {
namespace = "de.westnordost.streetcomplete"
}

val taskIsRunningTest = gradle.startParameter.taskNames
.any { it == "check" || it.startsWith("test") || it.contains("Test") }

//if (taskIsRunningTest) {
allOpen {
annotation("de.westnordost.streetcomplete.util.Mockable")
}
//}

// allOpen {
// annotation("kotlin.Metadata")
// }

val keystorePropertiesFile = rootProject.file("keystore.properties")
if (keystorePropertiesFile.exists()) {
val props = Properties()
Expand Down Expand Up @@ -116,6 +131,12 @@ dependencies {
testImplementation("org.mockito:mockito-core:$mockitoVersion")
testImplementation("org.mockito:mockito-inline:$mockitoVersion")
testImplementation(kotlin("test"))
testImplementation("io.mockative:mockative:2.2.2")
configurations
.filter { it.name.startsWith("ksp") && it.name.contains("Test") }
.forEach {
add(it.name, "io.mockative:mockative-processor:2.2.2")
}

androidTestImplementation("androidx.test:runner:1.5.2")
androidTestImplementation("androidx.test:rules:1.5.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package de.westnordost.streetcomplete.data

import de.westnordost.streetcomplete.data.osm.edits.EditType
import de.westnordost.streetcomplete.util.Mockable

@Mockable
class AllEditTypes(
registries: List<ObjectTypeRegistry<out EditType>>
) : AbstractCollection<EditType>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package de.westnordost.streetcomplete.data.download.tiles

import de.westnordost.streetcomplete.ApplicationConstants
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.Mockable
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds

@Mockable
class DownloadedTilesController(
private val dao: DownloadedTilesDao
) : DownloadedTilesSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import de.westnordost.streetcomplete.data.download.tiles.DownloadedTilesTable.Co
import de.westnordost.streetcomplete.data.download.tiles.DownloadedTilesTable.Columns.X
import de.westnordost.streetcomplete.data.download.tiles.DownloadedTilesTable.Columns.Y
import de.westnordost.streetcomplete.data.download.tiles.DownloadedTilesTable.NAME
import de.westnordost.streetcomplete.util.Mockable
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds

/** Keeps info in which areas things have been downloaded already in a tile grid */
@Mockable
class DownloadedTilesDao(private val db: Database) {

/** Persist that the given tile range has been downloaded (now) */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import de.westnordost.streetcomplete.data.osmnotes.notequests.OsmNoteQuestHidden
import de.westnordost.streetcomplete.data.osmnotes.notequests.OsmNoteQuestsHiddenController
import de.westnordost.streetcomplete.data.osmnotes.notequests.OsmNoteQuestsHiddenSource
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.Mockable
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds

/** All edits done by the user in one place: Edits made on notes, on map data, hidings of quests */
@Mockable
class EditHistoryController(
private val elementEditsController: ElementEditsController,
private val noteEditsController: NoteEditsController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import de.westnordost.streetcomplete.data.elementfilter.withOptionalUnitToDouble
import de.westnordost.streetcomplete.data.osm.mapdata.Element
import de.westnordost.streetcomplete.osm.getLastCheckDateKeys
import de.westnordost.streetcomplete.osm.toCheckDate
import de.westnordost.streetcomplete.util.Mockable
import de.westnordost.streetcomplete.util.ktx.toLocalDate
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate

@Mockable
sealed interface ElementFilter : Matcher<Element> {
abstract override fun toString(): String
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.westnordost.streetcomplete.data.meta

import android.content.res.AssetManager

class AndroidAssetAcess(private val assetManager: AssetManager) : AssetAccess {
override fun list(basepath: String): Array<out String>? {
return assetManager.list(basepath)
}

override fun open(s: String): java.io.InputStream {
return assetManager.open(s)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.westnordost.streetcomplete.data.meta

interface AssetAccess {
abstract fun list(basepath: String): Array<out String>?
abstract fun open(s: String): java.io.InputStream
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
import com.charleskorn.kaml.decodeFromStream
import de.westnordost.countryboundaries.CountryBoundaries
import de.westnordost.streetcomplete.util.Mockable
import java.io.File
import java.io.SequenceInputStream

class CountryInfos(private val assetManager: AssetManager) {
@Mockable
class CountryInfos(private val assetManager: AssetAccess) {
private val yaml = Yaml(configuration = YamlConfiguration(
strictMode = false, // ignore unknown properties
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package de.westnordost.streetcomplete.data.osm.created_elements

import de.westnordost.streetcomplete.data.osm.mapdata.ElementKey
import de.westnordost.streetcomplete.data.osm.mapdata.ElementType
import de.westnordost.streetcomplete.util.Mockable

@Mockable
class CreatedElementsController(
private val db: CreatedElementsDao
) : CreatedElementsSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import de.westnordost.streetcomplete.data.osm.created_elements.CreatedElementsTa
import de.westnordost.streetcomplete.data.osm.created_elements.CreatedElementsTable.NAME
import de.westnordost.streetcomplete.data.osm.mapdata.ElementKey
import de.westnordost.streetcomplete.data.osm.mapdata.ElementType
import de.westnordost.streetcomplete.util.Mockable

/** Persists the keys of the elements created by this app already uploaded to the OSM API */
@Mockable
class CreatedElementsDao(private val db: Database) {

fun putAll(entries: Collection<ElementKey>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import de.westnordost.streetcomplete.data.osm.edits.EditElementsTable.Columns.EL
import de.westnordost.streetcomplete.data.osm.edits.EditElementsTable.NAME
import de.westnordost.streetcomplete.data.osm.mapdata.ElementKey
import de.westnordost.streetcomplete.data.osm.mapdata.ElementType
import de.westnordost.streetcomplete.util.Mockable

@Mockable
class EditElementsDao(private val db: Database) {

fun put(id: Long, elementKeys: List<ElementKey>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import de.westnordost.streetcomplete.data.edithistory.Edit
import de.westnordost.streetcomplete.data.edithistory.ElementEditKey
import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.util.Mockable

data class ElementEdit(
/** (row) id of the edit. 0 if not inserted into DB yet */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.ElementKey
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataUpdates
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.Mockable
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds
import de.westnordost.streetcomplete.util.logs.Log

@Mockable
class ElementEditsController(
private val editsDB: ElementEditsDao,
private val editElementsDB: EditElementsDao,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import de.westnordost.streetcomplete.data.osm.edits.move.RevertMoveNodeAction
import de.westnordost.streetcomplete.data.osm.edits.split_way.SplitWayAction
import de.westnordost.streetcomplete.data.osm.edits.update_tags.RevertUpdateElementTagsAction
import de.westnordost.streetcomplete.data.osm.edits.update_tags.UpdateElementTagsAction
import de.westnordost.streetcomplete.util.Mockable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass

@Mockable
class ElementEditsDao(
private val db: Database,
private val allEditTypes: AllEditTypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package de.westnordost.streetcomplete.data.osm.edits

import de.westnordost.streetcomplete.data.osm.mapdata.ElementKey
import de.westnordost.streetcomplete.data.osm.mapdata.ElementType
import de.westnordost.streetcomplete.util.Mockable

/** Provides stable element ids for the creation of new elements */
@Mockable
class ElementIdProvider(elementKeys: Collection<ElementKey>) {
private val nodeIds: MutableList<Long>
private val wayIds: MutableList<Long>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import de.westnordost.streetcomplete.data.osm.edits.ElementIdProviderTable.NAME
import de.westnordost.streetcomplete.data.osm.mapdata.ElementIdUpdate
import de.westnordost.streetcomplete.data.osm.mapdata.ElementKey
import de.westnordost.streetcomplete.data.osm.mapdata.ElementType
import de.westnordost.streetcomplete.util.Mockable

/** Assigns new element ids for ElementEditActions that create new elements */
@Mockable
class ElementIdProviderDao(private val db: Database) {

fun assign(editId: Long, nodeCount: Int, wayCount: Int, relationCount: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ import de.westnordost.streetcomplete.data.osm.mapdata.Way
import de.westnordost.streetcomplete.data.osm.mapdata.key
import de.westnordost.streetcomplete.data.upload.ConflictException
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.Mockable
import de.westnordost.streetcomplete.util.math.contains
import de.westnordost.streetcomplete.util.math.intersect

/** Source for map data. It combines the original data downloaded with the edits made.
*
* This class is threadsafe.
* */
@Mockable
class MapDataWithEditsSource internal constructor(
private val mapDataController: MapDataController,
private val elementEditsController: ElementEditsController,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.westnordost.streetcomplete.data.osm.edits.update_tags

import de.westnordost.streetcomplete.util.Mockable
import kotlinx.serialization.Serializable

@Serializable
Expand All @@ -15,6 +16,7 @@ sealed class StringMapEntryChange {
}

@Serializable
@Mockable
data class StringMapEntryAdd(override val key: String, val value: String) : StringMapEntryChange() {

override fun toString() = "ADD \"$key\"=\"$value\""
Expand All @@ -25,6 +27,7 @@ data class StringMapEntryAdd(override val key: String, val value: String) : Stri
}

@Serializable
@Mockable
data class StringMapEntryModify(override val key: String, val valueBefore: String, val value: String) : StringMapEntryChange() {

override fun toString() = "MODIFY \"$key\"=\"$valueBefore\" -> \"$key\"=\"$value\""
Expand All @@ -35,6 +38,7 @@ data class StringMapEntryModify(override val key: String, val valueBefore: Strin
}

@Serializable
@Mockable
data class StringMapEntryDelete(override val key: String, val valueBefore: String) : StringMapEntryChange() {

override fun toString() = "DELETE \"$key\"=\"$valueBefore\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import de.westnordost.streetcomplete.data.osm.mapdata.MapDataChanges
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataController
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataUpdates
import de.westnordost.streetcomplete.data.upload.ConflictException
import de.westnordost.streetcomplete.util.Mockable

@Mockable
class ElementEditUploader(
private val changesetManager: OpenChangesetsManager,
private val mapDataApi: MapDataApi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package de.westnordost.streetcomplete.data.osm.edits.upload

import com.russhwolf.settings.ObservableSettings
import de.westnordost.streetcomplete.Prefs
import de.westnordost.streetcomplete.util.Mockable
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds

@Mockable
class LastEditTimeStore(private val prefs: ObservableSettings) {

fun touch() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import androidx.work.ExistingWorkPolicy.REPLACE
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import de.westnordost.streetcomplete.util.Mockable
import java.util.concurrent.TimeUnit

@Mockable
class ChangesetAutoCloser(private val context: Context) {

fun enqueue(delayInMilliseconds: Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import de.westnordost.streetcomplete.data.osm.edits.upload.changesets.OpenChange
import de.westnordost.streetcomplete.data.osm.edits.upload.changesets.OpenChangesetsTable.Columns.SOURCE
import de.westnordost.streetcomplete.data.osm.edits.upload.changesets.OpenChangesetsTable.NAME
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.util.Mockable

/** Keep track of changesets and the date of the last change that has been made to them */
@Mockable
class OpenChangesetsDao(private val db: Database) {

fun getAll(): Collection<OpenChangeset> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import de.westnordost.streetcomplete.data.osm.edits.upload.LastEditTimeStore
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataApi
import de.westnordost.streetcomplete.data.upload.ConflictException
import de.westnordost.streetcomplete.util.Mockable
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds
import de.westnordost.streetcomplete.util.logs.Log
import de.westnordost.streetcomplete.util.math.distanceTo
import java.util.Locale

/** Manages the creation and reusage of changesets */
@Mockable
class OpenChangesetsManager(
private val mapDataApi: MapDataApi,
private val openChangesetsDB: OpenChangesetsDao,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package de.westnordost.streetcomplete.data.osm.geometry

import de.westnordost.streetcomplete.data.osm.mapdata.BoundingBox
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.util.Mockable
import de.westnordost.streetcomplete.util.math.enclosingBoundingBox
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/** Information on the geometry of a quest */
@Serializable
@Mockable
sealed class ElementGeometry {
abstract val center: LatLon
// the bbox should not be serialized, his is why the bounds cannot be a (computed) property directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import de.westnordost.streetcomplete.data.osm.mapdata.MapData
import de.westnordost.streetcomplete.data.osm.mapdata.Node
import de.westnordost.streetcomplete.data.osm.mapdata.Relation
import de.westnordost.streetcomplete.data.osm.mapdata.Way
import de.westnordost.streetcomplete.util.Mockable
import de.westnordost.streetcomplete.util.ktx.isArea
import de.westnordost.streetcomplete.util.math.centerPointOfPolygon
import de.westnordost.streetcomplete.util.math.centerPointOfPolyline
import de.westnordost.streetcomplete.util.math.isRingDefinedClockwise
import kotlin.collections.ArrayList

/** Creates an ElementGeometry from an element and a collection of positions. */
@Mockable
class ElementGeometryCreator {

/** Create an ElementGeometry from any element, using the given MapData to find the positions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import de.westnordost.streetcomplete.data.osm.mapdata.ElementType.NODE
import de.westnordost.streetcomplete.data.osm.mapdata.ElementType.RELATION
import de.westnordost.streetcomplete.data.osm.mapdata.ElementType.WAY
import de.westnordost.streetcomplete.data.osm.mapdata.NodeDao
import de.westnordost.streetcomplete.util.Mockable

/** Stores the geometry of elements. Actually, stores nothing, but delegates the work to
* WayGeometryDao and RelationDao. Node geometry is never stored separately, but created
* from node position. */
@Mockable
class ElementGeometryDao(
private val nodeDao: NodeDao,
private val wayGeometryDao: WayGeometryDao,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.westnordost.streetcomplete.data.osm.geometry

import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.util.Mockable
import kotlinx.io.Buffer
import kotlinx.io.readByteArray
import kotlinx.io.readDouble
Expand All @@ -18,6 +19,7 @@ import kotlinx.io.writeDouble
* ... for every single coordinate that is part of this particular geometry. That's more than 2
* times the size as when using this method.
* */
@Mockable
class PolylinesSerializer {

fun serialize(polylines: List<List<LatLon>>): ByteArray {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import de.westnordost.streetcomplete.data.osm.geometry.RelationGeometryTable.Col
import de.westnordost.streetcomplete.data.osm.geometry.RelationGeometryTable.NAME
import de.westnordost.streetcomplete.data.osm.mapdata.ElementType
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.util.Mockable

/** Stores the geometry of relations */
@Mockable
class RelationGeometryDao(
private val db: Database,
private val polylinesSerializer: PolylinesSerializer,
Expand Down
Loading