forked from davidroman0O/vtable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation_toggle_test.go
More file actions
182 lines (155 loc) · 4.96 KB
/
animation_toggle_test.go
File metadata and controls
182 lines (155 loc) · 4.96 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
package vtable
import (
"fmt"
"testing"
"time"
)
// TableRowDataProvider for testing table animations
type TableRowDataProvider struct {
rows []TableRow
selection map[int]bool
}
func NewTableRowDataProvider(rowCount int) *TableRowDataProvider {
rows := make([]TableRow, rowCount)
for i := 0; i < rowCount; i++ {
rows[i] = TableRow{
Cells: []string{
fmt.Sprintf("%d", i+1),
fmt.Sprintf("Row %d", i+1),
},
}
}
return &TableRowDataProvider{
rows: rows,
selection: make(map[int]bool),
}
}
func (p *TableRowDataProvider) GetTotal() int {
return len(p.rows)
}
func (p *TableRowDataProvider) GetItems(request DataRequest) ([]Data[TableRow], error) {
start := request.Start
count := request.Count
if start >= len(p.rows) {
return []Data[TableRow]{}, nil
}
end := start + count
if end > len(p.rows) {
end = len(p.rows)
}
result := make([]Data[TableRow], end-start)
for i := start; i < end; i++ {
result[i-start] = Data[TableRow]{
ID: fmt.Sprintf("row-%d", i),
Item: p.rows[i],
Selected: p.selection[i],
Metadata: NewTypedMetadata(),
}
}
return result, nil
}
func (p *TableRowDataProvider) GetSelectionMode() SelectionMode { return SelectionMultiple }
func (p *TableRowDataProvider) SetSelected(index int, selected bool) bool {
if selected {
p.selection[index] = true
} else {
delete(p.selection, index)
}
return true
}
func (p *TableRowDataProvider) SelectAll() bool { return true }
func (p *TableRowDataProvider) ClearSelection() { p.selection = make(map[int]bool) }
func (p *TableRowDataProvider) GetSelectedIndices() []int { return []int{} }
func (p *TableRowDataProvider) GetSelectedIDs() []string { return []string{} }
func (p *TableRowDataProvider) SetSelectedByIDs(ids []string, selected bool) bool { return true }
func (p *TableRowDataProvider) SelectRange(startID, endID string) bool { return true }
func (p *TableRowDataProvider) GetItemID(item *TableRow) string { return "row" }
// TestAnimationToggle verifies that disabling and re-enabling animations works correctly
func TestAnimationToggle(t *testing.T) {
// Create provider
provider := NewTableRowDataProvider(50)
// Create table config
config := TableConfig{
Columns: []TableColumn{
{Title: "ID", Width: 10, Field: "id"},
{Title: "Name", Width: 20, Field: "name"},
},
ShowHeader: true,
ViewportConfig: ViewportConfig{
Height: 5,
ChunkSize: 10,
TopThresholdIndex: 1,
BottomThresholdIndex: 3,
},
}
// Create table
table, err := NewTeaTable(config, provider, *DefaultTheme())
if err != nil {
t.Fatalf("Failed to create TeaTable: %v", err)
}
// Create animated formatter
animationCallCount := 0
animatedFormatter := func(data Data[TableRow], index int, ctx RenderContext,
animationState map[string]any, isCursor bool, isTopThreshold bool, isBottomThreshold bool) RenderResult {
animationCallCount++
// Simple animation that changes content
counter := 0
if c, ok := animationState["counter"]; ok {
if ci, ok := c.(int); ok {
counter = ci
}
}
counter++
content := "Row " + data.Item.Cells[1] + " (tick: " + fmt.Sprintf("%d", counter) + ")"
return RenderResult{
Content: content,
RefreshTriggers: []RefreshTrigger{{
Type: TriggerTimer,
Interval: 100 * time.Millisecond,
}},
AnimationState: map[string]any{
"counter": counter,
},
}
}
// Set animated formatter
table.SetAnimatedFormatter(animatedFormatter)
// Initial state - animations should be enabled by default
if !table.IsAnimationEnabled() {
t.Error("Animations should be enabled by default")
}
// Process some animation ticks to establish baseline
initialCallCount := animationCallCount
table.processAnimations()
if animationCallCount <= initialCallCount {
t.Error("Animation formatter should have been called")
}
// Disable animations
table.DisableAnimations()
if table.IsAnimationEnabled() {
t.Error("Animations should be disabled")
}
// Animation formatter should not be called when disabled
disabledCallCount := animationCallCount
table.processAnimations()
if animationCallCount > disabledCallCount {
t.Error("Animation formatter should not be called when disabled")
}
// Re-enable animations
table.EnableAnimations()
if !table.IsAnimationEnabled() {
t.Error("Animations should be re-enabled")
}
// Animation formatter should be called again after re-enabling
reenabledCallCount := animationCallCount
table.processAnimations()
if animationCallCount <= reenabledCallCount {
t.Error("Animation formatter should be called after re-enabling")
}
// Verify that animation state is properly reset
// The cache should be cleared and animations should restart fresh
if len(table.cachedAnimationContent) == 0 {
// This is expected - cache should be cleared on re-enable
t.Log("Animation cache properly cleared on re-enable")
}
}