|
| 1 | +package org.owasp.mastestapp |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import androidx.activity.ComponentActivity |
| 5 | +import androidx.activity.compose.setContent |
| 6 | +import androidx.activity.enableEdgeToEdge |
| 7 | +import androidx.compose.foundation.layout.Column |
| 8 | +import androidx.compose.foundation.layout.padding |
| 9 | +import androidx.compose.material3.Button |
| 10 | +import androidx.compose.material3.DropdownMenu |
| 11 | +import androidx.compose.material3.DropdownMenuItem |
| 12 | +import androidx.compose.material3.Text |
| 13 | +import androidx.compose.runtime.Composable |
| 14 | +import androidx.compose.runtime.getValue |
| 15 | +import androidx.compose.runtime.mutableStateOf |
| 16 | +import androidx.compose.runtime.remember |
| 17 | +import androidx.compose.runtime.setValue |
| 18 | +import androidx.compose.ui.Modifier |
| 19 | +import androidx.compose.ui.graphics.Color |
| 20 | +import androidx.compose.ui.platform.LocalContext |
| 21 | +import androidx.compose.ui.platform.testTag |
| 22 | +import androidx.compose.ui.text.AnnotatedString |
| 23 | +import androidx.compose.ui.text.SpanStyle |
| 24 | +import androidx.compose.ui.text.buildAnnotatedString |
| 25 | +import androidx.compose.ui.text.font.FontFamily |
| 26 | +import androidx.compose.ui.text.withStyle |
| 27 | +import androidx.compose.ui.tooling.preview.Preview |
| 28 | +import androidx.compose.ui.unit.dp |
| 29 | +import androidx.compose.ui.unit.sp |
| 30 | +import kotlinx.serialization.json.Json |
| 31 | +import kotlinx.serialization.json.JsonArray |
| 32 | +import kotlinx.serialization.json.decodeFromJsonElement |
| 33 | + |
| 34 | +const val MASTG_TEXT_TAG = "mastgTestText" |
| 35 | + |
| 36 | +class MainActivity : ComponentActivity() { |
| 37 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 38 | + super.onCreate(savedInstanceState) |
| 39 | + enableEdgeToEdge() |
| 40 | + setContent { |
| 41 | + MainScreen() |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fun UpdateDisplayString( |
| 47 | + defaultMessage: String, |
| 48 | + result: String |
| 49 | +): AnnotatedString { |
| 50 | + return buildAnnotatedString { |
| 51 | + append(defaultMessage) |
| 52 | + try { |
| 53 | + val jsonArrayFromString = Json.parseToJsonElement(result) as JsonArray |
| 54 | + val demoResults = jsonArrayFromString.map { Json.decodeFromJsonElement<DemoResult>(it) } |
| 55 | + |
| 56 | + for (demoResult in demoResults) { |
| 57 | + when (demoResult.status) { |
| 58 | + Status.PASS -> { |
| 59 | + withStyle(style = SpanStyle(color = Color.Green)) { |
| 60 | + append("MASTG-DEMO-${demoResult.demoId} demonstrated a successful test:\n${demoResult.message}\n\n") |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + Status.FAIL -> { |
| 65 | + withStyle(style = SpanStyle(color = Color(0xFFFF9800))) { |
| 66 | + append("MASTG-DEMO-${demoResult.demoId} demonstrated a failed test:\n${demoResult.message}\n\n") |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Status.ERROR -> { |
| 71 | + withStyle(style = SpanStyle(color = Color.Red)) { |
| 72 | + append("MASTG-DEMO-${demoResult.demoId} failed:\n${demoResult.message}\n\n") |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } catch (_: Exception) { |
| 78 | + // not a valid set of DemoResult, so print the result without any parsing |
| 79 | + append(result) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +@Preview |
| 86 | +@Composable |
| 87 | +fun MainScreen() { |
| 88 | + val defaultMessage = "Click \"Start\" to send the data.\n\n" |
| 89 | + var displayString by remember { mutableStateOf(buildAnnotatedString { append(defaultMessage) }) } |
| 90 | + var selectedBloodType by remember { mutableStateOf("") } |
| 91 | + val context = LocalContext.current |
| 92 | + val mastgTestClass = MastgTest(context) |
| 93 | + // By default run the test in a separate thread, this ensures that network tests such as those using SSLSocket work properly. |
| 94 | + // However, some tests which interact with UI elements need to run on the main thread. |
| 95 | + // You can set shouldRunInMainThread = true in MastgTest.kt for those tests. |
| 96 | + val runInMainThread = MastgTest::class.members |
| 97 | + .find { it.name == "shouldRunInMainThread" } |
| 98 | + ?.call(mastgTestClass) as? Boolean ?: false |
| 99 | + |
| 100 | + BaseScreen( |
| 101 | + onStartClick = { |
| 102 | + if (runInMainThread) { |
| 103 | + val result = mastgTestClass.mastgTest(selectedBloodType) |
| 104 | + displayString = UpdateDisplayString(defaultMessage, result) |
| 105 | + } else { |
| 106 | + Thread { |
| 107 | + val result = mastgTestClass.mastgTest(selectedBloodType) |
| 108 | + android.os.Handler(android.os.Looper.getMainLooper()).post { |
| 109 | + displayString = UpdateDisplayString(defaultMessage, result) |
| 110 | + } |
| 111 | + }.start() |
| 112 | + } |
| 113 | + } |
| 114 | + ) { |
| 115 | + Column(modifier = Modifier.padding(16.dp)) { |
| 116 | + // Normal visible selection UI: list of radio buttons for blood types |
| 117 | + val bloodTypes = listOf("A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-") |
| 118 | + |
| 119 | + var expanded by remember { mutableStateOf(false) } |
| 120 | + |
| 121 | + Button(onClick = { expanded = !expanded }) { |
| 122 | + Text("Select Blood Type") |
| 123 | + } |
| 124 | + DropdownMenu( |
| 125 | + expanded = expanded, |
| 126 | + onDismissRequest = { expanded = false } |
| 127 | + ) { |
| 128 | + bloodTypes.forEach { |
| 129 | + DropdownMenuItem( |
| 130 | + text = { Text(it) }, |
| 131 | + onClick = { |
| 132 | + selectedBloodType = it |
| 133 | + expanded = false |
| 134 | + } |
| 135 | + ) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (selectedBloodType.isNotEmpty()) { |
| 140 | + Text( |
| 141 | + modifier = Modifier.padding(vertical = 16.dp), |
| 142 | + color = Color.White, |
| 143 | + text = "Selected Blood Type: $selectedBloodType" |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + Text( |
| 148 | + modifier = Modifier |
| 149 | + .padding(top = 8.dp) |
| 150 | + .testTag(MASTG_TEXT_TAG), |
| 151 | + text = displayString, |
| 152 | + color = Color.White, |
| 153 | + fontSize = 16.sp, |
| 154 | + fontFamily = FontFamily.Monospace |
| 155 | + ) |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments