forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 1
/
preferences_test.go
190 lines (145 loc) · 4.08 KB
/
preferences_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
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
package internal
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestPrefs_SetBool(t *testing.T) {
p := NewInMemoryPreferences()
p.SetBool("testBool", true)
assert.Equal(t, true, p.Bool("testBool"))
}
func TestPrefs_Bool(t *testing.T) {
p := NewInMemoryPreferences()
p.WriteValues(func(val map[string]interface{}) {
val["testBool"] = true
})
assert.Equal(t, true, p.Bool("testBool"))
}
func TestPrefs_BoolWithFallback(t *testing.T) {
p := NewInMemoryPreferences()
assert.Equal(t, true, p.BoolWithFallback("testBool", true))
p.SetBool("testBool", false)
assert.Equal(t, false, p.BoolWithFallback("testBool", true))
}
func TestPrefs_Bool_Zero(t *testing.T) {
p := NewInMemoryPreferences()
assert.Equal(t, false, p.Bool("testBool"))
}
func TestPrefs_SetFloat(t *testing.T) {
p := NewInMemoryPreferences()
p.SetFloat("testFloat", 1.7)
assert.Equal(t, 1.7, p.Float("testFloat"))
}
func TestPrefs_Float(t *testing.T) {
p := NewInMemoryPreferences()
p.WriteValues(func(val map[string]interface{}) {
val["testFloat"] = 1.2
})
assert.Equal(t, 1.2, p.Float("testFloat"))
}
func TestPrefs_FloatWithFallback(t *testing.T) {
p := NewInMemoryPreferences()
assert.Equal(t, 1.0, p.FloatWithFallback("testFloat", 1.0))
p.WriteValues(func(val map[string]interface{}) {
val["testFloat"] = 1.2
})
assert.Equal(t, 1.2, p.FloatWithFallback("testFloat", 1.0))
}
func TestPrefs_Float_Zero(t *testing.T) {
p := NewInMemoryPreferences()
assert.Equal(t, 0.0, p.Float("testFloat"))
}
func TestPrefs_SetInt(t *testing.T) {
p := NewInMemoryPreferences()
p.SetInt("testInt", 5)
assert.Equal(t, 5, p.Int("testInt"))
}
func TestPrefs_Int(t *testing.T) {
p := NewInMemoryPreferences()
p.WriteValues(func(val map[string]interface{}) {
val["testInt"] = 5
})
assert.Equal(t, 5, p.Int("testInt"))
}
func TestPrefs_IntWithFallback(t *testing.T) {
p := NewInMemoryPreferences()
assert.Equal(t, 2, p.IntWithFallback("testInt", 2))
p.WriteValues(func(val map[string]interface{}) {
val["testInt"] = 5
})
assert.Equal(t, 5, p.IntWithFallback("testInt", 2))
}
func TestPrefs_Int_Zero(t *testing.T) {
p := NewInMemoryPreferences()
assert.Equal(t, 0, p.Int("testInt"))
}
func TestPrefs_SetString(t *testing.T) {
p := NewInMemoryPreferences()
p.SetString("test", "value")
assert.Equal(t, "value", p.String("test"))
}
func TestPrefs_String(t *testing.T) {
p := NewInMemoryPreferences()
p.WriteValues(func(val map[string]interface{}) {
val["test"] = "value"
})
assert.Equal(t, "value", p.String("test"))
}
func TestPrefs_StringWithFallback(t *testing.T) {
p := NewInMemoryPreferences()
assert.Equal(t, "default", p.StringWithFallback("test", "default"))
p.WriteValues(func(val map[string]interface{}) {
val["test"] = "value"
})
assert.Equal(t, "value", p.StringWithFallback("test", "default"))
}
func TestPrefs_String_Zero(t *testing.T) {
p := NewInMemoryPreferences()
assert.Equal(t, "", p.String("test"))
}
func TestInMemoryPreferences_OnChange(t *testing.T) {
p := NewInMemoryPreferences()
called := false
p.AddChangeListener(func() {
called = true
})
p.SetString("dummy", "another")
time.Sleep(time.Millisecond * 100)
assert.True(t, called)
called = false
p.RemoveValue("dummy")
time.Sleep(time.Millisecond * 100)
assert.True(t, called)
}
func TestRemoveValue(t *testing.T) {
p := NewInMemoryPreferences()
p.SetBool("dummy", true)
p.SetFloat("pi", 3.14)
p.SetInt("number", 2)
p.SetString("month", "January")
p.RemoveValue("dummy")
p.RemoveValue("pi")
p.RemoveValue("number")
p.RemoveValue("month")
assert.Equal(t, false, p.Bool("dummy"))
assert.Equal(t, float64(0), p.Float("pi"))
assert.Equal(t, 0, p.Int("number"))
assert.Equal(t, "", p.String("month"))
}
func TestPrefs_SetSameValue(t *testing.T) {
p := NewInMemoryPreferences()
called := 0
p.AddChangeListener(func() {
called++
})
// We should not fire change when it hasn't changed.
for i := 0; i < 2; i++ {
p.SetBool("enabled", true)
time.Sleep(time.Millisecond * 100)
assert.Equal(t, 1, called)
}
p.SetBool("enabled", false)
time.Sleep(time.Millisecond * 100)
assert.Equal(t, 2, called)
}