-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package edu.stanford.spezi.core.design | ||
|
||
import androidx.compose.runtime.mutableStateOf | ||
Check warning on line 3 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt#L3 <detekt.NoUnusedImports>
Raw output
Check warning on line 3 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt#L3 <detekt.UnusedImports>
Raw output
|
||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onNodeWithText | ||
Check warning on line 5 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt#L5 <detekt.NoUnusedImports>
Raw output
Check warning on line 5 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt#L5 <detekt.UnusedImports>
Raw output
|
||
import edu.stanford.spezi.core.design.composables.FocusValidationRules | ||
import edu.stanford.spezi.core.design.simulator.FocusValidationRulesSimulator | ||
import edu.stanford.spezi.core.design.views.validation.state.ValidationContext | ||
Check warning on line 8 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt#L8 <detekt.NoUnusedImports>
Raw output
Check warning on line 8 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt#L8 <detekt.UnusedImports>
Raw output
|
||
import kotlinx.coroutines.delay | ||
Check warning on line 9 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt#L9 <detekt.NoUnusedImports>
Raw output
Check warning on line 9 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt#L9 <detekt.UnusedImports>
Raw output
|
||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class ValidationTest { | ||
|
||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
@Before | ||
fun init() { | ||
composeTestRule.setContent { | ||
FocusValidationRules() | ||
} | ||
} | ||
|
||
@Test | ||
fun testValidationWithFocus() { | ||
focusValidationRules { | ||
assertHasEngines(true) | ||
assertInputValid(false) | ||
assertPasswordMessageExists(false) | ||
assertEmptyMessageExists(false) | ||
clickValidateButton() | ||
assertLastState(false) | ||
assertPasswordMessageExists(true) | ||
// assertEmptyMessageExists(true) | ||
} | ||
} | ||
|
||
private fun focusValidationRules(block: FocusValidationRulesSimulator.() -> Unit) { | ||
FocusValidationRulesSimulator(composeTestRule).apply(block) | ||
} | ||
|
||
Check warning on line 43 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/ValidationTest.kt#L43 <detekt.NoBlankLineBeforeRbrace>
Raw output
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package edu.stanford.spezi.core.design.composables | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import edu.stanford.spezi.core.design.component.StringResource | ||
import edu.stanford.spezi.core.design.views.validation.Validate | ||
import edu.stanford.spezi.core.design.views.validation.ValidationRule | ||
import edu.stanford.spezi.core.design.views.validation.asciiLettersOnly | ||
import edu.stanford.spezi.core.design.views.validation.mediumPassword | ||
import edu.stanford.spezi.core.design.views.validation.minimalEmail | ||
import edu.stanford.spezi.core.design.views.validation.minimalPassword | ||
import edu.stanford.spezi.core.design.views.validation.nonEmpty | ||
import edu.stanford.spezi.core.design.views.validation.strongPassword | ||
import edu.stanford.spezi.core.design.views.validation.unicodeLettersOnly | ||
import edu.stanford.spezi.core.design.views.validation.views.VerifiableTextField | ||
|
||
@Composable | ||
fun DefaultValidationRules() { | ||
val input = remember { mutableStateOf("") } | ||
val rules = remember { | ||
listOf( | ||
ValidationRule.nonEmpty, | ||
ValidationRule.unicodeLettersOnly, | ||
ValidationRule.asciiLettersOnly, | ||
ValidationRule.minimalEmail, | ||
ValidationRule.minimalPassword, | ||
ValidationRule.mediumPassword, | ||
ValidationRule.strongPassword | ||
) | ||
} | ||
Validate(input.value, rules) { | ||
VerifiableTextField( | ||
StringResource("Field"), | ||
text = input | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package edu.stanford.spezi.core.design.composables | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Switch | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
Check warning on line 11 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/composables/FocusValidationRules.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/composables/FocusValidationRules.kt#L11 <detekt.NoUnusedImports>
Raw output
Check warning on line 11 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/composables/FocusValidationRules.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/composables/FocusValidationRules.kt#L11 <detekt.UnusedImports>
Raw output
|
||
import edu.stanford.spezi.core.design.component.StringResource | ||
import edu.stanford.spezi.core.design.views.validation.Validate | ||
import edu.stanford.spezi.core.design.views.validation.ValidationRule | ||
import edu.stanford.spezi.core.design.views.validation.minimalPassword | ||
import edu.stanford.spezi.core.design.views.validation.nonEmpty | ||
import edu.stanford.spezi.core.design.views.validation.state.ReceiveValidation | ||
import edu.stanford.spezi.core.design.views.validation.state.ValidationContext | ||
import edu.stanford.spezi.core.design.views.validation.views.VerifiableTextField | ||
|
||
enum class Field { | ||
INPUT, NON_EMPTY_INPUT | ||
} | ||
|
||
@Composable | ||
fun FocusValidationRules() { | ||
val input = remember { mutableStateOf("") } | ||
val nonEmptyInput = remember { mutableStateOf("") } | ||
val validationContext = remember { mutableStateOf(ValidationContext()) } | ||
val lastValid = remember { mutableStateOf<Boolean?>(null) } | ||
val switchFocus = remember { mutableStateOf(false) } | ||
|
||
ReceiveValidation(validationContext) { | ||
Column { | ||
Text("Has Engines: ${if (!validationContext.value.isEmpty) "Yes" else "No"}") | ||
Text("Input Valid: ${if (validationContext.value.allInputValid) "Yes" else "No"}") | ||
lastValid.value?.let { lastValid -> | ||
Text("Last state: ${if (lastValid) "valid" else "invalid"}") | ||
} | ||
Button( | ||
onClick = { | ||
val newLastValid = validationContext.value | ||
.validateHierarchy(switchFocus.value) | ||
lastValid.value = newLastValid | ||
} | ||
) { | ||
Text("Validate") | ||
} | ||
Row { | ||
Text("Switch Focus") | ||
Switch(switchFocus.value, onCheckedChange = { switchFocus.value = it }) | ||
} | ||
|
||
Validate(input.value, rules = listOf(ValidationRule.minimalPassword)) { | ||
VerifiableTextField(StringResource(Field.INPUT.name), input) | ||
} | ||
|
||
Validate(nonEmptyInput.value, rules = listOf(ValidationRule.nonEmpty)) { | ||
VerifiableTextField(StringResource(Field.NON_EMPTY_INPUT.name), nonEmptyInput) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package edu.stanford.spezi.core.design.simulator | ||
|
||
import androidx.compose.ui.test.junit4.ComposeTestRule | ||
|
||
class DefaultValidationRulesSimulator( | ||
Check warning on line 5 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/DefaultValidationRulesSimulator.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/DefaultValidationRulesSimulator.kt#L5 <detekt.UseDataClass>
Raw output
|
||
private val composeTestRule: ComposeTestRule, | ||
) { | ||
Check warning on line 7 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/DefaultValidationRulesSimulator.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/DefaultValidationRulesSimulator.kt#L7 <detekt.EmptyClassBlock>
Raw output
Check warning on line 7 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/DefaultValidationRulesSimulator.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/DefaultValidationRulesSimulator.kt#L7 <detekt.NoEmptyClassBody>
Raw output
|
||
|
||
Check warning on line 8 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/DefaultValidationRulesSimulator.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/DefaultValidationRulesSimulator.kt#L8 <detekt.NoBlankLineBeforeRbrace>
Raw output
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package edu.stanford.spezi.core.design.simulator | ||
|
||
import androidx.compose.material3.Text | ||
Check warning on line 3 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/FocusValidationRulesSimulator.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/FocusValidationRulesSimulator.kt#L3 <detekt.NoUnusedImports>
Raw output
Check warning on line 3 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/FocusValidationRulesSimulator.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/FocusValidationRulesSimulator.kt#L3 <detekt.UnusedImports>
Raw output
|
||
import androidx.compose.ui.test.assertHasClickAction | ||
import androidx.compose.ui.test.assertIsOn | ||
Check warning on line 5 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/FocusValidationRulesSimulator.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/FocusValidationRulesSimulator.kt#L5 <detekt.NoUnusedImports>
Raw output
Check warning on line 5 in core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/FocusValidationRulesSimulator.kt GitHub Actions / detekt[detekt] core/design/src/androidTest/kotlin/edu/stanford/spezi/core/design/simulator/FocusValidationRulesSimulator.kt#L5 <detekt.UnusedImports>
Raw output
|
||
import androidx.compose.ui.test.junit4.ComposeTestRule | ||
import androidx.compose.ui.test.onNodeWithText | ||
import androidx.compose.ui.test.performClick | ||
|
||
class FocusValidationRulesSimulator( | ||
private val composeTestRule: ComposeTestRule, | ||
) { | ||
private val passwordMessage = "Your password must be at least 8 characters long." | ||
private val emptyMessage = "This field cannot be empty." | ||
|
||
fun assertHasEngines(hasEngines: Boolean) { | ||
composeTestRule | ||
.onNodeWithText("Has Engines: ${if (hasEngines) "Yes" else "No"}") | ||
.assertExists() | ||
} | ||
|
||
fun assertInputValid(inputValid: Boolean) { | ||
composeTestRule | ||
.onNodeWithText("Input Valid: ${if (inputValid) "Yes" else "No"}") | ||
.assertExists() | ||
} | ||
|
||
fun assertPasswordMessageExists(exists: Boolean) { | ||
val node = composeTestRule.onNodeWithText(passwordMessage) | ||
if (exists) node.assertExists() else node.assertDoesNotExist() | ||
} | ||
|
||
fun assertEmptyMessageExists(exists: Boolean) { | ||
val node = composeTestRule.onNodeWithText(emptyMessage) | ||
if (exists) node.assertExists() else node.assertDoesNotExist() | ||
} | ||
|
||
fun clickValidateButton() { | ||
composeTestRule | ||
.onNodeWithText("Validate") | ||
.assertHasClickAction() | ||
.performClick() | ||
} | ||
|
||
fun assertLastState(valid: Boolean) { | ||
composeTestRule | ||
.onNodeWithText("Last state: ${if (valid) "valid" else "invalid"}") | ||
.assertExists() | ||
} | ||
} |