-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathscrollview_test.go
More file actions
244 lines (203 loc) · 5.53 KB
/
scrollview_test.go
File metadata and controls
244 lines (203 loc) · 5.53 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
package pony
import (
"fmt"
"testing"
uv "github.com/charmbracelet/ultraviolet"
"github.com/charmbracelet/x/exp/golden"
)
func TestScrollViewBasic(t *testing.T) {
const markup = `<scrollview height="5"><vstack><text>A</text><text>B</text><text>C</text><text>D</text><text>E</text><text>F</text><text>G</text></vstack></scrollview>`
tmpl, err := Parse[any](markup)
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
output := tmpl.Render(nil, 80, 24)
golden.RequireEqual(t, output)
}
func TestScrollViewWithOffset(t *testing.T) {
scroll := NewScrollView(
NewVStack(
NewText("Line 1"),
NewText("Line 2"),
NewText("Line 3"),
NewText("Line 4"),
NewText("Line 5"),
),
).Height(NewFixedConstraint(3)).Offset(0, 2)
// With offset 2, should start from line 3
// The viewport itself should be 3 lines tall
constraints := Constraints{
MinWidth: 0,
MaxWidth: 80,
MinHeight: 0,
MaxHeight: 24,
}
size := scroll.Layout(constraints)
if size.Height != 3 {
t.Errorf("ScrollView viewport height = %d, want 3", size.Height)
}
}
func TestScrollViewContentSize(t *testing.T) {
// Create content larger than viewport
var lines []Element
for i := 1; i <= 20; i++ {
lines = append(lines, NewText("Line"))
}
scroll := NewScrollView(NewVStack(lines...))
contentSize := scroll.ContentSize()
if contentSize.Height != 20 {
t.Errorf("ContentSize().Height = %d, want 20", contentSize.Height)
}
}
func TestScrollMethods(t *testing.T) {
scroll := NewScrollView(NewText("Content")).
Height(NewFixedConstraint(10))
// Test scroll down
scroll.ScrollDown(5, 100, 10)
if scroll.offsetY != 5 {
t.Errorf("ScrollDown: OffsetY = %d, want 5", scroll.offsetY)
}
// Test scroll up
scroll.ScrollUp(2)
if scroll.offsetY != 3 {
t.Errorf("ScrollUp: OffsetY = %d, want 3", scroll.offsetY)
}
// Test scroll bounds
scroll.ScrollDown(1000, 100, 10)
if scroll.offsetY > 90 {
t.Errorf("ScrollDown should limit to maxOffset = %d, got %d", 90, scroll.offsetY)
}
// Test scroll up to 0
scroll.ScrollUp(1000)
if scroll.offsetY != 0 {
t.Errorf("ScrollUp should limit to 0, got %d", scroll.offsetY)
}
}
func TestScrollViewInMarkup(t *testing.T) {
const markup = `
<scrollview height="5" scrollbar="true">
<vstack>
<text>Line 1</text>
<text>Line 2</text>
<text>Line 3</text>
<text>Line 4</text>
<text>Line 5</text>
<text>Line 6</text>
<text>Line 7</text>
</vstack>
</scrollview>
`
tmpl, err := Parse[any](markup)
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
output := tmpl.Render(nil, 80, 24)
golden.RequireEqual(t, output)
}
func TestScrollViewWithSlot(t *testing.T) {
const markup = `
<box border="rounded">
<slot name="scrollable" />
</box>
`
var lines []Element
for i := 1; i <= 20; i++ {
lines = append(lines, NewText(fmt.Sprintf("Line %d", i)))
}
scroll := NewScrollView(NewVStack(lines...)).
Height(NewFixedConstraint(10)).
Offset(0, 5)
tmpl, err := Parse[any](markup)
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
slots := map[string]Element{
"scrollable": scroll,
}
output := tmpl.RenderWithSlots(nil, slots, 80, 24)
golden.RequireEqual(t, output)
}
// Test ScrollView chaining methods.
func TestScrollViewWithMethods(t *testing.T) {
scroll := NewScrollView(NewText("test"))
scroll.Vertical(false)
scroll.Horizontal(true)
scroll.Scrollbar(false)
scroll.Width(NewFixedConstraint(10))
// Test Children
children := scroll.Children()
if len(children) != 1 {
t.Error("ScrollView Children should return child")
}
}
// Test horizontal scrolling methods.
func TestScrollHorizontalMethods(t *testing.T) {
scroll := NewScrollView(NewText("test"))
// Test ScrollLeft
scroll.offsetX = 10
scroll.ScrollLeft(5)
if scroll.offsetX != 5 {
t.Errorf("ScrollLeft: expected offset 5, got %d", scroll.offsetX)
}
scroll.ScrollLeft(10)
if scroll.offsetX != 0 {
t.Errorf("ScrollLeft should clamp to 0, got %d", scroll.offsetX)
}
// Test ScrollRight
scroll.offsetX = 0
scroll.ScrollRight(5, 100, 10)
if scroll.offsetX != 5 {
t.Errorf("ScrollRight: expected offset 5, got %d", scroll.offsetX)
}
scroll.ScrollRight(100, 100, 10)
if scroll.offsetX > 90 {
t.Errorf("ScrollRight should clamp to maxOffset 90, got %d", scroll.offsetX)
}
}
// Test ContentSize with nil child.
func TestScrollViewContentSizeNil(t *testing.T) {
scroll := &ScrollView{child: nil}
size := scroll.ContentSize()
if size.Width != 0 || size.Height != 0 {
t.Error("ContentSize with nil child should return zero size")
}
}
// Test horizontal scrollbar rendering.
func TestHorizontalScrollbar(t *testing.T) {
// Create wide content
var items []Element
for i := 0; i < 20; i++ {
items = append(items, NewText("Word"))
}
scroll := NewScrollView(NewHStack(items...).Spacing(1)).
Width(NewFixedConstraint(30)).
Height(NewFixedConstraint(5)).
Horizontal(true).
Vertical(false).
Offset(10, 0)
constraints := Constraints{
MinWidth: 0,
MaxWidth: 30,
MinHeight: 0,
MaxHeight: 5,
}
size := scroll.Layout(constraints)
if size.Width != 30 {
t.Errorf("Horizontal scroll layout width = %d, want 30", size.Width)
}
// Test rendering to actually trigger drawHorizontalScrollbar
buf := uv.NewScreenBuffer(size.Width, size.Height)
area := uv.Rect(0, 0, size.Width, size.Height)
scroll.Draw(buf, area)
output := buf.Render()
if len(output) == 0 {
t.Error("Should render horizontal scrollbar")
}
}
// Test children with nil child.
func TestScrollViewChildrenNil(t *testing.T) {
scroll := &ScrollView{child: nil}
if scroll.Children() != nil {
t.Error("ScrollView Children with nil child should return nil")
}
}