Skip to content

ui::testing::TestClipboard

Header: winui/testing/TestClipboard.hpp.

Derives from ui::Clipboard.

An in-memory Clipboard for headless tests. It holds one ClipboardContent and never touches the system clipboard.

Behaves like the real backend where a test can observe it: set_text() replaces every part, set_content() with nothing set fails and leaves the old content, every write and clear() advances content_changed(), and available_formats() lists the format names a real copy would put on the system clipboard. history_enabled/roaming_enabled are plain fields a test sets.

Install it with set_clipboard() so ui::clipboard() and every control use it. It uninstalls itself when destroyed, so a test that ends early never leaves a dangling clipboard behind.

Example:

// A test: Ctrl+V pastes from the clipboard.
ui::testing::TestClipboard clipboard;
ui::testing::set_clipboard(&clipboard);
clipboard.set_text("Portland");
REQUIRE(clipboard.contains(ui::StandardDataFormat::Text));
REQUIRE(clipboard.available_formats() == std::vector<std::string>{"CF_UNICODETEXT"});

auto box = std::make_unique<ui::TextBoxControl>();
ui::testing::set_focused(*box, true);
box->handle_key({ui::Key::V, ui::KeyAction::Down, {.ctrl = true}});
REQUIRE(box->text() == "Portland");

int changes = 0;
ui::Subscription watch = clipboard.content_changed().observe([&](const std::uint64_t&) { ++changes; });
clipboard.clear();
REQUIRE(changes == 1);
REQUIRE_FALSE(clipboard.text().has_value());

history_enabled

bool history_enabled = false

What is_history_enabled() reports; off by default.

roaming_enabled

bool roaming_enabled = false

What is_roaming_enabled() reports; off by default.

TestClipboard

TestClipboard() = default

~TestClipboard

~TestClipboard() override

Uninstalls it if it is still the installed clipboard.

TestClipboard

TestClipboard(const TestClipboard&) = delete

operator=

TestClipboard& operator=(const TestClipboard&) = delete

set_text

void set_text(const std::string& text) override

Replaces the whole content with text, as a real copy does.

text

std::optional<std::string> text() const override

The stored text, or nullopt if none was set.

set_content

bool set_content(const ClipboardContent& content, const ClipboardContentOptions& options = {}) override

Stores content and options and raises a change; false (nothing changed) when content has no part set.

bitmap

std::optional<Bitmap> bitmap() const override

The stored picture, returned as stored (no encode/decode round trip).

storage_items

std::vector<std::filesystem::path> storage_items() const override

The stored paths, in order.

requested_operation

DataPackageOperation requested_operation() const override

The stored operation, or None when there are no storage items for it to apply to.

contains

bool contains(StandardDataFormat format) const override

Whether the stored content has the part format names.

available_formats

std::vector<std::string> available_formats() const override

The system format names for the stored parts: CF_UNICODETEXT for text, PNG and CF_DIBV5 for a bitmap, CF_HDROP and Preferred DropEffect for storage items. The history and cloud opt-out markers follow when the options turned those off.

clear

void clear() override

Drops the content and options and raises a change.

is_history_enabled

bool is_history_enabled() const override

Reports history_enabled.

is_roaming_enabled

bool is_roaming_enabled() const override

Reports roaming_enabled.

options

const ClipboardContentOptions& options() const

The options the last set_content() was given.