Files
openchamber-android-native/third_party/mygo/ui/richtext/metrics_test.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

120 lines
4.0 KiB
Go

package richtext
import (
"math"
"testing"
"github.com/egoist/mygo/ui"
)
// about reports whether two lengths in DIPs are the same within a pixel's
// rounding.
func about(a, b float32) bool { return math.Abs(float64(a-b)) < 0.6 }
// A heading's LineHeight sets the height of its lines in ems of its size,
// on each line it wraps to; without it a heading takes the text's. The
// multiples are above any platform's own spacing (Segoe UI's ascent and
// descent are 1.33 ems), as a line is never shorter than its font's
// ascent and descent.
func TestHeadingLineHeight(t *testing.T) {
doc := NewDocument(TextOf(
Paragraph{Style: ParagraphStyle{Heading: 1}, Runs: []Run{{Text: "Title"}}},
Paragraph{Style: ParagraphStyle{Heading: 2}, Runs: []Run{{Text: "wraps wraps wraps wraps wraps wraps wraps wraps wraps"}}},
Paragraph{Style: ParagraphStyle{Heading: 3}, Runs: []Run{{Text: "Text's"}}},
Paragraph{Runs: []Run{{Text: "body"}}},
))
th := &Theme{Headings: [6]HeadingStyle{
{Size: 24.0 / 15, LineHeight: 1.5},
{Size: 20.0 / 15, LineHeight: 1.5},
{Size: 16.0 / 15},
}}
tt := ui.NewTester(func(c *ui.Context) {
EditorBase(c, doc, EditorOptions{Font: ui.Font{Size: 15, LineHeight: 1.6}, Theme: th}).Fill()
}, 300, 400)
tt.Frame()
boxes := doc.VisibleParagraphs()
if len(boxes) != 4 {
t.Fatalf("paragraphs in view: %+v", boxes)
}
if h := boxes[0].Height; !about(h, 36) {
t.Errorf("a heading of 24 with lines of 36: %v high", h)
}
// How many lines the second wraps to depends on the platform's font.
lines := len(doc.shownIn.Value().paras[1].layout.Lines())
if lines < 2 {
t.Errorf("the second heading is on %d lines, want it to wrap", lines)
}
if h := boxes[1].Height; !about(h, float32(lines)*30) {
t.Errorf("a heading of 20 with lines of 30, on %d lines: %v high", lines, h)
}
if h := boxes[2].Height; !about(h, 16*1.6) {
t.Errorf("a heading without LineHeight takes the text's, %v: %v high", 16*1.6, h)
}
if h := boxes[3].Height; !about(h, 24) {
t.Errorf("body text %v high", h)
}
}
// Space less than zero takes room away from the theme's, down to none:
// a paragraph never runs into the one above.
func TestSpaceTakesAway(t *testing.T) {
doc := NewDocument(NewText("one\ntwo\nthree"))
var room float32
tt := ui.NewTester(func(c *ui.Context) {
EditorBase(c, doc, EditorOptions{Font: ui.Font{Size: 15, LineHeight: 22.0 / 15}, Theme: &Theme{ParagraphSpacing: 8.0 / 15},
Space: func(p int) float32 {
if p == 1 {
return room
}
return 0
}}).Fill()
}, 300, 200)
gap := func() float32 {
tt.Frame()
tt.Frame()
b := doc.VisibleParagraphs()
return b[1].Top - (b[0].Top + b[0].Height)
}
for _, c := range []struct{ room, want float32 }{{0, 8}, {12, 20}, {-3, 5}, {-8, 0}, {-40, 0}} {
room = c.room
if g := gap(); !about(g, c.want) {
t.Errorf("Space %v: %v between the paragraphs, want %v", c.room, g, c.want)
}
}
b := doc.VisibleParagraphs()
if g := b[2].Top - (b[1].Top + b[1].Height); !about(g, 8) {
t.Errorf("the paragraph after: %v above it", g)
}
}
// Above a block of code, Space less than zero takes room away from the
// block's padding at its top, inside its background, down to none.
func TestSpaceTakesAwayCodePadding(t *testing.T) {
doc := NewDocument(TextOf(
para("before"),
Paragraph{Style: ParagraphStyle{CodeBlock: true}, Runs: []Run{{Text: "code"}}},
))
var room float32
tt := ui.NewTester(func(c *ui.Context) {
EditorBase(c, doc, EditorOptions{Font: ui.Font{Size: 15, LineHeight: 22.0 / 15}, Theme: &Theme{CodeBlockPadding: [2]float32{12.0 / 15, 10.0 / 15}},
Space: func(p int) float32 {
if p == 1 {
return room
}
return 0
}}).Fill()
}, 300, 200)
gap := func() float32 {
tt.Frame()
tt.Frame()
b := doc.VisibleParagraphs()
return b[1].Top - (b[0].Top + b[0].Height)
}
for _, c := range []struct{ room, want float32 }{{0, 10}, {-4, 6}, {-10, 0}, {-30, 0}} {
room = c.room
if g := gap(); !about(g, c.want) {
t.Errorf("Space %v above a block of code: %v between the paragraphs, want %v", c.room, g, c.want)
}
}
}