| 1 | //go:build windows |
| 2 | |
| 3 | package systray |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "golang.org/x/sys/windows" |
| 8 | "testing" |
| 9 | "unsafe" |
| 10 | ) |
| 11 | |
| 12 | func TestInitialGUIDFailureFallsBackWithoutDeletingOtherIcon(t *testing.T) { |
| 13 | previous := shellNotifyIcon |
| 14 | t.Cleanup(func() { shellNotifyIcon = previous }) |
| 15 | for _, failGUID := range []bool{false, true} { |
| 16 | t.Run(map[bool]string{false: "signed GUID", true: "GUID rejected"}[failGUID], func(t *testing.T) { |
| 17 | guid, _ := windows.GUIDFromString("{AF8B2B6E-CF17-43B9-AFB9-B0BF2695D8AC}") |
| 18 | tray := winTray{useIconGUID: true, iconGUID: guid, nid: ¬ifyIconData{Flags: 0x21, GuidItem: guid, Wnd: 42, ID: 100}} |
| 19 | calls := 0 |
| 20 | shellNotifyIcon = func(a ...uintptr) (uintptr, uintptr, error) { |
| 21 | if a[0] != 0 { |
| 22 | t.Fatalf("registration attempted operation %d, must not delete", a[0]) |
| 23 | } |
| 24 | n := (*notifyIconData)(unsafe.Pointer(a[1])) |
| 25 | calls++ |
| 26 | if calls == 1 && failGUID { |
| 27 | return 0, 0, errors.New("GUID belongs to another path") |
| 28 | } |
| 29 | wantGUID := !failGUID |
| 30 | if (n.Flags&0x20 != 0) != wantGUID || (n.GuidItem != windows.GUID{}) != wantGUID { |
| 31 | t.Fatalf("wrong registration identity: %+v", n) |
| 32 | } |
| 33 | if n.Wnd != 42 || n.ID != 100 { |
| 34 | t.Fatal("lost window identity") |
| 35 | } |
| 36 | return 1, 0, nil |
| 37 | } |
| 38 | if err := tray.addInitialIcon(); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | if tray.useIconGUID == failGUID { |
| 42 | t.Fatal("mode not retained") |
| 43 | } |
| 44 | // Explorer's taskbar-created handler re-adds the retained notification data. |
| 45 | if err := tray.nid.add(); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | if failGUID && calls != 3 || !failGUID && calls != 2 { |
| 49 | t.Fatalf("calls=%d", calls) |
| 50 | } |
| 51 | }) |
| 52 | } |
| 53 | } |
| 54 |