acdream/tests/AcDream.App.Tests/UI/Layout/DatRichTextTests.cs
Erik e24ec20882 fix(chargen): Campaign CC gate round 1 Batch E — text origin, caption escapes, value rects, scrollbars, name prefill
R2-1/R2-6 (description-box text clipped left of the frame, regressed from
Batch C's frame un-consume): root cause was never the un-consume change
itself — the Heritage/Profession/Town/Summary description boxes
(0x100003C4/0x100003E0/0x10000409/0x10000404) all author retail's four
independent text-inset margins (dat properties 0x23-0x26,
UIElement_Text::OnSetAttribute cases 0xf-0x12: margL=9/margR=26/margU=15/
margD=15), which this codebase never read at all, before or after Batch C.
Un-consuming the gold-frame children just made the pre-existing missing-
margin bug visible for the first time (the frame's own left border now
draws around the same x=0 origin text always used). Fixed end to end:
ElementInfo.MarginLeft/Right/Top/Bottom (read in
ApplyCanonicalLegacyProjection, propagated in Merge), UiText.MarginLeft/
Right/Top/Bottom (additive with the pre-existing Padding), a new pure
UiText.ContentOffsetX static consumed by the multi-line draw path's
per-line placement, and matching wrap-width shrinkage in
DatRichText.Compose and BuildText's own authored-multiline path. Scoped to
the multi-line (non-OneLine) path only.

R2-2/R2-3 (Attribute\n Credits renders the literal backslash-n; the live
credit value overlaps mid-caption): two stacked gaps. (1) UiButton
captions never escape-normalized the DAT's literal "\n" — centralized the
normalize into DatWidgetFactory's ResolveAuthoredString (the one choke
point every P0x17 resolution already shares) plus a NormalizeEscapes
helper for the per-state caption loop, so every caller normalizes
identically. (2) UiButton.Label only ever drew one line — retail's
UIElement_Button IS a UIElement_Text with OneLine=false on these buttons,
so a caption should word-wrap/stack like any other Type-12 box. Added
UiButton.DrawBlockLabel + the pure, unit-tested WrapBlockLines. The
value-overlap itself: ValueBox was never wrong (live-DAT-measured correct
child rects) — the caption was drawing unconfined across the button's
full width ("Available Skill Credits" measures 193px in a 231px button
whose value box starts at x=116). Fixed by confining the caption's own
drawable width to stop before ValueBox.X whenever a ValueLabel coexists.

R2-7a (Summary overview listbox missing its scrollbar): pure wiring gap —
the listbox authors a linked scrollbar via dat property 0x72
(ScrollbarElementId=0x10000401) that CharacterCreationSummaryPage's
constructor never resolved, unlike every other UiTemplateListBox owner in
the codebase. Fixed with the same resolve-and-wire pattern.

