forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings_desktop_test.go
97 lines (78 loc) · 1.91 KB
/
settings_desktop_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
// +build !android,!ios,!mobile,!nacl
package app
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"fyne.io/fyne"
"fyne.io/fyne/theme"
)
func TestDefaultTheme(t *testing.T) {
assert.Equal(t, theme.DarkTheme(), defaultTheme())
}
func TestEnsureDir(t *testing.T) {
tmpDir := testPath("fynetest")
ensureDirExists(tmpDir)
if st, err := os.Stat(tmpDir); err != nil || !st.IsDir() {
t.Error("Could not ensure directory exists")
}
os.Remove(tmpDir)
}
func TestWatchSettings(t *testing.T) {
settings := &settings{}
listener := make(chan fyne.Settings, 1)
settings.AddChangeListener(listener)
settings.fileChanged() // simulate the settings file changing
select {
case <-listener:
case <-time.After(100 * time.Millisecond):
t.Error("Settings listener was not called")
}
}
func TestWatchFile(t *testing.T) {
path := testPath("fyne-temp-watch.txt")
f, _ := os.Create(path)
f.Close()
defer os.Remove(path)
called := make(chan interface{}, 1)
watchFile(path, func() {
called <- true
})
file, _ := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
file.WriteString(" ")
file.Close()
select {
case <-called:
case <-time.After(100 * time.Millisecond):
t.Error("File watcher callback was not called")
}
}
func TestFileWatcher_FileDeleted(t *testing.T) {
path := testPath("fyne-temp-watch.txt")
f, _ := os.Create(path)
f.Close()
defer os.Remove(path)
called := make(chan interface{}, 1)
watcher := watchFile(path, func() {
called <- true
})
if watcher == nil {
assert.Fail(t, "Could not start watcher")
return
}
defer watcher.Close()
os.Remove(path)
f, _ = os.Create(path)
select {
case <-called:
case <-time.After(100 * time.Millisecond):
t.Error("File watcher callback was not called")
}
f.Close()
}
func testPath(child string) string {
// TMPDIR would be more normal but fsnotify cannot watch that on macOS...
return filepath.Join("testdata", child)
}