-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtextfield_test.go
More file actions
336 lines (294 loc) · 11.7 KB
/
Copy pathtextfield_test.go
File metadata and controls
336 lines (294 loc) · 11.7 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2026 The Ebitengine Authors
package debugui_test
import (
"testing"
"github.com/ebitengine/debugui"
)
func TestTextFieldStateCaretMovement(t *testing.T) {
// "aあb" consists of runes of 1, 3, and 1 bytes.
f := debugui.NewTextFieldState("aあb")
if start, end := f.Selection(); start != 5 || end != 5 {
t.Errorf("Selection() = %d, %d; want 5, 5", start, end)
}
f.MoveCaretLeft(false)
if start, end := f.Selection(); start != 4 || end != 4 {
t.Errorf("Selection() = %d, %d; want 4, 4", start, end)
}
f.MoveCaretLeft(false)
if start, end := f.Selection(); start != 1 || end != 1 {
t.Errorf("Selection() = %d, %d; want 1, 1", start, end)
}
f.MoveCaretLeft(false)
f.MoveCaretLeft(false)
if start, end := f.Selection(); start != 0 || end != 0 {
t.Errorf("Selection() = %d, %d; want 0, 0", start, end)
}
f.MoveCaretRight(false)
if start, end := f.Selection(); start != 1 || end != 1 {
t.Errorf("Selection() = %d, %d; want 1, 1", start, end)
}
f.MoveCaretRight(false)
if start, end := f.Selection(); start != 4 || end != 4 {
t.Errorf("Selection() = %d, %d; want 4, 4", start, end)
}
f.MoveCaretTo(100, false)
if start, end := f.Selection(); start != 5 || end != 5 {
t.Errorf("Selection() = %d, %d; want 5, 5", start, end)
}
f.MoveCaretTo(-1, false)
if start, end := f.Selection(); start != 0 || end != 0 {
t.Errorf("Selection() = %d, %d; want 0, 0", start, end)
}
}
func TestTextFieldStateSelection(t *testing.T) {
f := debugui.NewTextFieldState("hello")
f.MoveCaretTo(1, false)
f.MoveCaretTo(4, true)
if start, end := f.Selection(); start != 1 || end != 4 {
t.Errorf("Selection() = %d, %d; want 1, 4", start, end)
}
// Moving left without extending collapses the selection to its start.
f.MoveCaretLeft(false)
if start, end := f.Selection(); start != 1 || end != 1 {
t.Errorf("Selection() = %d, %d; want 1, 1", start, end)
}
// A selection extended backward has its caret before its anchor.
f.MoveCaretTo(4, false)
f.MoveCaretTo(1, true)
if start, end := f.Selection(); start != 1 || end != 4 {
t.Errorf("Selection() = %d, %d; want 1, 4", start, end)
}
// Moving right without extending collapses the selection to its end.
f.MoveCaretRight(false)
if start, end := f.Selection(); start != 4 || end != 4 {
t.Errorf("Selection() = %d, %d; want 4, 4", start, end)
}
// Extending shrinks the selection when the caret moves back toward the anchor.
f.MoveCaretTo(0, false)
f.MoveCaretTo(3, true)
f.MoveCaretLeft(true)
if start, end := f.Selection(); start != 0 || end != 2 {
t.Errorf("Selection() = %d, %d; want 0, 2", start, end)
}
f.SelectAll()
if start, end := f.Selection(); start != 0 || end != 5 {
t.Errorf("Selection() = %d, %d; want 0, 5", start, end)
}
}
func TestTextFieldStateDeleteSelection(t *testing.T) {
f := debugui.NewTextFieldState("hello world")
f.MoveCaretTo(5, false)
f.MoveCaretTo(11, true)
f.DeleteBackward()
if got, want := f.Text(), "hello"; got != want {
t.Errorf("Text() = %q; want %q", got, want)
}
if start, end := f.Selection(); start != 5 || end != 5 {
t.Errorf("Selection() = %d, %d; want 5, 5", start, end)
}
f = debugui.NewTextFieldState("hello world")
f.MoveCaretTo(6, false)
f.MoveCaretTo(0, true)
f.DeleteForward()
if got, want := f.Text(), "world"; got != want {
t.Errorf("Text() = %q; want %q", got, want)
}
if start, end := f.Selection(); start != 0 || end != 0 {
t.Errorf("Selection() = %d, %d; want 0, 0", start, end)
}
f = debugui.NewTextFieldState("hello")
f.SelectAll()
f.DeleteBackward()
if got, want := f.Text(), ""; got != want {
t.Errorf("Text() = %q; want %q", got, want)
}
}
func TestTextFieldStateDeleteRune(t *testing.T) {
// "aあb" consists of runes of 1, 3, and 1 bytes.
f := debugui.NewTextFieldState("aあb")
f.MoveCaretTo(4, false)
f.DeleteBackward()
if got, want := f.Text(), "ab"; got != want {
t.Errorf("Text() = %q; want %q", got, want)
}
if start, end := f.Selection(); start != 1 || end != 1 {
t.Errorf("Selection() = %d, %d; want 1, 1", start, end)
}
f.DeleteForward()
if got, want := f.Text(), "a"; got != want {
t.Errorf("Text() = %q; want %q", got, want)
}
if start, end := f.Selection(); start != 1 || end != 1 {
t.Errorf("Selection() = %d, %d; want 1, 1", start, end)
}
// Deleting at the boundaries is a no-op.
f.DeleteForward()
f.MoveCaretTo(0, false)
f.DeleteBackward()
if got, want := f.Text(), "a"; got != want {
t.Errorf("Text() = %q; want %q", got, want)
}
}
func TestTextFieldStateWordMovement(t *testing.T) {
// Byte offsets: 0 1 2
// 0123456789012345678901234
f := debugui.NewTextFieldState("foo, bar_baz qux")
// From the end, word-left lands at the start of each word, skipping separators.
f.MoveCaretWordLeft(false)
if start, end := f.Selection(); start != 14 || end != 14 {
t.Errorf("Selection() = %d, %d; want 14, 14 (start of \"qux\")", start, end)
}
// "bar_baz" is a single word because '_' is a word rune.
f.MoveCaretWordLeft(false)
if start, end := f.Selection(); start != 5 || end != 5 {
t.Errorf("Selection() = %d, %d; want 5, 5 (start of \"bar_baz\")", start, end)
}
f.MoveCaretWordLeft(false)
if start, end := f.Selection(); start != 0 || end != 0 {
t.Errorf("Selection() = %d, %d; want 0, 0 (start of \"foo\")", start, end)
}
// Word-left at the start is a no-op.
f.MoveCaretWordLeft(false)
if start, end := f.Selection(); start != 0 || end != 0 {
t.Errorf("Selection() = %d, %d; want 0, 0", start, end)
}
// From the start, word-right lands at the end of each word.
f.MoveCaretWordRight(false)
if start, end := f.Selection(); start != 3 || end != 3 {
t.Errorf("Selection() = %d, %d; want 3, 3 (end of \"foo\")", start, end)
}
f.MoveCaretWordRight(false)
if start, end := f.Selection(); start != 12 || end != 12 {
t.Errorf("Selection() = %d, %d; want 12, 12 (end of \"bar_baz\")", start, end)
}
f.MoveCaretWordRight(false)
if start, end := f.Selection(); start != 17 || end != 17 {
t.Errorf("Selection() = %d, %d; want 17, 17 (end of \"qux\")", start, end)
}
// Word movement extends the selection when requested.
f.MoveCaretTo(0, false)
f.MoveCaretWordRight(true)
if start, end := f.Selection(); start != 0 || end != 3 {
t.Errorf("Selection() = %d, %d; want 0, 3", start, end)
}
}
func TestWordRangeAt(t *testing.T) {
// Byte offsets: 0 1
// 0123456789012345
const s = "foo, bar_baz qux"
tests := []struct {
pos int
start, end int
}{
// Inside a word selects the whole word.
{pos: 0, start: 0, end: 3}, // start of "foo"
{pos: 1, start: 0, end: 3}, // middle of "foo"
{pos: 3, start: 0, end: 3}, // end of "foo", adjacent to ','
{pos: 14, start: 14, end: 17}, // start of "qux"
{pos: 17, start: 14, end: 17}, // end of the string, after "qux"
// '_' is a word rune, so "bar_baz" is one word.
{pos: 8, start: 5, end: 12},
// A click on a run of non-word runes selects that run; punctuation and spaces
// are both non-word, so the comma and the following space select together.
{pos: 4, start: 3, end: 5},
{pos: 13, start: 12, end: 14}, // the two spaces before "qux"
}
for _, tc := range tests {
if start, end := debugui.WordRangeAt(s, tc.pos); start != tc.start || end != tc.end {
t.Errorf("WordRangeAt(%q, %d) = %d, %d; want %d, %d", s, tc.pos, start, end, tc.start, tc.end)
}
}
// A position past either end clamps into range.
if start, end := debugui.WordRangeAt(s, -1); start != 0 || end != 3 {
t.Errorf("WordRangeAt(%q, -1) = %d, %d; want 0, 3", s, start, end)
}
if start, end := debugui.WordRangeAt(s, 100); start != 14 || end != 17 {
t.Errorf("WordRangeAt(%q, 100) = %d, %d; want 14, 17", s, start, end)
}
// An empty string has an empty word range.
if start, end := debugui.WordRangeAt("", 0); start != 0 || end != 0 {
t.Errorf("WordRangeAt(%q, 0) = %d, %d; want 0, 0", "", start, end)
}
}
func TestTextFieldStateMultiClick(t *testing.T) {
const interval = 30
f := debugui.NewTextFieldState("hello world")
// A single click places the caret without selecting.
f.HandleClick(2, false, 100, interval)
if start, end := f.Selection(); start != 2 || end != 2 {
t.Errorf("after single click: Selection() = %d, %d; want 2, 2", start, end)
}
// A second click within the interval is a double-click and selects the word.
f.HandleClick(2, false, 110, interval)
if start, end := f.Selection(); start != 0 || end != 5 {
t.Errorf("after double-click: Selection() = %d, %d; want 0, 5 (\"hello\")", start, end)
}
// A third click within the interval is a triple-click and selects the whole text.
f.HandleClick(2, false, 120, interval)
if start, end := f.Selection(); start != 0 || end != 11 {
t.Errorf("after triple-click: Selection() = %d, %d; want 0, 11", start, end)
}
// A fourth click within the interval keeps the triple-click selection.
f.HandleClick(2, false, 130, interval)
if start, end := f.Selection(); start != 0 || end != 11 {
t.Errorf("after fourth click: Selection() = %d, %d; want 0, 11", start, end)
}
// A click after the interval elapses restarts the sequence as a single click.
f.HandleClick(8, false, 200, interval)
if start, end := f.Selection(); start != 8 || end != 8 {
t.Errorf("after interval elapses: Selection() = %d, %d; want 8, 8", start, end)
}
// And the next quick click is a double-click again, selecting the word under it.
f.HandleClick(8, false, 210, interval)
if start, end := f.Selection(); start != 6 || end != 11 {
t.Errorf("after double-click: Selection() = %d, %d; want 6, 11 (\"world\")", start, end)
}
}
func TestTextFieldStateMultiClickDifferentPosition(t *testing.T) {
const interval = 30
f := debugui.NewTextFieldState("hello world")
// A single click places the caret and starts a potential drag-selection.
f.HandleClick(2, false, 100, interval)
if start, end := f.Selection(); start != 2 || end != 2 {
t.Errorf("after single click: Selection() = %d, %d; want 2, 2", start, end)
}
if !f.Dragging() {
t.Error("after single click: Dragging() = false; want true")
}
// A second click within the interval but at a different position is not a
// double-click: it restarts the sequence as a single click so it can begin a
// fresh drag-selection.
f.HandleClick(8, false, 110, interval)
if start, end := f.Selection(); start != 8 || end != 8 {
t.Errorf("after click at a different position: Selection() = %d, %d; want 8, 8", start, end)
}
if !f.Dragging() {
t.Error("after click at a different position: Dragging() = false; want true")
}
// A further click at that new position within the interval does escalate to a
// double-click, selecting the word under it.
f.HandleClick(8, false, 120, interval)
if start, end := f.Selection(); start != 6 || end != 11 {
t.Errorf("after double-click: Selection() = %d, %d; want 6, 11 (\"world\")", start, end)
}
if f.Dragging() {
t.Error("after double-click: Dragging() = true; want false")
}
}
func TestTextIndexFromX(t *testing.T) {
if got, want := debugui.TextIndexFromX("abc", -1), 0; got != want {
t.Errorf("TextIndexFromX(%q, -1) = %d; want %d", "abc", got, want)
}
if got, want := debugui.TextIndexFromX("abc", debugui.TextWidth("abc")+10), 3; got != want {
t.Errorf("TextIndexFromX(%q, %d) = %d; want %d", "abc", debugui.TextWidth("abc")+10, got, want)
}
// A position right on an inter-rune boundary resolves to that boundary.
if got, want := debugui.TextIndexFromX("abc", debugui.TextWidth("ab")), 2; got != want {
t.Errorf("TextIndexFromX(%q, %d) = %d; want %d", "abc", debugui.TextWidth("ab"), got, want)
}
// A position past the midpoint of a rune resolves to the next boundary.
if got, want := debugui.TextIndexFromX("aあb", debugui.TextWidth("a")+debugui.TextWidth("あ")*3/4), 4; got != want {
t.Errorf("TextIndexFromX(%q, ...) = %d; want %d", "aあb", got, want)
}
}