我怎么能模拟一个Shift +点击一个vadin按钮?



我用Vaadin 23创建了一个应用程序,其中包含按钮(com.vaadin.flow.component.button.Button),当按住shift键和不按住shift键时,它们会以不同的方式反应。

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

我现在正试图为此创建单元测试,但我不知道如何模拟shift键。Button类只提供了一个没有任何参数的.click()方法。

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

有没有一种方法(不创建自定义按钮类)来模拟vadin中的shift + click ?

Button.click实现为:

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

Button.fireEvent是受保护的,但是您可以使用ComponentUtil.fireEvent:

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

最新更新