Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204package ui
import ( "context" "image" "testing" "time"
"gioui.org/gpu/headless" "gioui.org/layout" "gioui.org/op" "gioui.org/unit"
"100gex/internal/rex/rextest" "100gex/internal/theme" "100gex/internal/workspace")
// TestHeaderCentred renders a focused pane and checks that its title sits// in the middle of its header, with as much space above as below.func TestHeaderCentred(t *testing.T) { const w, h = 900, 300 win, err := headless.NewWindow(w, h) if err != nil { t.Skipf("no headless GPU context: %v", err) } defer win.Release()
srv := rextest.Start(t) other := srv.Dial(t, "other") // Capitals only, so the ink is the cap height, with no descenders. sid, _ := rextest.NewSession(t, other, "/bin/sh", "-c", `printf '\033]0;HEADER TITLE\007'; exec cat`) c := srv.Dial(t, "100gex-test") ws, err := workspace.Start(context.Background(), c, srv.Endpoint, sid, nil, func() {}) if err != nil { t.Fatal(err) } defer ws.Close() u := New(ws, c, theme.NewShaper(), func() {}) defer u.Close()
var ops op.Ops for deadline := time.Now().Add(10 * time.Second); ; time.Sleep(20 * time.Millisecond) { ops.Reset() gtx := layout.Context{Ops: &ops, Metric: unit.Metric{PxPerDp: 1, PxPerSp: 1}, Constraints: layout.Exact(image.Pt(w, h)), Now: time.Now()} f, _, err := u.Layout(gtx) if err != nil { t.Fatal(err) } if len(f.Visible) == 1 && programTitle(f, f.Visible[0]) == "HEADER TITLE" { break } if time.Now().After(deadline) { t.Fatal("the pane never got its title") } } if err := win.Frame(&ops); err != nil { t.Fatal(err) } img := image.NewRGBA(image.Rect(0, 0, w, h)) if err := win.Screenshot(img); err != nil { t.Fatal(err) }
// The header is the run of purple rows in the middle of the pane, // between the title at its left and the buttons at its right; the // title is the white ink at its left. isPurple := func(x, y int) bool { c := img.RGBAAt(x, y); return c.R == 0x70 && c.G == 0x28 && c.B == 0xC5 } x := w/2 + 100 top, bottom := -1, -1 for y := 0; y < h; y++ { if isPurple(x, y) { if top < 0 { top = y } bottom = y } else if top >= 0 { break } } if top < 0 { t.Fatal("no header found") } inkTop, inkBottom := -1, -1 left := int(theme.SidebarWide) + 2*int(theme.Gap) for y := top; y <= bottom; y++ { for x := left; x < left+150; x++ { if c := img.RGBAAt(x, y); c.R > 200 && c.G > 200 && c.B > 200 { if inkTop < 0 { inkTop = y } inkBottom = y break } } } if inkTop < 0 { t.Fatal("no title ink in the header") } above, below := inkTop-top, bottom-inkBottom if d := above - below; d < -2 || d > 2 { t.Errorf("title not centred in its header: %dpx above, %dpx below", above, below) }}
// TestSessionRowsAligned renders the sidebar and checks that a session's// name starts at the same place whether or not it's the current session, so// that switching doesn't make the text jump.func TestSessionRowsAligned(t *testing.T) { const w, h = 900, 400 win, err := headless.NewWindow(w, h) if err != nil { t.Skipf("no headless GPU context: %v", err) } defer win.Release()
srv := rextest.Start(t) other := srv.Dial(t, "other") for _, label := range []string{"Alpha", "Atlas"} { rextest.Call(t, other, "session.create", map[string]any{"label": label, "initial_windows": []any{ map[string]any{"layout": rextest.Block("/bin/sh", "-c", "exec cat")}, }}, nil) } c := srv.Dial(t, "100gex-test") ws, err := workspace.Start(context.Background(), c, srv.Endpoint, "Alpha", nil, func() {}) if err != nil { t.Fatal(err) } defer ws.Close() u := New(ws, c, theme.NewShaper(), func() {}) defer u.Close()
// starts renders the window once the session shown is want, and // returns where each row's name starts: the leftmost ink, right of the // rows' borders and accent bars, in each band of rows with ink. starts := func(want string) []int { t.Helper() var ops op.Ops for deadline := time.Now().Add(10 * time.Second); ; time.Sleep(20 * time.Millisecond) { ops.Reset() gtx := layout.Context{Ops: &ops, Metric: unit.Metric{PxPerDp: 1, PxPerSp: 1}, Constraints: layout.Exact(image.Pt(w, h)), Now: time.Now()} if _, _, err := u.Layout(gtx); err != nil { t.Fatal(err) } if u.session != nil && u.Title() == want+" — 100gex" && len(u.ws.Sessions()) == 2 { break } if time.Now().After(deadline) { t.Fatalf("never showed %s", want) } } if err := win.Frame(&ops); err != nil { t.Fatal(err) } img := image.NewRGBA(image.Rect(0, 0, w, h)) if err := win.Screenshot(img); err != nil { t.Fatal(err) } left := 16 + int(theme.AccentWide) + 4 // past a row's border and accent bar var bands []int inBand, start := false, 0 for y := int(theme.TitlebarHigh) + 60; y < h-60; y++ { first, dark, right := -1, 0, int(theme.SidebarWide)-20 for x := left; x < right; x++ { if c := img.RGBAAt(x, y); c.R < 0x60 && c.G < 0x60 && c.B < 0x60 { if first < 0 { first = x } dark++ } } if dark > (right-left)/2 { first = -1 // a box's border, not text } switch { case first >= 0 && !inBand: inBand, start = true, first case first >= 0: start = min(start, first) case inBand: inBand = false bands = append(bands, start) } } return bands }
for _, current := range []string{"Alpha", "Atlas"} { if current == "Atlas" { u.ws.Switch(u.ws.Sessions()[1].SessionID) } got := starts(current) if len(got) < 2 { t.Fatalf("with %s current, found %d rows of text, want at least 2", current, len(got)) } for _, x := range got[1:] { if x != got[0] { t.Errorf("with %s current, rows of text start at %v, want one x", current, got) break } } }}