This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
/
tray_test.go
53 lines (48 loc) · 3.71 KB
/
tray_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
package astilectron
import (
"context"
"testing"
"github.com/asticode/go-astikit"
"github.com/stretchr/testify/assert"
)
func TestTray_Actions(t *testing.T) {
// Init
var d = newDispatcher()
var i = newIdentifier()
var wrt = &mockedWriter{}
var w = newWriter(wrt, &logger{})
var tr = newTray(context.Background(), &TrayOptions{
Image: astikit.StrPtr("/path/to/image"),
Tooltip: astikit.StrPtr("tooltip"),
}, d, i, w)
var m = tr.NewMenu([]*MenuItemOptions{
{
Label: astikit.StrPtr("Root 1"),
SubMenu: []*MenuItemOptions{
{Label: astikit.StrPtr("Item 1")},
{Label: astikit.StrPtr("Item 2")},
{Type: MenuItemTypeSeparator},
{Label: astikit.StrPtr("Item 3")},
},
}})
var p = &PositionOptions{
X: astikit.IntPtr(250),
Y: astikit.IntPtr(250),
}
// Actions
testObjectAction(t, func() error { return tr.Create() }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdCreate+"\",\"targetID\":\""+tr.id+"\",\"trayOptions\":{\"image\":\"/path/to/image\",\"tooltip\":\"tooltip\"}}\n", EventNameTrayEventCreated, nil)
testObjectAction(t, func() error { return tr.SetImage("test") }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdSetImage+"\",\"targetID\":\""+tr.id+"\",\"image\":\"test\"}\n", EventNameTrayEventImageSet, nil)
testObjectAction(t, func() error { return tr.PopUpContextMenu(&TrayPopUpOptions{}) }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdPopupContextMenu+"\",\"targetID\":\""+tr.id+"\"}\n", EventNameTrayEventContextMenuPoppedUp, nil)
testObjectAction(t, func() error { return tr.PopUpContextMenu(&TrayPopUpOptions{Position: p}) }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdPopupContextMenu+"\",\"targetID\":\""+tr.id+"\",\"menuPopupOptions\":{\"x\":250,\"y\":250}}\n", EventNameTrayEventContextMenuPoppedUp, nil)
testObjectAction(t, func() error { return tr.PopUpContextMenu(&TrayPopUpOptions{Menu: m}) }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdPopupContextMenu+"\",\"targetID\":\""+tr.id+"\",\"menu\":{\"id\":\""+m.id+"\",\"items\":[{\"id\":\"3\",\"options\":{\"label\":\"Root 1\"},\"rootId\":\""+m.rootID+"\",\"submenu\":{\"id\":\"4\",\"items\":[{\"id\":\"5\",\"options\":{\"label\":\"Item 1\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"6\",\"options\":{\"label\":\"Item 2\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"7\",\"options\":{\"type\":\"separator\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"8\",\"options\":{\"label\":\"Item 3\"},\"rootId\":\""+m.rootID+"\"}],\"rootId\":\""+m.rootID+"\"}}],\"rootId\":\""+m.rootID+"\"}}\n", EventNameTrayEventContextMenuPoppedUp, nil)
testObjectAction(t, func() error { return tr.PopUpContextMenu(&TrayPopUpOptions{Menu: m, Position: p}) }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdPopupContextMenu+"\",\"targetID\":\""+tr.id+"\",\"menu\":{\"id\":\""+m.id+"\",\"items\":[{\"id\":\"3\",\"options\":{\"label\":\"Root 1\"},\"rootId\":\""+m.rootID+"\",\"submenu\":{\"id\":\"4\",\"items\":[{\"id\":\"5\",\"options\":{\"label\":\"Item 1\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"6\",\"options\":{\"label\":\"Item 2\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"7\",\"options\":{\"type\":\"separator\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"8\",\"options\":{\"label\":\"Item 3\"},\"rootId\":\""+m.rootID+"\"}],\"rootId\":\""+m.rootID+"\"}}],\"rootId\":\""+m.rootID+"\"},\"menuPopupOptions\":{\"x\":250,\"y\":250}}\n", EventNameTrayEventContextMenuPoppedUp, nil)
testObjectAction(t, func() error { return tr.Destroy() }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdDestroy+"\",\"targetID\":\""+tr.id+"\"}\n", EventNameTrayEventDestroyed, nil)
assert.True(t, tr.ctx.Err() != nil)
}
func TestTray_NewMenu(t *testing.T) {
a, err := New(nil, Options{})
assert.NoError(t, err)
tr := a.NewTray(&TrayOptions{})
m := tr.NewMenu([]*MenuItemOptions{})
assert.Equal(t, tr.id, m.rootID)
}