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
| Property | Type | Description |
|---|---|---|
IsInitialized | bool | Whether Initialize() was called |
IsShown | bool | Whether Show() was called |
IsClosed | bool | Whether Close() was called |
InitialOptions | HermesWindowOptions? | Options passed to Initialize() |
Window State
| Property | Type | Description |
|---|---|---|
Title | string | Current window title |
Size | (int Width, int Height) | Current window size |
Position | (int X, int Y) | Current window position |
IsMaximized | bool | Whether window is maximized |
IsMinimized | bool | Whether 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 accordinglyRecording 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
| Property | Type | Description |
|---|---|---|
MethodCalls | IReadOnlyList<MethodCall> | All method invocations |
PropertyChanges | IReadOnlyList<PropertyChange> | All property mutations |
Events | IReadOnlyList<RecordedEvent> | All events raised |
WebMessagesReceived | IReadOnlyList<string> | Messages from JavaScript |
WebMessagesSent | IReadOnlyList<string> | Messages to JavaScript |
Navigations | IReadOnlyList<string> | URLs navigated to |
LastDragAction | string? | 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 eventWeb 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 areaCustom 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
- TestableHermesWindow — High-level test wrapper
- HermesAssert — Fluent assertions
- Testing Examples — Common patterns
