Skip to content

Commit ed5425a

Browse files
committed
Add cross-platform Pattern constructor precondition tests
Adds Test_org_eclipse_swt_graphics_Pattern to the shared test suite (org.eclipse.swt.tests) covering the constructor contracts documented in Pattern's Javadoc: - Image-based constructor: null image -> IllegalArgumentException - Image-based constructor: disposed image -> IllegalArgumentException - Gradient constructors (both overloads): null color1/color2 -> IllegalArgumentException - Gradient constructors (both overloads): disposed color1/color2 -> IllegalArgumentException - isDisposed() reflects disposed state correctly - Calling dispose() twice must not throw - toString() returns a non-null, non-empty string before and after disposal The GTK and macOS implementations have always validated these preconditions eagerly in the constructor. These tests lock in that shared contract and serve as a regression guard for the Win32 implementation, which was aligned with the other platforms as part of the Pattern clean-up.
1 parent 3e6a53a commit ed5425a

2 files changed

Lines changed: 203 additions & 0 deletions

File tree

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/AllGraphicsTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
Test_org_eclipse_swt_graphics_ImageLoaderEvent.class, //
3636
Test_org_eclipse_swt_graphics_PaletteData.class, //
3737
Test_org_eclipse_swt_graphics_Path.class, //
38+
Test_org_eclipse_swt_graphics_Pattern.class, //
3839
Test_org_eclipse_swt_graphics_Point.class, //
3940
Test_org_eclipse_swt_graphics_RGB.class, //
4041
Test_org_eclipse_swt_graphics_RGBA.class, //
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Heiko Klare - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.tests.junit;
15+
16+
import static org.junit.jupiter.api.Assertions.assertFalse;
17+
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
21+
import org.eclipse.swt.SWT;
22+
import org.eclipse.swt.graphics.Color;
23+
import org.eclipse.swt.graphics.Image;
24+
import org.eclipse.swt.graphics.Pattern;
25+
import org.eclipse.swt.widgets.Display;
26+
import org.junit.jupiter.api.AfterEach;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
30+
/**
31+
* Automated Test Suite for class org.eclipse.swt.graphics.Pattern
32+
*
33+
* @see org.eclipse.swt.graphics.Pattern
34+
*/
35+
public class Test_org_eclipse_swt_graphics_Pattern {
36+
37+
private Display display;
38+
39+
@BeforeEach
40+
public void setUp() {
41+
display = Display.getDefault();
42+
}
43+
44+
@AfterEach
45+
public void tearDown() {
46+
// display is shared; do not dispose
47+
}
48+
49+
// --- Image-based constructor ---
50+
51+
@Test
52+
public void test_Constructor_image_valid() {
53+
Image image = new Image(display, 10, 10);
54+
try {
55+
Pattern pattern = new Pattern(display, image);
56+
assertFalse(pattern.isDisposed(), "Newly constructed Pattern must not be disposed");
57+
pattern.dispose();
58+
} finally {
59+
image.dispose();
60+
}
61+
}
62+
63+
@Test
64+
public void test_Constructor_image_nullImage() {
65+
assertThrows(IllegalArgumentException.class, () -> new Pattern(display, (Image) null));
66+
}
67+
68+
@Test
69+
public void test_Constructor_image_disposedImage() {
70+
Image image = new Image(display, 10, 10);
71+
image.dispose();
72+
assertThrows(IllegalArgumentException.class, () -> new Pattern(display, image));
73+
}
74+
75+
// --- Gradient constructors ---
76+
77+
@Test
78+
public void test_Constructor_gradient_valid() {
79+
Color red = display.getSystemColor(SWT.COLOR_RED);
80+
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
81+
Pattern pattern = new Pattern(display, 0, 0, 100, 100, red, blue);
82+
assertFalse(pattern.isDisposed(), "Newly constructed Pattern must not be disposed");
83+
pattern.dispose();
84+
}
85+
86+
@Test
87+
public void test_Constructor_gradientAlpha_valid() {
88+
Color red = display.getSystemColor(SWT.COLOR_RED);
89+
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
90+
Pattern pattern = new Pattern(display, 0, 0, 100, 100, red, 128, blue, 255);
91+
assertFalse(pattern.isDisposed(), "Newly constructed Pattern must not be disposed");
92+
pattern.dispose();
93+
}
94+
95+
@Test
96+
public void test_Constructor_gradient_nullColor1() {
97+
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
98+
assertThrows(IllegalArgumentException.class,
99+
() -> new Pattern(display, 0, 0, 100, 100, null, blue));
100+
}
101+
102+
@Test
103+
public void test_Constructor_gradient_nullColor2() {
104+
Color red = display.getSystemColor(SWT.COLOR_RED);
105+
assertThrows(IllegalArgumentException.class,
106+
() -> new Pattern(display, 0, 0, 100, 100, red, null));
107+
}
108+
109+
@Test
110+
public void test_Constructor_gradientAlpha_nullColor1() {
111+
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
112+
assertThrows(IllegalArgumentException.class,
113+
() -> new Pattern(display, 0, 0, 100, 100, null, 255, blue, 255));
114+
}
115+
116+
@Test
117+
public void test_Constructor_gradientAlpha_nullColor2() {
118+
Color red = display.getSystemColor(SWT.COLOR_RED);
119+
assertThrows(IllegalArgumentException.class,
120+
() -> new Pattern(display, 0, 0, 100, 100, red, 255, null, 255));
121+
}
122+
123+
@Test
124+
public void test_Constructor_gradient_disposedColor1() {
125+
Color red = new Color(display, 255, 0, 0);
126+
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
127+
red.dispose();
128+
assertThrows(IllegalArgumentException.class,
129+
() -> new Pattern(display, 0, 0, 100, 100, red, blue));
130+
}
131+
132+
@Test
133+
public void test_Constructor_gradient_disposedColor2() {
134+
Color red = display.getSystemColor(SWT.COLOR_RED);
135+
Color blue = new Color(display, 0, 0, 255);
136+
blue.dispose();
137+
assertThrows(IllegalArgumentException.class,
138+
() -> new Pattern(display, 0, 0, 100, 100, red, blue));
139+
}
140+
141+
@Test
142+
public void test_Constructor_gradientAlpha_disposedColor1() {
143+
Color red = new Color(display, 255, 0, 0);
144+
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
145+
red.dispose();
146+
assertThrows(IllegalArgumentException.class,
147+
() -> new Pattern(display, 0, 0, 100, 100, red, 255, blue, 255));
148+
}
149+
150+
@Test
151+
public void test_Constructor_gradientAlpha_disposedColor2() {
152+
Color red = display.getSystemColor(SWT.COLOR_RED);
153+
Color blue = new Color(display, 0, 0, 255);
154+
blue.dispose();
155+
assertThrows(IllegalArgumentException.class,
156+
() -> new Pattern(display, 0, 0, 100, 100, red, 255, blue, 255));
157+
}
158+
159+
// --- isDisposed ---
160+
161+
@Test
162+
public void test_isDisposed() {
163+
Color red = display.getSystemColor(SWT.COLOR_RED);
164+
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
165+
Pattern pattern = new Pattern(display, 0, 0, 100, 100, red, blue);
166+
167+
assertFalse(pattern.isDisposed(), "Pattern must not be disposed before dispose() is called");
168+
pattern.dispose();
169+
assertTrue(pattern.isDisposed(), "Pattern must be disposed after dispose() is called");
170+
}
171+
172+
@Test
173+
public void test_dispose_twice() {
174+
Color red = display.getSystemColor(SWT.COLOR_RED);
175+
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
176+
Pattern pattern = new Pattern(display, 0, 0, 100, 100, red, blue);
177+
178+
// Disposing twice must not throw
179+
pattern.dispose();
180+
pattern.dispose();
181+
assertTrue(pattern.isDisposed());
182+
}
183+
184+
// --- toString ---
185+
186+
@Test
187+
public void test_toString() {
188+
Color red = display.getSystemColor(SWT.COLOR_RED);
189+
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
190+
Pattern pattern = new Pattern(display, 0, 0, 100, 100, red, blue);
191+
192+
String s = pattern.toString();
193+
assertNotNull(s, "toString() must not return null");
194+
assertFalse(s.isEmpty(), "toString() must not return an empty string");
195+
196+
pattern.dispose();
197+
s = pattern.toString();
198+
assertNotNull(s, "toString() on disposed Pattern must not return null");
199+
assertFalse(s.isEmpty(), "toString() on disposed Pattern must not return an empty string");
200+
}
201+
202+
}

0 commit comments

Comments
 (0)