Search in sources :

Example 1 with WebComponentBinding

use of com.vaadin.flow.server.webcomponent.WebComponentBinding in project flow by vaadin.

the class WebComponentUI method createWebComponentWrapper.

private Element createWebComponentWrapper(WebComponentConfiguration<? extends Component> configuration, WebComponentConnectEvent event) {
    Element rootElement = new Element(event.getTag());
    WebComponentBinding binding = configuration.createWebComponentBinding(Instantiator.get(this), rootElement, event.getAttributeJson());
    WebComponentWrapper wrapper = new WebComponentWrapper(rootElement, binding, getConfigurationRegistry().getShadowDomElements());
    return wrapper.getElement();
}
Also used : WebComponentBinding(com.vaadin.flow.server.webcomponent.WebComponentBinding) Element(com.vaadin.flow.dom.Element)

Example 2 with WebComponentBinding

use of com.vaadin.flow.server.webcomponent.WebComponentBinding in project flow by vaadin.

the class WebComponentTest method setProperty_throwsWhenGivenWrongPropertyTypeAsParameter.

@Test
public void setProperty_throwsWhenGivenWrongPropertyTypeAsParameter() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("Property 'property' of type " + "'java.lang.Integer' cannot be assigned value of type " + "'java.lang.String'!");
    PropertyConfigurationImpl<Component, Integer> intConfiguration = new PropertyConfigurationImpl<>(Component.class, "property", Integer.class, 0);
    WebComponentBinding<Component> binding = new WebComponentBinding<>(mock(Component.class));
    binding.bindProperty(intConfiguration, false, null);
    WebComponent<Component> webComponent = new WebComponent<>(binding, new Element("tag"));
    PropertyConfigurationImpl<Component, String> stringConfiguration = new PropertyConfigurationImpl<>(Component.class, "property", String.class, "value");
    webComponent.setProperty(stringConfiguration, "newValue");
}
Also used : WebComponentBinding(com.vaadin.flow.server.webcomponent.WebComponentBinding) Element(com.vaadin.flow.dom.Element) PropertyConfigurationImpl(com.vaadin.flow.server.webcomponent.PropertyConfigurationImpl) Component(com.vaadin.flow.component.Component) Test(org.junit.Test)

Example 3 with WebComponentBinding

use of com.vaadin.flow.server.webcomponent.WebComponentBinding in project flow by vaadin.

the class WebComponentTest method setProperty_throwsOnUnknownProperty.

@Test
public void setProperty_throwsOnUnknownProperty() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("WebComponent does not have a property identified");
    WebComponentBinding<Component> binding = new WebComponentBinding<>(mock(Component.class));
    WebComponent<Component> webComponent = new WebComponent<>(binding, new Element("tag"));
    PropertyConfigurationImpl<Component, String> configuration = new PropertyConfigurationImpl<>(Component.class, "property", String.class, "value");
    webComponent.setProperty(configuration, "newValue");
}
Also used : WebComponentBinding(com.vaadin.flow.server.webcomponent.WebComponentBinding) Element(com.vaadin.flow.dom.Element) PropertyConfigurationImpl(com.vaadin.flow.server.webcomponent.PropertyConfigurationImpl) Component(com.vaadin.flow.component.Component) Test(org.junit.Test)

Example 4 with WebComponentBinding

use of com.vaadin.flow.server.webcomponent.WebComponentBinding in project flow by vaadin.

the class WebComponentWrapperTest method constructWrapperAndGetBinding.

/**
 * @param exporter
 *            exporter of the correct type, defines C
 * @param element
 *            nullable root element
 * @param ui
 *            nullable WebComponentUI
 * @param <C>
 *            type of the exported component
 * @return web component wrapper
 */
private <C extends Component> WebComponentBinding<C> constructWrapperAndGetBinding(WebComponentExporter<C> exporter, Element element, WebComponentUI ui) {
    if (element == null) {
        element = new Element("tag");
    }
    WebComponentBinding<C> binding = (WebComponentBinding<C>) new WebComponentExporter.WebComponentConfigurationFactory().create(exporter).createWebComponentBinding(new MockInstantiator(), element, Json.createObject());
    wrapper = new WebComponentWrapper(element, binding) {

        @Override
        public Optional<UI> getUI() {
            return Optional.of(ui);
        }
    };
    return binding;
}
Also used : WebComponentBinding(com.vaadin.flow.server.webcomponent.WebComponentBinding) Optional(java.util.Optional) Element(com.vaadin.flow.dom.Element) MockInstantiator(com.vaadin.flow.server.MockInstantiator) WebComponentExporter(com.vaadin.flow.component.WebComponentExporter)

