From e482c4bc54392de5369421b8e239ad02936b2a0f Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sat, 31 Aug 2024 17:32:49 +0100 Subject: [PATCH 1/4] flake: enable test for all submodules --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 = From 848a8c482d29d9aace858935b40669b0275ef0e5 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sat, 31 Aug 2024 17:53:42 +0100 Subject: [PATCH 2/4] event/event_types: add Fullscreen type --- event/event_default.go | 2 +- event/event_types.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) 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_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 { From 9eeb8852f61bef327f4d7e095655bab3c5bbce7c Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sat, 31 Aug 2024 17:53:53 +0100 Subject: [PATCH 3/4] event/event_type: add assertion for each handler --- event/event_test.go | 97 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 86 insertions(+), 11 deletions(-) diff --git a/event/event_test.go b/event/event_test.go index 7ee04c7..54cf75a 100644 --- a/event/event_test.go +++ b/event/event_test.go @@ -2,11 +2,8 @@ package event import ( "testing" -) -var ( - h = &FakeEventHandler{} - c = &FakeEventClient{} + "github.com/thiagokokada/hyprland-go/internal/assert" ) type FakeEventClient struct { @@ -14,7 +11,15 @@ type FakeEventClient struct { } type FakeEventHandler struct { - DefaultEventHandler + t *testing.T + EventHandler +} + +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 +35,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 +101,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 { From 12996a945b3a1f6bf5b5b7b6b1ba15d66de22273 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sat, 31 Aug 2024 18:08:58 +0100 Subject: [PATCH 4/4] event/event_test: add TestReceive --- event/event_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/event/event_test.go b/event/event_test.go index 54cf75a..2f48960 100644 --- a/event/event_test.go +++ b/event/event_test.go @@ -1,8 +1,11 @@ package event import ( + "os" "testing" + "time" + "github.com/thiagokokada/hyprland-go" "github.com/thiagokokada/hyprland-go/internal/assert" ) @@ -15,6 +18,30 @@ type FakeEventHandler struct { 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{}