Skip to content

ui::testing::TestPrintHost

Header: winui/testing/TestPrintHost.hpp.

Derives from ui::PrintHost.

PrintHost test double: shows no dialog and prints to nothing. It runs the job on US Letter paper with the headless measurer, keeps each page's drawing, and answers as told.

Install it with set_print_host(); ui::print() then reaches it. It uninstalls itself when destroyed.

Example:

ui::testing::TestPrintHost printer;
ui::testing::set_print_host(&printer);
const ui::PrintResult result = ui::print(window, ui::PrintJob{
    .title      = "Report",
    .page_count = [](const ui::PrintPage&, const ui::PrintContext&) { return std::size_t{3}; },
    .draw_page  = [](ui::Renderer&, const ui::PrintPage&, const ui::PrintContext&) { /* draw the page */ },
});
REQUIRE(result == ui::PrintResult::Printed);
REQUIRE(printer.pages.size() == 3);

answer

PrintResult answer = PrintResult::Printed

The answer: Printed draws the chosen pages, Canceled and Failed draw nothing.

sheet

Size sheet {816.0f, 1056.0f}

The sheet, US Letter (8.5 x 11 inches) by default, with a quarter inch the printer can't reach.

unprintable

float unprintable = 24.0f

chosen

std::optional<std::vector<std::size_t>> chosen {}

The pages the user picked in the print dialog (0-based); unset prints them all.

titles

std::vector<std::string> titles

Each job's title and owner, in order.

owners

std::vector<const Window*> owners

pages

std::vector<TestRenderer> pages

The drawing of each printed page, in order, and its index in the document.

indices

std::vector<std::size_t> indices

TestPrintHost

TestPrintHost() = default

~TestPrintHost

~TestPrintHost() override

TestPrintHost

TestPrintHost(const TestPrintHost&) = delete

operator=

TestPrintHost& operator=(const TestPrintHost&) = delete

print

PrintResult print(const Window* owner, PrintJob& job) override

Runs job over owner (nullptr: none) and says how it ended.