|
| 1 | +package com.codename1.ui; |
| 2 | + |
| 3 | +import com.codename1.components.SpanButton; |
| 4 | +import com.codename1.junit.FormTest; |
| 5 | +import com.codename1.junit.UITestBase; |
| 6 | +import com.codename1.ui.geom.Dimension; |
| 7 | +import com.codename1.ui.layouts.BorderLayout; |
| 8 | +import com.codename1.ui.layouts.BoxLayout; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +class DraggableLeadComponentTest extends UITestBase { |
| 13 | + |
| 14 | + private static final int DRAG_DELTA = 25; |
| 15 | + |
| 16 | + @FormTest |
| 17 | + void draggableContainerStartsDragWithoutLead() { |
| 18 | + implementation.setBuiltinSoundsEnabled(false); |
| 19 | + Form form = Display.getInstance().getCurrent(); |
| 20 | + form.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); |
| 21 | + |
| 22 | + Button button = new Button("Normal Button, no lead"); |
| 23 | + button.setPreferredSize(new Dimension(200, 48)); |
| 24 | + Button other = new Button("Other"); |
| 25 | + other.setPreferredSize(new Dimension(120, 48)); |
| 26 | + Container draggableContainer = BorderLayout.centerCenterEastWest(button, other, null); |
| 27 | + draggableContainer.setDraggable(true); |
| 28 | + form.add(draggableContainer); |
| 29 | + form.revalidate(); |
| 30 | + |
| 31 | + Component draggedComponent = performPressDragAndReadDragged(form, button); |
| 32 | + assertEquals(draggableContainer, draggedComponent, "Draggable containers should start dragging even without a lead component"); |
| 33 | + } |
| 34 | + |
| 35 | + @FormTest |
| 36 | + void draggableContainerWithLeadButtonStartsDrag() { |
| 37 | + implementation.setBuiltinSoundsEnabled(false); |
| 38 | + Form form = Display.getInstance().getCurrent(); |
| 39 | + form.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); |
| 40 | + |
| 41 | + Button button = new Button("Normal Button, lead"); |
| 42 | + button.setPreferredSize(new Dimension(200, 48)); |
| 43 | + Button other = new Button("Other"); |
| 44 | + other.setPreferredSize(new Dimension(120, 48)); |
| 45 | + Container draggableContainer = BorderLayout.centerCenterEastWest(button, other, null); |
| 46 | + draggableContainer.setDraggable(true); |
| 47 | + draggableContainer.setLeadComponent(button); |
| 48 | + form.add(draggableContainer); |
| 49 | + form.revalidate(); |
| 50 | + |
| 51 | + Component draggedComponent = performPressDragAndReadDragged(form, button); |
| 52 | + assertEquals(draggableContainer, draggedComponent, "Lead component should initiate a drag on its container"); |
| 53 | + } |
| 54 | + |
| 55 | + @FormTest |
| 56 | + void draggableContainerWithSpanButtonLeadStartsDrag() { |
| 57 | + implementation.setBuiltinSoundsEnabled(false); |
| 58 | + Form form = Display.getInstance().getCurrent(); |
| 59 | + form.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); |
| 60 | + |
| 61 | + SpanButton spanButton = new SpanButton("SpanButton, lead"); |
| 62 | + spanButton.setPreferredSize(new Dimension(200, 48)); |
| 63 | + Button other = new Button("Other"); |
| 64 | + other.setPreferredSize(new Dimension(120, 48)); |
| 65 | + Container draggableContainer = BorderLayout.centerCenterEastWest(spanButton, other, null); |
| 66 | + draggableContainer.setDraggable(true); |
| 67 | + draggableContainer.setLeadComponent(other); |
| 68 | + form.add(draggableContainer); |
| 69 | + form.revalidate(); |
| 70 | + |
| 71 | + Component draggedComponent = performPressDragAndReadDragged(form, other); |
| 72 | + assertEquals(draggableContainer, draggedComponent, "Containers with SpanButton children should still drag when a lead component is provided"); |
| 73 | + } |
| 74 | + |
| 75 | + private Component performPressDragAndReadDragged(Form form, Component interactionComponent) { |
| 76 | + assertTrue(interactionComponent.getWidth() > 0, "Interaction component should be laid out with a width"); |
| 77 | + assertTrue(interactionComponent.getHeight() > 0, "Interaction component should be laid out with a height"); |
| 78 | + |
| 79 | + int startX = interactionComponent.getAbsoluteX() + Math.max(1, interactionComponent.getWidth() / 2); |
| 80 | + int startY = interactionComponent.getAbsoluteY() + Math.max(1, interactionComponent.getHeight() / 2); |
| 81 | + |
| 82 | + implementation.dispatchPointerPress(startX, startY); |
| 83 | + implementation.dispatchPointerDrag(startX + DRAG_DELTA, startY + DRAG_DELTA); |
| 84 | + implementation.dispatchPointerDrag(startX + (DRAG_DELTA * 2), startY + (DRAG_DELTA * 2)); |
| 85 | + Component draggedComponent = form.getDraggedComponent(); |
| 86 | + implementation.dispatchPointerRelease(startX + (DRAG_DELTA * 2), startY + (DRAG_DELTA * 2)); |
| 87 | + return draggedComponent; |
| 88 | + } |
| 89 | +} |
0 commit comments