1package ui
2
3import (
4 "testing"
5
6 "github.com/bnema/nefergui/internal/css"
7 "github.com/bnema/nefergui/internal/layout"
8 "github.com/bnema/nefergui/internal/text"
9)
10
11// Every visible string a view declares must reach the display list as shaped
12// glyphs. Snapshots alone once froze labelless controls; this invariant does not
13// depend on a golden file.
14func TestDeclaredTextIsPainted(t *testing.T) {
15 catalog, err := text.Load(text.DirectorySource("../../testdata/fonts"))
16 if err != nil {
17 t.Fatal(err)
18 }
19 r := newRuntime()
20 r.setTextEngine(text.NewEngine(catalog))
21 r.styles = css.Compile(css.UA(), css.Parse(`input,textarea{width:200px;height:40px}`))
22 checked, choice, v := false, "", 3.0
23 name, notes, filled := "", "", "set"
24 view := func(f *Frame) {
25 root := f.Root()
26 root.Button("Continuer", Key("button"))
27 root.Checkbox("Thème sombre", &checked, Key("checkbox"))
28 root.Radio("Option", "a", &choice, Key("radio"))
29 root.Slider("Volume", &v, 0, 10, 1, Key("slider"))
30 root.Input("Nom", &name, Key("input"), Placeholder("Ada"))
31 root.Textarea("Notes", ¬es, Key("textarea"), Placeholder("Écrire…"))
32 root.Input("Rempli", &filled, Key("filled"), Placeholder("hidden"))
33 root.Button("Off", Key("disabled"), Disabled(true))
34 }
35 r.Build(view)
36 painted := map[string]layout.Command{}
37 for _, cmd := range r.output.Display {
38 if cmd.Op == "text" && glyphCount(cmd) > 0 {
39 painted[cmd.Text] = cmd
40 }
41 }
42 for _, want := range []string{"Continuer", "Thème sombre", "Option", "Volume", "Ada", "Écrire…", "set", "Off"} {
43 if _, ok := painted[want]; !ok {
44 t.Errorf("%q declared but not painted; painted: %v", want, keys(painted))
45 }
46 }
47 if _, ok := painted["hidden"]; ok {
48 t.Error("placeholder painted over a non-empty value")
49 }
50 if p := painted["Ada"]; p.Opacity >= 1 || p.Opacity <= 0 {
51 t.Errorf("placeholder opacity %v, want dimmed", p.Opacity)
52 }
53 // The placeholder never enters the model, and typing replaces it.
54 if name != "" {
55 t.Fatalf("placeholder leaked into value: %q", name)
56 }
57 r.route(key("Tab"))
58 r.Build(view)
59 for i := 0; i < 4; i++ {
60 r.route(key("Tab"))
61 r.Build(view)
62 }
63 r.route(typed("b", "b"))
64 r.Build(view)
65 for _, cmd := range r.output.Display {
66 if cmd.Text == "Ada" {
67 t.Fatalf("placeholder still painted after typing %q", name)
68 }
69 }
70 if name != "b" {
71 t.Fatalf("typed into wrong control: name=%q", name)
72 }
73}
74
75// Placeholders follow editor text geometry: text-align and RTL position,
76// letter-spacing, and textarea wrapping.
77func TestPlaceholderGeometry(t *testing.T) {
78 catalog, err := text.Load(text.DirectorySource("../../testdata/fonts"))
79 if err != nil {
80 t.Fatal(err)
81 }
82 placeholder := func(sheet, typ, p string) layout.Command {
83 t.Helper()
84 r := newRuntime()
85 r.setTextEngine(text.NewEngine(catalog))
86 r.styles = css.Compile(css.UA(), css.Parse(sheet))
87 value := ""
88 r.Build(func(f *Frame) {
89 if typ == "textarea" {
90 f.Root().Textarea("", &value, Placeholder(p))
91 } else {
92 f.Root().Input("", &value, Placeholder(p))
93 }
94 })
95 for _, cmd := range r.output.Display {
96 if cmd.Text == p {
97 return cmd
98 }
99 }
100 t.Fatalf("placeholder %q not painted", p)
101 return layout.Command{}
102 }
103 span := func(cmd layout.Command) (lo, hi float64, lines int) {
104 lo, hi = 1e9, -1e9
105 ys := map[float64]bool{}
106 for _, run := range cmd.Runs {
107 ys[run.Y] = true
108 for _, g := range run.Glyphs {
109 lo, hi = min(lo, g.X), max(hi, g.X+g.Advance)
110 }
111 }
112 return lo, hi, len(ys)
113 }
114 box := `input,textarea{width:300px;height:80px;font-size:16px}`
115 leftLo, leftHi, _ := span(placeholder(box, "input", "Ada"))
116 if leftLo > 1 {
117 t.Errorf("start-aligned LTR placeholder at x=%v", leftLo)
118 }
119 if lo, hi, _ := span(placeholder(box+`input{text-align:right}`, "input", "Ada")); hi < 299 || lo < 200 {
120 t.Errorf("right-aligned placeholder spans %v..%v", lo, hi)
121 }
122 if lo, hi, _ := span(placeholder(box, "input", "مرحبا")); hi < 299 || lo < 200 {
123 t.Errorf("RTL start-aligned placeholder spans %v..%v", lo, hi)
124 }
125 if lo, hi, _ := span(placeholder(box+`input{letter-spacing:4px}`, "input", "Ada")); hi-lo < leftHi-leftLo+8 {
126 t.Errorf("letter-spacing ignored: width %v vs %v", hi-lo, leftHi-leftLo)
127 }
128 long := "Une longue phrase d'exemple qui dépasse la largeur du champ de texte"
129 if _, hi, lines := span(placeholder(box, "textarea", long)); lines < 2 || hi > 300.5 {
130 t.Errorf("textarea placeholder: %d lines, right edge %v", lines, hi)
131 }
132}
133
134func glyphCount(cmd layout.Command) int {
135 n := 0
136 for _, run := range cmd.Runs {
137 n += len(run.Glyphs)
138 }
139 return n
140}
141
142func keys(m map[string]layout.Command) []string {
143 out := make([]string, 0, len(m))
144 for k := range m {
145 out = append(out, k)
146 }
147 return out
148}