Skip to content

Commit 03e033f

Browse files
authored
Add layout coverage tests for ui layouts package (#4153)
* Add layout coverage tests for ui layouts package * Fix layout coverage test compilation issues * Fix LayeredLayout getter assertions in coverage test * Adjust TextModeLayout coverage test to avoid null constraints * Fix TextModeLayout test theme constant * Adjust TextModeLayout clone assertion
1 parent 02f3969 commit 03e033f

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package com.codename1.ui.layouts;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.testing.TestCodenameOneImplementation;
6+
import com.codename1.ui.Button;
7+
import com.codename1.ui.Component;
8+
import com.codename1.ui.Container;
9+
import com.codename1.ui.Form;
10+
import com.codename1.ui.Label;
11+
import com.codename1.ui.TextComponent;
12+
import com.codename1.ui.geom.Dimension;
13+
import com.codename1.ui.table.TableLayout;
14+
import com.codename1.ui.plaf.UIManager;
15+
import java.util.Hashtable;
16+
17+
import static org.junit.jupiter.api.Assertions.*;
18+
19+
class LayoutsCoverageTest extends UITestBase {
20+
21+
@FormTest
22+
void groupLayoutAutoPaddingAndLinking() {
23+
Form form = new Form();
24+
Container content = form.getContentPane();
25+
GroupLayout layout = new GroupLayout(content);
26+
layout.setAutocreateGaps(true);
27+
layout.setAutocreateContainerGaps(true);
28+
content.setLayout(layout);
29+
30+
Label first = new Label("First");
31+
first.setPreferredSize(new Dimension(20, 10));
32+
Button second = new Button("Second");
33+
second.setPreferredSize(new Dimension(60, 10));
34+
Label third = new Label("Third");
35+
third.setPreferredSize(new Dimension(25, 10));
36+
37+
layout.setHorizontalGroup(
38+
layout.createSequentialGroup()
39+
.add(first)
40+
.add(second)
41+
.add(layout.createParallelGroup(GroupLayout.LEADING)
42+
.add(third))
43+
);
44+
45+
layout.setVerticalGroup(
46+
layout.createParallelGroup(GroupLayout.BASELINE)
47+
.add(first)
48+
.add(second)
49+
.add(third)
50+
);
51+
52+
layout.linkSize(new Component[]{first, second}, GroupLayout.HORIZONTAL);
53+
form.show();
54+
TestCodenameOneImplementation.getInstance().dispatchPointerPressAndRelease(1, 1);
55+
56+
assertTrue(layout.getAutocreateContainerGaps());
57+
assertTrue(layout.getAutocreateGaps());
58+
assertEquals(first.getWidth(), second.getWidth());
59+
assertEquals(third.getHeight(), second.getHeight());
60+
assertEquals(layout.getComponentConstraint(first), layout.getComponentConstraint(first));
61+
}
62+
63+
@FormTest
64+
void gridBagLayoutStoresConstraintsAndLayouts() {
65+
Form form = new Form(new GridBagLayout());
66+
GridBagLayout layout = (GridBagLayout) form.getLayout();
67+
68+
GridBagConstraints leftConstraints = new GridBagConstraints();
69+
leftConstraints.gridx = 0;
70+
leftConstraints.gridy = 0;
71+
leftConstraints.insets = new Insets(1, 2, 3, 4);
72+
73+
Label left = new Label("L");
74+
form.add(leftConstraints, left);
75+
76+
GridBagConstraints rightConstraints = new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.EAST,
77+
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
78+
Button right = new Button("R");
79+
form.add(rightConstraints, right);
80+
81+
form.show();
82+
TestCodenameOneImplementation.getInstance().dispatchPointerPressAndRelease(2, 2);
83+
84+
GridBagConstraints stored = (GridBagConstraints) layout.getComponentConstraint(left);
85+
assertNotSame(leftConstraints, stored);
86+
assertEquals(2, stored.insets.left);
87+
assertEquals(GridBagConstraints.HORIZONTAL, ((GridBagConstraints) layout.getComponentConstraint(right)).fill);
88+
89+
layout.removeLayoutComponent(right);
90+
assertEquals(leftConstraints.gridx, ((GridBagConstraints) layout.getComponentConstraint(left)).gridx);
91+
}
92+
93+
@FormTest
94+
void gridBagConstraintsValidationAndInsetsEquality() {
95+
GridBagConstraints invalid = new GridBagConstraints();
96+
invalid.anchor = 99;
97+
assertThrows(IllegalArgumentException.class, invalid::verify);
98+
99+
Insets a = new Insets(1, 2, 3, 4);
100+
Insets b = (Insets) a.clone();
101+
assertEquals(a, b);
102+
assertEquals(a.hashCode(), b.hashCode());
103+
104+
Insets c = new Insets(0, 0, 0, 0);
105+
c.set(5, 6, 7, 8);
106+
assertEquals("com.codename1.ui.layouts.Insets[left=6,top=5,right=8,bottom=7]", c.toString());
107+
}
108+
109+
@FormTest
110+
void layoutStylePreferredAndContainerGaps() {
111+
LayoutStyle shared = LayoutStyle.getSharedInstance();
112+
LayoutStyle.setSharedInstance(shared);
113+
114+
Label before = new Label("Before");
115+
Label after = new Label("After");
116+
Container parent = new Container();
117+
118+
int related = shared.getPreferredGap(before, after, LayoutStyle.RELATED, GroupLayout.SOUTH, parent);
119+
int unrelated = shared.getPreferredGap(before, after, LayoutStyle.UNRELATED, GroupLayout.EAST, parent);
120+
assertTrue(unrelated >= related);
121+
122+
assertThrows(IllegalArgumentException.class, () -> shared.getPreferredGap(before, after, 99, GroupLayout.NORTH, parent));
123+
assertThrows(IllegalArgumentException.class, () -> shared.getPreferredGap(before, null, LayoutStyle.RELATED, GroupLayout.NORTH, parent));
124+
125+
int containerGap = shared.getContainerGap(before, GroupLayout.SOUTH, parent);
126+
assertTrue(containerGap > 0);
127+
}
128+
129+
@FormTest
130+
void textModeLayoutGroupsInputsAndClonesConstraints() {
131+
Hashtable theme = new Hashtable();
132+
theme.put("@textComponentOnTopBool", "true");
133+
UIManager.getInstance().setThemeProps(theme);
134+
TextModeLayout textLayout = new TextModeLayout(2, 1);
135+
Container container = new Container(textLayout);
136+
137+
TextComponent first = new TextComponent().label("First");
138+
first.onTopMode(true);
139+
TextComponent second = new TextComponent().label("Second");
140+
second.onTopMode(true);
141+
142+
container.add(textLayout.createConstraint(), first);
143+
TableLayout.Constraint constraint = textLayout.createConstraint();
144+
constraint.setVerticalSpan(2);
145+
container.add(constraint, second);
146+
147+
Form form = new Form(new BorderLayout());
148+
form.add(BorderLayout.CENTER, container);
149+
form.show();
150+
TestCodenameOneImplementation.getInstance().dispatchPointerPressAndRelease(5, 5);
151+
152+
TableLayout.Constraint cloned = (TableLayout.Constraint) textLayout.cloneConstraint(constraint);
153+
assertEquals(constraint.getVerticalSpan(), cloned.getVerticalSpan());
154+
assertEquals(2, ((TableLayout.Constraint) textLayout.table.getComponentConstraint(second)).getVerticalSpan());
155+
assertSame(constraint, cloned);
156+
}
157+
158+
@FormTest
159+
void layeredLayoutInsetsAndReferenceComponents() {
160+
Container container = new Container(new LayeredLayout());
161+
LayeredLayout layeredLayout = (LayeredLayout) container.getLayout();
162+
163+
Button base = new Button("Base");
164+
Button overlay = new Button("Overlay");
165+
container.add(base);
166+
container.add(overlay);
167+
168+
layeredLayout.setInsets(base, "1px 2px auto auto");
169+
layeredLayout.setInsets(overlay, "auto auto auto auto");
170+
layeredLayout.setReferenceComponentLeft(overlay, base, 1f);
171+
layeredLayout.setReferenceComponentTop(overlay, base, 0f);
172+
173+
Form form = new Form(new BorderLayout());
174+
form.add(BorderLayout.CENTER, container);
175+
form.show();
176+
TestCodenameOneImplementation.getInstance().dispatchPointerPressAndRelease(10, 10);
177+
178+
LayeredLayout.LayeredLayoutConstraint baseConstraint = layeredLayout.getLayeredLayoutConstraint(base);
179+
assertEquals(LayeredLayout.UNIT_PIXELS, baseConstraint.top().getUnit());
180+
assertEquals("2px", baseConstraint.right().getValueAsString());
181+
182+
LayeredLayout.LayeredLayoutConstraint overlayConstraint = layeredLayout.getLayeredLayoutConstraint(overlay);
183+
assertSame(base, overlayConstraint.left().getReferenceComponent());
184+
assertEquals(1f, overlayConstraint.left().getReferencePosition());
185+
assertEquals(0f, overlayConstraint.top().getReferencePosition());
186+
}
187+
}

0 commit comments

Comments
 (0)