Search in sources :

Example 1 with ComponentEventListener

use of com.vaadin.flow.component.ComponentEventListener in project flow by vaadin.

the class DropTargetTest method testDropTarget_dropInSideSameUI_moveComponentToTargetInDropEvent.

@Test
public void testDropTarget_dropInSideSameUI_moveComponentToTargetInDropEvent() {
    RouterLink source = new RouterLink();
    TestComponent target = new TestComponent();
    ui.add(source, target);
    DragSource<RouterLink> dragSource = DragSource.create(source);
    DropTarget<TestComponent> dropTarget = DropTarget.create(target);
    DragStartEvent<RouterLink> startEvent = new DragStartEvent<RouterLink>(source, true);
    ComponentUtil.fireEvent(source, startEvent);
    Assert.assertEquals(source, ui.getActiveDragSourceComponent());
    ComponentEventListener<DropEvent<TestComponent>> dropListener = event -> {
        event.getDragSourceComponent().ifPresent(dragSourceComponent -> {
            TestComponent component = event.getComponent();
            Assert.assertEquals("Invalid drag source component", dragSourceComponent, source);
            Assert.assertEquals("Invalid event source", component, target);
            target.add(dragSourceComponent);
        });
    // ELSE will be failed by the checks outside the listener
    };
    target.addDropListener(dropListener);
    DropEvent<TestComponent> dropEvent = new DropEvent<>(target, true, "all");
    ComponentUtil.fireEvent(target, dropEvent);
    Assert.assertEquals("Drag source component should have been moved", source.getParent().get(), target);
    DragEndEvent<RouterLink> endEvent = new DragEndEvent<>(source, true, "move");
    // should not throw for removing the active drag source
    ComponentUtil.fireEvent(source, endEvent);
    Assert.assertNull(ui.getActiveDragSourceComponent());
}
Also used : ComponentEventListener(com.vaadin.flow.component.ComponentEventListener) Tag(com.vaadin.flow.component.Tag) DndUtil(com.vaadin.flow.component.dnd.internal.DndUtil) Component(com.vaadin.flow.component.Component) ComponentUtil(com.vaadin.flow.component.ComponentUtil) RouterLink(com.vaadin.flow.router.RouterLink) Test(org.junit.Test) HasComponents(com.vaadin.flow.component.HasComponents) Assert(org.junit.Assert) AtomicReference(java.util.concurrent.atomic.AtomicReference) RouterLink(com.vaadin.flow.router.RouterLink) Test(org.junit.Test)

Aggregations

Component (com.vaadin.flow.component.Component)1 ComponentEventListener (com.vaadin.flow.component.ComponentEventListener)1 ComponentUtil (com.vaadin.flow.component.ComponentUtil)1 HasComponents (com.vaadin.flow.component.HasComponents)1 Tag (com.vaadin.flow.component.Tag)1 DndUtil (com.vaadin.flow.component.dnd.internal.DndUtil)1 RouterLink (com.vaadin.flow.router.RouterLink)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1