Double-click on a visual element
Use the DoubleClick() method to simulate a mouse double-click on a visual element in your tests. This method sends the appropriate mouse events to the target element, mimicking a real user interaction.
The following examples show how to simulate a double-click on a button:
[Test]
public void DoubleClickExample()
{
// Make sure the UI is totally up to date.
simulate.FrameUpdate();
var actionWasExecuted = false;
void ValidateDoubleClickAction(PointerDownEvent e)
{
if (e.clickCount == 2)
{
actionWasExecuted = true;
}
}
// Set up the button's double click functionality to flip a boolean.
Button button = rootVisualElement.Q<Button>("MyButton");
button.RegisterCallback<PointerDownEvent>(e => ValidateDoubleClickAction(e),
TrickleDown.TrickleDown);
// Validate that a single click does not trigger
// our button's custom functionality.
simulate.Click(button);
Assert.That(actionWasExecuted, Is.False,
"Button action was executed unexpectedly.");
// Double click on the button's position.
simulate.DoubleClick(button);
// Make sure the UI Toolkit update loop executes.
// This is technically not required for this specific case,
// since this button just flips a boolean when it is clicked.
// However, if the button performs actions linked to the UI Toolkit
// update loop, like styling or scheduling, it is required.
simulate.FrameUpdate();
Assert.That(actionWasExecuted, Is.True,
"Button action was not executed.");
}