Skip to content

Commit

Permalink
Cleaned all Draw2d Tests (#291)
Browse files Browse the repository at this point in the history
Fixed most warnings that resulted from stricter compiler rules.
  • Loading branch information
azoitl committed Nov 13, 2023
1 parent 764ad84 commit c761a9d
Show file tree
Hide file tree
Showing 31 changed files with 591 additions and 523 deletions.
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2004, 2010 IBM Corporation and others.
* Copyright (c) 2004, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -13,13 +13,10 @@

package org.eclipse.draw2d.test;

import java.util.Stack;
import java.util.ArrayDeque;
import java.util.Deque;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.SWTGraphics;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
Expand All @@ -34,27 +31,28 @@
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.SWTGraphics;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class AdvancedGraphicsTests extends BaseTestCase {

static final int LINE[] = new int[] { 5, 5, 20, 20, 35, 5, 50, 5 };
static final int POLY[] = new int[] { 5, 5, 45, 15, 20, 30, 20, 20, 45, 35, 5, 45 };
static final int[] LINE = { 5, 5, 20, 20, 35, 5, 50, 5 };
static final int[] POLY = { 5, 5, 45, 15, 20, 30, 20, 20, 45, 35, 5, 45 };
private SWTGraphics g;

private Image image;
private GC imageGC;
private Path path1;
private Path path2;
private Stack resources = new Stack();
private final Deque<Resource> resources = new ArrayDeque<>();

private void assertImageEquality(int width, int height) {
ImageData data = image.getImageData();
int src, dst;
int src;
int dst;
PaletteData palette = data.palette;
for (int y = 0; y < height; y++)
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
src = data.getPixel(x, y);
dst = data.getPixel(x, y + height);
Expand All @@ -64,39 +62,34 @@ private void assertImageEquality(int width, int height) {
RGB rgb2 = palette.getRGB(dst);
// HACK, image operations seem to differ by as much as 4
if (Math.abs(rgb1.red - rgb2.red) > 4 || Math.abs(rgb1.green - rgb2.green) > 4
|| Math.abs(rgb1.blue - rgb2.blue) > 4)
assertEquals("Discrepancy at coordinates <" + x + ", " + y + ">", rgb1, rgb2);
|| Math.abs(rgb1.blue - rgb2.blue) > 4) {
assertEquals("Discrepancy at coordinates <" + x + ", " + y + ">", rgb1, rgb2); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}
}
}
}

