public sealed class RunicTestClipboard : IDesktopClipboard, ITextClipboard An in-memory IDesktopClipboard for tests: it holds one DesktopClipboardContent, checks writes with the rules of the real clipboard, records each write and reports changes to watches.
Remarks
Set what the user copied with RunicTestClipboard.SetContent or RunicTestClipboard.SetText before the action that pastes, and read what the application copied from RunicTestClipboard.Content or RunicTestClipboard.Writes. Reads beyond their limit report PlatformFailureCode.TooLarge, as the real clipboard does. A write of files checks that they exist, as the real clipboard does.
Set RunicTestClipboard.UnavailableReason to make every call report unavailable; when the test window closes, calls report PlatformUnavailableReason.OwnerClosed and watches end. The type is safe to use from any thread.
Properties
Content
public DesktopClipboardContent? Content { get; } The clipboard's content, or null when it is empty.
UnavailableReason
public PlatformUnavailableReason? UnavailableReason { get; set; } When set, every call reports Unavailable with this reason.
Writes
public IReadOnlyList<DesktopClipboardContent> Writes { get; } The content of each successful write by the application, oldest first.
Methods
GetFormatsAsync
public ValueTask<PlatformResult<DesktopClipboardFormats>> GetFormatsAsync(CancellationToken cancellationToken = default) Reads which formats the clipboard has, without reading any of them.
Parameters
cancellationTokenCancels before the request reaches the window.
Returns
The formats; DesktopClipboardFormats.None for an empty clipboard or one with only formats Runic does not read.
Documentation from IDesktopClipboard.GetFormatsAsync.
ReadFilesAsync
public ValueTask<PlatformResult<ImmutableArray<string>>> ReadFilesAsync(int maximumCount, CancellationToken cancellationToken = default) Reads the clipboard's file list as fully qualified local paths; an empty list means no files.
Parameters
maximumCountThe most paths to return; more reports
PlatformFailureCode.TooLarge.cancellationTokenCancels before the request reaches the window.
Exceptions
ArgumentOutOfRangeExceptionmaximumCountis negative.
Remarks
Entries that are not local files, such as web URIs, are skipped.
Documentation from IDesktopClipboard.ReadFilesAsync.
ReadHtmlAsync
public ValueTask<PlatformResult<string?>> ReadHtmlAsync(int maximumCharacters, CancellationToken cancellationToken = default) Reads bounded HTML; a successful null means no HTML.
Parameters
maximumCharactersThe most characters to return; more reports
PlatformFailureCode.TooLarge.cancellationTokenCancels before the request reaches the window.
Exceptions
ArgumentOutOfRangeExceptionmaximumCharactersis negative.
Remarks
On Windows the result is the copied fragment of the CF_HTML document; elsewhere the HTML as offered.
Documentation from IDesktopClipboard.ReadHtmlAsync.
ReadImageAsync
public ValueTask<PlatformResult<byte[]?>> ReadImageAsync(int maximumBytes, CancellationToken cancellationToken = default) Reads the clipboard's image as PNG bytes; a successful null means no image.
Parameters
maximumBytesThe most PNG bytes to return; more reports
PlatformFailureCode.TooLarge.cancellationTokenCancels before the request reaches the window.
Exceptions
ArgumentOutOfRangeExceptionmaximumBytesis negative.
Remarks
An image that another application offers in another format, such as a bitmap on Windows or TIFF on macOS, is converted to PNG.
Documentation from IDesktopClipboard.ReadImageAsync.
ReadRtfAsync
public ValueTask<PlatformResult<string?>> ReadRtfAsync(int maximumCharacters, CancellationToken cancellationToken = default) Reads bounded Rich Text Format source; a successful null means no RTF.
Parameters
maximumCharactersThe most characters, which are bytes of RTF, to return; more reports
PlatformFailureCode.TooLarge.cancellationTokenCancels before the request reaches the window.
Exceptions
ArgumentOutOfRangeExceptionmaximumCharactersis negative.
Remarks
Each byte of the RTF becomes one character (Latin-1), as DesktopClipboardContent.Rtf describes.
Documentation from IDesktopClipboard.ReadRtfAsync.
ReadTextAsync
public ValueTask<PlatformResult<string?>> ReadTextAsync(int maximumCharacters, CancellationToken cancellationToken = default) Reads bounded text; successful null means no text format and an empty string means empty text.
Documentation from ITextClipboard.ReadTextAsync.
SetContent
public void SetContent(DesktopClipboardContent? content) Replaces the content as another application's copy would; watches report the change.
Parameters
contentThe new content, or
nullto empty the clipboard.
Remarks
Unlike a write, this does not check the content, so a test can offer content another application could.
SetText
public void SetText(string text) Replaces the content with text as another application's copy would.
Parameters
textThe text.
WatchAsync
public IAsyncEnumerable<PlatformResult<DesktopClipboardFormats>> WatchAsync(CancellationToken cancellationToken = default) Reports the clipboard's formats now, then after each change; never the content.
Parameters
cancellationTokenEnds the watch, which then throws
OperationCanceledException.
Remarks
Every change is reported, also one that leaves the same formats, such as other copied text; changes close together may arrive as one. Read the content when the user pastes, not on every change: other applications' content is theirs. When the window closes the watch reports PlatformUnavailableReason.OwnerClosed and ends; a window that cannot watch reports Unavailable once and ends.
macOS has no change notification: the watch compares the pasteboard's change count four times a second. On Wayland the compositor tells only a focused application about the clipboard, so changes arrive when a window of the application gains focus.
Documentation from IDesktopClipboard.WatchAsync.
WriteAsync
public ValueTask<PlatformResult<PlatformUnit>> WriteAsync(DesktopClipboardContent content, CancellationToken cancellationToken = default) Replaces the clipboard with every format of content, in one ownership.
Parameters
contentThe formats to offer.
cancellationTokenPrevents queued work; an initiated native write returns its actual outcome.
Returns
Success once the clipboard offers the content, or Failed with PlatformFailureCode.InvalidData or PlatformFailureCode.NotFound for content that breaks the rules of DesktopClipboardContent, before anything changed.
Exceptions
ArgumentExceptioncontentsets no format, orDesktopClipboardContent.Fileshas more than 4096 entries or an entry that is null, empty or not fully qualified.ArgumentNullExceptioncontentisnull.
Documentation from IDesktopClipboard.WriteAsync.
WriteTextAsync
public ValueTask<PlatformResult<PlatformUnit>> WriteTextAsync(string text, CancellationToken cancellationToken = default) Writes text. Cancellation prevents queued work; an initiated native write returns its actual outcome.
Documentation from ITextClipboard.WriteTextAsync.