Search in sources :

Example 16 with RouterLink

use of com.vaadin.flow.router.RouterLink 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)

Example 17 with RouterLink

use of com.vaadin.flow.router.RouterLink in project flow by vaadin.

the class DropTargetTest method testDropTarget_dropListener_correctData.

@Test
public void testDropTarget_dropListener_correctData() {
    RouterLink component = new RouterLink();
    ui.add(component);
    DropTarget<RouterLink> dropTarget = DropTarget.create(component);
    AtomicReference<DropEvent<RouterLink>> eventCapture = new AtomicReference<>();
    dropTarget.addDropListener(eventCapture::set);
    DropEvent<RouterLink> dropEvent = new DropEvent<>(component, true, "all");
    ComponentUtil.fireEvent(component, dropEvent);
    DropEvent<RouterLink> actualEvent = eventCapture.get();
    Assert.assertEquals(dropEvent, actualEvent);
    Assert.assertTrue(actualEvent.isFromClient());
    Assert.assertEquals(component, actualEvent.getComponent());
    Assert.assertEquals(EffectAllowed.ALL, actualEvent.getEffectAllowed());
    Assert.assertEquals(null, actualEvent.getDropEffect());
    Assert.assertFalse(actualEvent.getDragData().isPresent());
    dropTarget.setDropEffect(DropEffect.COPY);
    dropEvent = new DropEvent<>(component, false, "copymove");
    ComponentUtil.fireEvent(component, dropEvent);
    actualEvent = eventCapture.get();
    Assert.assertEquals(dropEvent, actualEvent);
    Assert.assertFalse(actualEvent.isFromClient());
    Assert.assertEquals(component, actualEvent.getComponent());
    Assert.assertEquals(EffectAllowed.COPY_MOVE, actualEvent.getEffectAllowed());
    Assert.assertEquals(DropEffect.COPY, actualEvent.getDropEffect());
    Assert.assertFalse(actualEvent.getDragData().isPresent());
    Assert.assertFalse(actualEvent.getDragSourceComponent().isPresent());
}
Also used : RouterLink(com.vaadin.flow.router.RouterLink) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 18 with RouterLink

use of com.vaadin.flow.router.RouterLink in project flow by vaadin.

the class DropTargetTest method testDropTarget_staticBuilder_wrapsComponent.

@Test
public void testDropTarget_staticBuilder_wrapsComponent() {
    RouterLink component = new RouterLink();
    DropTarget<RouterLink> dropTarget = DropTarget.create(component);
    Assert.assertTrue(component.getElement().getProperty(DndUtil.DROP_TARGET_ACTIVE_PROPERTY, false));
    Assert.assertNull(dropTarget.getDropEffect());
    DropTarget.configure(component, false);
    Assert.assertFalse(component.getElement().getProperty(DndUtil.DROP_TARGET_ACTIVE_PROPERTY, false));
    DropTarget.configure(component);
    Assert.assertFalse(component.getElement().getProperty(DndUtil.DROP_TARGET_ACTIVE_PROPERTY, false));
    DropTarget.configure(component, true);
    Assert.assertTrue(component.getElement().getProperty(DndUtil.DROP_TARGET_ACTIVE_PROPERTY, false));
    DropTarget.configure(component);
    Assert.assertTrue(component.getElement().getProperty(DndUtil.DROP_TARGET_ACTIVE_PROPERTY, false));
}
Also used : RouterLink(com.vaadin.flow.router.RouterLink) Test(org.junit.Test)

Example 19 with RouterLink

use of com.vaadin.flow.router.RouterLink in project flow by vaadin.

the class DragSourceTest method testDragSource_notAttachedToUIAndCatchesDragEndEvent_doesNotThrow.

@Test
public void testDragSource_notAttachedToUIAndCatchesDragEndEvent_doesNotThrow() {
    RouterLink component = new RouterLink();
    ui.add(component);
    DragSource<RouterLink> dragSource = DragSource.create(component);
    DragStartEvent<RouterLink> startEvent = new DragStartEvent<RouterLink>(component, true);
    ComponentUtil.fireEvent(component, startEvent);
    Assert.assertEquals(component, ui.getActiveDragSourceComponent());
    // the drop event could remove the component if in same UI
    ui.remove(component);
    DragEndEvent<RouterLink> endEvent = new DragEndEvent<>(component, true, "move");
    // should not throw for removing the active drag source
    ComponentUtil.fireEvent(component, endEvent);
    Assert.assertNull(ui.getActiveDragSourceComponent());
}
Also used : RouterLink(com.vaadin.flow.router.RouterLink) Test(org.junit.Test)

Example 20 with RouterLink

use of com.vaadin.flow.router.RouterLink in project flow by vaadin.

the class DragSourceTest method testDragSource_notAttachedToUIAndCatchesDragStartEvent_throws.

@Test(expected = IllegalStateException.class)
public void testDragSource_notAttachedToUIAndCatchesDragStartEvent_throws() {
    RouterLink component = new RouterLink();
    DragSource<RouterLink> dragSource = DragSource.create(component);
    DragStartEvent<RouterLink> startEvent = new DragStartEvent<RouterLink>(component, true);
    ComponentUtil.fireEvent(component, startEvent);
}
Also used : RouterLink(com.vaadin.flow.router.RouterLink) Test(org.junit.Test)

Aggregations

RouterLink (com.vaadin.flow.router.RouterLink)30 Test (org.junit.Test)23 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 TestComponent (com.vaadin.flow.component.ComponentTest.TestComponent)3 Element (com.vaadin.flow.dom.Element)3 MockUI (com.vaadin.tests.util.MockUI)3 Component (com.vaadin.flow.component.Component)2 Div (com.vaadin.flow.component.html.Div)2 DependencyList (com.vaadin.flow.component.internal.DependencyList)2 PendingJavaScriptInvocation (com.vaadin.flow.component.internal.PendingJavaScriptInvocation)2 Registration (com.vaadin.flow.shared.Registration)2 Dependency (com.vaadin.flow.shared.ui.Dependency)2 Assert (org.junit.Assert)2 Before (org.junit.Before)2 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 UI (com.vaadin.flow.component.UI)1 DnDUtilHelper (com.vaadin.flow.component.dnd.internal.DnDUtilHelper)1