Example 5 with WebComponentBinding

use of com.vaadin.flow.server.webcomponent.WebComponentBinding in project flow by vaadin.

the class WebComponentTest method setProperty_attemptsToWriteSupportedTypes.

@Test
public void setProperty_attemptsToWriteSupportedTypes() {
    Element element = spy(new Element("tag"));
    // configurations
    PropertyConfigurationImpl<Component, Integer> intConfiguration = new PropertyConfigurationImpl<>(Component.class, "int", Integer.class, 0);
    PropertyConfigurationImpl<Component, Double> doubleConfiguration = new PropertyConfigurationImpl<>(Component.class, "double", Double.class, 0.0);
    PropertyConfigurationImpl<Component, String> stringConfiguration = new PropertyConfigurationImpl<>(Component.class, "string", String.class, "");
    PropertyConfigurationImpl<Component, Boolean> booleanConfiguration = new PropertyConfigurationImpl<>(Component.class, "boolean", Boolean.class, false);
    PropertyConfigurationImpl<Component, JsonValue> jsonConfiguration = new PropertyConfigurationImpl<>(Component.class, "json", JsonValue.class, Json.createNull());
    // binding
    WebComponentBinding<Component> binding = new WebComponentBinding<>(mock(Component.class));
    binding.bindProperty(intConfiguration, false, null);
    binding.bindProperty(doubleConfiguration, false, null);
    binding.bindProperty(stringConfiguration, false, null);
    binding.bindProperty(booleanConfiguration, false, null);
    binding.bindProperty(jsonConfiguration, false, null);
    // test
    WebComponent<Component> webComponent = new WebComponent<>(binding, element);
    webComponent.setProperty(intConfiguration, 1);
    verify(element, Mockito.times(1)).executeJs(ArgumentMatchers.anyString(), ArgumentMatchers.any(), ArgumentMatchers.any());
    webComponent.setProperty(doubleConfiguration, 1.0);
    verify(element, Mockito.times(2)).executeJs(ArgumentMatchers.anyString(), ArgumentMatchers.any(), ArgumentMatchers.any());
    webComponent.setProperty(stringConfiguration, "asd");
    verify(element, Mockito.times(3)).executeJs(ArgumentMatchers.anyString(), ArgumentMatchers.any(), ArgumentMatchers.any());
    webComponent.setProperty(booleanConfiguration, true);
    verify(element, Mockito.times(4)).executeJs(ArgumentMatchers.anyString(), ArgumentMatchers.any(), ArgumentMatchers.any());
    // JsonValue has a different number of arguments
    webComponent.setProperty(jsonConfiguration, Json.create(true));
    verify(element, Mockito.times(5)).executeJs(ArgumentMatchers.anyString(), ArgumentMatchers.any());
}
Also used : Element(com.vaadin.flow.dom.Element) JsonValue(elemental.json.JsonValue) PropertyConfigurationImpl(com.vaadin.flow.server.webcomponent.PropertyConfigurationImpl) WebComponentBinding(com.vaadin.flow.server.webcomponent.WebComponentBinding) Component(com.vaadin.flow.component.Component) Test(org.junit.Test)

Aggregations

Element (com.vaadin.flow.dom.Element)5 WebComponentBinding (com.vaadin.flow.server.webcomponent.WebComponentBinding)5 Component (com.vaadin.flow.component.Component)3 PropertyConfigurationImpl (com.vaadin.flow.server.webcomponent.PropertyConfigurationImpl)3 Test (org.junit.Test)3 WebComponentExporter (com.vaadin.flow.component.WebComponentExporter)1 MockInstantiator (com.vaadin.flow.server.MockInstantiator)1 JsonValue (elemental.json.JsonValue)1 Optional (java.util.Optional)1