forked from JetBrains/compose-multiplatform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionTests.kt
212 lines (188 loc) · 7.76 KB
/
SelectionTests.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.compose.foundation.text.selection
import androidx.compose.foundation.DesktopPlatform
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyMapping
import androidx.compose.foundation.text.createPlatformDefaultKeyMapping
import androidx.compose.foundation.text.platformDefaultKeyMapping
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertIsFocused
import androidx.compose.ui.test.click
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performKeyInput
import androidx.compose.ui.test.performMouseInput
import androidx.compose.ui.test.pressKey
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import com.google.common.truth.Truth
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Rule
import org.junit.Test
class SelectionTests {
@get:Rule
val rule = createComposeRule()
private val originalPlatformKeyMapping = platformDefaultKeyMapping
@After
fun restoreRealDesktopPlatform() {
setPlatformDefaultKeyMapping(originalPlatformKeyMapping)
}
private fun setPlatformDefaultKeyMapping(value: KeyMapping) {
val field = Class.forName("androidx.compose.foundation.text.KeyMapping_desktopKt")
.getDeclaredField("platformDefaultKeyMapping")
field.isAccessible = true
val modifiersField = java.lang.reflect.Field::class.java.getDeclaredField("modifiers")
modifiersField.isAccessible = true
modifiersField.setInt(field, field.modifiers and java.lang.reflect.Modifier.FINAL.inv())
field.set(null, value)
}
@OptIn(ExperimentalTestApi::class, ExperimentalComposeUiApi::class)
@Test
fun `select using Shift_End and Shift_Home combinations with DesktopPlatform-Windows`() = runBlocking {
setPlatformDefaultKeyMapping(createPlatformDefaultKeyMapping(DesktopPlatform.Windows))
val state = mutableStateOf(TextFieldValue("line 1\nline 2\nline 3\nline 4\nline 5"))
rule.setContent {
BasicTextField(
value = state.value,
onValueChange = { state.value = it },
modifier = Modifier.testTag("textField")
)
}
rule.awaitIdle()
rule.onNodeWithTag("textField").performMouseInput {
click(Offset(0f, 0f))
}
rule.awaitIdle()
rule.onNodeWithTag("textField").assertIsFocused()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(0, 0))
rule.onNodeWithTag("textField").performKeyInput {
pressKey(Key.DirectionRight)
}
rule.awaitIdle()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(1, 1))
rule.onNodeWithTag("textField").performKeyInput {
keyDown(Key.ShiftLeft)
pressKey(Key.MoveEnd)
keyUp(Key.ShiftLeft)
}
rule.awaitIdle()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(1, 6))
rule.onNodeWithTag("textField").performKeyInput {
keyDown(Key.ShiftLeft)
pressKey(Key.MoveHome)
keyUp(Key.ShiftLeft)
}
rule.awaitIdle()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(1, 0))
}
@OptIn(ExperimentalTestApi::class, ExperimentalComposeUiApi::class)
@Test
fun `select using Shift_End and Shift_Home combinations with DesktopPlatform-MacOs`() = runBlocking {
setPlatformDefaultKeyMapping(createPlatformDefaultKeyMapping(DesktopPlatform.MacOS))
val state = mutableStateOf(TextFieldValue("line 1\nline 2\nline 3\nline 4\nline 5"))
rule.setContent {
BasicTextField(
value = state.value,
onValueChange = { state.value = it },
modifier = Modifier.testTag("textField")
)
}
rule.awaitIdle()
rule.onNodeWithTag("textField").performMouseInput {
click(Offset(0f, 0f))
}
rule.awaitIdle()
rule.onNodeWithTag("textField").assertIsFocused()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(0, 0))
rule.onNodeWithTag("textField").performKeyInput {
pressKey(Key.DirectionRight)
}
rule.awaitIdle()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(1, 1))
rule.onNodeWithTag("textField").performKeyInput {
keyDown(Key.ShiftLeft)
pressKey(Key.MoveEnd)
keyUp(Key.ShiftLeft)
}
rule.awaitIdle()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(1, 34))
rule.onNodeWithTag("textField").performKeyInput {
keyDown(Key.ShiftLeft)
pressKey(Key.MoveHome)
keyUp(Key.ShiftLeft)
}
rule.awaitIdle()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(1, 0))
}
@OptIn(ExperimentalTestApi::class, ExperimentalComposeUiApi::class)
@Test
fun `Ctrl + Backspace on an empty line with DesktopPlatform-Windows`() = runBlocking {
setPlatformDefaultKeyMapping(createPlatformDefaultKeyMapping(DesktopPlatform.Windows))
val state = mutableStateOf(TextFieldValue(""))
rule.setContent {
BasicTextField(
value = state.value,
onValueChange = { state.value = it },
modifier = Modifier.testTag("textField")
)
}
rule.awaitIdle()
rule.onNodeWithTag("textField").performMouseInput {
click(Offset(0f, 0f))
}
rule.awaitIdle()
rule.onNodeWithTag("textField").assertIsFocused()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(0, 0))
rule.onNodeWithTag("textField").performKeyInput {
keyDown(Key.CtrlLeft)
keyDown(Key.Backspace)
}
rule.awaitIdle()
}
@OptIn(ExperimentalTestApi::class, ExperimentalComposeUiApi::class)
@Test
fun `Ctrl + Backspace on an empty line with DesktopPlatform-Macos`() = runBlocking {
setPlatformDefaultKeyMapping(createPlatformDefaultKeyMapping(DesktopPlatform.MacOS))
val state = mutableStateOf(TextFieldValue(""))
rule.setContent {
BasicTextField(
value = state.value,
onValueChange = { state.value = it },
modifier = Modifier.testTag("textField")
)
}
rule.awaitIdle()
rule.onNodeWithTag("textField").performMouseInput {
click(Offset(0f, 0f))
}
rule.awaitIdle()
rule.onNodeWithTag("textField").assertIsFocused()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(0, 0))
rule.onNodeWithTag("textField").performKeyInput {
keyDown(Key.AltLeft)
keyDown(Key.Backspace)
}
rule.awaitIdle()
}
}