|
| 1 | +//go:build linux || freebsd || openbsd || netbsd || dragonfly || solaris || illumos || aix |
| 2 | + |
| 3 | +package x11tray |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/godbus/dbus/v5" |
| 12 | +) |
| 13 | + |
| 14 | +func skipIfNoDBus(t *testing.T) { |
| 15 | + t.Helper() |
| 16 | + conn, err := dbus.ConnectSessionBus() |
| 17 | + if err != nil { |
| 18 | + t.Skipf("D-Bus session bus not available: %v", err) |
| 19 | + } |
| 20 | + conn.Close() |
| 21 | +} |
| 22 | + |
| 23 | +func TestHealthCheck(t *testing.T) { |
| 24 | + skipIfNoDBus(t) |
| 25 | + |
| 26 | + err := HealthCheck() |
| 27 | + if err == nil { |
| 28 | + return // system tray available |
| 29 | + } |
| 30 | + // Verify error mentions the expected service |
| 31 | + if !strings.Contains(err.Error(), "StatusNotifierWatcher") && !strings.Contains(err.Error(), "system tray") { |
| 32 | + t.Errorf("HealthCheck() error = %v, want mention of StatusNotifierWatcher or system tray", err) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestProxyProcessStop(t *testing.T) { |
| 37 | + t.Run("nil", func(t *testing.T) { |
| 38 | + p := &ProxyProcess{} |
| 39 | + if err := p.Stop(); err != nil { |
| 40 | + t.Errorf("Stop() = %v, want nil", err) |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + t.Run("with cancel", func(t *testing.T) { |
| 45 | + ctx, cancel := context.WithCancel(context.Background()) |
| 46 | + p := &ProxyProcess{cancel: cancel} |
| 47 | + |
| 48 | + if err := p.Stop(); err != nil { |
| 49 | + t.Errorf("Stop() = %v, want nil", err) |
| 50 | + } |
| 51 | + select { |
| 52 | + case <-ctx.Done(): |
| 53 | + // expected |
| 54 | + default: |
| 55 | + t.Error("Stop() did not cancel context") |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("unstarted cmd", func(t *testing.T) { |
| 60 | + p := &ProxyProcess{cmd: exec.Command("echo", "test")} |
| 61 | + if err := p.Stop(); err != nil { |
| 62 | + t.Errorf("Stop() = %v, want nil", err) |
| 63 | + } |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +func TestTryProxy(t *testing.T) { |
| 68 | + p, err := TryProxy(context.Background()) |
| 69 | + if err == nil { |
| 70 | + p.Stop() |
| 71 | + return // snixembed installed and working |
| 72 | + } |
| 73 | + if !strings.Contains(err.Error(), "snixembed") { |
| 74 | + t.Errorf("TryProxy() error = %v, want mention of snixembed", err) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestEnsureTray(t *testing.T) { |
| 79 | + skipIfNoDBus(t) |
| 80 | + |
| 81 | + p, err := EnsureTray(context.Background()) |
| 82 | + if err == nil { |
| 83 | + if p != nil { |
| 84 | + p.Stop() |
| 85 | + } |
| 86 | + return |
| 87 | + } |
| 88 | + // Verify error is reasonable |
| 89 | + msg := err.Error() |
| 90 | + if !strings.Contains(msg, "tray") && !strings.Contains(msg, "proxy") && !strings.Contains(msg, "snixembed") { |
| 91 | + t.Errorf("EnsureTray() error = %v, want mention of tray/proxy/snixembed", err) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestShowContextMenu(t *testing.T) { |
| 96 | + // Should not panic regardless of D-Bus availability |
| 97 | + ShowContextMenu() |
| 98 | +} |
0 commit comments