-
Notifications
You must be signed in to change notification settings - Fork 1
/
slider_test.go
129 lines (116 loc) · 2.55 KB
/
slider_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
package goey
import (
"bitbucket.org/rj/goey/base"
"bitbucket.org/rj/goey/loop"
"fmt"
"strconv"
"testing"
)
func ExampleSlider() {
value := 0.0
// In a full application, this variable would be updated to point to
// the main window for the application.
var mainWindow *Window
// These functions are used to update the GUI. See below
var update func()
var render func() base.Widget
// Update function
update = func() {
err := mainWindow.SetChild(render())
if err != nil {
panic(err)
}
}
// Render function generates a tree of Widgets to describe the desired
// state of the GUI.
render = func() base.Widget {
// Prep - text for the button
text := "Value: " + strconv.FormatFloat(value, 'f', 1, 64)
// The GUI contains a single widget, this button.
return &VBox{
AlignMain: MainCenter,
AlignCross: CrossCenter,
Children: []base.Widget{
&Label{Text: text},
&Slider{
Value: value,
OnChange: func(v float64) {
value = v
update()
},
},
},
}
}
err := loop.Run(func() error {
w, err := NewWindow("Slider", render())
if err != nil {
return err
}
mainWindow = w
return nil
})
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("OK")
}
}
func TestSliderMount(t *testing.T) {
testingMountWidgets(t,
&Slider{Value: 50},
&Slider{Value: 10},
&Slider{Value: 0},
&Slider{Value: 100},
&Slider{Value: 50, Disabled: true},
&Slider{Value: 500, Max: 1000},
)
}
func TestSliderClose(t *testing.T) {
testingCloseWidgets(t,
&Slider{Value: 50},
&Slider{Value: 50, Disabled: true},
&Slider{Value: 500, Max: 1000},
)
}
func TestSliderFocus(t *testing.T) {
testingCheckFocusAndBlur(t,
&Slider{Value: 50},
&Slider{Value: 50},
&Slider{Value: 500, Max: 1000},
)
}
func TestSliderUpdate(t *testing.T) {
testingUpdateWidgets(t, []base.Widget{
&Slider{Value: 50},
&Slider{Value: 50, Disabled: true},
&Slider{Value: 500, Max: 1000},
}, []base.Widget{
&Slider{Value: 50},
&Slider{Value: 50, Min: 10, Max: 60},
&Slider{Value: 500, Max: 1000, Disabled: true},
})
}
func TestSlider_UpdateValue(t *testing.T) {
cases := []struct {
value float64
min, max float64
out float64
}{
{1, 0, 10, 1},
{0, 0, 10, 0},
{10, 0, 10, 10},
{-1, 0, 10, 0},
{11, 0, 10, 10},
{-1, 0, 0, 0},
{11, 0, 0, 0},
{-1, 0, -1, 0},
}
for i, v := range cases {
slider := Slider{Value: v.value, Min: v.min, Max: v.max}
slider.UpdateValue()
if slider.Value != v.out {
t.Errorf("Case %d: .Value does not match, got %f, want %f", i, slider.Value, v.out)
}
}
}