source: josm/trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/ImageDisplayTest.java

Last change on this file was 18684, checked in by taylor.smock, 14 months ago

Fix #22770: High precision scroll inputs may cause zoom events in the ImageDisplay

This is caused by the mouse wheel rotation being 0 (MouseWheelEvent#getWheelRotation )
which then caused the image viewer to perform a zoom event.

  • Property svn:eol-style set to native
File size: 6.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.geoimage;
3
4import static org.junit.jupiter.api.Assertions.assertAll;
5import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
6import static org.junit.jupiter.api.Assertions.assertEquals;
7import static org.junit.jupiter.api.Assertions.assertNotEquals;
8
9import java.awt.Dimension;
10import java.awt.Graphics2D;
11import java.awt.Rectangle;
12import java.awt.event.MouseWheelEvent;
13import java.awt.event.MouseWheelListener;
14import java.awt.image.BufferedImage;
15import java.io.IOException;
16import java.lang.reflect.Field;
17import java.nio.file.DirectoryStream;
18import java.nio.file.Files;
19import java.nio.file.Path;
20import java.nio.file.Paths;
21import java.util.function.IntFunction;
22import java.util.function.Supplier;
23
24import javax.swing.JPanel;
25
26import org.junit.jupiter.api.Disabled;
27import org.junit.jupiter.api.Test;
28import org.openstreetmap.josm.gui.layer.geoimage.ImageDisplay.VisRect;
29import org.openstreetmap.josm.gui.layer.imagery.ImageryFilterSettings;
30import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
31import org.openstreetmap.josm.tools.ReflectionUtils;
32
33/**
34 * Unit tests of {@link ImageDisplay} class.
35 */
36@BasicPreferences
37class ImageDisplayTest {
38 /**
39 * Unit test of {@link ImageDisplay#calculateDrawImageRectangle}.
40 */
41 @Test
42 void testCalculateDrawImageRectangle() {
43 assertEquals(new Rectangle(),
44 ImageDisplay.calculateDrawImageRectangle(new VisRect(), new Dimension()));
45 assertEquals(new Rectangle(0, 0, 10, 5),
46 ImageDisplay.calculateDrawImageRectangle(new VisRect(0, 0, 10, 5), new Dimension(10, 5)));
47 assertEquals(new Rectangle(0, 0, 10, 5),
48 ImageDisplay.calculateDrawImageRectangle(new VisRect(0, 0, 20, 10), new Dimension(10, 5)));
49 assertEquals(new Rectangle(0, 0, 20, 10),
50 ImageDisplay.calculateDrawImageRectangle(new VisRect(0, 0, 10, 5), new Dimension(20, 10)));
51 assertEquals(new Rectangle(5, 0, 24, 12),
52 ImageDisplay.calculateDrawImageRectangle(new VisRect(0, 0, 10, 5), new Dimension(35, 12)));
53 assertEquals(new Rectangle(0, 1, 8, 4),
54 ImageDisplay.calculateDrawImageRectangle(new VisRect(0, 0, 10, 5), new Dimension(8, 6)));
55 }
56
57 /**
58 * Performance test for {@link ImageDisplay.LoadImageRunnable}
59 * @throws Exception if any error occurs
60 */
61 @Test
62 @Disabled("Set working directory to image folder and run manually")
63 void testLoadImageRunnablePerformance() throws Exception {
64 ImageDisplay imageDisplay = new ImageDisplay(new ImageryFilterSettings());
65 imageDisplay.setSize(640, 480);
66 Graphics2D graphics = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB).createGraphics();
67 try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(".").toAbsolutePath(), "*.{JPG,jpg}")) {
68 for (Path p : stream) {
69 Runnable loadImage = imageDisplay.setImage0(new ImageEntry(p.toFile()));
70 loadImage.run();
71 imageDisplay.paintComponent(graphics);
72 }
73 }
74 }
75
76 /**
77 * Non-regression test for #22770 which occurs when a high resolution trackpad does not have a full mouse wheel event
78 */
79 @Test
80 void testNonRegression22770() {
81 final ImageDisplay imageDisplay = new ImageDisplay(new ImageryFilterSettings());
82 final Field visRectField = assertDoesNotThrow(() -> ImageDisplay.class.getDeclaredField("visibleRect"));
83 final Field wheelListenerField = assertDoesNotThrow(() -> ImageDisplay.class.getDeclaredField("imgMouseListener"));
84 ReflectionUtils.setObjectsAccessible(wheelListenerField, visRectField);
85 final Supplier<VisRect> visRectSupplier = () -> new VisRect((VisRect) assertDoesNotThrow(() -> visRectField.get(imageDisplay)));
86 final MouseWheelListener listener = (MouseWheelListener) assertDoesNotThrow(() -> wheelListenerField.get(imageDisplay));
87 final IntFunction<MouseWheelEvent> mouseEventProvider = wheelRotation -> new MouseWheelEvent(new JPanel(), 0, 0, 0,
88 320, 240, 320, 240, 0, false,
89 MouseWheelEvent.WHEEL_UNIT_SCROLL, wheelRotation, wheelRotation, wheelRotation);
90 imageDisplay.setSize(640, 480);
91 imageDisplay.setImage0(new TestImageEntry(640, 480)).run();
92 final VisRect initialVisibleRect = visRectSupplier.get();
93 assertEquals(640, initialVisibleRect.width);
94 assertEquals(480, initialVisibleRect.height);
95 // First, check that zoom in works
96 listener.mouseWheelMoved(mouseEventProvider.apply(-1));
97 assertNotEquals(initialVisibleRect, imageDisplay.getVisibleRect());
98 final VisRect zoomedInVisRect = visRectSupplier.get();
99 // If this fails, check to make certain that geoimage.zoom-step-factor defaults haven't changed
100 assertAll(() -> assertEquals(426, zoomedInVisRect.width),
101 () -> assertEquals(320, zoomedInVisRect.height));
102 // Now check that a zoom event with no wheel rotation does not cause movement
103 listener.mouseWheelMoved(mouseEventProvider.apply(0));
104 final VisRect noZoomVisRect = visRectSupplier.get();
105 assertAll(() -> assertEquals(426, noZoomVisRect.width),
106 () -> assertEquals(320, noZoomVisRect.height));
107
108 // Finally zoom out
109 listener.mouseWheelMoved(mouseEventProvider.apply(1));
110 final VisRect zoomOutVisRect = visRectSupplier.get();
111 assertAll(() -> assertEquals(640, zoomOutVisRect.width),
112 () -> assertEquals(480, zoomOutVisRect.height));
113 }
114
115 private static class TestImageEntry extends ImageEntry {
116 private final int width;
117 private final int height;
118 TestImageEntry(int width, int height) {
119 this.width = width;
120 this.height = height;
121 }
122
123 @Override
124 public int getWidth() {
125 return this.width;
126 }
127
128 @Override
129 public int getHeight() {
130 return this.height;
131 }
132
133 @Override
134 public BufferedImage read(Dimension target) throws IOException {
135 return new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
136 }
137 }
138}
Note: See TracBrowser for help on using the repository browser.