diff options
Diffstat (limited to 'gui/internal/model/preview_test.go')
| -rw-r--r-- | gui/internal/model/preview_test.go | 72 |
1 files changed, 63 insertions, 9 deletions
diff --git a/gui/internal/model/preview_test.go b/gui/internal/model/preview_test.go index b620a92..388ff7b 100644 --- a/gui/internal/model/preview_test.go +++ b/gui/internal/model/preview_test.go @@ -27,21 +27,21 @@ func TestPreviewKinds(t *testing.T) { binary := write("a.bin", []byte{0, 1, 2, 3, 0}) tmp := t.TempDir() - if p := MakePreview(context.Background(), text, tmp); p.Kind != PreviewText || + if p := MakePreview(context.Background(), text, tmp, 0); p.Kind != PreviewText || !strings.Contains(p.Text, "second line") { t.Errorf("text preview = %+v", p) } - if p := MakePreview(context.Background(), png, tmp); p.Kind != PreviewImage || p.Image != png { + if p := MakePreview(context.Background(), png, tmp, 0); p.Kind != PreviewImage || p.Image != png { t.Errorf("image preview = %+v", p) } - if p := MakePreview(context.Background(), binary, tmp); p.Kind != PreviewNone || + if p := MakePreview(context.Background(), binary, tmp, 0); p.Kind != PreviewNone || !strings.Contains(p.Note, "no preview") { t.Errorf("binary preview = %+v", p) } - if p := MakePreview(context.Background(), filepath.Join(dir, "gone.txt"), tmp); p.Kind != PreviewNone { + if p := MakePreview(context.Background(), filepath.Join(dir, "gone.txt"), tmp, 0); p.Kind != PreviewNone { t.Errorf("a missing file = %+v", p) } - if p := MakePreview(context.Background(), dir, tmp); p.Kind != PreviewNone { + if p := MakePreview(context.Background(), dir, tmp, 0); p.Kind != PreviewNone { t.Errorf("a directory = %+v", p) } } @@ -59,7 +59,7 @@ func TestPreviewReadsOnly(t *testing.T) { if err != nil { t.Fatal(err) } - MakePreview(context.Background(), p, t.TempDir()) + MakePreview(context.Background(), p, t.TempDir(), 0) after, err := os.Stat(p) if err != nil { t.Fatal(err) @@ -86,7 +86,7 @@ func TestPreviewShowsOnlyTheHead(t *testing.T) { if err := os.WriteFile(p, []byte(b.String()), 0o644); err != nil { t.Fatal(err) } - pv := MakePreview(context.Background(), p, t.TempDir()) + pv := MakePreview(context.Background(), p, t.TempDir(), 0) if pv.Kind != PreviewText { t.Fatalf("preview = %+v", pv) } @@ -129,8 +129,8 @@ func TestPdfPreviewsDoNotMixUp(t *testing.T) { second := pdf("second.pdf", "The second document") tmp := t.TempDir() - a := MakePreview(context.Background(), first, tmp) - b := MakePreview(context.Background(), second, tmp) + a := MakePreview(context.Background(), first, tmp, 0) + b := MakePreview(context.Background(), second, tmp, 0) if a.Kind != PreviewImage || b.Kind != PreviewImage { t.Fatalf("previews = %+v, %+v", a, b) } @@ -152,3 +152,57 @@ func TestPdfPreviewsDoNotMixUp(t *testing.T) { } } } + +// pngSize reads a PNG's dimensions from its header, so a test can say how +// large a page was rendered without decoding it. +func pngSize(t *testing.T, path string) (w, h int) { + t.Helper() + data, err := os.ReadFile(path) + if err != nil || len(data) < 24 || string(data[1:4]) != "PNG" { + t.Fatalf("%s is not a PNG: %v", path, err) + } + be := func(b []byte) int { + return int(b[0])<<24 | int(b[1])<<16 | int(b[2])<<8 | int(b[3]) + } + return be(data[16:20]), be(data[20:24]) +} + +// TestPreviewRendersToTheSizeAsked: a taller preview gets a larger page, so +// dragging the pane open does not just magnify a small render (his request, +// 2026-09-17). +func TestPreviewRendersToTheSizeAsked(t *testing.T) { + if _, err := exec.LookPath("pdftoppm"); err != nil { + t.Skip("pdftoppm is not installed") + } + if _, err := exec.LookPath("groff"); err != nil { + t.Skip("groff is not installed, so there is nothing to make a PDF with") + } + dir := t.TempDir() + path := filepath.Join(dir, "doc.pdf") + f, err := os.Create(path) + if err != nil { + t.Fatal(err) + } + cmd := exec.Command("groff", "-Tpdf", "-ms") + cmd.Stdin = strings.NewReader(".TL\nA document\n") + cmd.Stdout = f + if err := cmd.Run(); err != nil { + f.Close() + t.Skipf("groff cannot write a PDF here: %v", err) + } + f.Close() + + small := MakePreview(context.Background(), path, t.TempDir(), 200) + large := MakePreview(context.Background(), path, t.TempDir(), 900) + if small.Kind != PreviewImage || large.Kind != PreviewImage { + t.Fatalf("previews = %+v, %+v", small, large) + } + _, sh := pngSize(t, small.Image) + _, lh := pngSize(t, large.Image) + if lh <= sh { + t.Errorf("asking for a taller preview rendered %d px, no more than %d", lh, sh) + } + if sh < 700 { + t.Errorf("the smallest render is %d px, below the floor", sh) + } +} |
