RunicWindowTestHost<TViewModel> class

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

public sealed class RunicWindowTestHost<TViewModel> : IDisposable where TViewModel : class

Inherits or implements IDisposable.

Owns one headless Window content session and its generated root attachment. The caller retains ownership of the ViewModel and any dependency injection scope.

Constructors

RunicWindowTestHost

public RunicWindowTestHost(TViewModel viewModel, Func<IBridgeTransport, WindowContentSession, TViewModel, IDisposable> attach, RunicWindowTestHostOptions? options)

Creates a content session for viewModel and attaches its generated root bridge. Unless options say otherwise, the window uses a FakeTimeProvider and sequential ids.

Parameters

viewModel

The root ViewModel; the caller keeps ownership.

attach

The generated bridge factory, such as the Func<IBridgeTransport, WindowContentSession, TViewModel, IDisposable> that the generated AddRunicViews() registers.

options

The root route, View locator, clock and id source.

RunicWindowTestHost

public RunicWindowTestHost(TViewModel viewModel, Func<IBridgeTransport, WindowContentSession, TViewModel, IDisposable> attach)

Creates a content session for viewModel with a FakeTimeProvider and sequential ids, and attaches its generated root bridge.

Parameters

viewModel

The root ViewModel; the caller keeps ownership.

attach

The generated bridge factory.

Properties

AppearancePushes

public IReadOnlyList<DesktopAppearance?> AppearancePushes { get; }

Gets the appearance pushes the default connection received (appearance/1), in order: the window's appearance after the connection's hello and after each change of WindowContentSession.Appearance, which a test sets on RunicWindowTestHost.Content as RunicDesktopHost does from the desktop's settings. A push whose members are all null, an unknown appearance, is null.

ApplicationLocale

public IRunicApplicationLocale ApplicationLocale { get; }

The application's locale, which the window follows while it sets no locale of its own (IRunicWindowLocale): the IRunicApplicationLocale registered in RunicWindowTestHost.Services, or, for a host without one, a locale of this host alone. Switch it as the application does, and read the pushes from RunicWindowTestHost.Transport:

await host.ApplicationLocale.SetLocaleAsync("de");

Exceptions

ObjectDisposedException

The host was disposed.

Connections

public IRunicConnections Connections { get; }

The window's page connections, as the window scope's IRunicConnections reports them: each test connection that sent its hello, such as the one of RunicWindowTestHost.Root and those of RunicViewDriver.ForConnection, until InMemoryBridgeTransport.CloseConnection.

Remarks

Changes arrive in a turn of the window's model context; await a command's reply or the model context before reading IRunicConnections.Current.

Content

public WindowContentSession Content { get; }

The window content session.

public IReadOnlyList<RunicNavigationPathPush> NavigationPathPushes { get; }

Gets the navigation-path pushes the default connection received (navigation-path/1), in order: the window's route path after the connection's hello and after each change of its path region, and whether the page pushes, goes back in or replaces its browser history.

PendingUploadCount

public int PendingUploadCount { get; }

The number of uploads that the window holds and no command claimed yet.

Platform

public RunicTestPlatform? Platform { get; }

The window's platform fakes: the scope's RunicTestPlatform when RunicWindowTestHost.Services registers one with AddRunicTestPlatform(), or null.

Remarks

Script dialogs, message boxes, context menus and the clipboard through it, and read what the ViewModel did with them. Disposing the host closes it, as closing a window closes its platform services.

Presentation

public RunicTestWindowPresentation? Presentation { get; }

The window's recording presentation: the RunicTestWindowPresentation of a Window that RunicWindowTestHost.CreateWindowAsync opened, else the one that RunicTestWindowPresentationServiceCollectionExtensions.AddRunicTestWindowPresentation registered in RunicWindowTestHost.Services, or null without one.

Root

public RunicViewDriver<TViewModel> Root { get; }

Drives the root ViewModel's generated routes.

RootRoute

public string RootRoute { get; }

The generated root route name.

ServedBlobCount

public int ServedBlobCount { get; }

The number of blobs whose URLs the window serves now, as a test of their release.

Services

public IServiceProvider? Services { get; }

The services RunicWindowTestHost.Create resolved the window from, such as the window scope's provider, or null for a host created with a constructor. The caller keeps ownership of them.

Time

public TimeProvider Time { get; }

The window's clock: a FakeTimeProvider unless the options supply another clock.

