Search in sources :

Example 1 with ElementTemplateBuilder

use of com.vaadin.flow.template.angular.ElementTemplateBuilder in project flow by vaadin.

the class TemplateBuilderTest method testBasicTemplate.

@Test
public void testBasicTemplate() {
    // <div baz="lorem" foo="bar">baz</div> where baz is an attribute and
    // foo a property
    ElementTemplateBuilder builder = new ElementTemplateBuilder("div").setProperty("foo", new StaticBindingValueProvider("bar")).setAttribute("baz", new StaticBindingValueProvider("lorem")).setClassName("a-name", new StaticBindingValueProvider("a-value")).addChild(new TextTemplateBuilder(new StaticBindingValueProvider("baz")));
    List<TemplateNode> nodes = builder.build(null);
    Assert.assertFalse(nodes.isEmpty());
    ElementTemplateNode node = (ElementTemplateNode) nodes.get(0);
    Assert.assertFalse(node.getParent().isPresent());
    Assert.assertEquals("div", node.getTag());
    Assert.assertArrayEquals(new String[] { "foo" }, node.getPropertyNames().toArray());
    Assert.assertEquals("bar", node.getPropertyBinding("foo").get().getValue(null));
    Assert.assertArrayEquals(new String[] { "baz" }, node.getAttributeNames().toArray());
    Assert.assertEquals("lorem", node.getAttributeBinding("baz").get().getValue(null));
    Assert.assertArrayEquals(new String[] { "a-name" }, node.getClassNames().toArray());
    Assert.assertEquals("a-value", node.getClassNameBinding("a-name").get().getValue(null));
    Assert.assertEquals(1, node.getChildCount());
    TextTemplateNode child = (TextTemplateNode) node.getChild(0);
    Assert.assertSame(node, child.getParent().get());
    Assert.assertEquals("baz", child.getTextBinding().getValue(null));
    Assert.assertEquals(0, child.getChildCount());
}
Also used : TextTemplateNode(com.vaadin.flow.template.angular.TextTemplateNode) ElementTemplateNode(com.vaadin.flow.template.angular.ElementTemplateNode) TemplateNode(com.vaadin.flow.template.angular.TemplateNode) TextTemplateBuilder(com.vaadin.flow.template.angular.TextTemplateBuilder) TextTemplateNode(com.vaadin.flow.template.angular.TextTemplateNode) ElementTemplateBuilder(com.vaadin.flow.template.angular.ElementTemplateBuilder) ElementTemplateNode(com.vaadin.flow.template.angular.ElementTemplateNode) StaticBindingValueProvider(com.vaadin.flow.template.angular.StaticBindingValueProvider) Test(org.junit.Test)

Example 2 with ElementTemplateBuilder

use of com.vaadin.flow.template.angular.ElementTemplateBuilder in project flow by vaadin.

the class TemplateElementStateProviderTest method testNestedTemplateElements.

@Test
public void testNestedTemplateElements() {
    ElementTemplateBuilder builder = new ElementTemplateBuilder("parent").addChild(new ElementTemplateBuilder("child0")).addChild(new ElementTemplateBuilder("child1"));
    Element element = createElement(builder);
    Assert.assertEquals(2, element.getChildCount());
    Element child0 = element.getChild(0);
    Assert.assertEquals("child0", child0.getTag());
    Assert.assertEquals(element, child0.getParent());
    Element child1 = element.getChild(1);
    Assert.assertEquals("child1", child1.getTag());
    Assert.assertEquals(element, child1.getParent());
}
Also used : ElementTemplateBuilder(com.vaadin.flow.template.angular.ElementTemplateBuilder) Element(com.vaadin.flow.dom.Element) Test(org.junit.Test)

Example 3 with ElementTemplateBuilder

use of com.vaadin.flow.template.angular.ElementTemplateBuilder in project flow by vaadin.

the class TemplateElementStateProviderTest method testElementJsonProperties.

@Test
public void testElementJsonProperties() {
    ElementTemplateBuilder builder = new ElementTemplateBuilder("div").setProperty("a", new ModelValueBindingProvider("key"));
    Element element = createElement(builder);
    StateNode stateNode = element.getNode();
    JsonObject json = Json.createObject();
    json.put("foo", "bar");
    ModelMap.get(stateNode).setValue("key", json);
    Assert.assertEquals(json, element.getPropertyRaw("a"));
    Assert.assertEquals(new HashSet<>(Arrays.asList("a")), element.getPropertyNames().collect(Collectors.toSet()));
}
Also used : ElementTemplateBuilder(com.vaadin.flow.template.angular.ElementTemplateBuilder) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) ModelValueBindingProvider(com.vaadin.flow.template.angular.ModelValueBindingProvider) Test(org.junit.Test)

Example 4 with ElementTemplateBuilder

use of com.vaadin.flow.template.angular.ElementTemplateBuilder in project flow by vaadin.

the class TemplateElementStateProviderTest method testElementDoubleProperties.

@Test
public void testElementDoubleProperties() {
    ElementTemplateBuilder builder = new ElementTemplateBuilder("div").setProperty("a", new ModelValueBindingProvider("key"));
    Element element = createElement(builder);
    StateNode stateNode = element.getNode();
    ModelMap.get(stateNode).setValue("key", 1.1d);
    Assert.assertEquals(1.1d, element.getPropertyRaw("a"));
    Assert.assertEquals(new HashSet<>(Arrays.asList("a")), element.getPropertyNames().collect(Collectors.toSet()));
}
Also used : ElementTemplateBuilder(com.vaadin.flow.template.angular.ElementTemplateBuilder) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) ModelValueBindingProvider(com.vaadin.flow.template.angular.ModelValueBindingProvider) Test(org.junit.Test)

Example 5 with ElementTemplateBuilder

use of com.vaadin.flow.template.angular.ElementTemplateBuilder in project flow by vaadin.

the class TemplateElementStateProviderTest method testElementBooleanProperties.

@Test
public void testElementBooleanProperties() {
    ElementTemplateBuilder builder = new ElementTemplateBuilder("div").setProperty("a", new ModelValueBindingProvider("key"));
    Element element = createElement(builder);
    StateNode stateNode = element.getNode();
    ModelMap.get(stateNode).setValue("key", Boolean.TRUE);
    Assert.assertEquals(Boolean.TRUE, element.getPropertyRaw("a"));
    Assert.assertEquals(new HashSet<>(Arrays.asList("a")), element.getPropertyNames().collect(Collectors.toSet()));
}
Also used : ElementTemplateBuilder(com.vaadin.flow.template.angular.ElementTemplateBuilder) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) ModelValueBindingProvider(com.vaadin.flow.template.angular.ModelValueBindingProvider) Test(org.junit.Test)

Aggregations

ElementTemplateBuilder (com.vaadin.flow.template.angular.ElementTemplateBuilder)12 Test (org.junit.Test)11 Element (com.vaadin.flow.dom.Element)10 StateNode (com.vaadin.flow.internal.StateNode)4 ModelValueBindingProvider (com.vaadin.flow.template.angular.ModelValueBindingProvider)4 StaticBindingValueProvider (com.vaadin.flow.template.angular.StaticBindingValueProvider)4 TextTemplateBuilder (com.vaadin.flow.template.angular.TextTemplateBuilder)2 ElementTemplateNode (com.vaadin.flow.template.angular.ElementTemplateNode)1 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)1 TextTemplateNode (com.vaadin.flow.template.angular.TextTemplateNode)1 JsonObject (elemental.json.JsonObject)1 Optional (java.util.Optional)1