public sealed class RunicTestHost : IAsyncDisposable Plays the host of the Runic host protocol (runic-host/1) for a guest application in a test, without a native window: it starts the guest in hosted mode, completes the handshake, answers each Window's window-open and records its presentation requests, and exposes each Window's one-time launch address for a browser test.
Remarks
var start = RunicServeOnlyApplication.CreateStartInfo("path/to/Tool.dll");
start.ArgumentList.Add("hosted"); // The parameter the guest defines for hosted mode.
await using var host = await RunicTestHost.StartGuestAsync(start);
var window = await host.WaitForWindowAsync("MainWindow", TimeSpan.FromSeconds(30));
await page.GotoAsync(window.LaunchUrl.ToString()); // Playwright loads the guest's page as the host's window would.
Assert.True(await window.RequestCloseAsync());
Assert.Equal(0, await host.StopAsync());The guest must reach hosted mode through the parameter it defines, so the start information names it. The test host writes the offer to the guest's standard input, keeps standard input open while it runs and reads the channel line from standard output; the guest's other output and its standard error are captured for failure messages. It connects again when its connection closes before the hello reply, within the start timeout, answers only the base protocol's requests, the menus of menus/1 (RunicTestHostedWindow.MenuBar, RunicTestHostedWindow.InvokeMenuItemAsync), the dialogs of dialogs/1, which RunicTestHostOptions.FileDialog, RunicTestHostOptions.MessageBox and RunicTestHostOptions.ContextMenu answer, and the window services of window-services/1 with a simulated RunicTestHost.Clipboard, RunicTestHost.Monitors, recorded launches (RunicTestHostedWindow.LaunchRequests) and dropped files (RunicTestHostedWindow.DropFilesAsync), and lists a window once it answered its window-open. The guest starts with RunicTestHostOptions.Locale and RunicTestHostOptions.Appearance, which RunicTestHost.SetLocaleAsync and RunicTestHost.SetAppearanceAsync change. RunicTestHost.StopAsync sends shutdown and waits for the guest to exit; disposing closes standard input and the channel, as a host that went away does, and kills the guest's process tree when it has not exited in time. On Windows the guest runs in a job object that kills it when the test process ends. The protocol and this type are experimental; see docs/experimental.md.
Properties
Clipboard
public RunicTestClipboard Clipboard { get; } The host's simulated clipboard, which the guest's clipboard reads and writes with window-services/1: set what the user copied with RunicTestClipboard.SetText before the guest reads, and check RunicTestClipboard.Writes after it wrote. By default the guest reads it only for its active window (RunicTestHostOptions.ClipboardReadsOnlyActiveWindow).
Exited
public Task<int> Exited { get; } Completes with the guest's exit code when its process exited.
Features
public IReadOnlyList<string> Features { get; } The protocol features in use on the channel: those of RunicTestHostOptions.Features that the guest's hello reply lists too, such as menus/1 and dialogs/1.
Monitors
public IReadOnlyList<DesktopMonitor> Monitors { get; } The host's monitors that the guest lists: RunicTestHostOptions.Monitors until RunicTestHost.SetMonitorsAsync.
ProcessId
public int ProcessId { get; } The guest's process id.
StandardError
public string StandardError { get; } The guest's standard error so far.
StandardOutput
public string StandardOutput { get; } The guest's standard output so far, the channel line excepted.
Windows
public IReadOnlyList<RunicTestHostedWindow> Windows { get; } Every window the guest asked for, in order, refused and closed ones included. A window is listed once its window-open was answered.
Methods
DisposeAsync
public ValueTask DisposeAsync() Goes away as a host that exits does: closes the guest's standard input and the channel, waits up to 10 seconds for the guest to exit, and then kills its process tree.
SetAppearanceAsync
public Task SetAppearanceAsync(DesktopAppearance? appearance, CancellationToken cancellationToken = default) Reports a change of the host's appearance to the guest, as the appearance push does: the guest's Windows show it, in their pages' appearance (appearance/1), instead of the guest process's own desktop settings.
Parameters
appearanceThe host's appearance;
nullreports every member unknown.cancellationTokenCancels sending.
Returns
A task that completes when the push was sent.
SetLocaleAsync
public Task SetLocaleAsync(string? locale, IReadOnlyList<string>? languages = null, CancellationToken cancellationToken = default) Reports a change of the host's application locale and its user's languages to the guest, as the locale push does. The guest's IRunicApplicationLocale follows it unless the guest set a locale of its own.
Parameters
localeThe host's locale, a BCP 47 tag, or
nullfor none.languagesThe languages the host's user prefers;
nullfor none.cancellationTokenCancels sending.
Returns
A task that completes when the push was sent.
Exceptions
ArgumentExceptionlocaleor a language is not a locale tag.
SetMonitorsAsync
public Task SetMonitorsAsync(IReadOnlyList<DesktopMonitor> monitors, CancellationToken cancellationToken = default) Changes the host's monitors and reports the change to the guest's open windows (monitors-changed), whose monitor watches list them again.
Parameters
monitorsThe monitors, the primary one first.
cancellationTokenCancels sending.
Returns
A task that completes when the pushes were sent.
SetRegionAppearanceAsync
public Task SetRegionAppearanceAsync(RunicTestHostedWindow window, DesktopAppearance? appearance, CancellationToken cancellationToken = default) Changes the appearance override of a region window (region-appearance), as a host does with RunicHostedRegion.SetAppearanceAsync: the guest merges it with the host's appearance (RunicTestHostOptions.Appearance, RunicTestHost.SetAppearanceAsync) for that window's pages only, whether a region shows the window or not.
Parameters
windowThe region window.
appearanceThe override: each member that is known replaces the host's value, and an unknown one, such as
DesktopColorScheme.NoPreferenceornull, keeps it;nullremoves the override. It replaces the previous one as a whole.cancellationTokenCancels sending.
Returns
A task that completes when the push was sent.
Exceptions
InvalidOperationExceptionThe window is not a region window, or
region-appearance/1is not in use (RunicTestHostOptions.Featuresand the guest'shellodo not both list it).
StartGuestAsync
public static Task<RunicTestHost> StartGuestAsync(ProcessStartInfo startInfo, RunicTestHostOptions? options = null, CancellationToken cancellationToken = default) Starts a guest application in hosted mode and completes the handshake.
Parameters
startInfoHow to start the guest, with the arguments that select its hosted mode, such as
RunicServeOnlyApplication.CreateStartInfoplushosted. It is not changed: the guest starts with redirected standard streams and withoutRUNIC_APPLICATION_SERVE_ONLYandRUNIC_APPLICATION_CLOSE_AFTER_OPEN.optionsThe host's name, protocols, timeouts and answers;
nullfor the defaults.cancellationTokenCancels the start; the guest is then killed.
Returns
The running guest. Dispose it to end it.
Exceptions
RunicHostedApplicationExceptionThe guest could not be hosted:
guest-start-timeout,guest-exited,guest-not-hostable,guest-refusedorprotocol-unsupported. The guest was killed; the message has its output.
StopAsync
public Task<int> StopAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default) Ends the host session as a host does when it stops a guest: sends shutdown, waits for the guest to exit and kills its process tree when it has not exited within timeout.
Parameters
timeoutHow long to wait for the exit; the default is 10 seconds.
cancellationTokenCancels the wait; the guest is then killed.
Returns
The guest's exit code.
Exceptions
TimeoutExceptionThe guest did not exit in time and was killed.
InvalidOperationExceptionAn answer of
RunicTestHostOptionsfailed, such as a file dialog's that selected a relative path; the guest was stopped first.
WaitForMatchingWindowAsync
public Task<RunicTestHostedWindow> WaitForMatchingWindowAsync(Func<RunicTestHostedWindow, bool> match, TimeSpan timeout, CancellationToken cancellationToken = default) Waits for the first window that match accepts, in the order the guest asked for them.
Parameters
matchDecides about each window, such as
window => window.OwnerId == main.Id.timeoutHow long to wait.
cancellationTokenCancels the wait.
Returns
The window, opened or refused, once its window-open was answered.
Exceptions
TimeoutExceptionThe guest asked for no such window in time.
WaitForNthWindowAsync
public Task<RunicTestHostedWindow> WaitForNthWindowAsync(string windowType, int index, TimeSpan timeout, CancellationToken cancellationToken = default) Waits for the window of the Window type windowType that the guest asked for after index others of that type, such as the second DocumentWindow with index 1.
Parameters
windowTypeThe Window class's full name, or its name without the namespace.
indexHow many windows of that type come before it, from 0.
timeoutHow long to wait.
cancellationTokenCancels the wait.
Returns
The window, opened or refused, once its window-open was answered.
Exceptions
TimeoutExceptionThe guest asked for no such window in time.
WaitForWindowAsync
public Task<RunicTestHostedWindow> WaitForWindowAsync(string windowType, TimeSpan timeout, CancellationToken cancellationToken = default) Waits for the first window of the Window type windowType.
Parameters
windowTypeThe Window class's full name, or its name without the namespace, as
RunicServeOnlyApplication.WaitForWindowUrlAsynctakes it.timeoutHow long to wait.
cancellationTokenCancels the wait.
Returns
The window, opened or refused. Its window-open was answered: an opened window's RunicTestHostedWindow.Opened has completed.
Exceptions
TimeoutExceptionThe guest asked for no such window in time.