Skip to content

Commit

Permalink
Add label to Airplane Mode commands, plus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fishbowler committed Jun 2, 2024
1 parent be8b8d9 commit 297256d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package maestro.orchestra

import com.fasterxml.jackson.annotation.JsonProperty
import maestro.KeyCode
import maestro.Point
import maestro.ScrollDirection
Expand Down Expand Up @@ -871,30 +870,33 @@ data class StopRecordingCommand(
}

enum class AirplaneValue {
@JsonProperty("enabled")
Enable,
@JsonProperty("disabled")
Disable,
}

data class SetAirplaneModeCommand(
val value: AirplaneValue,
val label: String? = null,
) : Command {
override fun description(): String {
return when (value) {
AirplaneValue.Enable -> "Enable airplane mode"
AirplaneValue.Disable -> "Disable airplane mode"
}
return label
?: when (value) {
AirplaneValue.Enable -> "Enable airplane mode"
AirplaneValue.Disable -> "Disable airplane mode"
}
}

override fun evaluateScripts(jsEngine: JsEngine): Command {
return this
}
}

object ToggleAirplaneModeCommand : Command {
data class ToggleAirplaneModeCommand(
val label: String? = null,
) : Command {

override fun description(): String {
return "Toggle airplane mode"
return label ?: "Toggle airplane mode"
}

override fun evaluateScripts(jsEngine: JsEngine): Command {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ data class YamlFluentCommand(
val tapRepeat = TapRepeat(2, delay)
listOf(tapCommand(doubleTapOn, tapRepeat = tapRepeat))
}
setAirplaneMode != null -> listOf(MaestroCommand(SetAirplaneModeCommand(setAirplaneMode.value)))
toggleAirplaneMode != null -> listOf(MaestroCommand(ToggleAirplaneModeCommand))
setAirplaneMode != null -> listOf(MaestroCommand(SetAirplaneModeCommand(setAirplaneMode.value, setAirplaneMode.label)))
toggleAirplaneMode != null -> listOf(MaestroCommand(ToggleAirplaneModeCommand(toggleAirplaneMode.label)))
else -> throw SyntaxError("Invalid command: No mapping provided for $this")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package maestro.orchestra.yaml

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.TreeNode
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import maestro.orchestra.AirplaneValue

@JsonDeserialize(using = YamlSetAirplaneModeDeserializer::class)
data class YamlSetAirplaneMode(
val value: AirplaneValue,
val label: String? = null,
) {
companion object {
@JvmStatic
Expand All @@ -14,3 +22,50 @@ data class YamlSetAirplaneMode(
}
}
}

class YamlSetAirplaneModeDeserializer : JsonDeserializer<YamlSetAirplaneMode>() {

override fun deserialize(parser: JsonParser, ctxt: DeserializationContext): YamlSetAirplaneMode {
val mapper = (parser.codec as ObjectMapper)
val root: TreeNode = mapper.readTree(parser)
val input = root.fieldNames().asSequence().toList()
val label = getLabel(root)
when {
input.contains("value") -> {
val parsedValue = root.get("value").toString().replace("\"", "")
val returnValue = when (parsedValue) {
"enabled" -> AirplaneValue.Enable
"disabled" -> AirplaneValue.Disable
else -> throwInvalidInputException(input)
}
return YamlSetAirplaneMode(returnValue, label)
}
(root.isValueNode && root.toString().contains("enabled")) -> {
return YamlSetAirplaneMode(AirplaneValue.Enable, label)
}
(root.isValueNode && root.toString().contains("disabled")) -> {
return YamlSetAirplaneMode(AirplaneValue.Disable, label)
}
else -> throwInvalidInputException(input)
}
}

private fun throwInvalidInputException(input: List<String>): Nothing {
throw IllegalArgumentException(
"setAirplaneMode command takes either: \n" +
"\t1. enabled: To enable airplane mode\n" +
"\t2. disabled: To disable airplane mode\n" +
"\t3. value: To set airplane mode to a specific value (enabled or disabled) \n" +
"It seems you provided invalid input with: $input"
)
}

private fun getLabel(root: TreeNode): String? {
return if (root.path("label").isMissingNode) {
null
} else {
root.path("label").toString().replace("\"", "")
}
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package maestro.orchestra.yaml

class YamlToggleAirplaneMode
data class YamlToggleAirplaneMode(
val label: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,13 @@ internal class YamlCommandReaderTest {
mediaPaths = listOf(Paths.get("build/resources/test/YamlCommandReaderTest/023_image.png").toAbsolutePath().toString()),
label = "Add a picture to the device"
),
SetAirplaneModeCommand(
value = AirplaneValue.Enable,
label = "Turn on airplane mode for testing"
),
ToggleAirplaneModeCommand(
label = "Toggle airplane mode for testing"
),
RepeatCommand(
condition = Condition(visible = ElementSelector(textRegex = "Some important text")),
commands = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,14 @@ appId: com.example.app
direction: DOWN
label: "Swipe down a bit"
- addMedia:
files:
- "023_image.png"
label: "Add a picture to the device"
files:
- "023_image.png"
label: "Add a picture to the device"
- setAirplaneMode:
value: enabled
label: "Turn on airplane mode for testing"
- toggleAirplaneMode:
label: "Toggle airplane mode for testing"

# Repeats
- repeat:
Expand Down

0 comments on commit 297256d

Please sign in to comment.