RunicTestPlatform class

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

public sealed class RunicTestPlatform : IWindowPlatformServices, IPlatformCapabilities, IDesktopFileLauncher, IDisposable, IAsyncDisposable

Inherits or implements IWindowPlatformServices, IPlatformCapabilities, IDesktopFileLauncher, IDisposable, IAsyncDisposable.

The platform services of one test window: scripted, recording fakes of IWindowPlatformServices for RunicWindowTestHost, a serve-only browser test, or a ViewModel a test builds itself.

Remarks

A ViewModel can take this as IWindowPlatformServices or take its parts directly: RunicTestPlatform.FileDialogs as IFileDialogs, RunicTestPlatform.Clipboard as ITextClipboard or IDesktopClipboard, RunicTestPlatform.MessageBoxes as IDesktopMessageBoxes, the launcher as IDesktopFileLauncher, and RunicTestPlatform.Notifications as IDesktopNotifications. services.AddRunicTestPlatform() registers them for each window scope.

The services are open from the start, as a window's are once it opened. RunicTestPlatform.Close, which disposing calls, acts as the window closing: every service then reports PlatformUnavailableReason.OwnerClosed, file leases are released and clipboard watches end. Monitors, the menu bar, taskbar badges and progress, and drags report PlatformUnavailableReason.BackendUnavailable, as on a window without them.

Constructors

RunicTestPlatform

public RunicTestPlatform()

Creates the services of one open test window, with notifications of its own.

RunicTestPlatform

public RunicTestPlatform(RunicTestNotifications notifications)

Creates the services of one open test window that share the application's notifications.

Parameters

notifications

The application's notifications, shared by its windows.

Properties

Announcements

public IReadOnlyList<RunicAnnouncementCall> Announcements { get; }

The screen-reader announcements made, oldest first.

Clipboard

public RunicTestClipboard Clipboard { get; }

The window's in-memory clipboard.

ContextMenus

public RunicTestContextMenus ContextMenus { get; }

The window's scripted context menus, for RunicTestPlatform.ShowContextMenuAsync.

FileDialogs

public RunicTestFileDialogs FileDialogs { get; }

The window's scripted file dialogs.

IsClosed

public bool IsClosed { get; }

LaunchResult

public PlatformResult<PlatformUnit> LaunchResult { get; set; }

What IDesktopFileLauncher calls report while the window is open; success by default.

Launches

public IReadOnlyList<RunicLaunchCall> Launches { get; }

The files and URIs handed to native applications, oldest first.

MessageBoxes

public RunicTestMessageBoxes MessageBoxes { get; }

The window's scripted message boxes.

Notifications

public RunicTestNotifications Notifications { get; }

The application's recording notifications.

Methods

AnnounceAsync

public ValueTask<PlatformResult<PlatformUnit>> AnnounceAsync(string message, PlatformAnnouncementPriority priority = (PlatformAnnouncementPriority)0, CancellationToken cancellationToken = default)

Asks the screen reader to announce a message for the window, such as the result of an action.

Parameters

message

The text to speak, in the window's language.

priority

PlatformAnnouncementPriority.Polite, the default, waits for what the screen reader is saying; PlatformAnnouncementPriority.Assertive interrupts it.

cancellationToken

Cancels before the request reaches the window.

Returns

Success once the window's accessibility API accepted the announcement, or Unavailable: PlatformUnavailableReason.OwnerUnavailable before the window opens or on a host without a native owner, PlatformUnavailableReason.OwnerClosed once it closed, and PlatformUnavailableReason.BackendUnavailable when the window cannot announce.

Exceptions

ArgumentException

message is empty or white space.

ArgumentNullException

message is null.

ArgumentOutOfRangeException

priority is not a defined value.

Remarks

The host-neutral counterpart of an ARIA live region: the host raises the announcement through the platform's accessibility API for the window, so it reaches the screen reader where a live region in the page does not. An implementation that does not override this member reports PlatformUnavailableReason.OwnerUnavailable.

Documentation from IWindowPlatformServices.AnnounceAsync.

Close

public void Close()

Closes the window's services: they report PlatformUnavailableReason.OwnerClosed from now on.

Remarks

Releases every file lease and ends every clipboard watch. Closing again does nothing.

Dispose

public void Dispose()

