317 lines
13 KiB
C#
317 lines
13 KiB
C#
using AcDream.App.UI;
|
|
|
|
namespace AcDream.App.Tests.UI;
|
|
|
|
public class MarkupDocumentTests
|
|
{
|
|
private sealed class EditorBinding
|
|
{
|
|
public string Draft { get; private set; } = "initial";
|
|
public string Submitted { get; private set; } = string.Empty;
|
|
public string Selected { get; private set; } = "First";
|
|
public int SelectedIndex { get; private set; }
|
|
public IReadOnlyList<string> Choices => ["First", "Second"];
|
|
public IReadOnlyList<uint> ChoiceColors => [0xFF0000u, 0x00FF00u];
|
|
public Action<string> ChangeDraft => value => Draft = value;
|
|
public Action<string> SubmitDraft => value => Submitted = value;
|
|
public Action<string> SelectChoice => value => Selected = value;
|
|
public Action<int> SelectIndex => value => SelectedIndex = value;
|
|
}
|
|
|
|
[Fact]
|
|
public void FieldAndMenuBindEditablePluginState()
|
|
{
|
|
const string xml = """
|
|
<panel x="0" y="0" w="240" h="120">
|
|
<field x="4" y="4" w="120" h="20" text="{Draft}"
|
|
onchange="{ChangeDraft}" onsubmit="{SubmitDraft}" />
|
|
<menu x="4" y="32" w="120" h="20" items="{Choices}"
|
|
selected="{Selected}" onchange="{SelectChoice}" />
|
|
<list x="132" y="4" w="100" h="40" items="{Choices}"
|
|
colors="{ChoiceColors}"
|
|
selected="{SelectedIndex}" onchange="{SelectIndex}" />
|
|
</panel>
|
|
""";
|
|
var binding = new EditorBinding();
|
|
|
|
UiNineSlicePanel panel = MarkupDocument.Build(
|
|
xml,
|
|
binding,
|
|
_ => (1u, 32, 32));
|
|
|
|
UiField field = Assert.IsType<UiField>(panel.Children[0]);
|
|
UiMenu menu = Assert.IsType<UiMenu>(panel.Children[1]);
|
|
UiMarkupList list = Assert.IsType<UiMarkupList>(panel.Children[2]);
|
|
field.SetText("named profile");
|
|
field.OnSubmit?.Invoke(field.Text);
|
|
menu.OnSelect?.Invoke("Second");
|
|
list.OnEvent(new UiEvent
|
|
{
|
|
Type = UiEventType.MouseDown,
|
|
Data2 = 19,
|
|
});
|
|
|
|
Assert.Equal("named profile", binding.Draft);
|
|
Assert.Equal("named profile", binding.Submitted);
|
|
Assert.Equal("Second", binding.Selected);
|
|
Assert.Equal(1, binding.SelectedIndex);
|
|
Assert.Equal(2, menu.Items.Count);
|
|
Assert.Equal([0xFF0000u, 0x00FF00u], list.ItemColorsSource());
|
|
Assert.False(menu.OpenUpward);
|
|
}
|
|
|
|
[Fact]
|
|
public void ControlIdAndNameBecomeStablePluginControlNames()
|
|
{
|
|
const string xml = """
|
|
<panel x="0" y="0" w="200" h="80">
|
|
<button id="ById" x="4" y="4" w="80" h="20" text="One" />
|
|
<label name="ByName" x="4" y="28" text="Two" />
|
|
</panel>
|
|
""";
|
|
|
|
UiNineSlicePanel panel = MarkupDocument.Build(
|
|
xml, new object(), _ => (1u, 32, 32));
|
|
|
|
Assert.Equal("ById", panel.Children[0].Name);
|
|
Assert.Equal("ByName", panel.Children[1].Name);
|
|
}
|
|
|
|
private sealed class FakeBinding
|
|
{
|
|
public float HealthPercent => 0.5f;
|
|
public uint? HealthCurrent => 109;
|
|
public uint? HealthMax => 218;
|
|
public float? ManaPercent => null;
|
|
public uint? ManaCurrent => null;
|
|
public uint? ManaMax => null;
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_CreatesPanelWithMeterFillLabelAndGeometry()
|
|
{
|
|
const string xml =
|
|
"<panel id=\"acdream.vitals\" x=\"10\" y=\"30\" w=\"220\" h=\"96\" title=\"Vitals\">" +
|
|
" <meter id=\"health\" x=\"8\" y=\"24\" w=\"200\" h=\"14\" fill=\"{HealthPercent}\" cur=\"{HealthCurrent}\" max=\"{HealthMax}\" color=\"#FFFF0000\"/>" +
|
|
"</panel>";
|
|
|
|
var panel = MarkupDocument.Build(xml, new FakeBinding(), _ => ((uint)1, 32, 32));
|
|
|
|
Assert.IsType<UiNineSlicePanel>(panel);
|
|
Assert.Equal(10f, panel.Left);
|
|
Assert.Equal(220f, panel.Width);
|
|
Assert.Equal(2, panel.Children.Count); // title UiLabel + 1 meter
|
|
var meter = Assert.IsType<UiMeter>(panel.Children[1]);
|
|
Assert.Equal(8f, meter.Left);
|
|
Assert.Equal(200f, meter.Width);
|
|
Assert.Equal(0.5f, meter.Fill());
|
|
Assert.Equal("109/218", meter.Label());
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_NullBindingValuesYieldNullFillAndLabel()
|
|
{
|
|
const string xml =
|
|
"<panel id=\"v\" x=\"0\" y=\"0\" w=\"10\" h=\"10\" title=\"V\">" +
|
|
" <meter id=\"mana\" x=\"0\" y=\"0\" w=\"10\" h=\"2\" fill=\"{ManaPercent}\" cur=\"{ManaCurrent}\" max=\"{ManaMax}\" color=\"#FF0000FF\"/>" +
|
|
"</panel>";
|
|
var panel = MarkupDocument.Build(xml, new FakeBinding(), _ => ((uint)1, 32, 32));
|
|
var meter = Assert.IsType<UiMeter>(panel.Children[1]);
|
|
Assert.Null(meter.Fill());
|
|
Assert.Null(meter.Label());
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_ResizeAttrX_SetsHorizontalOnly()
|
|
{
|
|
const string xml = "<panel id=\"v\" x=\"0\" y=\"0\" w=\"100\" h=\"50\" title=\"V\" resize=\"x\"></panel>";
|
|
var panel = MarkupDocument.Build(xml, new object(), _ => ((uint)1, 32, 32));
|
|
Assert.True(panel.ResizeX);
|
|
Assert.False(panel.ResizeY);
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_ParsesNineSliceBarSpriteIds()
|
|
{
|
|
const string xml = "<panel id=\"v\" x=\"0\" y=\"0\" w=\"100\" h=\"50\" title=\"V\">" +
|
|
"<meter id=\"h\" x=\"0\" y=\"0\" w=\"100\" h=\"14\" fill=\"{HealthPercent}\" " +
|
|
"backleft=\"0x06001141\" backtile=\"0x06001140\" backright=\"0x0600113F\" " +
|
|
"frontleft=\"0x06001131\" fronttile=\"0x06001132\" frontright=\"0x06001133\"/>" +
|
|
"</panel>";
|
|
var panel = MarkupDocument.Build(xml, new FakeBinding(), _ => ((uint)7, 32, 32));
|
|
var meter = Assert.IsType<UiMeter>(panel.Children[1]);
|
|
Assert.Equal(0x06001141u, meter.BackLeft);
|
|
Assert.Equal(0x06001140u, meter.BackTile);
|
|
Assert.Equal(0x0600113Fu, meter.BackRight);
|
|
Assert.Equal(0x06001131u, meter.FrontLeft);
|
|
Assert.Equal(0x06001132u, meter.FrontTile);
|
|
Assert.Equal(0x06001133u, meter.FrontRight);
|
|
Assert.NotNull(meter.SpriteResolve);
|
|
}
|
|
|
|
private sealed class ButtonBinding
|
|
{
|
|
public int Clicks { get; private set; }
|
|
public string Status { get; set; } = "idle";
|
|
public Action Go => () => Clicks++;
|
|
public Action? Missing => null;
|
|
public bool CanGo { get; set; } = true;
|
|
public bool OptionsSelected { get; set; } = true;
|
|
public bool OptionsVisible { get; set; } = true;
|
|
public bool CombatEnabled { get; set; }
|
|
public float AttackPower { get; private set; } = 0.5f;
|
|
public Action ShowOptions => () => OptionsSelected = true;
|
|
public Action ToggleCombat => () => CombatEnabled = !CombatEnabled;
|
|
public Action<float> SetAttackPower => value => AttackPower = value;
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_ButtonInvokesTheBoundActionOnClick()
|
|
{
|
|
const string xml =
|
|
"<panel x=\"0\" y=\"0\" w=\"100\" h=\"60\">" +
|
|
" <button x=\"4\" y=\"8\" w=\"60\" h=\"20\" text=\"Buff\" onclick=\"{Go}\"/>" +
|
|
"</panel>";
|
|
|
|
var binding = new ButtonBinding();
|
|
var panel = MarkupDocument.Build(xml, binding, _ => ((uint)1, 32, 32));
|
|
|
|
var button = Assert.IsType<UiSimpleButton>(panel.Children[0]);
|
|
Assert.Equal("Buff", button.Text);
|
|
Assert.Equal(60f, button.Width);
|
|
|
|
button.OnEvent(new UiEvent { Type = UiEventType.Click });
|
|
button.OnEvent(new UiEvent { Type = UiEventType.Click });
|
|
Assert.Equal(2, binding.Clicks);
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_ButtonWithUnresolvableHandlerFailsLoudly()
|
|
{
|
|
// A silently dead button is worse than a panel that refuses to load:
|
|
// the user clicks and nothing happens, with nothing to diagnose.
|
|
const string xml =
|
|
"<panel x=\"0\" y=\"0\" w=\"100\" h=\"60\">" +
|
|
" <button x=\"0\" y=\"0\" w=\"10\" h=\"10\" text=\"X\" onclick=\"{NoSuchProperty}\"/>" +
|
|
"</panel>";
|
|
|
|
Assert.Throws<FormatException>(
|
|
() => MarkupDocument.Build(xml, new ButtonBinding(), _ => ((uint)1, 32, 32)));
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_BoundLabelTracksTheBindingRatherThanFreezing()
|
|
{
|
|
const string xml =
|
|
"<panel x=\"0\" y=\"0\" w=\"100\" h=\"60\">" +
|
|
" <label x=\"2\" y=\"4\" text=\"{Status}\"/>" +
|
|
"</panel>";
|
|
|
|
var binding = new ButtonBinding();
|
|
var panel = MarkupDocument.Build(xml, binding, _ => ((uint)1, 32, 32));
|
|
|
|
var label = Assert.IsType<UiLabel>(panel.Children[0]);
|
|
Assert.Equal("idle", label.TextSource!());
|
|
|
|
binding.Status = "casting";
|
|
Assert.Equal("casting", label.TextSource!());
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_LiteralLabelTextIsUsedVerbatim()
|
|
{
|
|
const string xml =
|
|
"<panel x=\"0\" y=\"0\" w=\"100\" h=\"60\">" +
|
|
" <label x=\"0\" y=\"0\" text=\"MossTank\"/>" +
|
|
"</panel>";
|
|
|
|
var panel = MarkupDocument.Build(xml, new ButtonBinding(), _ => ((uint)1, 32, 32));
|
|
var label = Assert.IsType<UiLabel>(panel.Children[0]);
|
|
Assert.Equal("MossTank", label.TextSource!());
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_ProjectsBoundEnabledAndButtonColors()
|
|
{
|
|
const string xml =
|
|
"<panel x=\"0\" y=\"0\" w=\"100\" h=\"60\">" +
|
|
"<button x=\"0\" y=\"0\" w=\"50\" h=\"20\" text=\"Go\" " +
|
|
"onclick=\"{Go}\" enabled=\"{CanGo}\" " +
|
|
"background=\"#FF112233\" border=\"#FF445566\"/>" +
|
|
"</panel>";
|
|
var binding = new ButtonBinding();
|
|
UiNineSlicePanel panel = MarkupDocument.Build(
|
|
xml, binding, _ => ((uint)1, 32, 32));
|
|
var button = Assert.IsType<UiSimpleButton>(panel.Children[0]);
|
|
|
|
Assert.NotNull(button.EnabledSource);
|
|
Assert.True(button.EnabledSource!());
|
|
binding.CanGo = false;
|
|
Assert.False(button.EnabledSource!());
|
|
Assert.Equal(new System.Numerics.Vector4(
|
|
0x11 / 255f, 0x22 / 255f, 0x33 / 255f, 1f),
|
|
button.BackgroundColor);
|
|
Assert.Equal(new System.Numerics.Vector4(
|
|
0x44 / 255f, 0x55 / 255f, 0x66 / 255f, 1f),
|
|
button.BorderColor);
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_RuntimeTooltipUsesRetailPopupLocatorAndLiveBinding()
|
|
{
|
|
const string xml =
|
|
"<panel x=\"0\" y=\"0\" w=\"100\" h=\"60\">" +
|
|
"<button x=\"0\" y=\"0\" w=\"50\" h=\"20\" text=\"Go\" " +
|
|
"tooltip=\"{Status}\"/>" +
|
|
"</panel>";
|
|
var binding = new ButtonBinding();
|
|
UiNineSlicePanel panel = MarkupDocument.Build(
|
|
xml, binding, _ => ((uint)1, 32, 32));
|
|
var button = Assert.IsType<UiSimpleButton>(panel.Children[0]);
|
|
|
|
Assert.Equal("idle", button.GetTooltipText());
|
|
Assert.True(button.AuthoredTooltipEnabled);
|
|
Assert.Equal(0x10000397u, button.AuthoredTooltipRootElementId);
|
|
Assert.Equal(0x21000041u, button.AuthoredTooltipLayoutDid);
|
|
|
|
binding.Status = "casting";
|
|
Assert.Equal("casting", button.GetTooltipText());
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_NestedGroupTabToggleAndSliderStayLiveAndInteractive()
|
|
{
|
|
const string xml =
|
|
"<panel x=\"0\" y=\"0\" w=\"300\" h=\"180\">" +
|
|
"<tab x=\"4\" y=\"4\" w=\"60\" h=\"18\" text=\"Options\" " +
|
|
"selected=\"{OptionsSelected}\" onclick=\"{ShowOptions}\"/>" +
|
|
"<group x=\"4\" y=\"28\" w=\"280\" h=\"140\" visible=\"{OptionsVisible}\">" +
|
|
"<toggle x=\"2\" y=\"2\" w=\"140\" h=\"20\" text=\"Enable Combat\" " +
|
|
"checked=\"{CombatEnabled}\" onclick=\"{ToggleCombat}\"/>" +
|
|
"<slider x=\"2\" y=\"30\" w=\"140\" h=\"16\" value=\"{AttackPower}\" " +
|
|
"onchange=\"{SetAttackPower}\"/>" +
|
|
"</group></panel>";
|
|
var binding = new ButtonBinding();
|
|
|
|
UiNineSlicePanel panel = MarkupDocument.Build(
|
|
xml,
|
|
binding,
|
|
_ => ((uint)1, 16, 16));
|
|
|
|
var tab = Assert.IsType<UiMarkupTabButton>(panel.Children[0]);
|
|
var group = Assert.IsType<UiPanel>(panel.Children[1]);
|
|
var toggle = Assert.IsType<UiMarkupToggle>(group.Children[0]);
|
|
var slider = Assert.IsType<UiScrollbar>(group.Children[1]);
|
|
Assert.True(tab.IsSelected);
|
|
Assert.True(group.VisibleSource!());
|
|
Assert.False(toggle.IsChecked);
|
|
|
|
toggle.OnEvent(new UiEvent { Type = UiEventType.Click });
|
|
Assert.True(binding.CombatEnabled);
|
|
Assert.True(toggle.IsChecked);
|
|
|
|
slider.ScalarChanged!(0.78f);
|
|
Assert.Equal(0.78f, binding.AttackPower);
|
|
Assert.Equal(0.78f, slider.ScalarPositionSource!());
|
|
}
|
|
}
|