Skip to content

RecordingWindowBackend

RecordingWindowBackend is a mock implementation of IHermesWindowBackend that records all interactions for verification in tests. It also supports simulating events to test application responses.

Overview

The recording backend captures:

  • Method calls with arguments and timestamps
  • Property changes with old/new values
  • Events raised
  • Web messages sent and received
  • Navigation history
  • Custom titlebar drag actions
  • Custom scheme registrations

Properties

State Properties

PropertyTypeDescription
IsInitializedboolWhether Initialize() was called
IsShownboolWhether Show() was called
IsClosedboolWhether Close() was called
InitialOptionsHermesWindowOptions?Options passed to Initialize()

Window State

PropertyTypeDescription
TitlestringCurrent window title
Size(int Width, int Height)Current window size
Position(int X, int Y)Current window position
IsMaximizedboolWhether window is maximized
IsMinimizedboolWhether window is minimized

Platform Simulation

csharp
var backend = new RecordingWindowBackend();
backend.Platform = HermesPlatform.macOS; // Simulate macOS

// Now your code can check backend.Platform and behave accordingly

Recording Access

Access recorded data through the Recording property:

csharp
var recording = backend.Recording;

// Check method calls
recording.MethodWasCalled("Show");

// Check navigation
recording.NavigatedTo("https://example.com");
recording.NavigatedToPattern("example");

// Check web messages
recording.SentWebMessageMatching("response");
recording.ReceivedWebMessageMatching("request");

// Check events
recording.EventWasRaised("Maximized");

WindowBackendRecording Collections

PropertyTypeDescription
MethodCallsIReadOnlyList<MethodCall>All method invocations
PropertyChangesIReadOnlyList<PropertyChange>All property mutations
EventsIReadOnlyList<RecordedEvent>All events raised
WebMessagesReceivedIReadOnlyList<string>Messages from JavaScript
WebMessagesSentIReadOnlyList<string>Messages to JavaScript
NavigationsIReadOnlyList<string>URLs navigated to
LastDragActionstring?Last drag region action

Record Types

csharp
// Method call record
public record MethodCall
{
    public string MethodName { get; init; }
    public object?[] Arguments { get; init; }
    public DateTimeOffset Timestamp { get; init; }
}

// Property change record
public record PropertyChange
{
    public string PropertyName { get; init; }
    public object? OldValue { get; init; }
    public object? NewValue { get; init; }
    public DateTimeOffset Timestamp { get; init; }
}

// Event record
public record RecordedEvent
{
    public string EventName { get; init; }
    public object?[] Arguments { get; init; }
    public DateTimeOffset Timestamp { get; init; }
}

Event Simulation

Window Events

csharp
backend.SimulateResize(1920, 1080);   // Fires Resized event
backend.SimulateMove(100, 200);       // Fires Moved event
backend.SimulateMaximize();           // Fires Maximized event
backend.SimulateRestore();            // Fires Restored event
backend.SimulateFocusIn();            // Fires FocusIn event
backend.SimulateFocusOut();           // Fires FocusOut event

Web Messages

csharp
// Simulate JavaScript sending a message to C#
backend.SimulateWebMessage("""{"type":"click","id":42}""");

Custom Titlebar Drag Regions

csharp
backend.SimulateDragRegionClick();       // User clicked on drag region
backend.SimulateDragRegionDoubleClick(); // User double-clicked (maximize)
backend.SimulateNonDragRegionClick();    // User clicked non-drag area

Custom Scheme Testing

Test custom resource schemes:

csharp
// Register a scheme handler
backend.RegisterCustomScheme("app", url =>
{
    if (url.EndsWith("style.css"))
    {
        var stream = new MemoryStream(Encoding.UTF8.GetBytes("body { }"));
        return (stream, "text/css");
    }
    return (null, null);
});

// Verify registration
Assert.True(backend.HasCustomScheme("app"));

// Test the handler
var (content, contentType) = backend.TestCustomScheme("app", "app://style.css")!.Value;
Assert.Equal("text/css", contentType);

Threading Simulation

The backend supports testing async operations:

csharp
// Queue an action (doesn't execute immediately when off UI thread)
backend.BeginInvoke(() => DoSomething());

// Process queued actions (simulates UI thread processing)
backend.ProcessPending();

Clearing Recordings

Reset recorded data between test phases:

csharp
backend.Recording.Clear();

Example: Verifying Method Call Sequence

csharp
[Fact]
public void Window_InitializesInCorrectOrder()
{
    var backend = new RecordingWindowBackend();
    var window = new HermesWindow(backend);

    window.SetTitle("Test").SetSize(800, 600);
    window.Show();

    var methods = backend.Recording.MethodCalls.Select(c => c.MethodName).ToList();

    Assert.Equal("Initialize", methods[0]);
    Assert.Equal("Show", methods[1]);
}

See Also