Closes the window's services; see RunicTestPlatform.Close.

DisposeAsync

public ValueTask DisposeAsync()

Closes the window's services; see RunicTestPlatform.Close.

Returns

A task that completes once every lease is released.

GetSnapshot

public CapabilitySnapshot GetSnapshot()

Captures the current capability statuses.

Documentation from IPlatformCapabilities.GetSnapshot.

LaunchAsync

public ValueTask<PlatformResult<PlatformUnit>> LaunchAsync(string path, DesktopFileOperation operation = (DesktopFileOperation)0, CancellationToken cancellationToken = default)

The caller must retain any sandbox/security-scoped access until the operation completes.

Remarks

Success acknowledges native handling, not that another application opened the file. Windows Open With can report success even when dismissed; UserDismissed is returned only when the native API distinguishes dismissal. Windows and macOS Open returns PermissionDenied for programs, scripts, installers and shortcuts instead of running them; Linux applies the desktop portal's policy. The Windows and macOS block lists are best effort: do not Open files from untrusted sources, such as downloads or names a page supplies; Reveal them or let the user ChooseApplication instead. Windows rejects UNC, device and alternate-stream paths.

Documentation from IDesktopFileLauncher.LaunchAsync.

LaunchUriAsync

public ValueTask<PlatformResult<PlatformUnit>> LaunchUriAsync(Uri uri, CancellationToken cancellationToken = default)

Opens an absolute http, https, mailto or ms-settings URI in the user's default handler, such as the default browser, the mail client or, for ms-settings, the Windows Settings app.

Parameters

uri

The URI. Relative URIs, paths (an absolute path is a file: URI) and every other scheme are rejected before anything native runs, so a launch never executes a program; open local files with IDesktopFileLauncher.LaunchAsync.

cancellationToken

Cancels before the request reaches the desktop.

Exceptions

ArgumentNullException

uri is null.

ArgumentException

uri is relative, a path, not an http, https, mailto or ms-settings URI, an ms-settings URI outside the page rule, or a mailto URI that names an attachment (attach, attachment).

Remarks

Success acknowledges that the desktop accepted the URI, not that an application showed it. Linux asks the desktop portal (OpenURI), which may let the user choose an application and reports PlatformFailureCode.UserDismissed when they cancel; Windows uses the shell's URL association; macOS uses NSWorkspace. Without a registered handler, for example no mail client for mailto, the desktop may offer to find an application (Windows 11 does, and reports success), or the result is PlatformUnavailableReason.BackendUnavailable or a failure, depending on the platform. An ms-settings URI must be exactly ms-settings: and a page of 1 to 64 letters, digits or hyphens, such as ms-settings:display, with no path, query, fragment or escapes; pages ending in -action, which act instead of showing a page, are refused. Only Windows opens it; Linux and macOS validate it the same way and return PlatformUnavailableReason.BackendUnavailable without a native call.

Documentation from IDesktopFileLauncher.LaunchUriAsync.

SetBadgeAsync

public ValueTask<PlatformResult<PlatformUnit>> SetBadgeAsync(DesktopBadge? badge, CancellationToken cancellationToken = default)

Shows a badge on the window's taskbar button or the application's Dock icon (PlatformCapabilityIds.TaskbarBadge).

Parameters

badge

The count or dot to show; null removes the badge.

cancellationToken

Cancels before the request reaches the window.

Returns

Success once the desktop took the badge, also on a desktop that does not show one, such as GNOME without a dock extension, or Unavailable: PlatformUnavailableReason.OwnerUnavailable before the window opens or on a host without a native owner, PlatformUnavailableReason.OwnerClosed once it closed, and PlatformUnavailableReason.BackendUnavailable when the desktop has no badges.

Exceptions

ArgumentException

badge has a count below 1 or a description with a NUL character.

Remarks

Windows shows the badge on this window's taskbar button, drawn by Runic as an overlay icon with DesktopBadge.Description as its accessible text. macOS and Linux have one badge for the application: the most recent call of any window wins, and a window's badge goes when the window closes. On Linux the badge needs the application's ID, as for notifications. An implementation that does not override this member checks the argument and reports PlatformUnavailableReason.OwnerUnavailable.

Documentation from IWindowPlatformServices.SetBadgeAsync.

