From 452756e3d33798df669577b1ceaf83f517620261 Mon Sep 17 00:00:00 2001 From: Anthony Date: Wed, 1 May 2024 17:49:10 +0100 Subject: [PATCH] feat(connector): add tests and run via XPC --- .../desktopMain/kotlin/tab/test/TestRun.kt | 80 ++++++++++++++++--- .../anthonyberg/connector/shared/xpc/XPC.kt | 17 ++++ 2 files changed, 87 insertions(+), 10 deletions(-) diff --git a/connector/composeApp/src/desktopMain/kotlin/tab/test/TestRun.kt b/connector/composeApp/src/desktopMain/kotlin/tab/test/TestRun.kt index 1d64bf3..102ec43 100644 --- a/connector/composeApp/src/desktopMain/kotlin/tab/test/TestRun.kt +++ b/connector/composeApp/src/desktopMain/kotlin/tab/test/TestRun.kt @@ -1,37 +1,50 @@ package tab.test import androidx.compose.foundation.VerticalScrollbar -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.fillMaxHeight -import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.rememberScrollbarAdapter import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.Check import androidx.compose.material.icons.outlined.Info -import androidx.compose.material3.HorizontalDivider -import androidx.compose.material3.Icon -import androidx.compose.material3.ListItem -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable +import androidx.compose.material.icons.outlined.Warning +import androidx.compose.material3.* +import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp import cafe.adriel.voyager.core.screen.Screen import io.anthonyberg.connector.shared.entity.Action +import io.anthonyberg.connector.shared.xpc.XPC +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch class TestRun ( private val actions: List ) : Screen { + + private var tested = mutableStateListOf() + @Composable override fun Content() { val lazyState = rememberLazyListState(0) + var running by remember { mutableStateOf(false) } + val scope = rememberCoroutineScope() Column( modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally, ) { + // Progress Indicator + if (running) { + LinearProgressIndicator( + progress = { tested.size / actions.size.toFloat() }, + modifier = Modifier.fillMaxWidth() + ) + } + Box( modifier = Modifier.fillMaxWidth(0.7F) ) { @@ -51,6 +64,14 @@ class TestRun ( ) } } + + if (!running and (tested.size == 0)) { + scope.launch { + running = true + runSteps() + running = false + } + } } @Composable @@ -58,9 +79,48 @@ class TestRun ( ListItem( headlineContent = { Text(action.type) }, supportingContent = { Text("Goal: ${action.goal}") }, - leadingContent = { Icon(Icons.Outlined.Info, null) } + leadingContent = { + if (action.step > tested.size) { + Icon(Icons.Outlined.Info, "Waiting") + } else if (action.step == tested.size) { + CircularProgressIndicator( + modifier = Modifier.size(24.dp) + ) + } else { + when (tested[action.step]) { + true -> Icon(Icons.Outlined.Check, "Passed") + false -> Icon( + imageVector = Icons.Outlined.Warning, + contentDescription = "Failed", + tint = MaterialTheme.colorScheme.error + ) + } + } + } ) HorizontalDivider() } + + private suspend fun runSteps() { + val xpc = XPC() + + // Checks if the simulator is running before running the other tests + if (!xpc.connected()) { + for (action in actions) { + tested.add(false) + } + return + } + + for (action in actions) { + delay(1000L) + + // TODO add try catch + val result = xpc.runChecklist(action.type, action.goal.toInt()) + + // TODO add more detailed results + tested.add(result) + } + } } diff --git a/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/xpc/XPC.kt b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/xpc/XPC.kt index 703f86f..9d41d6c 100644 --- a/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/xpc/XPC.kt +++ b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/xpc/XPC.kt @@ -26,4 +26,21 @@ class XPC { return false } } + + /** + * Sets a dataref in X-Plane to the set goal + * + * @param dref Dataref name in X-Plane to change the value for + * @param goal The value that should be set for the dataref in X-Plane + * + * @return `true` if successfully set, `false` otherwise + */ + @Throws(SocketException::class, IOException::class) + fun runChecklist(dref: String, goal: Int) : Boolean { + xpc.sendDREF(dref, goal.toFloat()) + + val result = xpc.getDREF(dref) + + return goal.toFloat() == result[0] + } }