use of com.vaadin.flow.function.SerializableConsumer in project flow by vaadin.
the class ShortcutRegistrationTest method listenOnUIIsClosing_eventIsPopulatedForANewUI.
@Test
public void listenOnUIIsClosing_eventIsPopulatedForANewUI() {
UI ui = Mockito.spy(UI.class);
Component owner = new FakeComponent();
Component[] components = new Component[] { ui };
ui.add(owner);
new ShortcutRegistration(owner, () -> components, event -> {
}, Key.KEY_A);
UI newUI = Mockito.spy(UI.class);
// close the previous UI
ui.close();
components[0] = newUI;
owner.getElement().removeFromTree();
newUI.add(owner);
ArgumentCaptor<SerializableConsumer> captor = ArgumentCaptor.forClass(SerializableConsumer.class);
verify(ui, atLeastOnce()).beforeClientResponse(eq(owner), captor.capture());
SerializableConsumer consumer = captor.getValue();
// Fake beforeClientExecution call.
consumer.accept(mock(ExecutionContext.class));
// the new UI should now also have expression with KeyA
Assert.assertTrue(hasKeyAInKeyDownExpression(newUI));
}
use of com.vaadin.flow.function.SerializableConsumer in project flow by vaadin.
the class PageTest method fetchCurrentUrl_consumerReceivesCorrectURL.
@Test
public void fetchCurrentUrl_consumerReceivesCorrectURL() {
// given
final UI mockUI = new MockUI();
final Page page = new Page(mockUI) {
@Override
public PendingJavaScriptResult executeJs(String expression, Serializable... params) {
super.executeJs(expression, params);
Assert.assertEquals("Expected javascript for fetching location is wrong.", "return window.location.href", expression);
return new PendingJavaScriptResult() {
@Override
public boolean cancelExecution() {
return false;
}
@Override
public boolean isSentToBrowser() {
return false;
}
@Override
public void then(SerializableConsumer<JsonValue> resultHandler, SerializableConsumer<String> errorHandler) {
resultHandler.accept(Json.create("http://localhost:8080/home"));
}
};
}
};
final AtomicReference<URL> callbackInvocations = new AtomicReference<>();
final SerializableConsumer<URL> receiver = details -> {
callbackInvocations.compareAndSet(null, details);
};
// when
page.fetchCurrentURL(receiver);
// then
Assert.assertEquals("Returned URL was wrong", "http://localhost:8080/home", callbackInvocations.get().toString());
}
Aggregations