|
| 1 | +package com.codename1.ui; |
| 2 | + |
| 3 | +import com.codename1.junit.FormTest; |
| 4 | +import com.codename1.junit.UITestBase; |
| 5 | +import com.codename1.testing.TestCodenameOneImplementation; |
| 6 | +import com.codename1.ui.events.ActionEvent; |
| 7 | +import com.codename1.ui.events.ActionListener; |
| 8 | +import com.codename1.ui.geom.Dimension; |
| 9 | +import com.codename1.ui.layouts.BoxLayout; |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +class DragFinishedListener3056Test extends UITestBase { |
| 13 | + |
| 14 | + @FormTest |
| 15 | + void dragFinishedListenerFiresForLeadComponent() { |
| 16 | + implementation.setBuiltinSoundsEnabled(false); |
| 17 | + Form form = Display.getInstance().getCurrent(); |
| 18 | + form.removeAll(); |
| 19 | + form.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); |
| 20 | + |
| 21 | + Container container = new Container(new BoxLayout(BoxLayout.Y_AXIS)); |
| 22 | + container.setPreferredSize(new Dimension(160, 160)); |
| 23 | + |
| 24 | + Label draggableIcon = new Label(); |
| 25 | + FontImage.setMaterialIcon(draggableIcon, FontImage.MATERIAL_3D_ROTATION, 10); |
| 26 | + draggableIcon.setPreferredSize(new Dimension(100, 100)); |
| 27 | + draggableIcon.setDraggable(true); |
| 28 | + |
| 29 | + Label secondary = new Label("Another label in the container"); |
| 30 | + |
| 31 | + container.add(draggableIcon); |
| 32 | + container.add(secondary); |
| 33 | + container.putClientProperty("isTest", Boolean.TRUE); |
| 34 | + container.setLeadComponent(draggableIcon); |
| 35 | + container.setDraggable(true); |
| 36 | + |
| 37 | + final int[] dragCount = {0}; |
| 38 | + final boolean[] dragFinishedCalled = {false}; |
| 39 | + final int[] finished = {-1, -1}; |
| 40 | + final Component[] eventSource = {null}; |
| 41 | + |
| 42 | + draggableIcon.addPointerDraggedListener(new ActionListener() { |
| 43 | + public void actionPerformed(ActionEvent evt) { |
| 44 | + dragCount[0]++; |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + draggableIcon.addDragFinishedListener(new ActionListener() { |
| 49 | + public void actionPerformed(ActionEvent evt) { |
| 50 | + dragFinishedCalled[0] = true; |
| 51 | + finished[0] = evt.getX(); |
| 52 | + finished[1] = evt.getY(); |
| 53 | + eventSource[0] = (Component) evt.getSource(); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + form.add(container); |
| 58 | + form.revalidate(); |
| 59 | + |
| 60 | + TestCodenameOneImplementation impl = implementation; |
| 61 | + int startX = draggableIcon.getAbsoluteX() + draggableIcon.getWidth() / 2; |
| 62 | + int startY = draggableIcon.getAbsoluteY() + draggableIcon.getHeight() / 2; |
| 63 | + int dragX = startX + 15; |
| 64 | + int dragY = startY + 15; |
| 65 | + int releaseX = startX + 25; |
| 66 | + int releaseY = startY + 20; |
| 67 | + |
| 68 | + impl.dispatchPointerPress(startX, startY); |
| 69 | + impl.dispatchPointerDrag(dragX, dragY); |
| 70 | + impl.dispatchPointerDrag(releaseX, releaseY); |
| 71 | + impl.dispatchPointerRelease(releaseX, releaseY); |
| 72 | + |
| 73 | + assertEquals(Boolean.TRUE, container.getClientProperty("isTest"), |
| 74 | + "Container should preserve client property used by the sample"); |
| 75 | + assertTrue(dragCount[0] > 0, "Pointer dragged listener should be invoked while dragging"); |
| 76 | + assertTrue(dragFinishedCalled[0], "Drag finished listener should fire after releasing drag"); |
| 77 | + assertSame(draggableIcon, eventSource[0], "Event source should match the draggable lead component"); |
| 78 | + assertTrue(finished[0] >= 0 && finished[1] >= 0, |
| 79 | + "Drag finished coordinates should be provided with non-negative values"); |
| 80 | + } |
| 81 | +} |
0 commit comments