Skip to content

Commit 99dad1a

Browse files
Add tests for asMutableTextFieldValueState()
1 parent aa12b64 commit 99dad1a

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.squareup.workflow1.ui.compose
2+
3+
import androidx.compose.runtime.LaunchedEffect
4+
import androidx.compose.runtime.getValue
5+
import androidx.compose.runtime.mutableStateOf
6+
import androidx.compose.runtime.setValue
7+
import androidx.compose.runtime.snapshotFlow
8+
import androidx.compose.ui.test.junit4.createComposeRule
9+
import androidx.compose.ui.text.TextRange
10+
import androidx.compose.ui.text.input.TextFieldValue
11+
import androidx.test.ext.junit.runners.AndroidJUnit4
12+
import com.google.common.truth.Truth.assertThat
13+
import com.squareup.workflow1.ui.TextController
14+
import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule
15+
import com.squareup.workflow1.ui.internal.test.IdlingDispatcherRule
16+
import leakcanary.DetectLeaksAfterTestSuccess
17+
import org.junit.Rule
18+
import org.junit.Test
19+
import org.junit.rules.RuleChain
20+
import org.junit.runner.RunWith
21+
22+
@RunWith(AndroidJUnit4::class)
23+
internal class TextControllerAsMutableStateTest {
24+
25+
private val composeRule = createComposeRule()
26+
27+
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess())
28+
.around(IdleAfterTestRule)
29+
.around(composeRule)
30+
.around(IdlingDispatcherRule)
31+
32+
@Test fun setTextInCompose() {
33+
val textController = TextController()
34+
composeRule.setContent {
35+
var state by textController.asMutableTextFieldValueState()
36+
LaunchedEffect(Unit) {
37+
state = TextFieldValue(text = "foo")
38+
}
39+
}
40+
composeRule.runOnIdle {
41+
assertThat(textController.textValue).isEqualTo("foo")
42+
}
43+
}
44+
45+
@Test fun setTextInComposeWithSelection() {
46+
val textController = TextController()
47+
val textFieldValue = mutableStateOf<TextFieldValue?>(null)
48+
composeRule.setContent {
49+
var state by textController.asMutableTextFieldValueState()
50+
LaunchedEffect(Unit) {
51+
state = TextFieldValue(text = "foobar", selection = TextRange(1, 3))
52+
}
53+
LaunchedEffect(Unit) {
54+
snapshotFlow { state }
55+
.collect {
56+
textFieldValue.value = it
57+
}
58+
}
59+
}
60+
composeRule.runOnIdle {
61+
assertThat(textController.textValue).isEqualTo("foobar")
62+
assertThat(textFieldValue.value).isEqualTo(
63+
TextFieldValue(
64+
text = "foobar",
65+
selection = TextRange(1, 3)
66+
)
67+
)
68+
}
69+
}
70+
71+
@Test fun setTextViaTextController() {
72+
val textController = TextController()
73+
val textFieldValue = mutableStateOf<TextFieldValue?>(null)
74+
composeRule.setContent {
75+
val state by textController.asMutableTextFieldValueState()
76+
LaunchedEffect(Unit) {
77+
snapshotFlow { state }
78+
.collect {
79+
textFieldValue.value = it
80+
}
81+
}
82+
}
83+
textController.textValue = "foo"
84+
composeRule.runOnIdle {
85+
assertThat(textFieldValue.value).isEqualTo(
86+
TextFieldValue(
87+
text = "foo",
88+
selection = TextRange(3)
89+
)
90+
)
91+
}
92+
}
93+
94+
@Test fun withInitialSelectionSet() {
95+
val textController = TextController("foobar")
96+
val textFieldValue = mutableStateOf<TextFieldValue?>(null)
97+
composeRule.setContent {
98+
val state by textController.asMutableTextFieldValueState(
99+
initialSelection = TextRange(
100+
start = 1,
101+
end = 3,
102+
),
103+
)
104+
LaunchedEffect(Unit) {
105+
snapshotFlow { state }
106+
.collect {
107+
textFieldValue.value = it
108+
}
109+
}
110+
}
111+
composeRule.runOnIdle {
112+
assertThat(textFieldValue.value).isEqualTo(
113+
TextFieldValue(
114+
text = "foobar",
115+
selection = TextRange(1, 3)
116+
)
117+
)
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)