forked from uprockcom/systray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systray_windows_test.go
132 lines (111 loc) · 2.9 KB
/
systray_windows_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
// +build windows
package systray
import (
"io/ioutil"
"runtime"
"sync/atomic"
"testing"
"time"
"unsafe"
"golang.org/x/sys/windows"
)
const iconFilePath = "example/icon/iconwin.ico"
func TestBaseWindowsTray(t *testing.T) {
systrayReady = func() {}
systrayExit = func() {}
runtime.LockOSThread()
if err := wt.initInstance(); err != nil {
t.Fatalf("initInstance failed: %s", err)
}
if err := wt.createMenu(); err != nil {
t.Fatalf("createMenu failed: %s", err)
}
defer func() {
pDestroyWindow.Call(uintptr(wt.window))
wt.wcex.unregister()
}()
if err := wt.setIcon(iconFilePath); err != nil {
t.Errorf("SetIcon failed: %s", err)
}
if err := wt.setTooltip("Cyrillic tooltip тест:)"); err != nil {
t.Errorf("SetIcon failed: %s", err)
}
var id int32 = 0
err := wt.addOrUpdateMenuItem(atomic.AddInt32(&id, 1), "Simple enabled", false, false)
if err != nil {
t.Errorf("mergeMenuItem failed: %s", err)
}
err = wt.addOrUpdateMenuItem(atomic.AddInt32(&id, 1), "Simple disabled", true, false)
if err != nil {
t.Errorf("mergeMenuItem failed: %s", err)
}
err = wt.addSeparatorMenuItem(atomic.AddInt32(&id, 1))
if err != nil {
t.Errorf("addSeparatorMenuItem failed: %s", err)
}
err = wt.addOrUpdateMenuItem(atomic.AddInt32(&id, 1), "Simple checked enabled", false, true)
if err != nil {
t.Errorf("mergeMenuItem failed: %s", err)
}
err = wt.addOrUpdateMenuItem(atomic.AddInt32(&id, 1), "Simple checked disabled", true, true)
if err != nil {
t.Errorf("mergeMenuItem failed: %s", err)
}
err = wt.hideMenuItem(1)
if err != nil {
t.Errorf("hideMenuItem failed: %s", err)
}
err = wt.hideMenuItem(100)
if err == nil {
t.Error("hideMenuItem failed: must return error on invalid item id")
}
err = wt.addOrUpdateMenuItem(2, "Simple disabled update", true, false)
if err != nil {
t.Errorf("mergeMenuItem failed: %s", err)
}
time.AfterFunc(1*time.Second, quit)
m := struct {
WindowHandle windows.Handle
Message uint32
Wparam uintptr
Lparam uintptr
Time uint32
Pt point
}{}
for {
ret, _, err := pGetMessage.Call(uintptr(unsafe.Pointer(&m)), 0, 0, 0)
res := int32(ret)
if res == -1 {
t.Errorf("win32 GetMessage failed: %v", err)
return
} else if res == 0 {
break
}
pTranslateMessage.Call(uintptr(unsafe.Pointer(&m)))
pDispatchMessage.Call(uintptr(unsafe.Pointer(&m)))
}
}
func TestWindowsRun(t *testing.T) {
onReady := func() {
b, err := ioutil.ReadFile(iconFilePath)
if err != nil {
t.Fatalf("Can't load icon file: %v", err)
}
SetIcon(b)
SetTitle("Test title с кириллицей")
bSomeBtn := AddMenuItem("Йа кнопко", "")
bSomeBtn.Check()
AddSeparator()
bQuit := AddMenuItem("Quit", "Quit the whole app")
go func() {
<-bQuit.ClickedCh
t.Log("Quit reqested")
Quit()
}()
time.AfterFunc(1*time.Second, Quit)
}
onExit := func() {
t.Log("Exit success")
}
Run(onReady, onExit)
}