Skip to content

Commit 75aef1b

Browse files
committed
tests: add compose tests for TerminalOutput component and clipboard functionality
1 parent 1fe8fc8 commit 75aef1b

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

app/src/androidTest/java/org/kabiri/android/usbterminal/ui/terminal/TerminalOutputAndroidTest.kt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import android.content.ClipboardManager
44
import android.content.Context
55
import androidx.activity.ComponentActivity
66
import androidx.compose.runtime.mutableStateListOf
7+
import androidx.compose.ui.platform.testTag
78
import androidx.compose.ui.test.assertIsDisplayed
89
import androidx.compose.ui.test.junit4.createAndroidComposeRule
910
import androidx.compose.ui.test.longClick
11+
import androidx.compose.ui.test.onNodeWithTag
1012
import androidx.compose.ui.test.onNodeWithText
1113
import androidx.compose.ui.test.performTouchInput
1214
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -74,4 +76,62 @@ class TerminalOutputAndroidTest {
7476
?.toString()
7577
assertThat(copied).isEqualTo("A\nB")
7678
}
79+
80+
@Test
81+
fun terminalOutput_appliesModifierTag() {
82+
// arrange
83+
val logs =
84+
mutableStateListOf(
85+
OutputText("Tagged line", OutputText.OutputType.TYPE_NORMAL),
86+
)
87+
88+
// act
89+
composeRule.setContent {
90+
UsbTerminalTheme {
91+
TerminalOutput(
92+
logs = logs,
93+
autoScroll = false,
94+
modifier =
95+
androidx.compose.ui.Modifier
96+
.testTag("terminal"),
97+
)
98+
}
99+
}
100+
101+
// assert: the tagged node exists and is visible, and the text is displayed
102+
composeRule.onNodeWithTag("terminal").assertExists().assertIsDisplayed()
103+
composeRule.onNodeWithText("Tagged line").assertIsDisplayed()
104+
}
105+
106+
@Test
107+
fun terminalOutput_handlesEmptyLogs_andLongPressCopiesEmpty() {
108+
// arrange
109+
val context = composeRule.activity
110+
val logs = mutableStateListOf<OutputText>()
111+
112+
composeRule.setContent {
113+
UsbTerminalTheme {
114+
TerminalOutput(
115+
logs = logs,
116+
autoScroll = false,
117+
modifier =
118+
androidx.compose.ui.Modifier
119+
.testTag("terminal"),
120+
)
121+
}
122+
}
123+
124+
// act: long-press the list itself
125+
composeRule.onNodeWithTag("terminal").performTouchInput { longClick() }
126+
composeRule.waitForIdle()
127+
128+
// assert: clipboard should contain empty string
129+
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
130+
val copied =
131+
clipboard.primaryClip
132+
?.getItemAt(0)
133+
?.coerceToText(context)
134+
?.toString()
135+
assertThat(copied).isEqualTo("")
136+
}
77137
}

app/src/main/java/org/kabiri/android/usbterminal/ui/terminal/TerminalOutput.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal fun TerminalOutput(
5959
contentPadding = PaddingValues(vertical = 8.dp),
6060
verticalArrangement = Arrangement.Bottom,
6161
) {
62-
itemsIndexed(logs, key = { index, item -> index }) { _, item ->
62+
itemsIndexed(logs, key = { index, _ -> index }) { _, item ->
6363
val color =
6464
when (item.type) {
6565
OutputText.OutputType.TYPE_ERROR -> MaterialTheme.colorScheme.error

0 commit comments

Comments
 (0)