-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathprogressbar_test.go
133 lines (102 loc) · 2.84 KB
/
progressbar_test.go
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
package widget_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/widget"
)
var globalProgressRenderer fyne.WidgetRenderer
func BenchmarkProgressbarCreateRenderer(b *testing.B) {
var renderer fyne.WidgetRenderer
widget := &widget.ProgressBar{}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
renderer = widget.CreateRenderer()
}
// Avoid having the value optimized out by the compiler.
globalProgressRenderer = renderer
}
func BenchmarkProgressBarLayout(b *testing.B) {
b.ReportAllocs() // We should see zero allocations.
bar := &widget.ProgressBar{}
renderer := bar.CreateRenderer()
for i := 0; i < b.N; i++ {
renderer.Layout(fyne.NewSize(100, 100))
}
}
func TestNewProgressBarWithData(t *testing.T) {
val := binding.NewFloat()
val.Set(0.4)
label := widget.NewProgressBarWithData(val)
waitForBinding()
assert.Equal(t, 0.4, label.Value)
}
func TestProgressBar_Binding(t *testing.T) {
bar := widget.NewProgressBar()
assert.Equal(t, 0.0, bar.Value)
val := binding.NewFloat()
val.Set(0.1)
bar.Bind(val)
waitForBinding()
assert.Equal(t, 0.1, bar.Value)
val.Set(0.4)
waitForBinding()
assert.Equal(t, 0.4, bar.Value)
bar.Unbind()
waitForBinding()
assert.Equal(t, 0.4, bar.Value)
}
func TestProgressBar_SetValue(t *testing.T) {
bar := widget.NewProgressBar()
assert.Equal(t, 0.0, bar.Min)
assert.Equal(t, 1.0, bar.Max)
assert.Equal(t, 0.0, bar.Value)
bar.SetValue(.5)
assert.Equal(t, .5, bar.Value)
}
func TestProgressBar_TextFormatter(t *testing.T) {
bar := widget.NewProgressBar()
formatted := false
bar.SetValue(0.2)
assert.Equal(t, false, formatted)
formatter := func() string {
formatted = true
return fmt.Sprintf("%.2f out of %.2f", bar.Value, bar.Max)
}
bar.TextFormatter = formatter
bar.SetValue(0.4)
assert.Equal(t, true, formatted)
}
func TestProgressRenderer_Layout(t *testing.T) {
bar, c := barOnCanvas()
test.AssertRendersToMarkup(t, "progressbar/empty.xml", c)
bar.SetValue(.5)
test.AssertRendersToMarkup(t, "progressbar/half.xml", c)
bar.SetValue(1)
test.AssertRendersToMarkup(t, "progressbar/full.xml", c)
}
func TestProgressRenderer_Layout_Overflow(t *testing.T) {
bar, c := barOnCanvas()
bar.SetValue(1)
test.AssertRendersToMarkup(t, "progressbar/full.xml", c)
bar.SetValue(1.2)
test.AssertRendersToMarkup(t, "progressbar/full.xml", c)
}
func TestProgressRenderer_ApplyTheme(t *testing.T) {
test.WithTestTheme(t, func() {
bar, c := barOnCanvas()
bar.SetValue(.2)
test.AssertRendersToMarkup(t, "progressbar/themed.xml", c)
})
}
func barOnCanvas() (*widget.ProgressBar, fyne.Canvas) {
bar := widget.NewProgressBar()
window := test.NewWindow(container.NewVBox(bar))
window.SetPadded(false)
window.Resize(fyne.NewSize(100, 100))
return bar, window.Canvas()
}