Search in sources :

Example 11 with TestComponent

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

the class ComponentEventBusTest method serverNoDataEvent_fire_noListeners.

@Test
public void serverNoDataEvent_fire_noListeners() {
    TestComponent c = new TestComponent();
    c.fireEvent(new ServerNoDataEvent(c, false));
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Test(org.junit.Test)

Example 12 with TestComponent

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

the class ComponentEventBusTest method multipleListenersForSameEvent_removeListener.

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

Example 13 with TestComponent

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

the class ComponentEventBusTest method domEvent_fireServerEvent.

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

Example 14 with TestComponent

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

the class ComponentEventBusTest method testFireEvent_noListeners_eventBusNotCreated.

@Test
public void testFireEvent_noListeners_eventBusNotCreated() {
    AtomicInteger eventBusCreated = new AtomicInteger();
    TestComponent c = new TestComponent() {

        @Override
        public ComponentEventBus getEventBus() {
            eventBusCreated.incrementAndGet();
            return super.getEventBus();
        }
    };
    c.fireEvent(new ServerEvent(c, new BigDecimal(0)));
    Assert.assertEquals(0, eventBusCreated.get());
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 15 with TestComponent

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

the class EventRpcHandlerTest method testElementEventData.

@Test
public void testElementEventData() throws Exception {
    TestComponent c = new TestComponent();
    Element element = c.getElement();
    UI ui = new UI();
    ui.add(c);
    AtomicInteger invocationData = new AtomicInteger(0);
    element.addEventListener("test-event", e -> invocationData.addAndGet((int) e.getEventData().getNumber("nr")));
    JsonObject eventData = Json.createObject();
    eventData.put("nr", 123);
    sendElementEvent(element, ui, "test-event", eventData);
    Assert.assertEquals(123, invocationData.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) JsonObject(elemental.json.JsonObject) Test(org.junit.Test) TemplateElementStateProviderTest(com.vaadin.flow.dom.TemplateElementStateProviderTest)

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