| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.layer.geoimage;
|
|---|
| 3 |
|
|---|
| 4 | import static org.junit.jupiter.api.Assertions.assertAll;
|
|---|
| 5 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
|---|
| 6 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
|---|
| 7 | import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
|---|
| 8 |
|
|---|
| 9 | import java.awt.Dimension;
|
|---|
| 10 | import java.awt.Graphics2D;
|
|---|
| 11 | import java.awt.Rectangle;
|
|---|
| 12 | import java.awt.event.MouseWheelEvent;
|
|---|
| 13 | import java.awt.event.MouseWheelListener;
|
|---|
| 14 | import java.awt.image.BufferedImage;
|
|---|
| 15 | import java.io.IOException;
|
|---|
| 16 | import java.lang.reflect.Field;
|
|---|
| 17 | import java.nio.file.DirectoryStream;
|
|---|
| 18 | import java.nio.file.Files;
|
|---|
| 19 | import java.nio.file.Path;
|
|---|
| 20 | import java.nio.file.Paths;
|
|---|
| 21 | import java.util.function.IntFunction;
|
|---|
| 22 | import java.util.function.Supplier;
|
|---|
| 23 |
|
|---|
| 24 | import javax.swing.JPanel;
|
|---|
| 25 |
|
|---|
| 26 | import org.junit.jupiter.api.Disabled;
|
|---|
| 27 | import org.junit.jupiter.api.Test;
|
|---|
| 28 | import org.openstreetmap.josm.gui.layer.geoimage.ImageDisplay.VisRect;
|
|---|
| 29 | import org.openstreetmap.josm.gui.layer.imagery.ImageryFilterSettings;
|
|---|
| 30 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
|---|
| 31 | import org.openstreetmap.josm.tools.ReflectionUtils;
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Unit tests of {@link ImageDisplay} class.
|
|---|
| 35 | */
|
|---|
| 36 | @BasicPreferences
|
|---|
| 37 | class 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 | }
|
|---|