ui::testing::TestContextMenuHost¶
Header: winui/testing/TestContextMenuHost.hpp.
Derives from ui::ContextMenuHost.
ContextMenuHost test double: records every menu a control asks to show and answers with a scripted choice instead of opening a popup.
Script the answer with pick() (by visible text) or pick_index() before the control's Element::handle_context_menu() runs; the script applies to the next show() only and then resets to a dismissal. Like the real menu, a separator or a disabled item can't be picked, so scripting one reads as a dismissal.
Example:
ui::testing::HeadlessTextMeasurer measurer;
ui::TextControl block{"orange text"};
block.set_selection_enabled(true);
(void)block.measure({400, 30}, measurer, ui::Theme::default_light());
block.arrange({0, 0, 400, 30});
block.select(0, 6);
ui::testing::set_focused(block, true);
ui::testing::TestClipboard clipboard;
ui::testing::TestContextMenuHost menu;
ui::testing::set_clipboard(&clipboard);
menu.pick("Copy");
REQUIRE(block.handle_context_menu(ui::Point{5, 5}, menu));
REQUIRE(menu.show_count() == 1);
REQUIRE(clipboard.text() == "orange");
pick¶
Makes the next show() choose the first non-separator item whose text equals text. Overrides an earlier pick_index(); no match (or a disabled match) dismisses.
pick_index¶
Makes the next show() choose the item at index (separators count). Overrides an earlier pick(); an out-of-range, separator or disabled index dismisses.
show_count¶
How many menus have been shown over this host's lifetime.
last_items¶
The items of the most recent show() (empty before the first one).
last_position¶
Where the most recent menu was anchored, in window DIPs (the caret for a keyboard-invoked menu).
last_texts¶
The last menu's item texts, separators as empty strings.
enabled¶
The enabled state of the last menu's item with visible text text (nullopt: absent).
show¶
Records position/items, bumps show_count(), and returns the scripted choice (then clears the script). Returns nullopt, a dismissal, when nothing pickable was scripted.