-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OutlinedTextFieldBox: Add instrumentation tests
- Loading branch information
1 parent
fe12f13
commit 43026b6
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
.../kotlin/com/edricchan/studybuddy/ui/widgets/compose/textfield/OutlinedTextFieldBoxTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.edricchan.studybuddy.ui.widgets.compose.textfield | ||
|
||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.test.assertHasClickAction | ||
import androidx.compose.ui.test.assertHasNoClickAction | ||
import androidx.compose.ui.test.assertIsDisplayed | ||
import androidx.compose.ui.test.assertIsEnabled | ||
import androidx.compose.ui.test.assertIsNotEnabled | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.test.performClick | ||
import com.edricchan.studybuddy.ui.theming.compose.StudyBuddyTheme | ||
import com.edricchan.studybuddy.ui.widgets.compose.assertExistsAndIsDisplayed | ||
import org.junit.Rule | ||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
|
||
class OutlinedTextFieldBoxTest { | ||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
@Test | ||
fun outlinedTextFieldBox() { | ||
composeTestRule.apply { | ||
setContent { | ||
StudyBuddyTheme { | ||
OutlinedTextFieldBox( | ||
modifier = boxModifier, | ||
headlineText = { OutlinedTextFieldBoxText() }, | ||
leadingIcon = { OutlinedTextFieldBoxLeadingIcon() }, | ||
trailingIcon = { OutlinedTextFieldBoxTrailingIcon() } | ||
) | ||
} | ||
} | ||
|
||
onNodeWithTag(boxTag).apply { | ||
assertExists() | ||
assertIsDisplayed() | ||
assertHasNoClickAction() | ||
} | ||
|
||
onNodeWithTag(textTag, useUnmergedTree = true).assertExistsAndIsDisplayed() | ||
|
||
onNodeWithTag(leadingIconTag, useUnmergedTree = true).assertExistsAndIsDisplayed() | ||
|
||
onNodeWithTag(trailingIconTag, useUnmergedTree = true).assertExistsAndIsDisplayed() | ||
} | ||
} | ||
|
||
@Test | ||
fun outlinedTextFieldBox_withOnClickAndEnabled() { | ||
var hasClicked = false | ||
var enabled by mutableStateOf(true) | ||
|
||
composeTestRule.apply { | ||
setContent { | ||
StudyBuddyTheme { | ||
OutlinedTextFieldBox( | ||
modifier = boxModifier, | ||
onClick = { hasClicked = true }, | ||
enabled = enabled, | ||
headlineText = { OutlinedTextFieldBoxText() }, | ||
leadingIcon = { OutlinedTextFieldBoxLeadingIcon() }, | ||
trailingIcon = { OutlinedTextFieldBoxTrailingIcon() } | ||
) | ||
} | ||
} | ||
|
||
val box = onNodeWithTag(boxTag).apply { | ||
assertExists() | ||
assertIsDisplayed() | ||
assertIsEnabled() | ||
assertHasClickAction() | ||
performClick() | ||
assertTrue(hasClicked, "Composable should have been clicked") | ||
} | ||
|
||
onNodeWithTag(textTag, useUnmergedTree = true).assertExistsAndIsDisplayed() | ||
onNodeWithTag(leadingIconTag, useUnmergedTree = true).assertExistsAndIsDisplayed() | ||
onNodeWithTag(trailingIconTag, useUnmergedTree = true).assertExistsAndIsDisplayed() | ||
|
||
enabled = false | ||
box.assertIsNotEnabled() | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...ose/src/androidTest/kotlin/com/edricchan/studybuddy/ui/widgets/compose/textfield/Utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
@file:Suppress("TestFunctionName") | ||
|
||
package com.edricchan.studybuddy.ui.widgets.compose.textfield | ||
|
||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.outlined.CheckCircle | ||
import androidx.compose.material.icons.outlined.Favorite | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.testTag | ||
|
||
internal const val boxTag = "Tag:OutlinedTextFieldBox" | ||
internal const val textTag = "Tag:Text" | ||
internal const val leadingIconTag = "Tag:LeadingIcon" | ||
internal const val trailingIconTag = "Tag:TrailingIcon" | ||
|
||
internal val boxModifier = Modifier.testTag(boxTag) | ||
internal val textModifier = Modifier.testTag(textTag) | ||
internal val leadingIconModifier = Modifier.testTag(leadingIconTag) | ||
internal val trailingIconModifier = Modifier.testTag(trailingIconTag) | ||
|
||
private const val itemText = "Content of text field" | ||
|
||
@Composable | ||
internal fun OutlinedTextFieldBoxText() { | ||
Text(modifier = textModifier, text = itemText) | ||
} | ||
|
||
@Composable | ||
internal fun OutlinedTextFieldBoxLeadingIcon() { | ||
Icon( | ||
modifier = leadingIconModifier, | ||
imageVector = Icons.Outlined.CheckCircle, | ||
contentDescription = null | ||
) | ||
} | ||
|
||
@Composable | ||
internal fun OutlinedTextFieldBoxTrailingIcon() { | ||
Icon( | ||
modifier = trailingIconModifier, | ||
imageVector = Icons.Outlined.Favorite, | ||
contentDescription = null | ||
) | ||
} |