Manual Setup of Headless Platform
warning
Install Packages
To set up the Headless platform, you need to install two packages:
- Avalonia.Headless, which also includes Avalonia.
- Avalonia.Themes.Fluent, as even headless controls need a theme.
tip
The Headless platform doesn't require any specific theme, and it is possible to swap FluentTheme with any other.
Setup Application
As in any other Avalonia app, an Application
instance needs to be created, and themes need to be applied. When using the Headless platform, the setup is not much different from a regular Avalonia app and can mostly be reused.
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Tests.App">
<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>
And the code:
using Avalonia;
using Avalonia.Headless;
public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
}
Run Headless Session
using Avalonia.Controls;
using Avalonia.Headless;
// Start Headless session passing Application type.
using var session = HeadlessUnitTestSession.StartNew(typeof(App));
// Since the Headless session has its own thread internally, we need to dispatch actions there:
await session.Dispatch(() =>
{
// Setup controls:
var textBox = new TextBox();
var window = new Window { Content = textBox };
// Open window:
window.Show();
// Focus text box:
textBox.Focus();
// Simulate text input:
window.KeyTextInput("Hello World");
// Assert:
if (textBox.Text != "Hello World")
{
throw new Exception("Assert");
}
}, CancellationToken.None);