Skip to content

Commit c3097d3

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 adee578 commit c3097d3

2 files changed

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

0 commit comments

Comments
 (0)