Search in sources :

Example 36 with Component

use of com.vaadin.flow.component.Component in project flow by vaadin.

the class PolymerTemplateTest method assertTemplateInitialization.

private void assertTemplateInitialization(TemplateInitialization template) {
    VirtualChildrenList feature = template.getStateNode().getFeature(VirtualChildrenList.class);
    assertEquals(2, feature.size());
    Optional<Component> child = com.vaadin.flow.dom.Element.get(feature.get(0)).getComponent();
    Assert.assertTrue(child.isPresent());
    Assert.assertEquals(TestPolymerTemplate.class, child.get().getClass());
    child = com.vaadin.flow.dom.Element.get(feature.get(1)).getComponent();
    Assert.assertTrue(child.isPresent());
    Assert.assertEquals(TemplateChild.class, child.get().getClass());
}
Also used : VirtualChildrenList(com.vaadin.flow.internal.nodefeature.VirtualChildrenList) Component(com.vaadin.flow.component.Component)

Example 37 with Component

use of com.vaadin.flow.component.Component in project flow by vaadin.

the class ComponentTest method setup.

@Before
public void setup() throws IntrospectionException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    component = createComponent();
    WHITE_LIST.add("visible");
    addProperties();
    BeanInfo componentInfo = Introspector.getBeanInfo(component.getClass());
    Stream.of(componentInfo.getPropertyDescriptors()).filter(descriptor -> !WHITE_LIST.contains(descriptor.getName())).forEach(this::assertProperty);
}
Also used : Component(com.vaadin.flow.component.Component) Set(java.util.Set) Test(org.junit.Test) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Introspector(java.beans.Introspector) List(java.util.List) Stream(java.util.stream.Stream) ParameterizedType(java.lang.reflect.ParameterizedType) HtmlComponent(com.vaadin.flow.component.HtmlComponent) Type(java.lang.reflect.Type) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Element(com.vaadin.flow.dom.Element) Optional(java.util.Optional) Assert(org.junit.Assert) Before(org.junit.Before) BeanInfo(java.beans.BeanInfo) Before(org.junit.Before)

Example 38 with Component

use of com.vaadin.flow.component.Component in project flow by vaadin.

the class ElementUtilTest method attachToComponent.

@Test
public void attachToComponent() {
    Element e = ElementFactory.createDiv();
    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 39 with Component

use of com.vaadin.flow.component.Component in project flow by vaadin.

the class ElementUtilTest method attachTwiceToComponent.

@Test(expected = IllegalStateException.class)
public void attachTwiceToComponent() {
    Element e = ElementFactory.createDiv();
    Component c = Mockito.mock(Component.class);
    ElementUtil.setComponent(e, c);
    ElementUtil.setComponent(e, c);
}
Also used : Element(com.vaadin.flow.dom.Element) Component(com.vaadin.flow.component.Component) Test(org.junit.Test)

Example 40 with Component

use of com.vaadin.flow.component.Component in project flow by vaadin.

the class TemplateInitializer method injectTemplateElement.

@SuppressWarnings("unchecked")
private void injectTemplateElement(Element element, Field field) {
    Class<?> fieldType = field.getType();
    if (Component.class.isAssignableFrom(fieldType)) {
        CustomElementRegistry.getInstance().wrapElementIfNeeded(element);
        Component component;
        Optional<Component> wrappedComponent = element.getComponent();
        if (wrappedComponent.isPresent()) {
            component = wrappedComponent.get();
        } else {
            Class<? extends Component> componentType = (Class<? extends Component>) fieldType;
            component = Component.from(element, componentType);
        }
        ReflectTools.setJavaFieldValue(template, field, component);
    } else if (Element.class.isAssignableFrom(fieldType)) {
        ReflectTools.setJavaFieldValue(template, field, element);
    } else {
        String msg = String.format("The field '%s' in '%s' has an @'%s' " + "annotation but the field type '%s' " + "does not extend neither '%s' nor '%s'", field.getName(), templateClass.getName(), Id.class.getSimpleName(), fieldType.getName(), Component.class.getSimpleName(), Element.class.getSimpleName());
        throw new IllegalArgumentException(msg);
    }
}
Also used : Element(com.vaadin.flow.dom.Element) Component(com.vaadin.flow.component.Component)

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