Home > Back-end >  How can I simulate a Shift Click on a Vaadin Button?
How can I simulate a Shift Click on a Vaadin Button?

Time:11-22

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 ?

CodePudding user response:

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
  • Related