1+ package org .eclipse .swt .tests .junit .draw2d ;
2+
3+ import java .util .ArrayDeque ;
4+ import java .util .Deque ;
5+
6+ import org .eclipse .swt .SWT ;
7+ import org .eclipse .swt .graphics .Color ;
8+ import org .eclipse .swt .graphics .GC ;
9+ import org .eclipse .swt .graphics .Image ;
10+ import org .eclipse .swt .graphics .ImageData ;
11+ import org .eclipse .swt .graphics .LineAttributes ;
12+ import org .eclipse .swt .graphics .PaletteData ;
13+ import org .eclipse .swt .graphics .Path ;
14+ import org .eclipse .swt .graphics .RGB ;
15+ import org .eclipse .swt .graphics .Resource ;
16+ import org .eclipse .swt .widgets .Display ;
17+ import org .eclipse .swt .widgets .Shell ;
18+ import org .junit .After ;
19+ import org .junit .Before ;
20+ import org .junit .Test ;
21+
22+ public class AdvancedGraphicsTests extends BaseTestCase {
23+
24+ static final int [] LINE = { 5 , 5 , 20 , 20 , 35 , 5 , 50 , 5 };
25+ static final int [] POLY = { 5 , 5 , 45 , 15 , 20 , 30 , 20 , 20 , 45 , 35 , 5 , 45 };
26+ private SWTGraphics g ;
27+
28+ private Image image ;
29+ private final Deque <Resource > resources = new ArrayDeque <>();
30+
31+ private void assertImageEquality (int width , int height ) {
32+ ImageData data = image .getImageData ();
33+ int src ;
34+ int dst ;
35+ PaletteData palette = data .palette ;
36+ for (int y = 0 ; y < height ; y ++) {
37+ for (int x = 0 ; x < width ; x ++) {
38+ src = data .getPixel (x , y );
39+ dst = data .getPixel (x , y + height );
40+
41+ if (src != dst ) {
42+ RGB rgb1 = palette .getRGB (src );
43+ RGB rgb2 = palette .getRGB (dst );
44+ // HACK, image operations seem to differ by as much as 4
45+ if (Math .abs (rgb1 .red - rgb2 .red ) > 4 || Math .abs (rgb1 .green - rgb2 .green ) > 4
46+ || Math .abs (rgb1 .blue - rgb2 .blue ) > 4 ) {
47+ assertEquals ("Discrepancy at coordinates <" + x + ", " + y + ">" , rgb1 , rgb2 ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
48+ }
49+ }
50+ }
51+ }
52+ }
53+
54+ private void displayImage () {
55+ final Shell shell = new Shell (SWT .DIALOG_TRIM );
56+ shell .addPaintListener (e -> e .gc .drawImage (image , 0 , 0 ));
57+ shell .setBounds (100 , 100 , 800 , 600 );
58+ shell .open ();
59+ Display d = shell .getDisplay ();
60+ d .asyncExec (() -> {
61+ if (!shell .isDisposed ()) {
62+ shell .close ();
63+ }
64+ });
65+ waitEventLoop (shell , 100 );
66+ }
67+
68+ private void performTestcase (Runnable painter , Runnable [] tests ) {
69+ g .pushState ();
70+ painter .run ();
71+ for (Runnable test : tests ) {
72+ g .translate (100 , 0 );
73+ test .run ();
74+ g .pushState ();
75+ painter .run ();
76+ }
77+
78+ for (int i = 0 ; i <= tests .length ; i ++) {
79+ g .popState ();
80+ g .translate (0 , 100 );
81+ painter .run ();
82+ }
83+
84+ displayImage ();
85+ assertImageEquality (100 * tests .length , 100 );
86+ }
87+
88+ @ Before
89+ public void setUp () {
90+ Path path1 = new Path (null );
91+ path1 .moveTo (20 , 5 );
92+ path1 .quadTo (40 , 5 , 50 , 25 );
93+ path1 .quadTo (20 , 25 , 20 , 45 );
94+ path1 .lineTo (0 , 25 );
95+ path1 .close ();
96+
97+ Path path2 = new Path (null );
98+ path2 .moveTo (15 , 30 );
99+ path2 .cubicTo (50 , 0 , 50 , 30 , 20 , 60 );
100+ path2 .close ();
101+
102+ image = new Image (Display .getDefault (), 800 , 600 );
103+ GC imageGC = new GC (image , SWT .NONE );
104+ g = new SWTGraphics (imageGC );
105+
106+ resources .push (path1 );
107+ resources .push (path2 );
108+ resources .push (image );
109+ resources .push (imageGC );
110+ }
111+
112+ @ After
113+ public void tearDown () {
114+ g .dispose ();
115+ while (!resources .isEmpty ()) {
116+ resources .pop ().dispose ();
117+ }
118+ }
119+
120+ @ Test
121+ public void testAntialias () {
122+ class AntialiasSettings implements Runnable {
123+ private final Color color ;
124+ private final int normal ;
125+ private final int text ;
126+
127+ AntialiasSettings (int text , int normal , Color color ) {
128+ this .text = text ;
129+ this .normal = normal ;
130+ this .color = color ;
131+ }
132+
133+ @ Override
134+ public void run () {
135+ g .setAntialias (normal );
136+ g .setTextAntialias (text );
137+ g .setForegroundColor (color );
138+ }
139+ }
140+
141+ g .setLineWidthFloat (9 );
142+ g .pushState ();
143+
144+ Runnable [] tests = new Runnable [4 ];
145+ tests [0 ] = new AntialiasSettings (SWT .ON , SWT .ON , ColorConstants .red );
146+ tests [1 ] = new AntialiasSettings (SWT .OFF , SWT .OFF , ColorConstants .blue );
147+ tests [2 ] = new AntialiasSettings (SWT .DEFAULT , SWT .ON , ColorConstants .black );
148+ tests [3 ] = new AntialiasSettings (SWT .ON , SWT .DEFAULT , ColorConstants .darkGreen );
149+ performTestcase (() -> {
150+ g .drawPolyline (LINE );
151+ g .drawString ("OWVO" , 35 , 20 ); //$NON-NLS-1$
152+ }, tests );
153+ }
154+
155+ @ Test
156+ public void testFillRules () {
157+
158+ class FillRules implements Runnable {
159+ private final int aa ;
160+ private final int rule ;
161+
162+ FillRules (int rule , int aa ) {
163+ this .rule = rule ;
164+ this .aa = aa ;
165+ }
166+
167+ @ Override
168+ public void run () {
169+ g .setFillRule (rule );
170+ // $TODO
171+ g .setAntialias (aa );
172+ }
173+ }
174+ g .setBackgroundColor (ColorConstants .red );
175+ g .pushState ();
176+
177+ Runnable [] tests = new Runnable [3 ];
178+ tests [0 ] = new FillRules (SWT .FILL_EVEN_ODD , SWT .ON );
179+ tests [1 ] = new FillRules (SWT .FILL_WINDING , SWT .OFF );
180+ tests [2 ] = new FillRules (SWT .FILL_EVEN_ODD , SWT .DEFAULT );
181+ performTestcase (() -> g .fillPolygon (POLY ), tests );
182+ }
183+
184+ // public void testInterpolation() {
185+ // class InterpolationSettings implements Runnable {
186+ // private final int level;
187+ // InterpolationSettings (int level) {
188+ // this.level = level;
189+ // }
190+ // public void run() {
191+ // g.setInterpolation(level);
192+ // }
193+ // }
194+ //
195+ // g.pushState();
196+ //
197+ // Runnable tests[] = new Runnable[4];
198+ // tests[0] = new InterpolationSettings(SWT.HIGH);
199+ // tests[1] = new InterpolationSettings(SWT.LOW);
200+ // tests[2] = new InterpolationSettings(SWT.NONE);
201+ // tests[3] = new InterpolationSettings(SWT.DEFAULT);
202+ // performTestcase(new Runnable() {
203+ // public void run() {
204+ // g.drawImage(TestImages.depth_24, 0, 0, 400, 400, 0, 0, 75, 75);
205+ // }
206+ // }, tests);
207+ // }
208+
209+ @ Test
210+ public void testLineJoinCap () {
211+
212+ class LineSettings implements Runnable {
213+ private final int cap ;
214+ private final int style ;
215+ private final int join ;
216+
217+ LineSettings (int join , int cap , int style ) {
218+ this .join = join ;
219+ this .cap = cap ;
220+ this .style = style ;
221+ }
222+
223+ @ Override
224+ public void run () {
225+ g .setLineCap (cap );
226+ g .setLineJoin (join );
227+ g .setLineStyle (style );
228+ }
229+ }
230+
231+ g .setLineWidthFloat (9 );
232+ g .pushState ();
233+
234+ Runnable [] tests = { new LineSettings (SWT .JOIN_ROUND , SWT .CAP_ROUND , SWT .LINE_DASH ),
235+ new LineSettings (SWT .JOIN_BEVEL , SWT .CAP_FLAT , SWT .LINE_DOT ),
236+ new LineSettings (SWT .JOIN_ROUND , SWT .CAP_SQUARE , SWT .LINE_SOLID ) };
237+
238+ performTestcase (() -> g .drawPolyline (LINE ), tests );
239+ }
240+
241+ @ Test
242+ public void testLineJoinCapAA () {
243+ g .setAntialias (SWT .ON );
244+ testLineJoinCap ();
245+ }
246+
247+ @ Test
248+ public void testLineAttributes () {
249+ class LineSettings implements Runnable {
250+ private final LineAttributes attributes ;
251+
252+ public LineSettings (LineAttributes attributes ) {
253+ this .attributes = attributes ;
254+ }
255+
256+ @ Override
257+ public void run () {
258+ g .setLineAttributes (attributes );
259+ }
260+ }
261+
262+ float [] dash = { 2.5f , 3 , 8 };
263+
264+ Runnable [] tests = {
265+ new LineSettings (new LineAttributes (0.0f , SWT .CAP_FLAT , SWT .JOIN_MITER , SWT .LINE_SOLID , null , 0 , 10 )),
266+ new LineSettings (new LineAttributes (1.0f , SWT .CAP_FLAT , SWT .JOIN_MITER , SWT .LINE_SOLID , null , 0 , 10 )),
267+ new LineSettings (new LineAttributes (2.5f , SWT .CAP_FLAT , SWT .JOIN_MITER , SWT .LINE_SOLID , null , 0 , 10 )),
268+ new LineSettings (new LineAttributes (5.0f , SWT .CAP_FLAT , SWT .JOIN_MITER , SWT .LINE_DASH , null , 0 , 10 )),
269+ new LineSettings (
270+ new LineAttributes (5.0f , SWT .CAP_FLAT , SWT .JOIN_ROUND , SWT .LINE_DASHDOTDOT , null , 0 , 10 )),
271+ new LineSettings (new LineAttributes (4.5f , SWT .CAP_FLAT , SWT .JOIN_MITER , SWT .LINE_SOLID , null , 0 , 10 )),
272+ new LineSettings (new LineAttributes (9.0f , SWT .CAP_FLAT , SWT .JOIN_ROUND , SWT .LINE_CUSTOM , dash , 0 , 10 )),
273+ new LineSettings (
274+ new LineAttributes (9.5f , SWT .CAP_FLAT , SWT .JOIN_ROUND , SWT .LINE_CUSTOM , dash , 5 , 10 )), };
275+
276+ performTestcase (() -> g .drawPolyline (LINE ), tests );
277+ }
278+
279+ @ Test
280+ public void testLineAttributesAA () {
281+ g .setAntialias (SWT .ON );
282+ testLineAttributes ();
283+ }
284+
285+ // public void testPathDraw() {
286+ //
287+ // class PathSettings implements Runnable {
288+ // private final int antialias;
289+ // private final Color color;
290+ // private final int style;
291+ // private final int width;
292+ // PathSettings(int antialias, int width, int style, Color color) {
293+ // this.antialias = antialias;
294+ // this.width = width;
295+ // this.style = style;
296+ // this.color = color;
297+ // }
298+ // public void run() {
299+ // g.setAntialias(antialias);
300+ // g.setLineWidth(width);
301+ // g.setLineStyle(style);
302+ // g.setForegroundColor(color);
303+ // }
304+ // }
305+ //
306+ // g.setBackgroundColor(ColorConstants.darkBlue);
307+ //
308+ // Runnable tests[] = new Runnable[5];
309+ // tests[0] = new PathSettings(SWT.ON, 3, SWT.LINE_SOLID,
310+ // ColorConstants.darkBlue);
311+ // tests[1] = new PathSettings(SWT.OFF, 0, SWT.LINE_DOT,
312+ // ColorConstants.red);
313+ // tests[2] = new PathSettings(SWT.DEFAULT, 1, SWT.LINE_DOT,
314+ // ColorConstants.darkBlue);
315+ // tests[3] = new PathSettings(SWT.DEFAULT, 2, SWT.LINE_DOT,
316+ // ColorConstants.darkGreen);
317+ // tests[4] = new PathSettings(SWT.ON, 2, SWT.LINE_DASHDOTDOT,
318+ // ColorConstants.black);
319+ // performTestcase(new Runnable() {
320+ // public void run() {
321+ // g.drawPath(path1);
322+ // g.drawPath(path2);
323+ // }
324+ // }, tests);
325+ //
326+ // path1.dispose();
327+ // }
328+
329+ // public void testPathFill() {
330+ //
331+ // class PathSettings implements Runnable {
332+ // private final int antialias;
333+ // private final int alpha;
334+ // PathSettings(int antialias, int alpha) {
335+ // this.antialias = antialias;
336+ // this.alpha = alpha;
337+ // }
338+ // public void run() {
339+ // g.setAntialias(antialias);
340+ // g.setAlpha(alpha);
341+ // }
342+ // }
343+ //
344+ // g.setBackgroundColor(ColorConstants.darkBlue);
345+ //
346+ // Runnable tests[] = new Runnable[4];
347+ // tests[0] = new PathSettings(SWT.ON, 200);
348+ // tests[1] = new PathSettings(SWT.OFF, 100);
349+ // tests[2] = new PathSettings(SWT.DEFAULT, 200);
350+ // tests[3] = new PathSettings(SWT.ON, 150);
351+ // performTestcase(new Runnable() {
352+ // public void run() {
353+ // g.setFillRule(SWT.FILL_EVEN_ODD);
354+ // g.fillPath(path1);
355+ // g.fillPath(path2);
356+ // }
357+ // }, tests);
358+ //
359+ // path1.dispose();
360+ // }
361+
362+ }
0 commit comments