|
| 1 | +package com.codename1.ui; |
| 2 | + |
| 3 | +import com.codename1.junit.FormTest; |
| 4 | +import com.codename1.junit.UITestBase; |
| 5 | +import com.codename1.ui.events.ActionEvent; |
| 6 | +import com.codename1.ui.geom.Dimension; |
| 7 | +import com.codename1.ui.layouts.GridLayout; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 12 | + |
| 13 | +/** |
| 14 | + * Port of the DropListenerSample demo into a regression test that fires drag and drop |
| 15 | + * events through the full Codename One stack using {@link TestCodenameOneImplementation}. |
| 16 | + */ |
| 17 | +class DropListenerSampleTest extends UITestBase { |
| 18 | + |
| 19 | + @FormTest |
| 20 | + void dropListenerReceivesDropEventsAndDelegatesToTarget() { |
| 21 | + implementation.setBuiltinSoundsEnabled(false); |
| 22 | + Form form = Display.getInstance().getCurrent(); |
| 23 | + form.setLayout(new GridLayout(1, 2)); |
| 24 | + |
| 25 | + TrackingContainer draggable = new TrackingContainer(); |
| 26 | + draggable.setPreferredSize(new Dimension(80, 80)); |
| 27 | + draggable.setDraggable(true); |
| 28 | + |
| 29 | + TrackingDropTarget dropTarget = new TrackingDropTarget(); |
| 30 | + dropTarget.setPreferredSize(new Dimension(80, 80)); |
| 31 | + dropTarget.setDropTarget(true); |
| 32 | + |
| 33 | + form.add(draggable); |
| 34 | + form.add(dropTarget); |
| 35 | + form.revalidate(); |
| 36 | + |
| 37 | + assertTrue(draggable.getWidth() > 0 && draggable.getHeight() > 0, |
| 38 | + "Draggable component should have layout dimensions"); |
| 39 | + assertTrue(dropTarget.getWidth() > 0 && dropTarget.getHeight() > 0, |
| 40 | + "Drop target should have layout dimensions"); |
| 41 | + |
| 42 | + final int[] dropListenerCount = {0}; |
| 43 | + final ActionEvent[] lastEvent = {null}; |
| 44 | + draggable.addDropListener(evt -> { |
| 45 | + dropListenerCount[0]++; |
| 46 | + lastEvent[0] = evt; |
| 47 | + }); |
| 48 | + |
| 49 | + int startX = draggable.getAbsoluteX() + draggable.getWidth() / 2; |
| 50 | + int startY = draggable.getAbsoluteY() + draggable.getHeight() / 2; |
| 51 | + int targetX = dropTarget.getAbsoluteX() + dropTarget.getWidth() / 2; |
| 52 | + int targetY = dropTarget.getAbsoluteY() + dropTarget.getHeight() / 2; |
| 53 | + |
| 54 | + implementation.dispatchPointerPress(startX, startY); |
| 55 | + implementation.dispatchPointerDrag(startX + draggable.getWidth() / 4, startY); |
| 56 | + implementation.dispatchPointerDrag(startX + draggable.getWidth() / 3, startY + draggable.getHeight() / 6); |
| 57 | + implementation.dispatchPointerDrag((startX + targetX) / 2, (startY + targetY) / 2); |
| 58 | + implementation.dispatchPointerDrag(targetX, targetY); |
| 59 | + implementation.dispatchPointerDrag(targetX, targetY + dropTarget.getHeight() / 4); |
| 60 | + implementation.dispatchPointerRelease(targetX, targetY); |
| 61 | + |
| 62 | + assertTrue(dropListenerCount[0] > 0, "Drop listener should be invoked when dropping on a target"); |
| 63 | + assertNotNull(lastEvent[0], "Drop event should be captured"); |
| 64 | + assertEquals(dropTarget, lastEvent[0].getComponent(), "Drop listener should receive the drop target component"); |
| 65 | + assertEquals(draggable, lastEvent[0].getSource(), "Source component should be the draggable container"); |
| 66 | + assertTrue(dropTarget.getDragOverCount() > 0, "Drag over callbacks should occur on the drop target"); |
| 67 | + assertEquals(1, dropTarget.getDropCount(), "Drop target should be notified exactly once"); |
| 68 | + assertEquals(draggable, dropTarget.getLastDragged(), "Drop target should receive the dragged component instance"); |
| 69 | + } |
| 70 | + |
| 71 | + private static class TrackingContainer extends Container { |
| 72 | + TrackingContainer() { |
| 73 | + getStyle().setBgTransparency(255); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private static class TrackingDropTarget extends Container { |
| 78 | + private int dropCount; |
| 79 | + private Component lastDragged; |
| 80 | + private int dragOverCount; |
| 81 | + |
| 82 | + TrackingDropTarget() { |
| 83 | + getStyle().setBgTransparency(255); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected boolean draggingOver(Component dragged, int x, int y) { |
| 88 | + dragOverCount++; |
| 89 | + return super.draggingOver(dragged, x, y); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void drop(Component dragged, int x, int y) { |
| 94 | + dropCount++; |
| 95 | + lastDragged = dragged; |
| 96 | + } |
| 97 | + |
| 98 | + int getDropCount() { |
| 99 | + return dropCount; |
| 100 | + } |
| 101 | + |
| 102 | + Component getLastDragged() { |
| 103 | + return lastDragged; |
| 104 | + } |
| 105 | + |
| 106 | + int getDragOverCount() { |
| 107 | + return dragOverCount; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments