1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNotNull;
|
---|
7 |
|
---|
8 | import java.awt.Transparency;
|
---|
9 | import java.awt.image.BufferedImage;
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.util.logging.Handler;
|
---|
13 | import java.util.logging.LogRecord;
|
---|
14 | import java.util.logging.Logger;
|
---|
15 |
|
---|
16 | import javax.swing.ImageIcon;
|
---|
17 |
|
---|
18 | import org.junit.BeforeClass;
|
---|
19 | import org.junit.Test;
|
---|
20 | import org.openstreetmap.josm.JOSMFixture;
|
---|
21 | import org.openstreetmap.josm.TestUtils;
|
---|
22 |
|
---|
23 | import com.kitfox.svg.SVGConst;
|
---|
24 |
|
---|
25 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Unit tests of {@link ImageProvider} class.
|
---|
29 | */
|
---|
30 | public class ImageProviderTest {
|
---|
31 |
|
---|
32 | private static final class LogHandler14319 extends Handler {
|
---|
33 | boolean failed;
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public void publish(LogRecord record) {
|
---|
37 | if ("Could not load image: https://host-in-the-trusted-network.com/test.jpg".equals(record.getMessage())) {
|
---|
38 | failed = true;
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void flush() {
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public void close() throws SecurityException {
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Setup test.
|
---|
53 | */
|
---|
54 | @BeforeClass
|
---|
55 | public static void setUp() {
|
---|
56 | JOSMFixture.createUnitTestFixture().init();
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/9984">#9984</a>
|
---|
61 | * @throws IOException if an error occurs during reading
|
---|
62 | */
|
---|
63 | @Test
|
---|
64 | public void testTicket9984() throws IOException {
|
---|
65 | File file = new File(TestUtils.getRegressionDataFile(9984, "tile.png"));
|
---|
66 | assertEquals(Transparency.TRANSLUCENT, ImageProvider.read(file, true, true).getTransparency());
|
---|
67 | assertEquals(Transparency.TRANSLUCENT, ImageProvider.read(file, false, true).getTransparency());
|
---|
68 | long expectedTransparency = Utils.getJavaVersion() < 11 ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
|
---|
69 | assertEquals(expectedTransparency, ImageProvider.read(file, false, false).getTransparency());
|
---|
70 | assertEquals(expectedTransparency, ImageProvider.read(file, true, false).getTransparency());
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/10030">#10030</a>
|
---|
75 | * @throws IOException if an error occurs during reading
|
---|
76 | */
|
---|
77 | @Test
|
---|
78 | public void testTicket10030() throws IOException {
|
---|
79 | File file = new File(TestUtils.getRegressionDataFile(10030, "tile.jpg"));
|
---|
80 | BufferedImage img = ImageProvider.read(file, true, true);
|
---|
81 | assertNotNull(img);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/14319">#14319</a>
|
---|
86 | * @throws IOException if an error occurs during reading
|
---|
87 | */
|
---|
88 | @Test
|
---|
89 | @SuppressFBWarnings(value = "LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE")
|
---|
90 | public void testTicket14319() throws IOException {
|
---|
91 | LogHandler14319 handler = new LogHandler14319();
|
---|
92 | Logger.getLogger(SVGConst.SVG_LOGGER).addHandler(handler);
|
---|
93 | ImageIcon img = new ImageProvider(
|
---|
94 | new File(TestUtils.getRegressionDataDir(14319)).getAbsolutePath(), "attack.svg").get();
|
---|
95 | assertNotNull(img);
|
---|
96 | assertFalse(handler.failed);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Test fetching an image using {@code wiki://} protocol.
|
---|
101 | */
|
---|
102 | @Test
|
---|
103 | public void testWikiProtocol() {
|
---|
104 | // https://commons.wikimedia.org/wiki/File:OpenJDK_logo.svg
|
---|
105 | assertNotNull(ImageProvider.get("wiki://OpenJDK_logo.svg"));
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Test fetching an image using {@code data:} URL.
|
---|
110 | */
|
---|
111 | @Test
|
---|
112 | public void testDataUrl() {
|
---|
113 | // Red dot image, taken from https://en.wikipedia.org/wiki/Data_URI_scheme#HTML
|
---|
114 | assertNotNull(ImageProvider.get("data:image/png;base64," +
|
---|
115 | "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4"+
|
---|
116 | "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="));
|
---|
117 | }
|
---|
118 | }
|
---|