Skip to content

Commit

Permalink
Merge pull request #18 from thiagokokada/improve-event-testing
Browse files Browse the repository at this point in the history
Improve event testing
  • Loading branch information
thiagokokada authored Aug 31, 2024
2 parents ecee9b0 + 12996a9 commit 5b4dc25
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 14 deletions.
2 changes: 1 addition & 1 deletion event/event_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
124 changes: 113 additions & 11 deletions event/event_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
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 {
EventClient
}

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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion event/event_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -109,6 +109,8 @@ type MoveWorkspace struct {
MonitorName
}

type Fullscreen bool

type MonitorName string

type FocusedMonitor struct {
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 5b4dc25

Please sign in to comment.