-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
composeApp/src/commonTest/kotlin/org/ooni/engine/EngineTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.ooni.engine | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.test.runTest | ||
import org.ooni.engine.models.NetworkType | ||
import org.ooni.engine.models.TaskEvent | ||
import org.ooni.engine.models.TaskOrigin | ||
import org.ooni.engine.models.TaskSettings | ||
import org.ooni.probe.di.Dependencies | ||
import org.ooni.probe.shared.Platform | ||
import org.ooni.probe.shared.PlatformInfo | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class EngineTest { | ||
private val json = Dependencies.buildJson() | ||
private val networkTypeFinder = NetworkTypeFinder { NetworkType.NoInternet } | ||
|
||
@Test | ||
fun startTaskAndGetEvents() = | ||
runTest { | ||
val bridge = TestOonimkallBridge() | ||
bridge.addNextEvents("""{"key":"status.started","value":{}}""") | ||
val engine = buildEngine(bridge) | ||
|
||
val events = | ||
engine.startTask( | ||
name = "web_connectivity", | ||
inputs = listOf("https://ooni.org"), | ||
TaskOrigin.OoniRun, | ||
).toList() | ||
|
||
assertEquals(1, events.size) | ||
assertEquals(TaskEvent.Started, events.first()) | ||
|
||
val settings = json.decodeFromString<TaskSettings>(bridge.lastStartTaskSettingsSerialized!!) | ||
assertEquals("web_connectivity", settings.name) | ||
assertEquals(listOf("https://ooni.org"), settings.inputs) | ||
assertEquals(NetworkType.NoInternet, settings.annotations.networkType) | ||
} | ||
|
||
private fun buildEngine(bridge: OonimkallBridge) = | ||
Engine( | ||
bridge = bridge, | ||
json = json, | ||
baseFilePath = "", | ||
cacheDir = "", | ||
taskEventMapper = TaskEventMapper(networkTypeFinder, json), | ||
networkTypeFinder = networkTypeFinder, | ||
platformInfo = | ||
object : PlatformInfo { | ||
override val version = "1" | ||
override val platform = Platform.Ios | ||
override val osVersion = "1" | ||
override val model = "test" | ||
}, | ||
backgroundDispatcher = Dispatchers.Default, | ||
) | ||
} |
58 changes: 58 additions & 0 deletions
58
composeApp/src/commonTest/kotlin/org/ooni/engine/TestOonimkallBridge.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.ooni.engine | ||
|
||
/* | ||
Bridge implementation to be used in tests | ||
*/ | ||
class TestOonimkallBridge : OonimkallBridge { | ||
// Test helpers | ||
|
||
private val nextEvents = mutableListOf<String>() | ||
|
||
fun addNextEvents(vararg events: String) { | ||
nextEvents.addAll(events) | ||
} | ||
|
||
var lastStartTaskSettingsSerialized: String? = null | ||
private set | ||
|
||
var lastSessionConfig: OonimkallBridge.SessionConfig? = null | ||
private set | ||
|
||
var submitMeasurement: ((String) -> OonimkallBridge.SubmitMeasurementResults)? = null | ||
var checkIn: ((OonimkallBridge.CheckInConfig) -> OonimkallBridge.CheckInResults)? = null | ||
var httpDo: ((OonimkallBridge.HTTPRequest) -> OonimkallBridge.HTTPResponse)? = null | ||
|
||
// Base implementation | ||
|
||
override fun startTask(settingsSerialized: String): OonimkallBridge.Task { | ||
lastStartTaskSettingsSerialized = settingsSerialized | ||
return object : OonimkallBridge.Task { | ||
override fun interrupt() {} | ||
|
||
override fun isDone() = nextEvents.isEmpty() | ||
|
||
override fun waitForNextEvent(): String { | ||
return nextEvents.removeAt(0) | ||
} | ||
} | ||
} | ||
|
||
override fun newSession(sessionConfig: OonimkallBridge.SessionConfig): OonimkallBridge.Session { | ||
lastSessionConfig = sessionConfig | ||
return Session() | ||
} | ||
|
||
class Session : OonimkallBridge.Session { | ||
override fun submitMeasurement(measurement: String): OonimkallBridge.SubmitMeasurementResults { | ||
return submitMeasurement(measurement) | ||
} | ||
|
||
override fun checkIn(config: OonimkallBridge.CheckInConfig): OonimkallBridge.CheckInResults { | ||
return checkIn(config) | ||
} | ||
|
||
override fun httpDo(request: OonimkallBridge.HTTPRequest): OonimkallBridge.HTTPResponse { | ||
return httpDo(request) | ||
} | ||
} | ||
} |