private void displayImage() {
final Shell shell = new Shell(SWT.DIALOG_TRIM);
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
e.gc.drawImage(image, 0, 0);
}
});
shell.addPaintListener(e -> e.gc.drawImage(image, 0, 0));
shell.setBounds(100, 100, 800, 600);
shell.open();
Display d = shell.getDisplay();
d.asyncExec(new Runnable() {
@Override
public void run() {
if (!shell.isDisposed())
shell.close();
d.asyncExec(() -> {
if (!shell.isDisposed()) {
shell.close();
}
});
waitEventLoop(shell, 100);
}

private void performTestcase(Runnable painter, Runnable tests[]) {
private void performTestcase(Runnable painter, Runnable[] tests) {
g.pushState();
painter.run();
for (int i = 0; i < tests.length; i++) {
for (Runnable test : tests) {
g.translate(100, 0);
tests[i].run();
test.run();
g.pushState();
painter.run();
}
Expand All @@ -113,20 +106,20 @@ private void performTestcase(Runnable painter, Runnable tests[]) {

@Before
public void setUp() throws Exception {
path1 = new Path(null);
Path path1 = new Path(null);
path1.moveTo(20, 5);
path1.quadTo(40, 5, 50, 25);
path1.quadTo(20, 25, 20, 45);
path1.lineTo(0, 25);
path1.close();

path2 = new Path(null);
Path path2 = new Path(null);
path2.moveTo(15, 30);
path2.cubicTo(50, 0, 50, 30, 20, 60);
path2.close();

image = new Image(Display.getDefault(), 800, 600);
imageGC = new GC(image, SWT.NONE);
GC imageGC = new GC(image, SWT.NONE);
g = new SWTGraphics(imageGC);

resources.push(path1);
Expand All @@ -136,10 +129,11 @@ public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
public void tearDown() {
g.dispose();
while (!resources.isEmpty())
((Resource) resources.pop()).dispose();
while (!resources.isEmpty()) {
resources.pop().dispose();
}
}

@Test
Expand All @@ -166,17 +160,14 @@ public void run() {
g.setLineWidthFloat(9);
g.pushState();

Runnable tests[] = new Runnable[4];
Runnable[] tests = new Runnable[4];
tests[0] = new AntialiasSettings(SWT.ON, SWT.ON, ColorConstants.red);
tests[1] = new AntialiasSettings(SWT.OFF, SWT.OFF, ColorConstants.blue);
tests[2] = new AntialiasSettings(SWT.DEFAULT, SWT.ON, ColorConstants.black);
tests[3] = new AntialiasSettings(SWT.ON, SWT.DEFAULT, ColorConstants.darkGreen);
performTestcase(new Runnable() {
@Override
public void run() {
g.drawPolyline(LINE);
g.drawString("OWVO", 35, 20);
}
performTestcase(() -> {
g.drawPolyline(LINE);
g.drawString("OWVO", 35, 20); //$NON-NLS-1$
}, tests);
}

Expand All @@ -202,16 +193,11 @@ public void run() {
g.setBackgroundColor(ColorConstants.red);
g.pushState();

Runnable tests[] = new Runnable[3];
Runnable[] tests = new Runnable[3];
tests[0] = new FillRules(SWT.FILL_EVEN_ODD, SWT.ON);
tests[1] = new FillRules(SWT.FILL_WINDING, SWT.OFF);
tests[2] = new FillRules(SWT.FILL_EVEN_ODD, SWT.DEFAULT);
performTestcase(new Runnable() {
@Override
public void run() {
g.fillPolygon(POLY);
}
}, tests);
performTestcase(() -> g.fillPolygon(POLY), tests);
}

// public void testInterpolation() {
Expand Down Expand Up @@ -264,16 +250,11 @@ public void run() {
g.setLineWidthFloat(9);
g.pushState();

Runnable[] tests = new Runnable[] { new LineSettings(SWT.JOIN_ROUND, SWT.CAP_ROUND, SWT.LINE_DASH),
Runnable[] tests = { new LineSettings(SWT.JOIN_ROUND, SWT.CAP_ROUND, SWT.LINE_DASH),
new LineSettings(SWT.JOIN_BEVEL, SWT.CAP_FLAT, SWT.LINE_DOT),
new LineSettings(SWT.JOIN_ROUND, SWT.CAP_SQUARE, SWT.LINE_SOLID) };

performTestcase(new Runnable() {
@Override
public void run() {
g.drawPolyline(LINE);
}
}, tests);
performTestcase(() -> g.drawPolyline(LINE), tests);
}

@Test
Expand All @@ -285,7 +266,7 @@ public void testLineJoinCapAA() {
@Test
public void testLineAttributes() {
class LineSettings implements Runnable {
private LineAttributes attributes;
private final LineAttributes attributes;

public LineSettings(LineAttributes attributes) {
this.attributes = attributes;
Expand All @@ -297,9 +278,9 @@ public void run() {
}
}

float[] dash = new float[] { 2.5f, 3, 8 };
float[] dash = { 2.5f, 3, 8 };

Runnable[] tests = new Runnable[] {
Runnable[] tests = {
new LineSettings(new LineAttributes(0.0f, SWT.CAP_FLAT, SWT.JOIN_MITER, SWT.LINE_SOLID, null, 0, 10)),
new LineSettings(new LineAttributes(1.0f, SWT.CAP_FLAT, SWT.JOIN_MITER, SWT.LINE_SOLID, null, 0, 10)),
new LineSettings(new LineAttributes(2.5f, SWT.CAP_FLAT, SWT.JOIN_MITER, SWT.LINE_SOLID, null, 0, 10)),
Expand All @@ -311,12 +292,7 @@ public void run() {
new LineSettings(
new LineAttributes(9.5f, SWT.CAP_FLAT, SWT.JOIN_ROUND, SWT.LINE_CUSTOM, dash, 5, 10)), };

performTestcase(new Runnable() {
@Override
public void run() {
g.drawPolyline(LINE);
}
}, tests);
performTestcase(() -> g.drawPolyline(LINE), tests);
}

@Test
Expand Down Expand Up @@ -422,7 +398,7 @@ public void run() {
}

// Initial state
Font f = new Font(null, "Helvetica", 50, SWT.BOLD);
Font f = new Font(null, "Helvetica", 50, SWT.BOLD); //$NON-NLS-1$
resources.push(f);
g.setFont(f);
g.setBackgroundColor(ColorConstants.yellow);
Expand All @@ -434,14 +410,9 @@ public void run() {
resources.push(gradient);
resources.push(image);

Runnable tests[] = new Runnable[1];
Runnable[] tests = new Runnable[1];
tests[0] = new SetPattern(image, gradient);
performTestcase(new Runnable() {
@Override
public void run() {
g.fillText("W", 0, 0);
}
}, tests);
performTestcase(() -> g.fillText("W", 0, 0), tests); //$NON-NLS-1$

}

Expand Down
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/

package org.eclipse.draw2d.test;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Figure;
Expand Down Expand Up @@ -122,7 +123,7 @@ public void testMoveTarget() {
public void testMoveTargetParent() {
count = 0;
nestedCoordinates.translate(10, 10);
assertTrue("Count != 1 :" + count, count == 1);
assertTrue("Count != 1 :" + count, count == 1); //$NON-NLS-1$
}

@Test
Expand Down
Expand Up @@ -12,18 +12,17 @@
*******************************************************************************/
package org.eclipse.draw2d.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Interval;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Shell;

import org.junit.Assert;

Expand All @@ -34,23 +33,8 @@ public abstract class BaseTestCase extends Assert {

protected static final Font TAHOMA = new Font(null, "Tahoma", 8, 0);//$NON-NLS-1$

protected boolean callBooleanMethod(Object receiver, String method) {
try {
Method m = receiver.getClass().getMethod(method, null);
Boolean result = (Boolean) m.invoke(receiver, null);
return result.booleanValue();
} catch (NoSuchMethodException exc) {
fail(exc.getMessage());
} catch (IllegalAccessException exc) {
fail(exc.getMessage());
} catch (InvocationTargetException exc) {
fail(exc.getMessage());
}
return false;
}

public void assertEquals(Image expected, Image actual) {
assertTrue("The given images did not match",
assertTrue("The given images did not match", //$NON-NLS-1$
Arrays.equals(expected.getImageData().data, actual.getImageData().data));
}

Expand Down
Expand Up @@ -11,6 +11,7 @@
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.draw2d.test;

import org.eclipse.swt.widgets.Display;

import org.junit.Assert;
Expand All @@ -21,23 +22,20 @@
public class ColorConstantTest extends Assert {

@Test
public void test_ColorConstantInit() {
final Boolean result[] = new Boolean[2];
public static void testColorConstantInit() {
final Boolean[] result = new Boolean[2];
result[0] = Boolean.FALSE;
result[1] = Boolean.FALSE;

Thread testThread = new Thread() {
@Override
public void run() {
try {
Class.forName("org.eclipse.draw2d.ColorConstants");
Class.forName("org.eclipse.draw2d.ColorConstants"); //$NON-NLS-1$
result[0] = Boolean.TRUE;
} catch (Error e) {
result[0] = Boolean.FALSE;
} catch (Exception ex) {
} catch (Error | Exception e) {
result[0] = Boolean.FALSE;
}

result[1] = Boolean.TRUE;
}
};
Expand All @@ -49,6 +47,5 @@ public void run() {
}

assertTrue(result[0].booleanValue());

}
}

0 comments on commit c761a9d

Please sign in to comment.