# Deterministic UI tests

Test a counter without opening a native window. The test runtime supplies window services and software drawing; the test controls when layout, input, and animation occur.

The complete example below lives in an xUnit project. It needs Core, Core.DI, ToolKit.UI, Runtime.Test, bundled Inter fonts, and xUnit. The repository's Docs test project already supplies those references. It is test code, not app startup code for a native host.

```csharp
using System;
using Xui.Core.DI;
using Xui.Core.UI;
using Xui.Runtime.Test;
using Xui.ToolKit.UI.Design;
using Xui.ToolKit.UI.Widgets;
using Xunit;

public sealed class CounterApplication(IServiceProvider services) : Xui.Core.Abstract.Application(services)
{
    public override void Start() => this.CreateAndShowMainWindowOnce<CounterWindow>();
}

public sealed class CounterWindow : Xui.Core.Abstract.Window
{
    public CounterWindow(IServiceProvider services) : base(services)
    {
        var result = new Label
        {
            Id = "Counter.Value",
            Text = "0",
            FontFamily = "Inter",
            FontSize = 24
        };
        var count = 0;
        var button = new Button
        {
            Id = "Counter.Add",
            Text = "Add one"
        };
        button.Clicked = () => result.Text = (++count).ToString(System.Globalization.CultureInfo.InvariantCulture);
        Content = new DesignSystemView(new VerticalStack { Margin = 24, Content = [result, button] });
    }
}

public sealed class CounterTest
{
    [Fact]
    public void ClickingAddChangesTheVisibleValue()
    {
        using var app = new TestSinglePageApp<CounterApplication, CounterWindow>((400, 600));
        app.Run(() =>
        {
            app.Render();
            var button = app.RequireView("Counter.Add", "incrementing the counter");
            app.MouseDown(button);
            app.MouseUp(button);
            app.Advance(TimeSpan.FromMilliseconds(250));
            app.Render();
            var result = Assert.IsType<Label>(app.RequireView("Counter.Value", "reading the counter"));
            Assert.Equal("1", result.Text);
        });
    }
}
```

## Read the interaction in order

`Render()` establishes the initial layout. `RequireView` finds a stable ID and reports the intended action if it is missing. The mouse helpers target that view through the harness's coordinate conversion. Down and up travel through the window and event router; they do not invoke `Clicked` directly.

`Advance` moves the controlled frame clock so press feedback can finish. A second render brings the visual state current, and the assertion verifies that the label changed to `1`.

Use `app.Run` around the scenario. It preserves an earlier assertion failure if cleanup also discovers a snapshot mismatch. Dispose the harness even when the test fails.

## Remove sources of chance

Pass clocks, random sources, and data services into production code. Choose fixed data for a test instead of fetching a live network response. Advance animations with the harness rather than `Task.Delay`. Use bundled fonts whenever text metrics affect layout.

The harness also offers desktop and phone-emulator variants. A phone variant tests geometry and translated touch behavior; it does not turn a software test into a real iOS or Android IME test.

## Try another action

Send a second down/up pair and assert `2`. Then resize or use an emulator variant and verify the same behavior. Keep the assertion about the user's result, not private implementation fields.

For pixel and layout changes, add a [reviewed visual baseline](../visual-regressions/index.md) after the behavior assertion.
