Search in sources :

Example 16 with TestComponent

use of com.vaadin.flow.component.ComponentTest.TestComponent in project flow by vaadin.

the class EventRpcHandlerTest method testElementEventNoData.

@Test
public void testElementEventNoData() throws Exception {
    TestComponent c = new TestComponent();
    Element element = c.getElement();
    UI ui = new UI();
    ui.add(c);
    AtomicInteger invocations = new AtomicInteger(0);
    element.addEventListener("test-event", e -> invocations.incrementAndGet());
    sendElementEvent(element, ui, "test-event", null);
    Assert.assertEquals(1, invocations.get());
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) UI(com.vaadin.flow.component.UI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(com.vaadin.flow.dom.Element) Test(org.junit.Test) TemplateElementStateProviderTest(com.vaadin.flow.dom.TemplateElementStateProviderTest)

Example 17 with TestComponent

use of com.vaadin.flow.component.ComponentTest.TestComponent in project flow by vaadin.

the class MapSyncRpcHandlerTest method testSynchronizeProperty.

@Test
public void testSynchronizeProperty() throws Exception {
    TestComponent c = new TestComponent();
    Element element = c.getElement();
    ElementPropertyMap.getModel(element.getNode()).setUpdateFromClientFilter(name -> true);
    UI ui = new UI();
    ui.add(c);
    Assert.assertFalse(element.hasProperty(TEST_PROPERTY));
    sendSynchronizePropertyEvent(element, ui, TEST_PROPERTY, "value1");
    Assert.assertEquals("value1", element.getPropertyRaw(TEST_PROPERTY));
    sendSynchronizePropertyEvent(element, ui, TEST_PROPERTY, "value2");
    Assert.assertEquals("value2", element.getPropertyRaw(TEST_PROPERTY));
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) Test(org.junit.Test)

Example 18 with TestComponent

use of com.vaadin.flow.component.ComponentTest.TestComponent in project flow by vaadin.

the class MapSyncRpcHandlerTest method syncJSON_jsonIsPropertyValueOfStateNode_propertySetToNode.

@Test
public void syncJSON_jsonIsPropertyValueOfStateNode_propertySetToNode() throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);
    StateNode node = element.getNode();
    // Set model value directly via ElementPropertyMap
    ElementPropertyMap propertyMap = node.getFeature(ElementPropertyMap.class);
    propertyMap.setUpdateFromClientFilter(name -> true);
    ElementPropertyMap modelMap = propertyMap.resolveModelMap("foo");
    // fake StateNode has been created for the model
    StateNode model = modelMap.getNode();
    modelMap.setProperty("bar", "baz");
    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", model.getId());
    // send sync request
    sendSynchronizePropertyEvent(element, ui, "foo", json);
    Serializable testPropertyValue = propertyMap.getProperty("foo");
    Assert.assertTrue(testPropertyValue instanceof StateNode);
    StateNode newNode = (StateNode) testPropertyValue;
    Assert.assertSame(model, newNode);
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Serializable(java.io.Serializable) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 19 with TestComponent

use of com.vaadin.flow.component.ComponentTest.TestComponent in project flow by vaadin.

the class ComponentEventBusTest method nonDomEvent_removeListener.

@Test
public void nonDomEvent_removeListener() {
    TestComponent component = new TestComponent();
    EventTracker<ServerEvent> eventTracker = new EventTracker<>();
    Registration remover = component.addListener(ServerEvent.class, eventTracker);
    remover.remove();
    component.fireEvent(new ServerEvent(component, new BigDecimal("12.2")));
    eventTracker.assertEventNotCalled();
    assertNoListeners(component.getEventBus());
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Registration(com.vaadin.flow.shared.Registration) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 20 with TestComponent

use of com.vaadin.flow.component.ComponentTest.TestComponent in project flow by vaadin.

the class ComponentEventBusTest method multipleListenersForSameEvent_fireEvent.

@Test
public void multipleListenersForSameEvent_fireEvent() {
    TestComponent component = new TestComponent();
    EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>();
    EventTracker<MappedToDomEvent> eventTracker2 = new EventTracker<>();
    component.addListener(MappedToDomEvent.class, eventTracker);
    component.addListener(MappedToDomEvent.class, eventTracker2);
    JsonObject eventData = createData("event.someData", 42, "event.moreData", 19);
    fireDomEvent(component, "dom-event", eventData);
    eventTracker.assertEventCalled(component, true);
    eventTracker2.assertEventCalled(component, true);
    Assert.assertEquals("19", eventTracker.getEvent().getMoreData());
    Assert.assertEquals("19", eventTracker2.getEvent().getMoreData());
    Assert.assertEquals(42, eventTracker.getEvent().getSomeData());
    Assert.assertEquals(42, eventTracker2.getEvent().getSomeData());
    Assert.assertEquals(component, eventTracker.getEvent().getSource());
    Assert.assertEquals(component, eventTracker2.getEvent().getSource());
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Aggregations

TestComponent (com.vaadin.flow.component.ComponentTest.TestComponent)27 Test (org.junit.Test)26 JsonObject (elemental.json.JsonObject)11 UI (com.vaadin.flow.component.UI)6 Element (com.vaadin.flow.dom.Element)6 Registration (com.vaadin.flow.shared.Registration)4 StateNode (com.vaadin.flow.internal.StateNode)3 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)3 Serializable (java.io.Serializable)3 BigDecimal (java.math.BigDecimal)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 TemplateElementStateProviderTest (com.vaadin.flow.dom.TemplateElementStateProviderTest)2 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)1 JsonValue (elemental.json.JsonValue)1 Before (org.junit.Before)1