SetProgressAsync

public ValueTask<PlatformResult<PlatformUnit>> SetProgressAsync(DesktopProgress? progress, CancellationToken cancellationToken = default)

Shows progress on the window's taskbar button or the application's Dock icon (PlatformCapabilityIds.TaskbarProgress).

Parameters

progress

The progress to show; null removes it.

cancellationToken

Cancels before the request reaches the window.

Returns

The results of IWindowPlatformServices.SetBadgeAsync.

Exceptions

ArgumentException

progress has a value outside 0 to 1 or an undefined state.

Remarks

Windows fills this window's taskbar button, yellow for DesktopProgressState.Paused and red for DesktopProgressState.Error. macOS draws a bar on the Dock icon and Linux docks on their launcher, for the application as with badges. An implementation that does not override this member checks the argument and reports PlatformUnavailableReason.OwnerUnavailable.

Documentation from IWindowPlatformServices.SetProgressAsync.

ShowContextMenuAsync

public ValueTask<PlatformResult<DesktopMenuItem?>> ShowContextMenuAsync(ImmutableArray<DesktopMenuEntry> items, DesktopContextMenuOptions? options = null, CancellationToken cancellationToken = default)

Shows a native context menu for the window and returns the item the user chose.

Parameters

items

The menu's entries: the rules of DesktopMenuItem and DesktopMenu, at most four nested submenus and 256 entries, and at least one entry. Command ids may repeat; DesktopMenu.Role is ignored.

options

Where the menu opens; null opens it at the pointer.

cancellationToken

Closes the menu, which then throws OperationCanceledException.

Returns

Success with the chosen item, one of items, or with null when the user dismissed the menu, a later context menu of the window replaced it, or it closed with the window. Unavailable: PlatformUnavailableReason.OwnerUnavailable before the window opens or on a host without a native owner, PlatformUnavailableReason.OwnerClosed once it closed, and PlatformUnavailableReason.BackendUnavailable when the window has no native context menus (PlatformCapabilityIds.ContextMenu).

Exceptions

ArgumentException

items is empty or breaks a menu rule, or options sets only one coordinate or one that is not a finite number.

Remarks

The menu is modal for the window. Disabled items and items of disabled submenus cannot be chosen; a shortcut is shown as a hint and not registered. The result is the item record, so the caller runs its command, for example with DesktopMenuItem.CommandParameter as the row the menu is for.

A page asks for the menu through an ordinary command, for example showRowMenu(key, x, y) from its contextmenu event, after calling preventDefault() so the web view's own menu stays closed. An implementation that does not override this member checks the arguments and reports PlatformUnavailableReason.OwnerUnavailable.

Documentation from IWindowPlatformServices.ShowContextMenuAsync.

StartDragAsync

public ValueTask<PlatformResult<DesktopDragEffects>> StartDragAsync(DesktopDragData data, CancellationToken cancellationToken = default)

Starts a native drag of files, text or URIs out of the window, and returns the effect of the drop.

Parameters

data

What to drag; the rules of DesktopDragData.

cancellationToken

Cancels before the drag starts; a running drag ends with the pointer.

Returns

Success with the effect the drop target chose, one of DesktopDragData.AllowedEffects, or DesktopDragEffects.None when the user cancelled the drag, dropped it where nothing accepted it, or no mouse button was held when the call arrived. Unavailable: PlatformUnavailableReason.OwnerUnavailable before the window opens or on a host without a native owner, PlatformUnavailableReason.OwnerClosed once it closed, and PlatformUnavailableReason.BackendUnavailable when the window cannot start drags (PlatformCapabilityIds.DragOut).

Exceptions

ArgumentException

data breaks a rule of DesktopDragData.

ArgumentNullException

data is null.

Remarks

Call it while the user holds the primary mouse button on the window: a page asks for the drag through an ordinary command from its dragstart event, after calling preventDefault() so the web view starts no drag of its own, and the ViewModel builds data from its own state, for example the files of the row the command names. The paths never come from the page.

DesktopDragEffects.Move means the target asked for a move: the files may already be gone, for example after an optimized move on Windows, so delete what remains. An implementation that does not override this member checks the arguments and reports PlatformUnavailableReason.OwnerUnavailable.

Documentation from IWindowPlatformServices.StartDragAsync.