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 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
---|
8 | import static org.openstreetmap.josm.gui.mappaint.MapCSSRendererTest.assertImageEquals;
|
---|
9 |
|
---|
10 | import java.awt.Dimension;
|
---|
11 | import java.awt.Image;
|
---|
12 | import java.awt.Point;
|
---|
13 | import java.awt.Transparency;
|
---|
14 | import java.awt.image.BufferedImage;
|
---|
15 | import java.io.File;
|
---|
16 | import java.io.IOException;
|
---|
17 | import java.util.Arrays;
|
---|
18 | import java.util.Collections;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.function.UnaryOperator;
|
---|
21 | import java.util.logging.Handler;
|
---|
22 | import java.util.logging.LogRecord;
|
---|
23 | import java.util.logging.Logger;
|
---|
24 |
|
---|
25 | import javax.swing.ImageIcon;
|
---|
26 |
|
---|
27 | import org.junit.jupiter.api.BeforeAll;
|
---|
28 | import org.junit.jupiter.api.BeforeEach;
|
---|
29 | import org.junit.jupiter.api.Test;
|
---|
30 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
31 | import org.junit.jupiter.params.ParameterizedTest;
|
---|
32 | import org.junit.jupiter.params.provider.ValueSource;
|
---|
33 | import org.openstreetmap.josm.JOSMFixture;
|
---|
34 | import org.openstreetmap.josm.TestUtils;
|
---|
35 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
36 | import org.openstreetmap.josm.data.osm.Node;
|
---|
37 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
38 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
39 | import org.openstreetmap.josm.gui.tagging.presets.items.Key;
|
---|
40 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
41 | import org.xml.sax.SAXException;
|
---|
42 |
|
---|
43 | import com.kitfox.svg.SVGConst;
|
---|
44 |
|
---|
45 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Unit tests of {@link ImageProvider} class.
|
---|
49 | */
|
---|
50 | public class ImageProviderTest {
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Setup test.
|
---|
54 | */
|
---|
55 | @RegisterExtension
|
---|
56 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
57 | public JOSMTestRules test = new JOSMTestRules();
|
---|
58 |
|
---|
59 | private static final class LogHandler14319 extends Handler {
|
---|
60 | boolean failed;
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void publish(LogRecord record) {
|
---|
64 | if ("Could not load image: https://host-in-the-trusted-network.com/test.jpg".equals(record.getMessage())) {
|
---|
65 | failed = true;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public void flush() {
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public void close() throws SecurityException {
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Setup test.
|
---|
80 | */
|
---|
81 | @BeforeAll
|
---|
82 | public static void setUp() {
|
---|
83 | JOSMFixture.createUnitTestFixture().init();
|
---|
84 | }
|
---|
85 |
|
---|
86 | @BeforeEach
|
---|
87 | public void resetPixelDensity() {
|
---|
88 | GuiSizesHelper.setPixelDensity(1.0f);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/9984">#9984</a>
|
---|
93 | * @throws IOException if an error occurs during reading
|
---|
94 | */
|
---|
95 | @Test
|
---|
96 | public void testTicket9984() throws IOException {
|
---|
97 | File file = new File(TestUtils.getRegressionDataFile(9984, "tile.png"));
|
---|
98 | assertEquals(Transparency.TRANSLUCENT, ImageProvider.read(file, true, true).getTransparency());
|
---|
99 | assertEquals(Transparency.TRANSLUCENT, ImageProvider.read(file, false, true).getTransparency());
|
---|
100 | long expectedTransparency = Utils.getJavaVersion() < 11 ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
|
---|
101 | assertEquals(expectedTransparency, ImageProvider.read(file, false, false).getTransparency());
|
---|
102 | assertEquals(expectedTransparency, ImageProvider.read(file, true, false).getTransparency());
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/10030">#10030</a>
|
---|
107 | * @throws IOException if an error occurs during reading
|
---|
108 | */
|
---|
109 | @Test
|
---|
110 | public void testTicket10030() throws IOException {
|
---|
111 | File file = new File(TestUtils.getRegressionDataFile(10030, "tile.jpg"));
|
---|
112 | BufferedImage img = ImageProvider.read(file, true, true);
|
---|
113 | assertNotNull(img);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/14319">#14319</a>
|
---|
118 | * @throws IOException if an error occurs during reading
|
---|
119 | */
|
---|
120 | @Test
|
---|
121 | @SuppressFBWarnings(value = "LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE")
|
---|
122 | public void testTicket14319() throws IOException {
|
---|
123 | LogHandler14319 handler = new LogHandler14319();
|
---|
124 | Logger.getLogger(SVGConst.SVG_LOGGER).addHandler(handler);
|
---|
125 | ImageIcon img = new ImageProvider(
|
---|
126 | new File(TestUtils.getRegressionDataDir(14319)).getAbsolutePath(), "attack.svg").get();
|
---|
127 | assertNotNull(img);
|
---|
128 | assertFalse(handler.failed);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/19551">#19551</a>
|
---|
133 | * @throws SAXException If the type cannot be set (shouldn't throw)
|
---|
134 | */
|
---|
135 | @Test
|
---|
136 | public void testTicket19551() throws SAXException {
|
---|
137 | TaggingPreset badPreset = new TaggingPreset();
|
---|
138 | badPreset.setType("node,way,relation,closedway");
|
---|
139 | Key key = new Key();
|
---|
140 | key.key = "amenity";
|
---|
141 | key.value = "fuel";
|
---|
142 | badPreset.data.add(key);
|
---|
143 | TaggingPreset goodPreset = new TaggingPreset();
|
---|
144 | goodPreset.setType("node,way,relation,closedway");
|
---|
145 | goodPreset.data.add(key);
|
---|
146 | goodPreset.iconName = "stop";
|
---|
147 | TaggingPresets.addTaggingPresets(Arrays.asList(goodPreset, badPreset));
|
---|
148 | Node node = new Node(LatLon.ZERO);
|
---|
149 | node.put("amenity", "fuel");
|
---|
150 | assertDoesNotThrow(() -> OsmPrimitiveImageProvider.getResource(node, Collections.emptyList()));
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Test fetching an image using {@code data:} URL.
|
---|
155 | */
|
---|
156 | @Test
|
---|
157 | public void testDataUrl() {
|
---|
158 | // Red dot image, taken from https://en.wikipedia.org/wiki/Data_URI_scheme#HTML
|
---|
159 | assertNotNull(ImageProvider.get("data:image/png;base64," +
|
---|
160 | "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4"+
|
---|
161 | "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="));
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Unit test of {@link ImageResource#getImageIcon(java.awt.Dimension)}
|
---|
166 | * @throws IOException if an I/O error occurs
|
---|
167 | */
|
---|
168 | @Test
|
---|
169 | public void testImageIcon() throws IOException {
|
---|
170 | ImageResource resource = new ImageProvider("presets/misc/housenumber_small").getResource();
|
---|
171 | testImage(12, 9, "housenumber_small-AUTO-null", resource.getImageIcon());
|
---|
172 | testImage(12, 9, "housenumber_small-AUTO-default", resource.getImageIcon(ImageResource.DEFAULT_DIMENSION));
|
---|
173 | testImage(8, 8, "housenumber_small-AUTO-08x08", resource.getImageIcon(new Dimension(8, 8)));
|
---|
174 | testImage(16, 16, "housenumber_small-AUTO-16x16", resource.getImageIcon(new Dimension(16, 16)));
|
---|
175 | testImage(24, 24, "housenumber_small-AUTO-24x24", resource.getImageIcon(new Dimension(24, 24)));
|
---|
176 | testImage(36, 27, "housenumber_small-AUTO-36x27", resource.getImageIcon(new Dimension(36, 27)));
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Unit test of {@link ImageResource#getImageIconBounded(java.awt.Dimension)}
|
---|
181 | * @throws IOException if an I/O error occurs
|
---|
182 | */
|
---|
183 | @Test
|
---|
184 | public void testImageIconBounded() throws IOException {
|
---|
185 | ImageResource resource = new ImageProvider("presets/misc/housenumber_small").getResource();
|
---|
186 | testImage(8, 6, "housenumber_small-BOUNDED-08x08", resource.getImageIconBounded(new Dimension(8, 8)));
|
---|
187 | testImage(12, 9, "housenumber_small-BOUNDED-16x16", resource.getImageIconBounded(new Dimension(16, 16)));
|
---|
188 | testImage(12, 9, "housenumber_small-BOUNDED-24x24", resource.getImageIconBounded(new Dimension(24, 24)));
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Unit test of {@link ImageResource#getPaddedIcon(java.awt.Dimension)}
|
---|
193 | * @throws IOException if an I/O error occurs
|
---|
194 | */
|
---|
195 | @Test
|
---|
196 | public void testImageIconPadded() throws IOException {
|
---|
197 | ImageResource resource = new ImageProvider("presets/misc/housenumber_small").getResource();
|
---|
198 | testImage(8, 8, "housenumber_small-PADDED-08x08", resource.getPaddedIcon(new Dimension(8, 8)));
|
---|
199 | testImage(16, 16, "housenumber_small-PADDED-16x16", resource.getPaddedIcon(new Dimension(16, 16)));
|
---|
200 | testImage(24, 24, "housenumber_small-PADDED-24x24", resource.getPaddedIcon(new Dimension(24, 24)));
|
---|
201 | }
|
---|
202 |
|
---|
203 | private static void testImage(int width, int height, String reference, ImageIcon icon) throws IOException {
|
---|
204 | final BufferedImage image = (BufferedImage) icon.getImage();
|
---|
205 | final File referenceFile = getReferenceFile(reference);
|
---|
206 | assertEquals("width", width, image.getWidth(null));
|
---|
207 | assertEquals("height", height, image.getHeight(null));
|
---|
208 | assertImageEquals(reference, referenceFile, image, 0, 0, null);
|
---|
209 | }
|
---|
210 |
|
---|
211 | private static File getReferenceFile(String reference) {
|
---|
212 | return new File(TestUtils.getTestDataRoot() + "/" + ImageProviderTest.class.getSimpleName() + "/" + reference + ".png");
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Test getting a bounded icon given some UI scaling configured.
|
---|
217 | */
|
---|
218 | @Test
|
---|
219 | public void testGetImageIconBounded() {
|
---|
220 | int scale = 2;
|
---|
221 | GuiSizesHelper.setPixelDensity(scale);
|
---|
222 |
|
---|
223 | ImageProvider imageProvider = new ImageProvider("open").setOptional(true);
|
---|
224 | ImageResource resource = imageProvider.getResource();
|
---|
225 | Dimension iconDimension = ImageProvider.ImageSizes.SMALLICON.getImageDimension();
|
---|
226 | ImageIcon icon = resource.getImageIconBounded(iconDimension);
|
---|
227 | Image image = icon.getImage();
|
---|
228 | List<Image> resolutionVariants = HiDPISupport.getResolutionVariants(image);
|
---|
229 | if (resolutionVariants.size() > 1) {
|
---|
230 | assertEquals(2, resolutionVariants.size());
|
---|
231 | int expectedVirtualWidth = ImageProvider.ImageSizes.SMALLICON.getVirtualWidth();
|
---|
232 | assertEquals(expectedVirtualWidth * scale, resolutionVariants.get(0).getWidth(null));
|
---|
233 | assertEquals((int) Math.round(expectedVirtualWidth * scale * HiDPISupport.getHiDPIScale()),
|
---|
234 | resolutionVariants.get(1).getWidth(null));
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Test getting an image for a crosshair cursor.
|
---|
240 | */
|
---|
241 | @ParameterizedTest
|
---|
242 | @ValueSource(floats = {1.0f, 1.5f, 3.0f})
|
---|
243 | public void testGetCursorImageForCrosshair(float guiScale) throws IOException {
|
---|
244 | GuiSizesHelper.setPixelDensity(guiScale);
|
---|
245 | Point hotSpot = new Point();
|
---|
246 | final UnaryOperator<Dimension> bestCursorSizeFunction = dim -> dim;
|
---|
247 | Image image = ImageProvider.getCursorImage("crosshair", null, bestCursorSizeFunction, hotSpot);
|
---|
248 | assertCursorDimensionsCorrect(new Point.Double(10.0, 10.0), image, bestCursorSizeFunction, hotSpot);
|
---|
249 | assertImageEquals("cursor", getReferenceFile("cursor-crosshair-" + Math.round(guiScale * 10)),
|
---|
250 | ((BufferedImage) image), 0, 0, null);
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Test getting an image for a custom cursor with overlay.
|
---|
255 | */
|
---|
256 | @ParameterizedTest
|
---|
257 | @ValueSource(floats = {1.0f, 1.5f, 3.0f})
|
---|
258 | public void testGetCursorImageWithOverlay(float guiScale) throws IOException {
|
---|
259 | GuiSizesHelper.setPixelDensity(guiScale);
|
---|
260 | Point hotSpot = new Point();
|
---|
261 | final UnaryOperator<Dimension> bestCursorSizeFunction = dim -> dim;
|
---|
262 | Image image = ImageProvider.getCursorImage("normal", "selection", bestCursorSizeFunction, hotSpot);
|
---|
263 | assertCursorDimensionsCorrect(new Point.Double(3.0, 2.0), image, bestCursorSizeFunction, hotSpot);
|
---|
264 | assertImageEquals("cursor", getReferenceFile("cursor-normal-selection-" + Math.round(guiScale * 10)),
|
---|
265 | ((BufferedImage) image), 0, 0, null);
|
---|
266 | }
|
---|
267 |
|
---|
268 | private void assertCursorDimensionsCorrect(Point.Double originalHotspot, Image image,
|
---|
269 | UnaryOperator<Dimension> bestCursorSizeFunction, Point hotSpot) {
|
---|
270 | int originalCursorSize = ImageProvider.CURSOR_SIZE_HOTSPOT_IS_RELATIVE_TO;
|
---|
271 | Dimension bestCursorSize = bestCursorSizeFunction.apply(new Dimension(originalCursorSize, originalCursorSize));
|
---|
272 | Image bestCursorImage = HiDPISupport.getResolutionVariant(image, bestCursorSize.width, bestCursorSize.height);
|
---|
273 | int bestCursorImageWidth = bestCursorImage.getWidth(null);
|
---|
274 | assertEquals((int) Math.round(bestCursorSize.getWidth()), bestCursorImageWidth);
|
---|
275 | int bestCursorImageHeight = bestCursorImage.getHeight(null);
|
---|
276 | assertEquals((int) Math.round(bestCursorSize.getHeight()), bestCursorImageHeight);
|
---|
277 | assertEquals(originalHotspot.x / originalCursorSize * bestCursorImageWidth, hotSpot.x, 1 /* at worst one pixel off */);
|
---|
278 | assertEquals(originalHotspot.y / originalCursorSize * bestCursorImageHeight, hotSpot.y, 1 /* at worst one pixel off */);
|
---|
279 | }
|
---|
280 |
|
---|
281 | }
|
---|