Search in sources :

Example 1 with ElementListenerMap

use of com.vaadin.flow.internal.nodefeature.ElementListenerMap in project flow by vaadin.

the class ComponentTest method declarativeSyncProperties_propertiesAreRegisteredWithProperDisabledUpdateMode.

@Test
public void declarativeSyncProperties_propertiesAreRegisteredWithProperDisabledUpdateMode() {
    TestDiv div = new TestDiv();
    ElementListenerMap feature = div.getElement().getNode().getFeature(ElementListenerMap.class);
    Set<String> props = feature.getExpressions("bar");
    Assert.assertEquals(1, props.size());
    Assert.assertTrue(props.contains("}baz"));
    Assert.assertEquals(DisabledUpdateMode.ALWAYS, feature.getPropertySynchronizationMode("baz"));
    props = feature.getExpressions("foo");
    Assert.assertEquals(1, props.size());
    Assert.assertTrue(props.contains("}bar"));
    Assert.assertEquals(DisabledUpdateMode.ONLY_WHEN_ENABLED, feature.getPropertySynchronizationMode("bar"));
}
Also used : ElementListenerMap(com.vaadin.flow.internal.nodefeature.ElementListenerMap) Test(org.junit.Test)

Example 2 with ElementListenerMap

use of com.vaadin.flow.internal.nodefeature.ElementListenerMap in project flow by vaadin.

the class ElementTest method domPropertyListener_unregisterCleansEverything.

@Test
public void domPropertyListener_unregisterCleansEverything() {
    Element element = ElementFactory.createDiv();
    DomListenerRegistration registration = element.addPropertyChangeListener("property", "event", event -> {
        Assert.fail("Unexpected event");
    });
    registration.remove();
    Assert.assertNull("The property should not be synchronized", element.getNode().getFeature(ElementListenerMap.class).getPropertySynchronizationMode("property"));
    ElementListenerMap listenerMap = element.getNode().getFeature(ElementListenerMap.class);
    Assert.assertEquals("There should be no DOM listener", Collections.emptySet(), ElementListenersTest.getExpressions(listenerMap, "event"));
    // Should not trigger assert in the listener
    element.setProperty("property", "value");
}
Also used : ElementListenerMap(com.vaadin.flow.internal.nodefeature.ElementListenerMap) Test(org.junit.Test) ElementListenersTest(com.vaadin.flow.internal.nodefeature.ElementListenersTest)

Example 3 with ElementListenerMap

use of com.vaadin.flow.internal.nodefeature.ElementListenerMap in project flow by vaadin.

the class ElementTest method domPropertyListener_registersListenerAndDomTrigger.

@Test
public void domPropertyListener_registersListenerAndDomTrigger() {
    Element element = ElementFactory.createDiv();
    AtomicReference<Serializable> listenerValue = new AtomicReference<>();
    element.addPropertyChangeListener("property", "event", event -> {
        if (listenerValue.getAndSet(event.getValue()) != null) {
            Assert.fail("Unexpected event");
        }
    });
    Assert.assertEquals("The property should be synchronized", DisabledUpdateMode.ONLY_WHEN_ENABLED, element.getNode().getFeature(ElementListenerMap.class).getPropertySynchronizationMode("property"));
    ElementListenerMap listenerMap = element.getNode().getFeature(ElementListenerMap.class);
    Assert.assertEquals("A DOM event synchronization should be defined", Collections.singleton(JsonConstants.SYNCHRONIZE_PROPERTY_TOKEN + "property"), ElementListenersTest.getExpressions(listenerMap, "event"));
    element.setProperty("property", "value");
    Assert.assertEquals("Listener shold be registered", listenerValue.get(), "value");
}
Also used : Serializable(java.io.Serializable) ElementListenerMap(com.vaadin.flow.internal.nodefeature.ElementListenerMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test) ElementListenersTest(com.vaadin.flow.internal.nodefeature.ElementListenersTest)

Example 4 with ElementListenerMap

use of com.vaadin.flow.internal.nodefeature.ElementListenerMap in project flow by vaadin.

the class ShortcutRegistrationTest method hasKeyAInKeyDownExpression.

private boolean hasKeyAInKeyDownExpression(Component component) {
    ElementListenerMap map = component.getElement().getNode().getFeature(ElementListenerMap.class);
    // Once the shortcut listener is registered the expression should
    // contain KeyA
    boolean hasKeyA = false;
    for (String expression : map.getExpressions("keydown")) {
        if (expression.contains(Key.KEY_A.getKeys().get(0))) {
            hasKeyA = true;
        }
    }
    return hasKeyA;
}
Also used : ElementListenerMap(com.vaadin.flow.internal.nodefeature.ElementListenerMap)

Example 5 with ElementListenerMap

use of com.vaadin.flow.internal.nodefeature.ElementListenerMap in project flow by vaadin.

the class DomEventTest method getEventSettings.

private <T extends ComponentEvent<Component>> JsonObject getEventSettings(Class<T> eventType) {
    Component component = new Component(new Element("element")) {
    };
    component.addListener(eventType, e -> {
    });
    ElementListenerMap elementListenerMap = component.getElement().getNode().getFeature(ElementListenerMap.class);
    List<NodeChange> changes = new ArrayList<>();
    elementListenerMap.collectChanges(changes::add);
    Assert.assertEquals(1, changes.size());
    MapPutChange change = (MapPutChange) changes.get(0);
    Assert.assertEquals("event", change.getKey());
    ConstantPoolKey value = (ConstantPoolKey) change.getValue();
    JsonObject constantPoolUpdate = Json.createObject();
    value.export(constantPoolUpdate);
    String[] keys = constantPoolUpdate.keys();
    Assert.assertEquals(1, keys.length);
    JsonObject eventSettings = constantPoolUpdate.getObject(keys[0]);
    return eventSettings;
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) NodeChange(com.vaadin.flow.internal.change.NodeChange) ElementListenerMap(com.vaadin.flow.internal.nodefeature.ElementListenerMap) Element(com.vaadin.flow.dom.Element) ArrayList(java.util.ArrayList) JsonObject(elemental.json.JsonObject) ConstantPoolKey(com.vaadin.flow.internal.ConstantPoolKey)

Aggregations

ElementListenerMap (com.vaadin.flow.internal.nodefeature.ElementListenerMap)5 Test (org.junit.Test)3 ElementListenersTest (com.vaadin.flow.internal.nodefeature.ElementListenersTest)2 Element (com.vaadin.flow.dom.Element)1 ConstantPoolKey (com.vaadin.flow.internal.ConstantPoolKey)1 MapPutChange (com.vaadin.flow.internal.change.MapPutChange)1 NodeChange (com.vaadin.flow.internal.change.NodeChange)1 JsonObject (elemental.json.JsonObject)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1