use of com.vaadin.flow.component.Component in project flow by vaadin.
the class UidlWriter method encodeChanges.
/**
* Encodes the state tree changes of the given UI. The executions registered
* at
* {@link StateTree#beforeClientResponse(com.vaadin.flow.internal.StateNode, com.vaadin.flow.function.SerializableConsumer)}
* at evaluated before the changes are encoded.
*
* @param ui
* the UI
* @param stateChanges
* a JSON array to put state changes into
* @param templates
* a JSON object to put new template nodes into
* @see StateTree#runExecutionsBeforeClientResponse()
*/
private void encodeChanges(UI ui, JsonArray stateChanges, JsonObject templates) {
UIInternals uiInternals = ui.getInternals();
StateTree stateTree = uiInternals.getStateTree();
stateTree.runExecutionsBeforeClientResponse();
Consumer<TemplateNode> templateEncoder = new Consumer<TemplateNode>() {
@Override
public void accept(TemplateNode templateNode) {
// Send to client if it's a new template
if (!uiInternals.isTemplateSent(templateNode)) {
uiInternals.setTemplateSent(templateNode);
templates.put(Integer.toString(templateNode.getId()), templateNode.toJson(this));
}
}
};
Set<Class<? extends Component>> componentsWithDependencies = new LinkedHashSet<>();
stateTree.collectChanges(change -> {
// Ensure new templates are sent to the client
runIfNewTemplateChange(change, templateEncoder);
if (attachesComponent(change)) {
change.getNode().getFeature(ComponentMapping.class).getComponent().ifPresent(component -> addComponentHierarchy(ui, componentsWithDependencies, component));
}
// Encode the actual change
stateChanges.set(stateChanges.length(), change.toJson(uiInternals.getConstantPool()));
});
componentsWithDependencies.forEach(uiInternals::addComponentDependencies);
}
use of com.vaadin.flow.component.Component in project flow by vaadin.
the class ElementUtilTest method attachTwoComponents.
@Test(expected = IllegalStateException.class)
public void attachTwoComponents() {
Element e = ElementFactory.createDiv();
Component c = Mockito.mock(Component.class);
Component c2 = Mockito.mock(Component.class);
ElementUtil.setComponent(e, c);
ElementUtil.setComponent(e, c2);
}
use of com.vaadin.flow.component.Component in project flow by vaadin.
the class ElementUtilTest method attachComponentToTextElement.
@Test
public void attachComponentToTextElement() {
Element e = Element.createText("Text text");
Component c = Mockito.mock(Component.class);
ElementUtil.setComponent(e, c);
Assert.assertEquals(c, e.getComponent().get());
}
use of com.vaadin.flow.component.Component in project flow by vaadin.
the class RouterTest method assertExceptionComponent.
private void assertExceptionComponent(Class<?> errorClass, String... exceptionTexts) {
Optional<Component> visibleComponent = ui.getElement().getChild(0).getComponent();
Assert.assertTrue("No navigation component visible", visibleComponent.isPresent());
Component routeNotFoundError = visibleComponent.get();
Assert.assertEquals(errorClass, routeNotFoundError.getClass());
String errorText = getErrorText(routeNotFoundError);
for (String exceptionText : exceptionTexts) {
Assert.assertTrue("Expected the error text to contain '" + exceptionText + "'", errorText.contains(exceptionText));
}
}
use of com.vaadin.flow.component.Component in project flow by vaadin.
the class NavigationStateRendererTest method instantiatorUse.
@Test
public void instantiatorUse() throws ServiceException {
MockVaadinServletService service = new MockVaadinServletService();
service.init(new MockInstantiator() {
@Override
public <T extends HasElement> T createRouteTarget(Class<T> routeTargetType, NavigationEvent event) {
Assert.assertEquals(Component.class, routeTargetType);
return (T) new Text("foo");
}
});
MockUI ui = new MockUI(new MockVaadinSession(service));
NavigationEvent event = new NavigationEvent(new Router(new TestRouteRegistry()), new Location(""), ui, NavigationTrigger.PAGE_LOAD);
NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(ChildConfiguration.class));
Component routeTarget = renderer.getRouteTarget(Component.class, event);
Assert.assertEquals(Text.class, routeTarget.getClass());
UI.setCurrent(null);
}
Aggregations