Skip to content

ui::testing::TestRenderer

Header: winui/testing/TestRenderer.hpp.

Derives from ui::Renderer.

Renderer test double that records every draw call as a RenderCommand instead of painting.

This is the headless testing boundary: any Element can render() into it without a native window, a GPU or a desktop session, and the resulting command stream is deterministic. Each override appends exactly one command of the matching alternative; images and SVGs are recorded by their natural size only, never their pixels. render/Describe.hpp turns the stream into the text used by the semantic golden files.

Example:

ui::ButtonControl       button{"Hello"};
ui::testing::TestRenderer renderer;
button.render(renderer); // Theme::default_light() by default

REQUIRE(renderer.count() == 3); // background, elevation border, label
REQUIRE(std::holds_alternative<ui::FillRoundedRect>(renderer.commands[0]));
REQUIRE(renderer.count_of<ui::DrawTextRun>() == 1);
REQUIRE(std::get<ui::DrawTextRun>(renderer.commands[2]).text == "Hello");

renderer.clear(); // reuse for the next frame

commands

std::vector<RenderCommand> commands

Every recorded command, in draw order. Public so tests can index and std::get directly.

scale

float scale = 1.0f

What device_scale() reports, 1 by default: set it to test a mark drawn per device pixel at another DPI (1.5 for 144 DPI).

TestRenderer

TestRenderer()

TestRenderer

TestRenderer(const TestRenderer& other)

TestRenderer

TestRenderer(TestRenderer&& other) noexcept

operator=

TestRenderer& operator=(const TestRenderer& other)

operator=

TestRenderer& operator=(TestRenderer&& other) noexcept

~TestRenderer

~TestRenderer() override

fill_rounded_rect

void fill_rounded_rect(const Rect& bounds, float radius, const Color& color) override

Records a FillRoundedRect.

stroke_rounded_rect

void stroke_rounded_rect(const Rect& bounds, float radius, const Color& color, float stroke_width) override

Records a StrokeRoundedRect.

draw_text

void draw_text(std::string_view text, const Rect& bounds, const Color& color, const TextStyle& style) override

Records a DrawTextRun, copying text into an owned string.

fill_rounded_rect_gradient

void fill_rounded_rect_gradient(const Rect& bounds, float radius, const LinearGradientBrush& brush) override

Records a FillRoundedRectGradient.

stroke_rounded_rect_gradient

void stroke_rounded_rect_gradient(const Rect& bounds, float radius, const LinearGradientBrush& brush, float extent_px, float stroke_width) override

Records a StrokeRoundedRectGradient.

stroke_line

void stroke_line(Point from, Point to, const Color& color, float stroke_width) override

Records a StrokeLine.

stroke_wavy_line

void stroke_wavy_line(Point from, Point to, const Color& color, float stroke_width, float amplitude, float wavelength) override

Records a StrokeWavyLine (the spelling squiggle).

fill_polygon

void fill_polygon(const FillPolygon& polygon) override

Records the FillPolygon as-is.

stroke_polyline

void stroke_polyline(const StrokePolyline& polyline) override

Records the StrokePolyline as-is.

draw_geometry

void draw_geometry(const DrawGeometry& geometry) override

Records the DrawGeometry as-is.

push_clip

void push_clip(const Rect& bounds) override

Records a PushClip.

pop_clip

void pop_clip() override

Records a PopClip.

push_clip_ellipse

void push_clip_ellipse(Point center, float radius_x, float radius_y) override

Records a PushClipEllipse.

push_clip_rounded_rect

void push_clip_rounded_rect(const Rect& bounds, float radius) override

Records a PushClipRoundedRect.

push_opacity_layer

void push_opacity_layer(float opacity) override

Records a PushOpacityLayer.

pop_opacity_layer

void pop_opacity_layer() override

Records a PopOpacityLayer.

push_matrix

void push_matrix(const Matrix& m) override

Records a PushTransform decomposed as (m11, offset {dx, dy}, m22, m12, m21).

pop_transform

void pop_transform() override

Records a PopTransform.

set_rasterization

void set_rasterization(const SetRasterization& policy) override

Records the SetRasterization policy as-is.

device_scale

float device_scale() const override

Returns scale.

fill_ellipse

void fill_ellipse(Point center, float radius_x, float radius_y, const Color& color) override

Records a FillEllipse.

stroke_ellipse

void stroke_ellipse(Point center, float radius_x, float radius_y, const Color& color, float stroke_width) override

Records a StrokeEllipse.

fill_rect_pattern

void fill_rect_pattern(const Rect& bounds, Pattern pattern, const Color& fg, const Color& bg) override

Records a FillRectPattern.

draw_image_nine_slice

void draw_image_nine_slice(const std::string& image_name, const Rect& bounds, const Thickness& slice_margins) override

Records a DrawImageNineSlice by image name.

draw_image

void draw_image(const Bitmap& image, const Rect& bounds, Stretch stretch) override

Records a DrawImage carrying the image's pixel size, not its pixels.

draw_image_rect

void draw_image_rect(const Bitmap& image, const Rect& source, const Rect& bounds) override

Records a DrawImageRect carrying the image's pixel size and source rect.

draw_svg

void draw_svg(const SvgImage& svg, const Rect& bounds) override

Records a DrawSvg carrying the document's natural size.

clear

void clear()

Drops every recorded command, e.g. between two render() passes.

count

std::size_t count() const

Number of recorded commands (commands.size()).

count_of

template <typename T>
std::size_t count_of() const

How many recorded commands hold alternative T (e.g. count_of<DrawTextRun>()).