diff --git a/bundles/org.eclipse.swt.svg.tests/.classpath b/bundles/org.eclipse.swt.svg.tests/.classpath new file mode 100644 index 00000000000..7417a95f02f --- /dev/null +++ b/bundles/org.eclipse.swt.svg.tests/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/bundles/org.eclipse.swt.svg.tests/.project b/bundles/org.eclipse.swt.svg.tests/.project new file mode 100644 index 00000000000..6cd18c46394 --- /dev/null +++ b/bundles/org.eclipse.swt.svg.tests/.project @@ -0,0 +1,28 @@ + + + org.eclipse.swt.svg.tests + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/bundles/org.eclipse.swt.svg.tests/.settings/org.eclipse.jdt.core.prefs b/bundles/org.eclipse.swt.svg.tests/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000000..23fa13b1705 --- /dev/null +++ b/bundles/org.eclipse.swt.svg.tests/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 +org.eclipse.jdt.core.compiler.compliance=21 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=21 diff --git a/bundles/org.eclipse.swt.svg.tests/JUnit Tests/org/eclipse/swt/svg/tests/junit/Test_org_eclipse_swt_graphics_SVGRasterizer.java b/bundles/org.eclipse.swt.svg.tests/JUnit Tests/org/eclipse/swt/svg/tests/junit/Test_org_eclipse_swt_graphics_SVGRasterizer.java new file mode 100644 index 00000000000..b0581e94ea3 --- /dev/null +++ b/bundles/org.eclipse.swt.svg.tests/JUnit Tests/org/eclipse/swt/svg/tests/junit/Test_org_eclipse_swt_graphics_SVGRasterizer.java @@ -0,0 +1,90 @@ +package org.eclipse.swt.svg.tests.junit; + +import static org.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem; +import static org.junit.Assert.fail; + +import java.io.File; +import java.nio.file.Path; +import org.eclipse.swt.SWT; +import org.eclipse.swt.SWTException; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.ImageDataProvider; +import org.eclipse.swt.graphics.ImageFileNameProvider; +import org.eclipse.swt.tests.junit.SwtTestUtil; +import org.eclipse.swt.widgets.Display; +import org.junit.Before; +import org.junit.Test; + +public class Test_org_eclipse_swt_graphics_SVGRasterizer { + + Display display; + + ImageFileNameProvider imageFileNameProvider = zoom -> { + String fileName = "collapseall.svg"; + return getPath(fileName); + }; + + ImageDataProvider imageDataProvider = zoom -> { + String fileName = "collapseall.svg"; + return new ImageData(getPath(fileName), zoom); + }; + + @Before + public void setUp() { + display = Display.getDefault(); + } + + String getPath(String fileName) { + String urlPath; + String pluginPath = System.getProperty("PLUGIN_PATH"); + if (pluginPath == null) { + urlPath = Path.of("", "data/" + fileName).toUri().toString().replace("file:///", ""); + } else { + urlPath = pluginPath + "/data/" + fileName; + } + + if (File.separatorChar != '/') urlPath = urlPath.replace('/', File.separatorChar); + if (SwtTestUtil.isWindows && urlPath.indexOf(File.separatorChar) == 0) urlPath = urlPath.substring(1); + urlPath = urlPath.replaceAll("%20", " "); + return urlPath; + } + + @Test + public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvider() { + // Valid provider + Image image = new Image(display, imageFileNameProvider); + image.dispose(); + // Corrupt Image provider + ImageFileNameProvider provider = zoom -> { + String fileName = "corrupt.svg"; + return getPath(fileName); + }; + try { + image = new Image(display, provider); + image.dispose(); + fail("No exception thrown for corrupt image file."); + } catch (SWTException e) { + assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e); + } + } + + @Test + public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageDataProvider() { + // Valid provider + Image image = new Image(display, imageDataProvider); + image.dispose(); + // Corrupt Image provider + ImageDataProvider provider = zoom -> { + String fileName = "corrupt.svg"; + return new ImageData(getPath(fileName), zoom); + }; + try { + image = new Image(display, provider); + image.dispose(); + fail("No exception thrown for corrupt image file."); + } catch (SWTException e) { + assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e); + } + } +} diff --git a/bundles/org.eclipse.swt.svg.tests/JUnit Tests/org/eclipse/swt/svg/tests/junit/collapseall.svg b/bundles/org.eclipse.swt.svg.tests/JUnit Tests/org/eclipse/swt/svg/tests/junit/collapseall.svg new file mode 100644 index 00000000000..587c3c3497e --- /dev/null +++ b/bundles/org.eclipse.swt.svg.tests/JUnit Tests/org/eclipse/swt/svg/tests/junit/collapseall.svg @@ -0,0 +1,223 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + diff --git a/bundles/org.eclipse.swt.svg.tests/JUnit Tests/org/eclipse/swt/svg/tests/junit/corrupt.svg b/bundles/org.eclipse.swt.svg.tests/JUnit Tests/org/eclipse/swt/svg/tests/junit/corrupt.svg new file mode 100644 index 00000000000..edac6e7e9f2 --- /dev/null +++ b/bundles/org.eclipse.swt.svg.tests/JUnit Tests/org/eclipse/swt/svg/tests/junit/corrupt.svg @@ -0,0 +1,210 @@ + + + +< inkscape:version="0.91 r13725" + sodipodi:docname="collapseall.svg"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + diff --git a/bundles/org.eclipse.swt.svg.tests/META-INF/MANIFEST.MF b/bundles/org.eclipse.swt.svg.tests/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..c33b7d7f436 --- /dev/null +++ b/bundles/org.eclipse.swt.svg.tests/META-INF/MANIFEST.MF @@ -0,0 +1,13 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Tests +Bundle-SymbolicName: org.eclipse.swt.svg.tests +Bundle-Version: 1.0.0.qualifier +Require-Bundle: org.eclipse.swt, + org.eclipse.jface, + org.eclipse.swt.tests, + org.junit, + org.eclipse.core.runtime +Automatic-Module-Name: org.eclipse.swt.svg.tests +Bundle-ClassPath: . +Bundle-RequiredExecutionEnvironment: JavaSE-21 diff --git a/bundles/org.eclipse.swt.svg.tests/build.properties b/bundles/org.eclipse.swt.svg.tests/build.properties new file mode 100644 index 00000000000..4763cdab3fc --- /dev/null +++ b/bundles/org.eclipse.swt.svg.tests/build.properties @@ -0,0 +1,4 @@ +source.. = JUnit Tests/, +output.. = bin/ +bin.includes = .,\ + META-INF/ diff --git a/bundles/org.eclipse.swt.svg.tests/data/collapseall.svg b/bundles/org.eclipse.swt.svg.tests/data/collapseall.svg new file mode 100644 index 00000000000..587c3c3497e --- /dev/null +++ b/bundles/org.eclipse.swt.svg.tests/data/collapseall.svg @@ -0,0 +1,223 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + diff --git a/bundles/org.eclipse.swt.svg.tests/data/corrupt.svg b/bundles/org.eclipse.swt.svg.tests/data/corrupt.svg new file mode 100644 index 00000000000..edac6e7e9f2 --- /dev/null +++ b/bundles/org.eclipse.swt.svg.tests/data/corrupt.svg @@ -0,0 +1,210 @@ + + + +< inkscape:version="0.91 r13725" + sodipodi:docname="collapseall.svg"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + diff --git a/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java b/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java index 22b21268f90..4d96419ab4b 100644 --- a/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java +++ b/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java @@ -18,6 +18,7 @@ import java.io.*; import java.util.*; import org.eclipse.swt.graphics.SVGRasterizer; +import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.PaletteData; import org.eclipse.swt.graphics.RGB; @@ -77,8 +78,10 @@ public void close() throws IOException { svgDocument.render(null, g); g.dispose(); return convertToSWT(image); + } else { + SWT.error(SWT.ERROR_INVALID_IMAGE); + return null; } - return null; } private ImageData convertToSWT(BufferedImage bufferedImage) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java index a7bd3e96757..29a1decc627 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java @@ -193,7 +193,7 @@ public ImageData[] load(InputStream stream, int zoom) { } } } catch (IOException e) { - //ignore. + SWT.error(SWT.ERROR_INVALID_IMAGE); } } return loadDefault(stream);