Natty
Natty

Reputation: 539

How can I simulate a Shift + Click on a Vaadin Button?

I created an application with Vaadin 23 containing buttons (com.vaadin.flow.component.button.Button) that reacts in differents ways when clicked with and without holding the shift key.

Button button = new Button("Click me", event -> {
  if (event.isShiftKey()) {
    System.out.println("Shift + click");
  } else {
    System.out.println("Click");
  }
});

I am now trying to create unit tests for this but I don't know how to simulate the shift key. The Button class only offers a .click() method without any parameters.

button.click(); // The "else" part of the event is called

Is there a way (without creating a custom Button class) to simulate a shift + click in Vaadin ?

Upvotes: 1

Views: 227

Answers (1)

Javier
Javier

Reputation: 12398

Button.click is implemented as:

public void click() {
        fireEvent(new ClickEvent<>(this, false, 0, 0, 0, 0, 0, 0, false, false,
                false, false));
}

Button.fireEvent is protected, but you can use ComponentUtil.fireEvent:

ComponentUtil.fireEvent(
  this,
  new ClickEvent<>(this, false, 0, 0, 0, 0, 0, 0, false, true, false, false));
  //                                                       ^
  //                                                     shiftKey

Upvotes: 2

Related Questions