RunicTestHost class

Namespace Runic.Application.Testing in Runic.Application.Testing 0.23.0

public sealed class RunicTestHost : IAsyncDisposable

Inherits or implements 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.

Guest

public RunicTestGuest Guest { get; }

The guest application as its hello reply named it.

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

appearance

The host's appearance; null reports every member unknown.

cancellationToken

Cancels 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

locale

The host's locale, a BCP 47 tag, or null for none.

languages

The languages the host's user prefers; null for none.

cancellationToken

Cancels sending.

Returns

A task that completes when the push was sent.

Exceptions

ArgumentException

locale or 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

monitors

The monitors, the primary one first.

cancellationToken

Cancels 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

window

The region window.

appearance

The override: each member that is known replaces the host's value, and an unknown one, such as DesktopColorScheme.NoPreference or null, keeps it; null removes the override. It replaces the previous one as a whole.

cancellationToken

Cancels sending.

Returns

A task that completes when the push was sent.

Exceptions

InvalidOperationException

The window is not a region window, or region-appearance/1 is not in use (RunicTestHostOptions.Features and the guest's hello do 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

startInfo

How to start the guest, with the arguments that select its hosted mode, such as RunicServeOnlyApplication.CreateStartInfo plus hosted. It is not changed: the guest starts with redirected standard streams and without RUNIC_APPLICATION_SERVE_ONLY and RUNIC_APPLICATION_CLOSE_AFTER_OPEN.

options

The host's name, protocols, timeouts and answers; null for the defaults.

cancellationToken

Cancels the start; the guest is then killed.

Returns

The running guest. Dispose it to end it.

Exceptions

RunicHostedApplicationException

The guest could not be hosted: guest-start-timeout, guest-exited, guest-not-hostable, guest-refused or protocol-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

timeout

How long to wait for the exit; the default is 10 seconds.

cancellationToken

Cancels the wait; the guest is then killed.

Returns

The guest's exit code.

Exceptions

TimeoutException

The guest did not exit in time and was killed.

InvalidOperationException

An answer of RunicTestHostOptions failed, 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

match

Decides about each window, such as window => window.OwnerId == main.Id.

timeout

How long to wait.

cancellationToken

Cancels the wait.

Returns

The window, opened or refused, once its window-open was answered.

Exceptions

TimeoutException

The 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

windowType

The Window class's full name, or its name without the namespace.

index

How many windows of that type come before it, from 0.

timeout

How long to wait.

cancellationToken

Cancels the wait.

Returns

The window, opened or refused, once its window-open was answered.

Exceptions

TimeoutException

The 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

windowType

The Window class's full name, or its name without the namespace, as RunicServeOnlyApplication.WaitForWindowUrlAsync takes it.

timeout

How long to wait.

cancellationToken

Cancels the wait.

Returns

The window, opened or refused. Its window-open was answered: an opened window's RunicTestHostedWindow.Opened has completed.

Exceptions

TimeoutException

The guest asked for no such window in time.