TestableHermesWindow
TestableHermesWindow is the primary entry point for testing Hermes window behavior. It wraps a HermesWindow with a RecordingWindowBackend, providing fluent configuration and event simulation capabilities.
Creating a Test Window
csharp
// Basic creation
using var testWindow = new TestableHermesWindow();
// With custom backend
var backend = new RecordingWindowBackend { Platform = HermesPlatform.macOS };
using var testWindow = new TestableHermesWindow(backend);Properties
| Property | Type | Description |
|---|---|---|
Window | HermesWindow | The underlying HermesWindow instance |
Backend | RecordingWindowBackend | The mock backend for verification |
Recording | WindowBackendRecording | Access to recorded interactions |
Fluent Configuration
All configuration methods return this for chaining:
csharp
using var testWindow = new TestableHermesWindow()
.SetTitle("My App")
.SetSize(1024, 768)
.SetPosition(100, 100)
.Center()
.SetResizable(true)
.SetChromeless(false)
.SetCustomTitleBar(true)
.Load("https://example.com");Window Properties
csharp
SetTitle(string title) // Set window title
SetSize(int width, int height) // Set initial size
SetPosition(int x, int y) // Set initial position
Center() // Center on screen when shown
SetResizable(bool resizable) // Enable/disable resize
SetChromeless(bool chromeless) // Remove window chrome
SetCustomTitleBar(bool enabled) // Enable custom titlebar mode
Maximize() // Start maximized
Minimize() // Start minimizedContent Loading
csharp
Load(string url) // Load URL in WebView
LoadHtml(string html) // Load HTML content directlyEvent Handlers
csharp
OnWebMessage(Action<string> handler) // Messages from JavaScript
OnMaximized(Action handler) // Window maximized
OnRestored(Action handler) // Window restored
OnResized(Action<int, int> handler) // Window resized (width, height)
OnMoved(Action<int, int> handler) // Window moved (x, y)
OnClosing(Action handler) // Window closingLifecycle Methods
csharp
// Show the window (non-blocking in tests)
testWindow.Show();
// Show and wait for close (non-blocking in tests)
testWindow.WaitForClose();
// Close the window
testWindow.Close();TIP
In test mode, Show() and WaitForClose() do not actually block. They record the operation and continue immediately.
Event Simulation
Simulate user interactions and system events:
csharp
// Web messages
testWindow.SimulateWebMessage("""{"type":"event","data":"test"}""");
// Window state
testWindow.SimulateResize(1920, 1080);
testWindow.SimulateMove(200, 100);
testWindow.SimulateMaximize();
testWindow.SimulateRestore();
// Focus
testWindow.SimulateFocusIn();
testWindow.SimulateFocusOut();
// Custom titlebar drag regions
testWindow.SimulateDragRegionClick();
testWindow.SimulateDragRegionDoubleClick();
testWindow.SimulateNonDragRegionClick();Complete Example
csharp
[Fact]
public void Window_HandlesMaximizeRestoreCycle()
{
var events = new List<string>();
using var testWindow = new TestableHermesWindow()
.SetTitle("Test App")
.SetSize(800, 600)
.OnMaximized(() => events.Add("maximized"))
.OnRestored(() => events.Add("restored"));
testWindow.Show();
// Simulate maximize/restore cycle
testWindow.SimulateMaximize();
testWindow.SimulateRestore();
// Verify events fired in order
Assert.Equal(["maximized", "restored"], events);
// Verify final state
HermesAssert.IsNotMaximized(testWindow.Backend);
}See Also
- RecordingWindowBackend — Direct backend access
- HermesAssert — Assertion helpers
- Testing Examples — Common patterns
