Skip to content

Commit 20220ec

Browse files
authored
Add draggable tabs unit test (#4203)
1 parent c031ce6 commit 20220ec

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.layouts.BorderLayout;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertSame;
10+
11+
class DraggableTabsTest extends UITestBase {
12+
13+
@FormTest
14+
void draggingTabHeaderReordersTabs() {
15+
implementation.setBuiltinSoundsEnabled(false);
16+
Form form = Display.getInstance().getCurrent();
17+
form.removeAll();
18+
form.setLayout(new BorderLayout());
19+
20+
Tabs tabs = new Tabs();
21+
Label tabOne = new Label("Tab 1");
22+
Label tabTwo = new Label("Tab 2");
23+
Label tabThree = new Label("Tab 3");
24+
Label tabFour = new Label("Tab 4");
25+
tabs.addTab("T1", tabOne);
26+
tabs.addTab("T2", tabTwo);
27+
tabs.addTab("T3", tabThree);
28+
tabs.addTab("T4", tabFour);
29+
30+
enableTabDragging(tabs);
31+
32+
form.add(BorderLayout.CENTER, tabs);
33+
form.revalidate();
34+
35+
Container headers = tabs.getTabsContainer();
36+
Component firstHeader = headers.getComponentAt(0);
37+
Component thirdHeader = headers.getComponentAt(2);
38+
39+
int startX = firstHeader.getAbsoluteX() + firstHeader.getWidth() / 2;
40+
int startY = firstHeader.getAbsoluteY() + firstHeader.getHeight() / 2;
41+
int dragX = startX + 10;
42+
int dragY = startY + 5;
43+
int targetX = thirdHeader.getAbsoluteX() + thirdHeader.getWidth() / 2;
44+
int targetY = thirdHeader.getAbsoluteY() + thirdHeader.getHeight() / 2;
45+
46+
TestCodenameOneImplementation impl = implementation;
47+
impl.dispatchPointerPress(startX, startY);
48+
impl.dispatchPointerDrag(dragX, dragY);
49+
impl.dispatchPointerDrag(targetX, targetY);
50+
impl.dispatchPointerRelease(targetX, targetY);
51+
52+
assertEquals(4, tabs.getTabCount());
53+
assertEquals("T2", tabs.getTabTitle(0));
54+
assertEquals("T1", tabs.getTabTitle(1));
55+
assertEquals("T3", tabs.getTabTitle(2));
56+
assertEquals("T4", tabs.getTabTitle(3));
57+
assertSame(tabTwo, tabs.getTabComponentAt(0));
58+
assertSame(tabOne, tabs.getTabComponentAt(1));
59+
assertSame(tabThree, tabs.getTabComponentAt(2));
60+
assertSame(tabFour, tabs.getTabComponentAt(3));
61+
}
62+
63+
private void enableTabDragging(final Tabs tabs) {
64+
final Container tabsContainer = tabs.getTabsContainer();
65+
tabsContainer.setDropTarget(true);
66+
for (final Component header : tabsContainer) {
67+
header.setDraggable(true);
68+
header.addDropListener(e -> {
69+
e.consume();
70+
int x = e.getX();
71+
int y = e.getY();
72+
int sourceIndex = tabsContainer.getComponentIndex(header);
73+
if (sourceIndex < 0) {
74+
return;
75+
}
76+
Component destination = tabsContainer.getComponentAt(x, y);
77+
if (destination == header) {
78+
return;
79+
}
80+
int destIndex = tabsContainer.getComponentIndex(destination);
81+
if (destIndex < 0 || destIndex == sourceIndex) {
82+
return;
83+
}
84+
String title = tabs.getTabTitle(sourceIndex);
85+
Component content = tabs.getTabComponentAt(sourceIndex);
86+
tabs.removeTabAt(sourceIndex);
87+
if (destIndex > sourceIndex) {
88+
tabs.insertTab(title, null, content, destIndex - 1);
89+
} else {
90+
tabs.insertTab(title, null, content, destIndex);
91+
}
92+
tabsContainer.animateLayout(0);
93+
});
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)