package ui import ( "image" "slices" "testing" "gioui.org/f32" "gioui.org/io/pointer" "100gex/internal/rex" "100gex/internal/rex/rextest" "100gex/internal/rexsession" ) // TestTabDrag drags tabs to new places in the window order, and checks that // clicks, and presses that wobble less than dragSlop, still switch tabs. func TestTabDrag(t *testing.T) { srv := rextest.Start(t) other := srv.Dial(t, "other-device") sid, _ := rextest.NewSession(t, other, rextest.RawCat...) for _, label := range []string{"two", "three"} { rextest.Call(t, other, "session.new_window", map[string]any{ "session_id": sid, "window_label": label, "layout": rextest.Block(rextest.RawCat...), "focus": false, }, nil) } c := srv.Dial(t, "100gex-test") h := startUI(t, srv, c, sid) f := h.until("three tabs", func(f rexsession.Frame) bool { return len(f.View.Windows) == 3 && f.Ready() }) ids := func(f rexsession.Frame) []string { var out []string for _, w := range f.View.Windows { out = append(out, w.WindowID) } return out } first, second, third := ids(f)[0], ids(f)[1], ids(f)[2] serverOrder := func() []string { var v rex.View rextest.Call(t, other, "session.view", map[string]any{"session_id": sid}, &v) var out []string for _, w := range v.Windows { out = append(out, w.WindowID) } return out } send := func(kind pointer.Kind, p image.Point) { h.router.Queue(pointer.Event{Kind: kind, Source: pointer.Mouse, Position: f32.Pt(float32(p.X), float32(p.Y)), Buttons: pointer.ButtonPrimary}) h.frame() } middle := func(id string) image.Point { r := h.u.tabRects[id] return r.Min.Add(r.Size().Div(2)) } // drag presses at from and moves to to in steps, a frame each, as a // person's drag does, then lets go. drag := func(from, to image.Point) { send(pointer.Press, from) const steps = 8 for i := 1; i <= steps; i++ { send(pointer.Move, from.Add(to.Sub(from).Mul(i).Div(steps))) } send(pointer.Release, to) } waitOrder := func(what string, want ...string) { t.Helper() // The view changes at once; the server, once the request is made. h.until(what, func(f rexsession.Frame) bool { return slices.Equal(ids(f), want) && slices.Equal(serverOrder(), want) }) } // The third tab, dropped over the first's left half, goes first. drag(middle(third), h.u.tabRects[first].Min.Add(image.Pt(4, 10))) waitOrder("the third tab moved first", third, first, second) if h.u.tabDrag.active { t.Error("the drag is still going after the release") } if got := h.frame().View.ActiveWindowID; got != first { t.Errorf("dragging a tab switched to %s; the first window was active", got) } // The tab now first, dragged past the end, goes last. drag(middle(third), image.Pt(testWidth-100, middle(third).Y)) waitOrder("the third tab moved last", first, second, third) // A short move, past the second's middle, swaps neighbours. r := h.u.tabRects[second] drag(middle(first), image.Pt(r.Max.X-2, middle(first).Y)) waitOrder("the first tab moved after the second", second, first, third) // A press that wobbles less than dragSlop is a click: it switches to the tab // and leaves the order be. p := middle(third) send(pointer.Press, p) send(pointer.Move, p.Add(image.Pt(2, 1))) send(pointer.Release, p.Add(image.Pt(2, 1))) h.until("the click to switch to the third tab", func(f rexsession.Frame) bool { return f.View.ActiveWindowID == third }) if got := ids(h.frame()); !slices.Equal(got, []string{second, first, third}) { t.Errorf("a click moved tabs: %v", got) } }