Skip to content

ui::testing::HeadlessWindowHost

Header: winui/testing/HeadlessUI.hpp.

The in-memory window host HeadlessUI installs. It follows the same window-state rules as the real host without a native window, so window-state and title-bar tests can drive it.

  • Requests apply synchronously by default. Set defer_requests to queue them instead, so a test can observe the gap between a request and the system's answer, then call apply_pending_requests() to let them land.
  • simulate_state() is the other direction: the user clicking the caption's maximize button, or Win+Down. It reports the new state without anyone having asked.
  • Everything the window sends the host is recorded, and click_system_menu_item() and click_thumbnail_button() stand in for the user picking an entry.

Like the real host, it remembers the normal bounds across minimize and maximize, and whether full screen was entered from maximized, so restore() goes back to the right place.

Example:

ui::testing::HeadlessUI ui;
ui::Window& window = ui.window();

ui.host().defer_requests = true;
window.request_state(ui::WindowState::Maximized);
REQUIRE(window.state().get() == ui::WindowState::Normal); // asked, not yet answered
ui.host().apply_pending_requests();
REQUIRE(window.state().get() == ui::WindowState::Maximized);

window.set_system_menu({{.text = "Always on top", .on_click = [&] { /* ... */ }}});
REQUIRE(ui.host().click_system_menu_item(0));

window.close();
REQUIRE(ui.host().close_requests == 1);

defer_requests

bool defer_requests = false

When true, requests are queued instead of applied (see the class comment).

normal_bounds

Rect normal_bounds {0.0f, 0.0f, 800.0f, 600.0f}

The bounds a restore to Normal lands on, and what bounds() reports.

frame

Thickness frame {}

What the frame takes around the client area, for resize() and the size limits.

screen_work_area

Rect screen_work_area {0.0f, 0.0f, 1920.0f, 1040.0f}

What work_area() reports.

close_requests

int close_requests = 0

How many times close() was called.

style_applications

int style_applications = 0

How many times set_style() was called.

activations

int activations = 0

How many times activate() ran.

wake_requests

std::atomic<int> wake_requests {0}

How many times wake() was called; atomic since wake() may come from any thread.

HeadlessWindowHost

HeadlessWindowHost(Window& window)

A host for window, which must outlive it. Does not install itself; HeadlessUI does.

~HeadlessWindowHost

~HeadlessWindowHost()

HeadlessWindowHost

HeadlessWindowHost(const HeadlessWindowHost&) = delete

operator=

HeadlessWindowHost& operator=(const HeadlessWindowHost&) = delete

request_state

void request_state(WindowState state)

Applies state at once and reports it back, or queues it while defer_requests is set.

restore

void restore()

Leaves Minimized/Maximized/Fullscreen for the state it came from; queued like request_state() while defer_requests is set.

close

void close()

Only counts the request in close_requests; the window is not destroyed.

wake

void wake()

Only counts the request in wake_requests; safe from any thread.

set_title

void set_title(std::string_view title)

Records the title for title().

set_style

void set_style(const WindowStyle& style)

Records the style for style() and counts it in style_applications.

set_chrome

void set_chrome(const ChromeOverrides& chrome)

Records the chrome overrides for chrome().

placement

std::optional<WindowPlacement> placement() const

normal_bounds, the current state, and whether a restore would go back to Maximized.

set_icon

void set_icon(const WindowIcon* icon)

Records only whether there is an icon (has_icon()).

set_system_menu

void set_system_menu(const std::vector<MenuItem>& items)

Records the entries' texts, checks and enabled states for click_system_menu_item().

set_taskbar_progress

void set_taskbar_progress(const TaskbarProgress& progress)

Records the progress for taskbar_progress().

set_taskbar_overlay

void set_taskbar_overlay(const WindowIcon* icon, std::string_view description)

Records whether there is an overlay and its description.

request_attention

void request_attention(bool flash)

Records the flash request for flashing().

set_thumbnail_buttons

void set_thumbnail_buttons(const std::vector<ThumbnailButton>& buttons)

Fixes the button count on the first call (at most 7), as the taskbar does. Later calls only update which of those buttons are clickable.

move

void move(Point position)

Moves normal_bounds to position.

resize

void resize(Size size)

Resizes normal_bounds so its client part, less frame, is size, clamped to the size limits.

bounds

std::optional<Rect> bounds() const

Always normal_bounds.

work_area

std::optional<Rect> work_area() const

Always screen_work_area.

set_visible

void set_visible(bool visible, bool activate)

Reports visibility to the window, and activates it when asked to.

activate

void activate()

Counts the activation in activations and reports the window active.

set_size_limits

void set_size_limits(Size min, Size max)

Records the client size limits and re-clamps the current size, as a real host does.

apply_pending_requests

void apply_pending_requests()

Applies every queued request in order, reporting each resulting state.

pending_request_count

std::size_t pending_request_count() const

How many requests are queued while defer_requests is set.

simulate_state

void simulate_state(WindowState state)

A change the user makes from outside the app. Reports state and, while not minimized, remembers it for restore().

simulate_active

void simulate_active(bool active)

The user activating another window, or coming back.

simulate_caption_insets

void simulate_caption_insets(const CaptionInsets& insets)

Reports the caption-button insets of an extended title bar, as the system would.

click_system_menu_item

bool click_system_menu_item(std::size_t index)

Picks the app's system-menu entry index, as the user would. False if the host was never given that many entries.

click_thumbnail_button

bool click_thumbnail_button(std::size_t index)

Clicks thumbnail button index. False if it doesn't exist, or is disabled or hidden.

title

const std::string& title() const

The last title set_title() was given.

style

const WindowStyle& style() const

The last style set_style() was given.

chrome

const ChromeOverrides& chrome() const

The last chrome overrides set_chrome() was given.

has_icon

bool has_icon() const

Whether the last set_icon() passed an icon.

system_menu_texts

const std::vector<std::string>& system_menu_texts() const

The system menu entries' texts, in order; empty text is a separator.

system_menu_checks

const std::vector<bool>& system_menu_checks() const

The system menu entries' check marks, parallel to system_menu_texts().

taskbar_progress

const TaskbarProgress& taskbar_progress() const

The last taskbar progress set.

has_overlay

bool has_overlay() const

Whether a taskbar overlay icon is set.

overlay_description

const std::string& overlay_description() const

The overlay's accessible description.

flashing

bool flashing() const

Whether the last request_attention() asked to flash.

thumbnail_button_count

std::size_t thumbnail_button_count() const

How many buttons the taskbar holds, fixed by the first set_thumbnail_buttons() call.

min_size

const Size& min_size() const

The minimum client size last set; zero on an axis is none.

max_size

const Size& max_size() const

The maximum client size last set; zero on an axis is none.