Skip to content

Commit aa795b6

Browse files
authored
Add CheckboxTest2900 unit tests (#4193)
1 parent ef66c7b commit aa795b6

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.codename1.ui;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.ui.layouts.BorderLayout;
6+
import com.codename1.ui.layouts.FlowLayout;
7+
import com.codename1.ui.plaf.DefaultLookAndFeel;
8+
import com.codename1.ui.plaf.UIManager;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
public class CheckboxTest2900Test extends UITestBase {
16+
17+
@FormTest
18+
void checkboxesToggleSelectionWithOppositeSide() {
19+
UIManager manager = UIManager.getInstance();
20+
manager.setLookAndFeel(new DefaultLookAndFeel(manager));
21+
22+
Form form = new Form("Hi >>0 World", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
23+
Container panel = new Container(new FlowLayout());
24+
form.add(BorderLayout.CENTER, panel);
25+
26+
List<CheckBox> checkBoxes = new ArrayList<CheckBox>();
27+
for (int i = 0; i < 20; i++) {
28+
CheckBox checkBox = new CheckBox("Test Checkbox #" + i);
29+
checkBox.setSelected(true);
30+
checkBox.setOppositeSide((i & 1) == 0);
31+
panel.add(checkBox);
32+
checkBoxes.add(checkBox);
33+
}
34+
35+
form.show();
36+
DisplayTest.flushEdt();
37+
flushSerialCalls();
38+
DisplayTest.flushEdt();
39+
40+
CheckBox oppositeSide = checkBoxes.get(0);
41+
CheckBox regularSide = checkBoxes.get(1);
42+
43+
assertTrue(oppositeSide.isOppositeSide(), "Even indexed checkboxes should use the opposite side");
44+
assertFalse(regularSide.isOppositeSide(), "Odd indexed checkboxes should keep the default side");
45+
46+
assertTrue(oppositeSide.isSelected(), "Opposite side checkbox starts selected");
47+
assertTrue(regularSide.isSelected(), "Regular checkbox starts selected");
48+
49+
implementation.tapComponent(oppositeSide);
50+
DisplayTest.flushEdt();
51+
flushSerialCalls();
52+
DisplayTest.flushEdt();
53+
54+
implementation.tapComponent(regularSide);
55+
DisplayTest.flushEdt();
56+
flushSerialCalls();
57+
DisplayTest.flushEdt();
58+
59+
assertFalse(oppositeSide.isSelected(), "Pointer events should toggle the opposite side checkbox");
60+
assertFalse(regularSide.isSelected(), "Pointer events should toggle the regular checkbox");
61+
62+
implementation.tapComponent(oppositeSide);
63+
implementation.tapComponent(regularSide);
64+
DisplayTest.flushEdt();
65+
flushSerialCalls();
66+
DisplayTest.flushEdt();
67+
68+
assertTrue(oppositeSide.isSelected(), "Second tap should re-select the opposite side checkbox");
69+
assertTrue(regularSide.isSelected(), "Second tap should re-select the regular checkbox");
70+
}
71+
72+
@FormTest
73+
void drawImagePreservesClipAndScales() {
74+
Image source = Image.createImage(4, 4, 0xffff0000);
75+
Image target = Image.createImage(16, 16, 0xff00ff00);
76+
77+
Graphics graphics = target.getGraphics();
78+
graphics.setClip(2, 3, 10, 8);
79+
int[] originalClip = graphics.getClip();
80+
81+
drawImage(graphics, source, 2, 3, 14, 11, 0, 0, 4, 4);
82+
83+
int[] clipAfterDraw = graphics.getClip();
84+
assertArrayEquals(originalClip, clipAfterDraw, "Custom drawImage should restore the original clip");
85+
86+
graphics.setColor(0xff112233);
87+
graphics.fillRect(0, 0, target.getWidth(), target.getHeight());
88+
89+
int unchangedPixel = target.getRGB()[0];
90+
int insideClipPixel = target.getRGB()[target.getWidth() * 4 + 4];
91+
92+
assertEquals(0xff00ff00, unchangedPixel, "Pixels outside the clip should remain unchanged");
93+
assertEquals(0xff112233, insideClipPixel, "Drawing after the helper should respect the restored clip region");
94+
}
95+
96+
private void drawImage(Graphics gc, Image im,
97+
int dx, int dy, int dx2, int dy2,
98+
int fx, int fy, int fx2, int fy2) {
99+
if (gc != null) {
100+
int w = dx2 - dx;
101+
int h = dy2 - dy;
102+
int sw = fx2 - fx;
103+
int sh = fy2 - fy;
104+
int imw = im.getWidth();
105+
int imh = im.getHeight();
106+
double xscale = w / (double) sw;
107+
double yscale = h / (double) sh;
108+
int[] clip = gc.getClip();
109+
110+
if (clip != null && clip.length >= 4) {
111+
gc.clipRect(dx, dy, w, h);
112+
int finx = dx - (int) (fx * xscale);
113+
int finy = dy - (int) (fy * yscale);
114+
int finw = (int) (imw * xscale);
115+
int finh = (int) (imh * yscale);
116+
gc.drawImage(im, finx, finy, finw, finh);
117+
gc.setClip(clip);
118+
}
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)