diff --git a/app/src/androidTest/java/com/android/periodpals/ui/alert/AlertScreenTest.kt b/app/src/androidTest/java/com/android/periodpals/ui/alert/AlertScreenTest.kt index f9728a36c..25e6c5c5f 100644 --- a/app/src/androidTest/java/com/android/periodpals/ui/alert/AlertScreenTest.kt +++ b/app/src/androidTest/java/com/android/periodpals/ui/alert/AlertScreenTest.kt @@ -122,4 +122,31 @@ class AlertScreenTest { verify(navigationActions, never()).navigateTo(any()) verify(navigationActions, never()).navigateTo(any()) } + + @Test + fun createInvalidAlertNoMessage() { + // Leave message empty + composeTestRule.onNodeWithTag("alertProduct").performClick() + composeTestRule.waitForIdle() + composeTestRule.onNodeWithText("Pads").performClick() + composeTestRule.waitForIdle() + + composeTestRule.onNodeWithTag("alertUrgency").performClick() + composeTestRule.waitForIdle() + composeTestRule.onNodeWithText("!! Medium").performClick() + composeTestRule.waitForIdle() + + composeTestRule.onNodeWithTag("alertLocation").performTextInput("Rolex") + + composeTestRule.onNodeWithTag("alertSubmit").performClick() + verify(navigationActions, never()).navigateTo(any()) + verify(navigationActions, never()).navigateTo(any()) + } + + @Test + fun createInvalidAlertAllEmptyFields() { + composeTestRule.onNodeWithTag("alertSubmit").assertIsDisplayed().performClick() + verify(navigationActions, never()).navigateTo(any()) + verify(navigationActions, never()).navigateTo(any()) + } } diff --git a/app/src/main/java/com/android/periodpals/MainActivity.kt b/app/src/main/java/com/android/periodpals/MainActivity.kt index 592561408..9c06ff9ef 100644 --- a/app/src/main/java/com/android/periodpals/MainActivity.kt +++ b/app/src/main/java/com/android/periodpals/MainActivity.kt @@ -1,10 +1,5 @@ package com.android.periodpals -// import androidx.navigation.compose.NavHost -// import androidx.navigation.compose.composable -// import androidx.navigation.navigation -// import com.android.periodpals.ui.navigation.Route -// import com.android.periodpals.ui.navigation.Screen import android.Manifest import android.content.Context import android.content.pm.PackageManager diff --git a/app/src/main/java/com/android/periodpals/ui/alert/AlertScreen.kt b/app/src/main/java/com/android/periodpals/ui/alert/AlertScreen.kt index 90e9a0589..f6c6749b9 100644 --- a/app/src/main/java/com/android/periodpals/ui/alert/AlertScreen.kt +++ b/app/src/main/java/com/android/periodpals/ui/alert/AlertScreen.kt @@ -71,6 +71,7 @@ fun AlertScreen(navigationActions: NavigationActions) { ExposedDropdownMenuSample( listOf("Tampons", "Pads", "No Preference"), "Product Needed", + "Please choose a product", "alertProduct", setProductIsSelected, ) @@ -79,6 +80,7 @@ fun AlertScreen(navigationActions: NavigationActions) { ExposedDropdownMenuSample( listOf("!!! High", "!! Medium", "! Low"), "Urgency level", + "Please choose an urgency level", "alertUrgency", setUrgencyIsSelected, ) @@ -104,14 +106,12 @@ fun AlertScreen(navigationActions: NavigationActions) { // Submit Button Button( onClick = { - if (location.isEmpty()) { - Toast.makeText(context, "Please enter a location", Toast.LENGTH_SHORT).show() - } else if (!productIsSelected) { - Toast.makeText(context, "Please select a product", Toast.LENGTH_SHORT).show() - } else if (!urgencyIsSelected) { - Toast.makeText(context, "Please select an urgency level", Toast.LENGTH_SHORT) - .show() + val errorMessage = + validateFields(productIsSelected, urgencyIsSelected, location, message) + if (errorMessage != null) { + Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show() } else { + Toast.makeText(context, "Alert sent", Toast.LENGTH_SHORT).show() navigationActions.navigateTo(Screen.ALERT_LIST) } }, @@ -130,12 +130,13 @@ fun AlertScreen(navigationActions: NavigationActions) { fun ExposedDropdownMenuSample( list: List, label: String, + instruction: String, testTag: String, setIsSelected: (Boolean) -> Unit, ) { var options = list var expanded by remember { mutableStateOf(false) } - var text by remember { mutableStateOf("Please choose one option") } + var text by remember { mutableStateOf(instruction) } ExposedDropdownMenuBox( modifier = Modifier.testTag(testTag), @@ -168,3 +169,19 @@ fun ExposedDropdownMenuSample( } } } + +/** Validates the fields of the alert screen. */ +private fun validateFields( + productIsSelected: Boolean, + urgencyIsSelected: Boolean, + location: String, + message: String +): String? { + return when { + !productIsSelected -> "Please select a product" + !urgencyIsSelected -> "Please select an urgency level" + location.isEmpty() -> "Please enter a location" + message.isEmpty() -> "Please write your message" + else -> null + } +} diff --git a/app/src/main/java/com/android/periodpals/ui/profile/CreateProfile.kt b/app/src/main/java/com/android/periodpals/ui/profile/CreateProfile.kt index 89dd88731..754e084ea 100644 --- a/app/src/main/java/com/android/periodpals/ui/profile/CreateProfile.kt +++ b/app/src/main/java/com/android/periodpals/ui/profile/CreateProfile.kt @@ -196,24 +196,14 @@ private fun validateFields( description: String ): String? { return when { - !validateEmail(email) -> "Please enter an email" - !validateName(name) -> "Please enter a name" + email.isEmpty() -> "Please enter an email" + name.isEmpty() -> "Please enter a name" !validateDate(date) -> "Invalid date" - !validateDescription(description) -> "Please enter a description" + description.isEmpty() -> "Please enter a description" else -> null } } -/** Validates the email is not empty. */ -private fun validateEmail(email: String): Boolean { - return email.isNotEmpty() -} - -/** Validates the name is not empty. */ -private fun validateName(name: String): Boolean { - return name.isNotEmpty() -} - /** Validates the date is in the format DD/MM/YYYY and is a valid date. */ fun validateDate(date: String): Boolean { val parts = date.split("/")