Skip to content

Commit

Permalink
Merge pull request #31 from yan-yangyang/add-ui-tests-for-settings-sc…
Browse files Browse the repository at this point in the history
…reen

Add settings screen UI tests
  • Loading branch information
yan-yangyang authored May 13, 2024
2 parents c68f4e7 + c6eb280 commit 36a253e
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import com.rickyhu.hushkeyboard.R
Expand All @@ -27,7 +28,11 @@ fun AddSpaceBetweenNotationSwitchItem(
)
},
trailingContent = {
Switch(checked = value, onCheckedChange = onValueChanged)
Switch(
checked = value,
onCheckedChange = onValueChanged,
modifier = Modifier.testTag("AddSpaceAfterNotationSwitch")
)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import com.rickyhu.hushkeyboard.R
Expand All @@ -27,7 +28,11 @@ fun VibrateOnTapSwitchItem(
)
},
trailingContent = {
Switch(checked = value, onCheckedChange = onValueChanged)
Switch(
checked = value,
onCheckedChange = onValueChanged,
modifier = Modifier.testTag("VibrateOnTapSwitch")
)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import com.rickyhu.hushkeyboard.R
Expand All @@ -38,11 +39,13 @@ fun WideNotationOptionDropdownItem(
Text(text = currentOption.toString())

DropdownMenu(
modifier = Modifier.testTag("WideNotationOptionDropdownMenu"),
expanded = expanded,
onDismissRequest = { expanded = false }
) {
for (option in WideNotationOption.values()) {
DropdownMenuItem(
modifier = Modifier.testTag("WideNotationOptionDropdownMenuItem"),
text = { Text(option.toString()) },
onClick = {
onOptionSelected(option)
Expand Down
124 changes: 122 additions & 2 deletions app/src/test/java/com/rickyhu/hushkeyboard/ui/SettingsScreenUiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.rickyhu.hushkeyboard.settings.SettingsContent
import com.rickyhu.hushkeyboard.settings.SettingsState
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -23,15 +25,22 @@ class SettingsScreenUiTest {
@get:Rule
val composeTestRule = createComposeRule()

private var addSpaceAfterNotationSwitchValue = true
private var vibrateOnTapSwitchValue = true

@Before
fun setUp() {
composeTestRule.setContent {
SettingsContent(
state = SettingsState(),
onThemeSelected = {},
onWideNotationOptionSelected = {},
onAddSpaceBetweenNotationChanged = {},
onVibrateOnTapChanged = {}
onAddSpaceBetweenNotationChanged = { value ->
addSpaceAfterNotationSwitchValue = value
},
onVibrateOnTapChanged = { value ->
vibrateOnTapSwitchValue = value
}
)
}
}
Expand Down Expand Up @@ -67,4 +76,115 @@ class SettingsScreenUiTest {
.onAllNodesWithTag("ThemeOptionDropdownMenuItem")
.assertAll(isEnabled())
}

@Test
fun `Wide notation item should exist, should be enabled, and should have click action`() {
composeTestRule
.onNodeWithText("Wide notation")
.assertExists()
.assertIsEnabled()
.assertHasClickAction()
}

@Test
fun `WideNotationDropdownMenu should display after WideNotationDropdownItem is clicked`() {
composeTestRule
.onNodeWithText("Wide notation")
.performClick()

composeTestRule
.onNodeWithTag("WideNotationOptionDropdownMenu")
.assertIsEnabled()
.assertIsDisplayed()
}

@Test
fun `All WideNotationDropdownMenuItems should display after WideNotationDropdown is clicked`() {
composeTestRule
.onNodeWithText("Wide notation")
.performClick()

composeTestRule
.onAllNodesWithTag("WideNotationOptionDropdownMenuItem")
.assertAll(isEnabled())
}

@Test
fun `Add space after notation should exist, should be enabled, and should have click action`() {
composeTestRule
.onNodeWithText("Add space after notation")
.assertExists()
.assertIsEnabled()
.assertHasClickAction()
}

@Test
fun `Auto space switch should exist, should be enabled, and should have click action`() {
composeTestRule
.onNodeWithTag("AddSpaceAfterNotationSwitch")
.assertExists()
.assertIsEnabled()
.assertHasClickAction()
}

@Test
fun `Auto space switch value should be true in initial state`() {
composeTestRule
.onNodeWithTag("AddSpaceAfterNotationSwitch")

assertTrue(addSpaceAfterNotationSwitchValue)
}

@Test
fun `Auto space switch value should be false after it is clicked`() {
composeTestRule
.onNodeWithTag("AddSpaceAfterNotationSwitch")
.performClick()

assertFalse(addSpaceAfterNotationSwitchValue)
}

@Test
fun `Vibrate on tap should exist, should be enabled, and should have click action`() {
composeTestRule
.onNodeWithText("Vibrate on tap")
.assertExists()
.assertIsEnabled()
.assertHasClickAction()
}

@Test
fun `Vibrate on tap switch should exist, should be enabled, and should have click action`() {
composeTestRule
.onNodeWithTag("VibrateOnTapSwitch")
.assertExists()
.assertIsEnabled()
.assertHasClickAction()
}

@Test
fun `Vibrate On Tap Switch value should be true in initial state`() {
composeTestRule
.onNodeWithTag("VibrateOnTapSwitch")

assertTrue(vibrateOnTapSwitchValue)
}

@Test
fun `Vibrate On Tap Switch value should be false after it is clicked`() {
composeTestRule
.onNodeWithTag("VibrateOnTapSwitch")
.performClick()

assertFalse(vibrateOnTapSwitchValue)
}

@Test
fun `Version should exist, should be enabled, and should have click action`() {
composeTestRule
.onNodeWithText("Version")
.assertExists()
.assertIsEnabled()
.assertHasClickAction()
}
}

0 comments on commit 36a253e

Please sign in to comment.