Skip to content

Commit 1106bb5

Browse files
authored
Refactor of the graphics testing pipeline (#4185)
1 parent 9ddcd0d commit 1106bb5

30 files changed

+670
-744
lines changed

CodenameOne/src/com/codename1/ui/Display.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,8 +2698,6 @@ public int convertToPixels(float value, byte unitType) {
26982698
* @since 8.0
26992699
*/
27002700
public int convertToPixels(float value, byte unitType, boolean horizontal) {
2701-
2702-
27032701
switch (unitType) {
27042702
case Style.UNIT_TYPE_REM:
27052703
return Math.round(value * Font.getDefaultFont().getHeight());

CodenameOne/src/com/codename1/ui/Graphics.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ public void drawString(String str, int x, int y) {
636636
* @param character - the character to be drawn
637637
* @param x the x coordinate of the baseline of the text
638638
* @param y the y coordinate of the baseline of the text
639+
* @deprecated use drawString instead, this method is inefficient
639640
*/
640641
public void drawChar(char character, int x, int y) {
641642
drawString("" + character, x, y);
@@ -650,6 +651,7 @@ public void drawChar(char character, int x, int y) {
650651
* @param length the number of characters to be drawn
651652
* @param x the x coordinate of the baseline of the text
652653
* @param y the y coordinate of the baseline of the text
654+
* @deprecated use drawString instead, this method is inefficient
653655
*/
654656
public void drawChars(char[] data, int offset, int length, int x, int y) {
655657
if (!(current instanceof CustomFont)) {
@@ -1089,9 +1091,9 @@ void drawImageArea(Image img, int x, int y, int imageX, int imageY, int imageWid
10891091
* Draws a closed polygon defined by arrays of x and y coordinates.
10901092
* Each pair of (x, y) coordinates defines a point.
10911093
*
1092-
* @param xPoints - a an array of x coordinates.
1093-
* @param yPoints - a an array of y coordinates.
1094-
* @param nPoints - a the total number of points.
1094+
* @param xPoints - an array of x coordinates.
1095+
* @param yPoints - an array of y coordinates.
1096+
* @param nPoints - the total number of points.
10951097
*/
10961098
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
10971099
int[] cX = xPoints;
@@ -1149,7 +1151,7 @@ public int concatenateAlpha(int a) {
11491151
}
11501152

11511153
/**
1152-
* Returnes the alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque
1154+
* Returns the alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque
11531155
* and 0 is completely transparent
11541156
*
11551157
* @return the alpha value
@@ -1169,59 +1171,59 @@ public void setAlpha(int a) {
11691171
}
11701172

11711173
/**
1172-
* Returns true if anti-aliasing for standard rendering operations is supported,
1173-
* notice that text anti-aliasing is a separate attribute.
1174+
* Returns true if antialiasing for standard rendering operations is supported,
1175+
* notice that text antialiasing is a separate attribute.
11741176
*
1175-
* @return true if anti aliasing is supported
1177+
* @return true if antialiasing is supported
11761178
*/
11771179
public boolean isAntiAliasingSupported() {
11781180
return impl.isAntiAliasingSupported(nativeGraphics);
11791181
}
11801182

11811183
/**
1182-
* Returns true if anti-aliasing for text is supported,
1183-
* notice that text anti-aliasing is a separate attribute from standard anti-alisaing.
1184+
* Returns true if antialiasing for text is supported,
1185+
* notice that text antialiasing is a separate attribute from standard anti-alisaing.
11841186
*
1185-
* @return true if text anti aliasing is supported
1187+
* @return true if text antialiasing is supported
11861188
*/
11871189
public boolean isAntiAliasedTextSupported() {
11881190
return impl.isAntiAliasedTextSupported(nativeGraphics);
11891191
}
11901192

11911193

11921194
/**
1193-
* Returns true if anti-aliasing for standard rendering operations is turned on.
1195+
* Returns true if antialiasing for standard rendering operations is turned on.
11941196
*
1195-
* @return true if anti aliasing is active
1197+
* @return true if antialiasing is active
11961198
*/
11971199
public boolean isAntiAliased() {
11981200
return impl.isAntiAliased(nativeGraphics);
11991201
}
12001202

12011203
/**
1202-
* Set whether anti-aliasing for standard rendering operations is turned on.
1204+
* Set whether antialiasing for standard rendering operations is turned on.
12031205
*
1204-
* @param a true if anti aliasing is active
1206+
* @param a true if antialiasing is active
12051207
*/
12061208
public void setAntiAliased(boolean a) {
12071209
impl.setAntiAliased(nativeGraphics, a);
12081210
}
12091211

12101212
/**
1211-
* Indicates whether anti-aliasing for text is active,
1212-
* notice that text anti-aliasing is a separate attribute from standard anti-alisaing.
1213+
* Indicates whether antialiasing for text is active,
1214+
* notice that text antialiasing is a separate attribute from standard anti-alisaing.
12131215
*
1214-
* @return true if text anti aliasing is supported
1216+
* @return true if text antialiasing is supported
12151217
*/
12161218
public boolean isAntiAliasedText() {
12171219
return impl.isAntiAliasedText(nativeGraphics);
12181220
}
12191221

12201222
/**
1221-
* Set whether anti-aliasing for text is active,
1222-
* notice that text anti-aliasing is a separate attribute from standard anti-alisaing.
1223+
* Set whether antialiasing for text is active,
1224+
* notice that text antialiasing is a separate attribute from standard anti-alisaing.
12231225
*
1224-
* @param a true if text anti aliasing is supported
1226+
* @param a true if text antialiasing is supported
12251227
*/
12261228
public void setAntiAliasedText(boolean a) {
12271229
impl.setAntiAliasedText(nativeGraphics, a);

CodenameOne/src/com/codename1/ui/Image.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public static Image exifRotation(String capturedImage, String rotatedImage, int
392392
boolean isJpeg = isJPEG(fss.openInputStream(capturedImage));
393393
boolean isPNG = isPNG(fss.openInputStream(capturedImage));
394394
String format;
395-
// IMPORTANT: we cannot relies on the file extension of the capturedImage path,
395+
// IMPORTANT: we cannot rely on the file extension of the capturedImage path,
396396
// because some Android devices return images from the gallery without extension!
397397
if (!isJpeg && !isPNG) {
398398
// Only jpeg and png images are supported, but some devices can return also different formats from the gallery (like gif).
@@ -472,7 +472,7 @@ public static Image exifRotation(String capturedImage, String rotatedImage, int
472472

473473
/**
474474
* <p>
475-
* Gets the EXIF orientation tag of an image, if it's available.</p>
475+
* Gets the EXIF orientation tag of an image if it's available.</p>
476476
* <p>
477477
* The Exif Orientation Tag is a number from 0 to 8, for the explanation of
478478
* each value see the

Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6831,7 +6831,7 @@ public Object createMutableImage(int width, int height, int fillColor) {
68316831
BufferedImage b = createTrackableBufferedImage(width, height);
68326832
if (a != 0) {
68336833
Graphics2D g = b.createGraphics();
6834-
g.setColor(new Color(fillColor));
6834+
g.setColor(new Color(fillColor, true));
68356835
g.fillRect(0, 0, width, height);
68366836
g.dispose();
68376837
}

maven/core-unittests/src/test/java/com/codename1/analytics/AnalyticsServiceTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.codename1.io.ConnectionRequest;
44
import com.codename1.junit.FormTest;
5+
import com.codename1.junit.TestLogger;
56
import com.codename1.junit.UITestBase;
67

78
import java.util.List;
@@ -27,6 +28,7 @@ void testVisitQueuesAnalyticsRequest() {
2728

2829
@FormTest
2930
void testCrashReportQueued() {
31+
TestLogger.install();
3032
implementation.clearQueuedRequests();
3133
AnalyticsService.init("UA-2", "app.example.com");
3234
AnalyticsService.setAppsMode(true);
@@ -35,5 +37,6 @@ void testCrashReportQueued() {
3537
List<ConnectionRequest> requests = implementation.getQueuedRequests();
3638
assertEquals(1, requests.size());
3739
assertTrue(requests.get(0).getUrl().contains("google-analytics"));
40+
TestLogger.remove();
3841
}
3942
}

scripts/android/tests/ProcessScreenshots.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ private static PNGImage loadPng(Path path) throws IOException {
335335
byte[] data = Files.readAllBytes(path);
336336
for (int i = 0; i < PNG_SIGNATURE.length; i++) {
337337
if (data[i] != PNG_SIGNATURE[i]) {
338-
throw new IOException(path + " is not a PNG file (missing signature)");
338+
throw new IOException(path + " is not a PNG file (missing signature) on " + path);
339339
}
340340
}
341341
int offset = PNG_SIGNATURE.length;
@@ -350,7 +350,7 @@ private static PNGImage loadPng(Path path) throws IOException {
350350
byte[] type = java.util.Arrays.copyOfRange(data, offset + 4, offset + 8);
351351
offset += 8;
352352
if (offset + length + 4 > data.length) {
353-
throw new IOException("PNG chunk truncated before CRC");
353+
throw new IOException("PNG chunk truncated before CRC while processing: " + path);
354354
}
355355
byte[] chunkData = java.util.Arrays.copyOfRange(data, offset, offset + length);
356356
offset += length + 4; // skip data + CRC
@@ -364,7 +364,7 @@ private static PNGImage loadPng(Path path) throws IOException {
364364
int filter = chunkData[11] & 0xFF;
365365
interlace = chunkData[12] & 0xFF;
366366
if (compression != 0 || filter != 0) {
367-
throw new IOException("Unsupported PNG compression or filter method");
367+
throw new IOException("Unsupported PNG compression or filter method on " + path);
368368
}
369369
} else if ("IDAT".equals(chunkType)) {
370370
idatChunks.add(chunkData);
@@ -373,10 +373,10 @@ private static PNGImage loadPng(Path path) throws IOException {
373373
}
374374
}
375375
if (width <= 0 || height <= 0) {
376-
throw new IOException("Missing IHDR chunk");
376+
throw new IOException("Missing IHDR chunk on " + path);
377377
}
378378
if (interlace != 0) {
379-
throw new IOException("Interlaced PNGs are not supported");
379+
throw new IOException("Interlaced PNGs are not supported " + path);
380380
}
381381
int bytesPerPixel = bytesPerPixel(bitDepth, colorType);
382382
byte[] combined = concat(idatChunks);

scripts/hellocodenameone/common/src/main/css/theme.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,12 @@ SideCommand {
5252
font-size: 4mm;
5353
border-bottom: 2px solid #cccccc;
5454
}
55+
56+
GraphicsForm {
57+
background: #cccccc;
58+
}
59+
60+
GraphicsComponent {
61+
padding: 0px;
62+
margin: 1mm;
63+
}
Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codenameone.examples.hellocodenameone;
22

3+
import com.codename1.system.Lifecycle;
34
import com.codename1.testing.TestReporting;
45
import com.codename1.ui.Button;
56
import com.codename1.ui.BrowserComponent;
@@ -15,33 +16,15 @@
1516
import com.codenameone.examples.hellocodenameone.tests.Cn1ssDeviceRunner;
1617
import com.codenameone.examples.hellocodenameone.tests.Cn1ssDeviceRunnerReporter;
1718

18-
public class HelloCodenameOne {
19-
private Form current;
20-
private static boolean deviceRunnerExecuted;
21-
19+
public class HelloCodenameOne extends Lifecycle {
20+
@Override
2221
public void init(Object context) {
22+
super.init(context);
2323
TestReporting.setInstance(new Cn1ssDeviceRunnerReporter());
2424
}
2525

26-
public void start() {
27-
if (current != null) {
28-
current.show();
29-
return;
30-
}
31-
Form c = CN.getCurrentForm();
32-
if (!deviceRunnerExecuted || c == null) {
33-
deviceRunnerExecuted = true;
34-
new Thread(() -> new Cn1ssDeviceRunner().runSuite()).start();
35-
return;
36-
}
37-
c.show();
38-
}
39-
40-
public void stop() {
41-
current = Display.getInstance().getCurrent();
42-
}
43-
44-
public void destroy() {
45-
// Nothing to clean up for this sample
26+
@Override
27+
public void runApp() {
28+
new Thread(() -> new Cn1ssDeviceRunner().runSuite()).start();
4629
}
4730
}

scripts/hellocodenameone/common/src/main/java/com/codenameone/examples/hellocodenameone/tests/AbstractGraphicsScreenshotTest.java

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,118 @@
11
package com.codenameone.examples.hellocodenameone.tests;
22

3+
import com.codename1.ui.CN;
34
import com.codename1.ui.Component;
5+
import com.codename1.ui.Font;
46
import com.codename1.ui.Form;
7+
import com.codename1.ui.Graphics;
8+
import com.codename1.ui.Image;
9+
import com.codename1.ui.geom.Rectangle;
510
import com.codename1.ui.layouts.BorderLayout;
11+
import com.codename1.ui.layouts.GridLayout;
612

7-
abstract class AbstractGraphicsScreenshotTest extends BaseTest {
8-
protected abstract Component createContent();
13+
public abstract class AbstractGraphicsScreenshotTest extends BaseTest {
14+
private final int[] colorSet = {0xff0000, 0xff00, 0xff, 0xffffff};
15+
private int currentColor = -1;
16+
private int factor = 0;
17+
18+
protected void nextColor(Graphics g) {
19+
if(factor == 0) {
20+
factor = CN.getDisplayWidth() < 400 ? 5 : 1;
21+
}
22+
if(currentColor == -1) {
23+
currentColor = 0;
24+
g.setColor(colorSet[0]);
25+
}
26+
g.darkerColor(1);
27+
if(g.getColor() == 0) {
28+
currentColor++;
29+
if (currentColor == colorSet.length) {
30+
currentColor = 0;
31+
}
32+
g.setColor(colorSet[currentColor]);
33+
}
34+
}
35+
36+
protected abstract void drawContent(Graphics g, Rectangle bounds);
937

1038
protected abstract String screenshotName();
1139

40+
static abstract class CleanPaintComponent extends Component {
41+
CleanPaintComponent() {
42+
setUIID("GraphicsComponent");
43+
}
44+
45+
@Override
46+
public void paint(Graphics g) {
47+
int alpha = g.getAlpha();
48+
int color = g.getColor();
49+
Font font = g.getFont();
50+
g.pushClip();
51+
cleanPaint(g);
52+
g.popClip();
53+
g.setFont(font);
54+
g.setColor(color);
55+
g.setAlpha(alpha);
56+
}
57+
58+
protected abstract void cleanPaint(Graphics g);
59+
}
60+
1261
@Override
1362
public boolean runTest() {
14-
Form form = createForm("Graphics", new BorderLayout(), screenshotName());
15-
form.add(BorderLayout.CENTER, createContent());
63+
Form form = createForm(screenshotName(), new GridLayout(2, 2), screenshotName());
64+
form.setUIID("GraphicsForm");
65+
form.add(new CleanPaintComponent() {
66+
@Override
67+
public void cleanPaint(Graphics g) {
68+
currentColor = -1;
69+
g.setAntiAliased(false);
70+
g.setAntiAliasedText(false);
71+
g.fillRect(getX(), getY(), getWidth(), getHeight());
72+
drawContent(g, getBounds());
73+
}
74+
});
75+
form.add(new CleanPaintComponent() {
76+
@Override
77+
public void cleanPaint(Graphics g) {
78+
currentColor = -1;
79+
g.setAntiAliased(true);
80+
g.setAntiAliasedText(true);
81+
g.fillRect(getX(), getY(), getWidth(), getHeight());
82+
drawContent(g, getBounds());
83+
}
84+
});
85+
form.add(new CleanPaintComponent() {
86+
private Image img;
87+
@Override
88+
public void cleanPaint(Graphics g) {
89+
if (img == null || img.getWidth() != getWidth() || img.getHeight() != getHeight()) {
90+
currentColor = -1;
91+
img = Image.createImage(getWidth(), getHeight());
92+
Graphics imgGraphics = img.getGraphics();
93+
imgGraphics.setAntiAliased(false);
94+
imgGraphics.setAntiAliasedText(false);
95+
drawContent(imgGraphics, new Rectangle(0, 0, img.getWidth(), img.getHeight()));
96+
}
97+
g.drawImage(img, getX(), getY());
98+
}
99+
});
100+
form.add(new CleanPaintComponent() {
101+
private Image img;
102+
@Override
103+
public void cleanPaint(Graphics g) {
104+
if (img == null || img.getWidth() != getWidth() || img.getHeight() != getHeight()) {
105+
currentColor = -1;
106+
img = Image.createImage(getWidth(), getHeight());
107+
Graphics imgGraphics = img.getGraphics();
108+
imgGraphics.setAntiAliased(true);
109+
imgGraphics.setAntiAliasedText(true);
110+
drawContent(imgGraphics, new Rectangle(0, 0, img.getWidth(), img.getHeight()));
111+
}
112+
g.drawImage(img, getX(), getY());
113+
}
114+
});
115+
16116
form.show();
17117
return true;
18118
}

0 commit comments

Comments
 (0)