Search in sources :

Example 61 with Component

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);
}
Also used : TemplateNode(com.vaadin.flow.template.angular.TemplateNode) LinkedHashSet(java.util.LinkedHashSet) StateTree(com.vaadin.flow.internal.StateTree) Consumer(java.util.function.Consumer) UIInternals(com.vaadin.flow.component.internal.UIInternals) Component(com.vaadin.flow.component.Component)

Example 62 with Component

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);
}
Also used : Element(com.vaadin.flow.dom.Element) Component(com.vaadin.flow.component.Component) Test(org.junit.Test)

Example 63 with Component

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());
}
Also used : Element(com.vaadin.flow.dom.Element) Component(com.vaadin.flow.component.Component) Test(org.junit.Test)

Example 64 with Component

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));
    }
}
Also used : Component(com.vaadin.flow.component.Component)

Example 65 with Component

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);
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) Router(com.vaadin.flow.router.Router) Text(com.vaadin.flow.component.Text) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) MockInstantiator(com.vaadin.flow.server.MockInstantiator) Component(com.vaadin.flow.component.Component) Location(com.vaadin.flow.router.Location) NavigationStateRenderer(com.vaadin.flow.router.internal.NavigationStateRenderer) Test(org.junit.Test)

Aggregations

Component (com.vaadin.flow.component.Component)66 Test (org.junit.Test)9 Element (com.vaadin.flow.dom.Element)7 UI (com.vaadin.flow.component.UI)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 HasElement (com.vaadin.flow.component.HasElement)2 NativeButton (com.vaadin.flow.component.html.NativeButton)2 VirtualChildrenList (com.vaadin.flow.internal.nodefeature.VirtualChildrenList)2 AfterNavigationEvent (com.vaadin.flow.router.AfterNavigationEvent)2 BeforeEnterEvent (com.vaadin.flow.router.BeforeEnterEvent)2 BeforeLeaveEvent (com.vaadin.flow.router.BeforeLeaveEvent)2 ContinueNavigationAction (com.vaadin.flow.router.BeforeLeaveEvent.ContinueNavigationAction)2 Location (com.vaadin.flow.router.Location)2 LocationChangeEvent (com.vaadin.flow.router.LocationChangeEvent)2 NavigationEvent (com.vaadin.flow.router.NavigationEvent)2 RouterLayout (com.vaadin.flow.router.RouterLayout)2 ErrorStateRenderer (com.vaadin.flow.router.internal.ErrorStateRenderer)2 ViewClassLocator (com.vaadin.flow.uitest.servlet.ViewClassLocator)2 Optional (java.util.Optional)2