acdream/tests/AcDream.UI.Abstractions.Tests/FakePanelRenderer.cs
Erik b131514d51 feat(ui): #14 IPanelRenderer widget extension - TextColored, Checkbox, Combo, InputTextSubmit, BeginTable, etc.
Adds 14 widget signatures to IPanelRenderer + ImGuiPanelRenderer impl:
TextColored, CollapsingHeader, TreeNode/TreePop, Checkbox, Button,
Combo, SliderFloat, PlotLines, BeginTable/TableNextColumn/EndTable,
InputTextSubmit (Enter-key submit), Spacing, Dummy, TextWrapped.

InputTextSubmit uses ImGuiInputTextFlags.EnterReturnsTrue and clears
the buffer + emits via `out submitted` on the frame Enter is pressed.
PlotLines passes `ref values[0]` with empty-array guard. CollapsingHeader
defaultOpen=true uses ImGuiTreeNodeFlags.DefaultOpen (= 0x20).

FakePanelRenderer test double records (Method, Args) tuples and
exposes knobs to drive ref/out values. 17 new tests dispatch through
IPanelRenderer (not the concrete fake) so tests fail to compile when
the interface itself lacks a method - real RED -> GREEN signal.

Tests: 26 -> 43 in UI.Abstractions.Tests. Total solution 881 green.
Foundation for Phase I.2 (DebugPanel) and I.4 (ChatPanel input field).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 19:03:28 +02:00

142 lines
5.5 KiB
C#

using System.Numerics;
namespace AcDream.UI.Abstractions.Tests;
/// <summary>
/// In-memory test double for <see cref="IPanelRenderer"/>. Records every
/// widget call so tests can assert against the recorded trace without
/// running an ImGui context. New widgets added to the interface must
/// implement here too — that compilation error IS the TDD red signal.
/// </summary>
internal sealed class FakePanelRenderer : IPanelRenderer
{
/// <summary>Ordered list of (method, args) pairs recorded across this renderer's lifetime.</summary>
public List<(string Method, object?[] Args)> Calls { get; } = new();
/// <summary>If <see cref="Begin"/> is called, return this value. Defaults to true so panels render.</summary>
public bool BeginReturns { get; set; } = true;
// -- Inputs the panel under test can mutate via ref args -----------
/// <summary>Pre-set return value for the next <see cref="Checkbox"/> — caller flips bool to simulate user click.</summary>
public bool CheckboxNextReturn { get; set; }
public bool? CheckboxNextValue { get; set; }
/// <summary>Pre-set return value for the next <see cref="Button"/> — caller sets to simulate user click.</summary>
public bool ButtonNextReturn { get; set; }
/// <summary>Pre-set return value for the next <see cref="Combo"/>.</summary>
public bool ComboNextReturn { get; set; }
public int? ComboNextSelectedIndex { get; set; }
/// <summary>Pre-set return for <see cref="SliderFloat"/>.</summary>
public bool SliderFloatNextReturn { get; set; }
public float? SliderFloatNextValue { get; set; }
/// <summary>Pre-set return for <see cref="CollapsingHeader"/>.</summary>
public bool CollapsingHeaderNextReturn { get; set; } = true;
/// <summary>Pre-set return for <see cref="TreeNode"/>.</summary>
public bool TreeNodeNextReturn { get; set; } = true;
/// <summary>Pre-set return for <see cref="BeginTable"/>.</summary>
public bool BeginTableNextReturn { get; set; } = true;
/// <summary>Pre-set "submitted" string for the next <see cref="InputTextSubmit"/>; null = no submit this frame.</summary>
public string? InputTextSubmitNextSubmitted { get; set; }
public string? InputTextSubmitNextBufferAfter { get; set; }
public bool Begin(string title)
{
Calls.Add(("Begin", new object?[] { title }));
return BeginReturns;
}
public void End() => Calls.Add(("End", Array.Empty<object?>()));
public void Text(string text) => Calls.Add(("Text", new object?[] { text }));
public void SameLine() => Calls.Add(("SameLine", Array.Empty<object?>()));
public void Separator() => Calls.Add(("Separator", Array.Empty<object?>()));
public void ProgressBar(float fraction, float width, string? overlay = null)
=> Calls.Add(("ProgressBar", new object?[] { fraction, width, overlay }));
public void TextColored(Vector4 rgba, string text)
=> Calls.Add(("TextColored", new object?[] { rgba, text }));
public bool CollapsingHeader(string label, bool defaultOpen = true)
{
Calls.Add(("CollapsingHeader", new object?[] { label, defaultOpen }));
return CollapsingHeaderNextReturn;
}
public bool TreeNode(string label)
{
Calls.Add(("TreeNode", new object?[] { label }));
return TreeNodeNextReturn;
}
public void TreePop() => Calls.Add(("TreePop", Array.Empty<object?>()));
public bool Checkbox(string label, ref bool value)
{
Calls.Add(("Checkbox", new object?[] { label, value }));
if (CheckboxNextValue is bool nv) value = nv;
return CheckboxNextReturn;
}
public bool Button(string label)
{
Calls.Add(("Button", new object?[] { label }));
return ButtonNextReturn;
}
public bool Combo(string label, ref int selectedIndex, string[] items)
{
Calls.Add(("Combo", new object?[] { label, selectedIndex, items }));
if (ComboNextSelectedIndex is int idx) selectedIndex = idx;
return ComboNextReturn;
}
public bool SliderFloat(string label, ref float value, float min, float max)
{
Calls.Add(("SliderFloat", new object?[] { label, value, min, max }));
if (SliderFloatNextValue is float v) value = v;
return SliderFloatNextReturn;
}
public void PlotLines(
string label,
float[] values,
int count,
int offset = 0,
string? overlay = null,
float? min = null,
float? max = null,
Vector2? size = null)
=> Calls.Add(("PlotLines", new object?[] { label, values, count, offset, overlay, min, max, size }));
public void BeginTable(string id, int columns)
=> Calls.Add(("BeginTable", new object?[] { id, columns }));
public void TableNextColumn() => Calls.Add(("TableNextColumn", Array.Empty<object?>()));
public void EndTable() => Calls.Add(("EndTable", Array.Empty<object?>()));
public bool InputTextSubmit(string label, ref string buffer, int maxLen, out string? submitted)
{
Calls.Add(("InputTextSubmit", new object?[] { label, buffer, maxLen }));
submitted = InputTextSubmitNextSubmitted;
if (submitted is not null)
buffer = InputTextSubmitNextBufferAfter ?? string.Empty;
return submitted is not null;
}
public void Spacing() => Calls.Add(("Spacing", Array.Empty<object?>()));
public void Dummy(Vector2 size) => Calls.Add(("Dummy", new object?[] { size }));
public void TextWrapped(string text) => Calls.Add(("TextWrapped", new object?[] { text }));
}