R2-7b (how-to box scrollbar overlaps text, no thumb): traced to a
downstream symptom of R2-1, not an independent bug — UiScrollbar only
paints its thumb when the linked model has overflow, and the pre-fix wrap
width (un-inset) produced fewer/shorter lines than fit the view. Pinned
directly against the real installed strings/font (Aluvian's how-to text)
that the margin-correct width overflows. No UiScrollbar code changed.

R2-8 (name field should show "[ Name ]"): re-checked the one hypothesis
Batch A's GF-15 closure left open — an authored initial-text string on
the field's own P0x17. Confirmed absent on every state in the installed
DAT. No code change; Batch A's closure stands, now pinned as a live-DAT
regression test.

App suite 5334/3 (was 5321/3, +13, zero regressions). Runtime 1735/0
unchanged. Full solution Release build green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-16 14:07:19 +02:00

147 lines
5.3 KiB
C#

using System.Numerics;
using AcDream.App.UI;
using AcDream.App.UI.Layout;
namespace AcDream.App.Tests.UI.Layout;
/// <summary>
/// Campaign CC gate round 1 Batch C: unit tests for the shared
/// escape-normalize + word-wrap + per-segment-color helper feeding
/// GF-2/GF-3/GF-11a and the Summary how-to text (Commit 3).
/// </summary>
public class DatRichTextTests
{
private static readonly Vector4 White = Vector4.One;
private static readonly Vector4 Green = new(0f, 1f, 0f, 1f);
private static UiText MakeTarget(float width) =>
new() { Width = width, Height = 200f };
[Fact]
public void Compose_NormalizesLiteralBackslashNEscape()
{
UiText target = MakeTarget(1000f); // wide enough that nothing wraps
var segments = new[] { new DatRichText.Segment("line one\\nline two", White) };
var lines = DatRichText.Compose(target, segments);
Assert.Equal(2, lines.Count);
Assert.Equal("line one", lines[0].Text);
Assert.Equal("line two", lines[1].Text);
}
[Fact]
public void Compose_WordWrapsToTheTargetWidth()
{
// Bitmap-font-shaped measure: 8px/char, matching BuildText's own
// authored-string fallback measure.
UiText target = MakeTarget(80f); // 10 chars per line at 8px/char
var segments = new[]
{
new DatRichText.Segment("one two three four five six seven eight", White),
};
var lines = DatRichText.Compose(target, segments);
Assert.True(lines.Count > 1, "a long segment must wrap to more than one line");
foreach (UiText.Line line in lines)
Assert.True(line.Text.Length * 8f <= 80f, $"line '{line.Text}' overflowed the target width");
}
[Fact]
public void Compose_EachSegmentKeepsItsOwnColorAcrossItsWrappedLines()
{
UiText target = MakeTarget(1000f);
var segments = new[]
{
new DatRichText.Segment("Header:", Green),
new DatRichText.Segment("Body text.", White),
};
var lines = DatRichText.Compose(target, segments);
Assert.Equal(2, lines.Count);
Assert.Equal(Green, lines[0].Color);
Assert.Equal(White, lines[1].Color);
}
[Fact]
public void Compose_NullOrEmptySegmentText_IsSkipped()
{
UiText target = MakeTarget(1000f);
var segments = new[]
{
new DatRichText.Segment(null, White),
new DatRichText.Segment(string.Empty, White),
new DatRichText.Segment("real text", White),
};
var lines = DatRichText.Compose(target, segments);
Assert.Single(lines);
Assert.Equal("real text", lines[0].Text);
}
[Fact]
public void Compose_NoSeparatorInsertedBetweenSegments()
{
// Retail's own composition calls concatenate directly
// (AppendStringInfoWithFont / append_n_chars, no interposed
// literal) — this helper must not invent one either.
UiText target = MakeTarget(1000f);
var segments = new[]
{
new DatRichText.Segment("first", White),
new DatRichText.Segment("second", White),
};
var lines = DatRichText.Compose(target, segments);
// Each segment still wraps independently (so "first"/"second" stay
// on separate output lines, not glued into "firstsecond") — but no
// BLANK line is inserted between them unless the segment's own
// text carried one.
Assert.Equal(2, lines.Count);
Assert.Equal("first", lines[0].Text);
Assert.Equal("second", lines[1].Text);
}
[Fact]
public void Compose_WordWrapsToTheTargetWidth_MinusTheFourRetailMargins()
{
// R2-1 (Campaign CC gate round 1 Batch E): the wrap width must
// shrink by BOTH Padding and the four retail margins (properties
// 0x23-0x26 — MarginLeft's own doc comment on UiText), not just the
// element's raw Width. A 100px-wide box with margL=10/margR=10
// leaves only 80px of usable width — one 10-char/8px-per-char word
// ("aaaaaaaaaa", 80px) must fit on one line, but appending an 11th
// 'a' (88px) must force a wrap.
UiText fits = new() { Width = 100f, Height = 200f, MarginLeft = 10f, MarginRight = 10f };
var fitsLines = DatRichText.Compose(fits, [new DatRichText.Segment("aaaaaaaaaa", White)]);
Assert.Single(fitsLines);
UiText overflows = new() { Width = 100f, Height = 200f, MarginLeft = 10f, MarginRight = 10f };
var overflowLines = DatRichText.Compose(overflows, [new DatRichText.Segment("aaaaaaaaaaa", White)]);
Assert.True(overflowLines.Count > 1, "an 88px word in an 80px content width must wrap");
}
[Fact]
public void PaletteColor_ReturnsAuthoredPaletteEntry_WhenPresent()
{
UiText target = new()
{
FontColorPalette = [White, Green],
};
Assert.Equal(White, DatRichText.PaletteColor(target, 0, Green));
Assert.Equal(Green, DatRichText.PaletteColor(target, 1, White));
}
[Fact]
public void PaletteColor_FallsBack_WhenPaletteTooShortOrMissing()
{
UiText target = new(); // empty palette
Assert.Equal(Green, DatRichText.PaletteColor(target, 1, Green));
Assert.Equal(Green, DatRichText.PaletteColor(target, -1, Green));
}
}