use of com.vaadin.flow.function.SerializableConsumer in project flow by vaadin.
the class ShortcutRegistrationTest method clientResponse.
/**
* Works only with the {@code registration} member variable, but allows
* configuring the {@code listenOn} component
*
* Simulates a "beforeClientResponse" callback for the given
* {@link ShortcutRegistration}
*/
private void clientResponse(Component[] listenOnMock) {
for (Component component : listenOnMock) {
when(component.getElement()).thenReturn(new Element("tag"));
when(component.getEventBus()).thenReturn(new ComponentEventBus(component));
}
ArgumentCaptor<SerializableConsumer> captor = ArgumentCaptor.forClass(SerializableConsumer.class);
verify(ui, atLeastOnce()).beforeClientResponse(eq(lifecycleOwner), captor.capture());
SerializableConsumer consumer = captor.getValue();
// Fake beforeClientExecution call.
consumer.accept(mock(ExecutionContext.class));
}
use of com.vaadin.flow.function.SerializableConsumer in project flow by vaadin.
the class ShortcutRegistrationTest method reattachComponent_detachListenerIsAddedOnEveryAttach_listenOnUIIsClosing_eventIsPopulatedForANewUI.
@Test
public void reattachComponent_detachListenerIsAddedOnEveryAttach_listenOnUIIsClosing_eventIsPopulatedForANewUI() {
UI ui = Mockito.spy(UI.class);
Component owner = new FakeComponent();
Registration registration = Mockito.mock(Registration.class);
AtomicInteger count = new AtomicInteger();
Mockito.when(ui.addDetachListener(any())).thenAnswer(invocation -> {
count.incrementAndGet();
return registration;
});
Component[] components = new Component[] { ui };
ui.add(owner);
new ShortcutRegistration(owner, () -> components, event -> {
}, Key.KEY_A);
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));
Assert.assertEquals(1, count.get());
ui.remove(owner);
// reattach
ui.add(owner);
// Fake beforeClientExecution call.
consumer.accept(mock(ExecutionContext.class));
Assert.assertEquals(2, count.get());
UI newUI = Mockito.spy(UI.class);
// close the previous UI
ui.close();
components[0] = newUI;
owner.getElement().removeFromTree();
newUI.add(owner);
verify(newUI, atLeastOnce()).beforeClientResponse(eq(owner), captor.capture());
// Fake beforeClientExecution call.
captor.getValue().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 retrieveExtendedClientDetails_twice_jsOnceAndCallbackTwice.
@Test
public void retrieveExtendedClientDetails_twice_jsOnceAndCallbackTwice() {
// given
final UI mockUI = new MockUI();
final Page page = new Page(mockUI) {
@Override
public PendingJavaScriptResult executeJs(String expression, Serializable... params) {
super.executeJs(expression, params);
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) {
final HashMap<String, String> params = new HashMap<>();
params.put("v-sw", "2560");
params.put("v-sh", "1450");
params.put("v-tzo", "-270");
params.put("v-rtzo", "-210");
params.put("v-dstd", "60");
params.put("v-dston", "true");
params.put("v-tzid", "Asia/Tehran");
params.put("v-curdate", "1555000000000");
params.put("v-td", "false");
params.put("v-wn", "ROOT-1234567-0.1234567");
resultHandler.accept(JsonUtils.createObject(params, Json::create));
}
};
}
};
final AtomicInteger callbackInvocations = new AtomicInteger();
final Page.ExtendedClientDetailsReceiver receiver = details -> {
callbackInvocations.incrementAndGet();
};
// when
page.retrieveExtendedClientDetails(receiver);
page.retrieveExtendedClientDetails(receiver);
// then
final int jsInvocations = mockUI.getInternals().dumpPendingJavaScriptInvocations().size();
Assert.assertEquals(1, jsInvocations);
Assert.assertEquals(2, callbackInvocations.get());
}
use of com.vaadin.flow.function.SerializableConsumer in project flow by vaadin.
the class PageTest method retrieveExtendedClientDetails_twice_theSecondResultComesDifferentBeforeCachedValueIsSet.
@Test
public void retrieveExtendedClientDetails_twice_theSecondResultComesDifferentBeforeCachedValueIsSet() {
// given
final UI mockUI = new MockUI();
List<Runnable> invocations = new ArrayList<>();
final Page page = new Page(mockUI) {
@Override
public PendingJavaScriptResult executeJs(String expression, Serializable... params) {
super.executeJs(expression, params);
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) {
final HashMap<String, String> params = new HashMap<>();
params.put("v-sw", "2560");
params.put("v-sh", "1450");
params.put("v-tzo", "-270");
params.put("v-rtzo", "-210");
params.put("v-dstd", "60");
params.put("v-dston", "true");
params.put("v-tzid", "Asia/Tehran");
params.put("v-curdate", "1555000000000");
params.put("v-td", "false");
if (invocations.isEmpty()) {
params.put("v-wn", "ROOT-1234567-0.1234567");
} else {
params.put("v-wn", "foo");
}
invocations.add(() -> resultHandler.accept(JsonUtils.createObject(params, Json::create)));
}
};
}
};
final AtomicInteger callbackInvocations = new AtomicInteger();
final Page.ExtendedClientDetailsReceiver receiver = details -> {
callbackInvocations.incrementAndGet();
};
// when
page.retrieveExtendedClientDetails(receiver);
page.retrieveExtendedClientDetails(receiver);
// then : before cached value is set the second retrieve is requested
invocations.forEach(Runnable::run);
Assert.assertEquals(2, callbackInvocations.get());
Assert.assertEquals("ROOT-1234567-0.1234567", mockUI.getInternals().getExtendedClientDetails().getWindowName());
}
use of com.vaadin.flow.function.SerializableConsumer in project flow by vaadin.
the class ShortcutRegistrationTest method listenOnComponentIsChanged_eventIsPopulatedForANewListenOnComponent.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void listenOnComponentIsChanged_eventIsPopulatedForANewListenOnComponent() {
UI ui = Mockito.spy(UI.class);
Component owner = new FakeComponent();
Component initialComponentToListenOn = new FakeComponent();
Component[] components = new Component[] { initialComponentToListenOn };
ui.add(owner);
ui.add(initialComponentToListenOn);
new ShortcutRegistration(owner, () -> components, event -> {
}, Key.KEY_A);
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));
// Once the shortcut listener is registered the expression should
// contain KeyA
Assert.assertTrue(hasKeyAInKeyDownExpression(initialComponentToListenOn));
Component replacementComponentToListenOn = new FakeComponent();
components[0] = replacementComponentToListenOn;
ui.add(components[0]);
// detach the original "listen on" component: the new one replaces the
// old one
ui.remove(initialComponentToListenOn);
// now re-attach the owner
ui.remove(owner);
ui.add(owner);
consumer.accept(mock(ExecutionContext.class));
// the new component should now also have expression with KeyA
Assert.assertTrue(hasKeyAInKeyDownExpression(replacementComponentToListenOn));
}
Aggregations