package ui import ( "strings" "testing" "github.com/gdamore/tcell/v2" ) func rowText(screen tcell.SimulationScreen, y, x0, x1 int) string { var b strings.Builder for x := x0; x < x1; x++ { ch, _, _, _ := screen.GetContent(x, y) b.WriteRune(ch) } return b.String() } func TestDetailPaneLayout(t *testing.T) { a := newTestApp(t) screen := tcell.NewSimulationScreen("") if err := screen.Init(); err != nil { t.Fatal(err) } defer screen.Fini() screen.SetSize(60, 40) d := newDetailPane(a) d.SetRect(0, 0, 60, 40) d.Show(detailText{head: "[::b]The Matrix (1999)[::-]\nStatus: released", plot: "Plot text goes here"}, coverRef{}) // Without a poster everything is one text. d.Draw(screen) if got := rowText(screen, 1, 1, 30); !strings.HasPrefix(got, "The Matrix (1999)") { t.Errorf("first line = %q", got) } if got := rowText(screen, 3, 1, 30); !strings.HasPrefix(got, "Plot text goes here") { t.Errorf("without a poster the plot follows the head: %q", got) } // With a poster: head on the left, poster on the right, plot below both. d.hasCover = true d.cover.setImage(stripes(40, 60)) d.Draw(screen) if got := rowText(screen, 1, 1, 30); !strings.HasPrefix(got, "The Matrix (1999)") { t.Errorf("head moved: %q", got) } coverX := -1 for x := 59; x > 30; x-- { if ch, _, st, _ := screen.GetContent(x, 1); ch != ' ' || st != tcell.StyleDefault { coverX = x break } } if coverX < 55 { t.Errorf("no poster against the right edge (last drawn column %d)", coverX) } // The plot starts below the poster: 40*2/5 = 24 columns -> 24*3/2/2 = 18 rows. plotRow := -1 for y := 1; y < 39; y++ { if strings.HasPrefix(rowText(screen, y, 1, 30), "Plot text goes here") { plotRow = y break } } if plotRow < 18 { t.Errorf("plot at row %d, want below the poster", plotRow) } // A pane too short for the poster falls back to plain text. d.SetRect(0, 0, 60, 14) d.Draw(screen) if !d.combined { t.Error("a short pane must drop the poster") } }