Skip to content

Commit 5929df8

Browse files
committed
test: add Compose tests for SettingContent to verify displayed texts
1 parent db7935e commit 5929df8

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ dependencies {
291291

292292
// Instrumented Test Libraries
293293
androidTestImplementation(composeBom)
294+
androidTestImplementation(libs.compose.ui.test.junit4)
295+
debugImplementation(libs.compose.ui.test.manifest)
294296
androidTestImplementation(libs.coroutines.test)
295297
androidTestImplementation(libs.androidx.test.core)
296298
androidTestImplementation(libs.androidx.test.ext.junit)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.kabiri.android.usbterminal.ui.setting
2+
3+
import androidx.activity.ComponentActivity
4+
import androidx.compose.ui.test.assertIsDisplayed
5+
import androidx.compose.ui.test.junit4.createAndroidComposeRule
6+
import androidx.compose.ui.test.onNodeWithText
7+
import androidx.test.ext.junit.runners.AndroidJUnit4
8+
import io.mockk.every
9+
import io.mockk.mockk
10+
import kotlinx.coroutines.flow.flowOf
11+
import org.hamcrest.CoreMatchers.notNullValue
12+
import org.hamcrest.MatcherAssert.assertThat
13+
import org.junit.Rule
14+
import org.junit.Test
15+
import org.junit.runner.RunWith
16+
import org.kabiri.android.usbterminal.R
17+
import org.kabiri.android.usbterminal.ui.theme.UsbTerminalTheme
18+
19+
@RunWith(AndroidJUnit4::class)
20+
class SettingContentAndroidTest {
21+
@get:Rule
22+
val composeRule = createAndroidComposeRule<ComponentActivity>()
23+
24+
private fun showContet(viewModel: SettingViewModel) {
25+
composeRule.setContent {
26+
UsbTerminalTheme {
27+
SettingContent(
28+
settingViewModel = viewModel,
29+
onDismiss = {},
30+
)
31+
}
32+
}
33+
}
34+
35+
@Test
36+
fun settingContent_displaysExpectedTexts() {
37+
// arrange
38+
val context = composeRule.activity
39+
assertThat(context, notNullValue())
40+
val viewModel = mockk<SettingViewModel>(relaxed = true)
41+
every { viewModel.currentBaudRate } returns flowOf(9600)
42+
every { viewModel.currentAutoScroll } returns flowOf(true)
43+
44+
// act
45+
showContet(viewModel)
46+
47+
// assert
48+
composeRule.onNodeWithText(context.getString(R.string.settings_title)).assertIsDisplayed()
49+
composeRule
50+
.onNodeWithText(context.getString(R.string.settings_subtitle))
51+
.assertIsDisplayed()
52+
53+
// Baud rate label (ensure unmerged tree so label is found)
54+
composeRule
55+
.onNodeWithText(
56+
context.getString(R.string.settings_label_baud_rate),
57+
useUnmergedTree = true,
58+
).assertIsDisplayed()
59+
60+
// Buttons and bottom text
61+
composeRule
62+
.onNodeWithText(context.getString(R.string.settings_bt_reset_default))
63+
.assertIsDisplayed()
64+
composeRule
65+
.onNodeWithText(context.getString(R.string.settings_bt_dismiss_sheet))
66+
.assertIsDisplayed()
67+
composeRule
68+
.onNodeWithText(context.getString(R.string.settings_bottom_text))
69+
.assertIsDisplayed()
70+
71+
// Auto-scroll label
72+
composeRule
73+
.onNodeWithText(context.getString(R.string.settings_label_auto_scroll))
74+
.assertIsDisplayed()
75+
}
76+
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ compose-foundation = { module = "androidx.compose.foundation:foundation" }
6464
compose-material3 = { module = "androidx.compose.material3:material3" }
6565
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }
6666
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
67+
compose-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4" }
68+
compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest" }
6769

6870
# --- Google/Material ---
6971
material = { module = "com.google.android.material:material", version.ref = "material" }

0 commit comments

Comments
 (0)