diff --git a/event/event_default.go b/event/event_default.go index a78c83a..673e891 100644 --- a/event/event_default.go +++ b/event/event_default.go @@ -8,7 +8,7 @@ type DefaultEventHandler struct{} func (e *DefaultEventHandler) Workspace(WorkspaceName) {} func (e *DefaultEventHandler) FocusedMonitor(FocusedMonitor) {} func (e *DefaultEventHandler) ActiveWindow(ActiveWindow) {} -func (e *DefaultEventHandler) Fullscreen(bool) {} +func (e *DefaultEventHandler) Fullscreen(Fullscreen) {} func (e *DefaultEventHandler) MonitorRemoved(MonitorName) {} func (e *DefaultEventHandler) MonitorAdded(MonitorName) {} func (e *DefaultEventHandler) CreateWorkspace(WorkspaceName) {} diff --git a/event/event_test.go b/event/event_test.go index 7ee04c7..2f48960 100644 --- a/event/event_test.go +++ b/event/event_test.go @@ -1,12 +1,12 @@ package event import ( + "os" "testing" -) + "time" -var ( - h = &FakeEventHandler{} - c = &FakeEventClient{} + "github.com/thiagokokada/hyprland-go" + "github.com/thiagokokada/hyprland-go/internal/assert" ) type FakeEventClient struct { @@ -14,7 +14,39 @@ type FakeEventClient struct { } type FakeEventHandler struct { - DefaultEventHandler + t *testing.T + EventHandler +} + +func TestReceive(t *testing.T) { + if os.Getenv("HYPRLAND_INSTANCE_SIGNATURE") == "" { + t.Skip("HYPRLAND_INSTANCE_SIGNATURE not set, skipping test") + } + + // Generate an event + go func() { + c := hyprland.MustClient() + time.Sleep(100 * time.Millisecond) + c.Dispatch("exec kitty") + }() + + // We must capture this event + c := MustEventClient() + data, err := c.Receive() + + assert.NoError(t, err) + assert.True(t, len(data) >= 0) + for _, d := range data { + assert.NotEqual(t, string(d.Data), "") + assert.NotEqual(t, string(d.Type), "") + } +} + +func TestSubscribe(t *testing.T) { + h := &FakeEventHandler{t: t} + c := &FakeEventClient{} + err := SubscribeWithoutLoop(*c, h, AllEvents...) + assert.NoError(t, err) } func (f *FakeEventClient) Receive() ([]ReceivedData, error) { @@ -30,7 +62,7 @@ func (f *FakeEventClient) Receive() ([]ReceivedData, error) { }, { Type: EventActiveWindow, - Data: "jetbrains-goland,hyprland-ipc-ipc – ipc.go", + Data: "nvim,nvim event/event_test.go", }, { Type: EventFullscreen, @@ -96,11 +128,81 @@ func (f *FakeEventClient) Receive() ([]ReceivedData, error) { }, nil } -func TestSubscribe(t *testing.T) { - err := SubscribeWithoutLoop(*c, h, AllEvents...) - if err != nil { - t.Error(err) - } +func (h *FakeEventHandler) Workspace(w WorkspaceName) { + assert.Equal(h.t, w, "1") +} + +func (h *FakeEventHandler) FocusedMonitor(m FocusedMonitor) { + assert.Equal(h.t, m.WorkspaceName, "1") + assert.Equal(h.t, m.MonitorName, "1") +} + +func (h *FakeEventHandler) ActiveWindow(w ActiveWindow) { + assert.Equal(h.t, w.Name, "nvim") + assert.Equal(h.t, w.Title, "nvim event/event_test.go") +} + +func (h *FakeEventHandler) Fullscreen(f Fullscreen) { + assert.Equal(h.t, f, true) +} + +func (h *FakeEventHandler) MonitorRemoved(m MonitorName) { + assert.Equal(h.t, m, "1") +} + +func (h *FakeEventHandler) MonitorAdded(m MonitorName) { + assert.Equal(h.t, m, "1") +} + +func (h *FakeEventHandler) CreateWorkspace(w WorkspaceName) { + assert.Equal(h.t, w, "1") +} + +func (h *FakeEventHandler) DestroyWorkspace(w WorkspaceName) { + assert.Equal(h.t, w, "1") +} + +func (h *FakeEventHandler) MoveWorkspace(w MoveWorkspace) { + assert.Equal(h.t, w.WorkspaceName, "1") + assert.Equal(h.t, w.MonitorName, "1") +} + +func (h *FakeEventHandler) ActiveLayout(l ActiveLayout) { + assert.Equal(h.t, l.Name, "Russian") + assert.Equal(h.t, l.Type, "AT Translated Set 2 keyboard") +} + +func (h *FakeEventHandler) OpenWindow(o OpenWindow) { + assert.Equal(h.t, o.Address, "80e62df0") + assert.Equal(h.t, o.Class, "jetbrains-goland") + assert.Equal(h.t, o.Title, "win430") + assert.Equal(h.t, o.WorkspaceName, "2") +} + +func (h *FakeEventHandler) CloseWindow(c CloseWindow) { + assert.Equal(h.t, c.Address, "80e62df0") +} + +func (h *FakeEventHandler) MoveWindow(m MoveWindow) { + assert.Equal(h.t, m.Address, "80e62df0") + assert.Equal(h.t, m.WorkspaceName, "1") +} + +func (h *FakeEventHandler) OpenLayer(l OpenLayer) { + assert.Equal(h.t, l, "wofi") +} + +func (h *FakeEventHandler) CloseLayer(l CloseLayer) { + assert.Equal(h.t, l, "wofi") +} + +func (h *FakeEventHandler) SubMap(s SubMap) { + assert.Equal(h.t, s, "1") +} + +func (h *FakeEventHandler) Screencast(s Screencast) { + assert.Equal(h.t, s.Owner, "0") + assert.Equal(h.t, s.Sharing, true) } func SubscribeWithoutLoop(c FakeEventClient, ev EventHandler, events ...EventType) error { diff --git a/event/event_types.go b/event/event_types.go index 67f0734..2b9819b 100644 --- a/event/event_types.go +++ b/event/event_types.go @@ -29,7 +29,7 @@ type EventHandler interface { // ActiveWindow emitted on the active window being changed. ActiveWindow(w ActiveWindow) // Fullscreen emitted when a fullscreen status of a window changes. - Fullscreen(f bool) + Fullscreen(f Fullscreen) // MonitorRemoved emitted when a monitor is removed (disconnected). MonitorRemoved(m MonitorName) // MonitorAdded emitted when a monitor is added (connected). @@ -109,6 +109,8 @@ type MoveWorkspace struct { MonitorName } +type Fullscreen bool + type MonitorName string type FocusedMonitor struct { diff --git a/flake.nix b/flake.nix index b17cafb..91e3996 100644 --- a/flake.nix +++ b/flake.nix @@ -96,7 +96,7 @@ cd ${./.} export CI=1 - go test -bench=. -coverprofile ${covOut} -v > ${testLog} 2>&1 + go test -bench=. -coverprofile ${covOut} -v ./... > ${testLog} 2>&1 go tool cover -html=${covOut} -o ${covHtml} ''; hyprlandConf =