Transport

public InMemoryBridgeTransport Transport { get; }

The in-memory transport that records routes and publications.

ViewModel

public TViewModel ViewModel { get; }

The root ViewModel.

Methods

Dispose

public void Dispose()

Detaches the root bridge and disposes the content session and transport.

DrainAsync

public Task<RunicWindowCloseResult> DrainAsync(TimeSpan timeout, CancellationToken cancellationToken = default)

Stops operation admission and waits up to timeout for accepted Window operations, as a closing window does (WindowContentSession.DrainAsync). The timeout runs on RunicWindowTestHost.Time, so with a fake clock it passes only when the test advances it.

Parameters

timeout

How long accepted operations may run, or Timeout.InfiniteTimeSpan.

cancellationToken

Stops the wait only; admission stays closed.

FlushWindowPresentationAsync

public Task FlushWindowPresentationAsync(CancellationToken cancellationToken = default)

Waits until the changes that RunicWindowTestHost.Presentation reported so far, such as those of a command's SetVisibleAsync, reached the ViewModel: the window's presentation applies them in later turns of its model context.

Parameters

cancellationToken

Cancels waiting for the turn.

Remarks

Call it outside a model turn; without a model context it returns at once.

public Task<RunicNavigationReply> NavigateBackAsync(CancellationToken cancellationToken = default)

Goes back as the window's Back does, the mouse's Back button and Alt+Left in a page that installed it (navigation-path/1): RunicNavigator.BackAsync in the innermost presented region that can.

Parameters

cancellationToken

Cancels the request, as a closed page connection does.

OpenNavigationPathAsync

public Task<RunicNavigationReply> OpenNavigationPathAsync(string path, CancellationToken cancellationToken = default)

Opens a route path as a page's browser does when the user goes Back or Forward (popstate) or edits the address bar (navigation-path/1): in the window's path region (RunicPresentation.SyncNavigationPath), with NavigationRouteOpen.BackToOrPush. RunicNavigationReply.Path is the path the page's URL shows afterwards, the previous one when a guard refused.

Parameters

path

The route path, such as customers/42.

cancellationToken

Cancels the request, as a closed page connection does.

ReadBlobAsync

public Task<RunicServedBlob> ReadBlobAsync(string url, string? range = null, bool head = false, CancellationToken cancellationToken = default)

Serves a blob URL from the window's state or a command result (blobs/1) as the host serves it to the page, and returns the response: its status, headers and content. A released or unknown URL answers 404.

Parameters

url

The blob's url, such as state.Value("avatar").GetProperty("url").GetString().

range

A Range header to send, such as bytes=0-99, or null.

head

Sends HEAD instead of GET.

cancellationToken

Cancels the request.

SetWindowActivityAsync

public Task SetWindowActivityAsync(RunicWindowActivity activity, CancellationToken cancellationToken = default)

Changes the activity of RunicWindowTestHost.Presentation, as a real window reports that it gained or lost the focus, was minimized or hidden, and waits until the ViewModel received the change in a turn of the window's model context.

Parameters

activity

The new activity, such as new RunicWindowActivity(IsActive: false, IsMinimized: false, IsVisible: true).

cancellationToken

Cancels waiting for the turn.

Exceptions

InvalidOperationException

The services resolve no RunicTestWindowPresentation.

UploadAsync

public Task<RunicUpload> UploadAsync(ReadOnlyMemory<byte> content, string fileName = "upload", string contentType = "application/octet-stream", string connectionKey = "connection-1", CancellationToken cancellationToken = default)

Simulates a page's upload (blobs/1): stores content as the window's host stores a page's upload, with the window's upload limits, and returns the upload, which a command input takes, as Root.ExecuteAsync(vm => vm.AttachCommand, upload).

Parameters

content

The uploaded bytes.

fileName

The file name the page reports.

contentType

The media type the page reports.

connectionKey

The connection that uploads, by its transport key, as RunicViewDriver.ConnectionKey; a command decodes the upload only on that connection. As a page has its connection's upload token only after its hello, the connection sends its hello first if it has not yet.

cancellationToken

Cancels storing.

Exceptions

InvalidOperationException

The window refused the upload, for example over its limit.

View

public RunicViewDriver<TContent> View<TContent>(PageReference reference) where TContent : class

Drives the routes of presented content, such as a reference from a snapshot.