Skip to content

Commit 3ec10c3

Browse files
authored
Add drag finished listener unit test (#4199)
1 parent 1106bb5 commit 3ec10c3

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

maven/core-unittests/src/test/java/com/codename1/testing/TestCodenameOneImplementation.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,30 @@ public void dispatchPointerRelease(int x, int y) {
13081308
sendPointerEventToCurrentForm(false, x, y);
13091309
}
13101310

1311+
public void dispatchPointerDrag(final int x, final int y) {
1312+
final Display display = Display.getInstance();
1313+
if (display == null) {
1314+
return;
1315+
}
1316+
1317+
Runnable r = new Runnable() {
1318+
public void run() {
1319+
Form current = display.getCurrent();
1320+
if (current == null) {
1321+
return;
1322+
}
1323+
1324+
current.pointerDragged(x, y);
1325+
}
1326+
};
1327+
1328+
if (display.isEdt()) {
1329+
r.run();
1330+
} else {
1331+
display.callSeriallyAndWait(r);
1332+
}
1333+
}
1334+
13111335
public void dispatchPointerPressAndRelease(int x, int y) {
13121336
dispatchPointerPress(x, y);
13131337
dispatchPointerRelease(x, y);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)