[19227] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.testutils;
|
---|
| 3 |
|
---|
| 4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
| 5 |
|
---|
| 6 | import java.awt.event.MouseEvent;
|
---|
[19270] | 7 | import java.util.concurrent.TimeUnit;
|
---|
[19227] | 8 |
|
---|
| 9 | import org.awaitility.Awaitility;
|
---|
| 10 | import org.awaitility.Durations;
|
---|
| 11 | import org.openstreetmap.josm.data.coor.ILatLon;
|
---|
| 12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
| 13 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
| 14 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * Utils for doing stuff in the {@link org.openstreetmap.josm.actions.mapmode.MapMode}
|
---|
| 18 | */
|
---|
| 19 | public final class MapModeUtils {
|
---|
| 20 | private MapModeUtils() {
|
---|
| 21 | // Hide the constructor
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * Click at a specified lat/lon
|
---|
| 26 | * Note that we use {@link org.openstreetmap.josm.actions.mapmode.MapMode} from {@link org.openstreetmap.josm.gui.MapFrame}
|
---|
| 27 | * from {@link MainApplication#getMap()}.
|
---|
| 28 | * @param coordinates The coordinates to click at (lat, lon, lat, lon, ...)
|
---|
| 29 | */
|
---|
| 30 | public static void clickAt(double... coordinates) {
|
---|
| 31 | assertEquals(0, coordinates.length % 2, "coordinates must be a multiple of 2");
|
---|
| 32 | for (int i = 0; i < coordinates.length; i += 2) {
|
---|
| 33 | clickAt(new LatLon(coordinates[i], coordinates[i + 1]));
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * Click at a specified lat/lon
|
---|
| 39 | * Note that we use {@link org.openstreetmap.josm.actions.mapmode.MapMode} from {@link org.openstreetmap.josm.gui.MapFrame}
|
---|
| 40 | * from {@link MainApplication#getMap()}.
|
---|
| 41 | * @param coordinates The coordinates to click at
|
---|
| 42 | */
|
---|
| 43 | public static void clickAt(ILatLon... coordinates) {
|
---|
| 44 | assertEquals(0, coordinates.length % 2, "coordinates must be a multiple of 2");
|
---|
| 45 | for (ILatLon coordinate : coordinates) {
|
---|
| 46 | clickAt(coordinate);
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Click at a specified lat/lon
|
---|
| 52 | * Note that we use {@link org.openstreetmap.josm.actions.mapmode.MapMode} from {@link org.openstreetmap.josm.gui.MapFrame}
|
---|
| 53 | * from {@link MainApplication#getMap()}.
|
---|
| 54 | * @param location The location to click at
|
---|
| 55 | */
|
---|
| 56 | public static void clickAt(ILatLon location) {
|
---|
| 57 | clickAt(1, location);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Click at a specified lat/lon
|
---|
| 62 | * Note that we use {@link org.openstreetmap.josm.actions.mapmode.MapMode} from {@link org.openstreetmap.josm.gui.MapFrame}
|
---|
| 63 | * from {@link MainApplication#getMap()}.
|
---|
| 64 | * @param location The location to click at
|
---|
| 65 | * @param times The number of times to click
|
---|
| 66 | */
|
---|
| 67 | public static void clickAt(int times, ILatLon location) {
|
---|
| 68 | for (int i = 0; i < times; i++) {
|
---|
| 69 | final var click = mouseClickAt(location);
|
---|
| 70 | MainApplication.getMap().mapMode.mousePressed(click);
|
---|
| 71 | MainApplication.getMap().mapMode.mouseReleased(click);
|
---|
| 72 | GuiHelper.runInEDTAndWait(() -> { /* Sync UI thread */ });
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * Perform a click-n-drag operation
|
---|
| 78 | * @param from The originating point
|
---|
| 79 | * @param to The end point
|
---|
| 80 | */
|
---|
| 81 | public static void dragFromTo(ILatLon from, ILatLon to) {
|
---|
| 82 | MainApplication.getMap().mapMode.mousePressed(mouseClickAt(from));
|
---|
| 83 | // Some actions wait a period of time to avoid accidental dragging.
|
---|
[19270] | 84 | Awaitility.await().pollDelay(Durations.FIVE_HUNDRED_MILLISECONDS).atLeast(490, TimeUnit.MILLISECONDS).until(() -> true);
|
---|
[19227] | 85 | MainApplication.getMap().mapMode.mouseDragged(mouseClickAt(from));
|
---|
| 86 | MainApplication.getMap().mapMode.mouseDragged(mouseClickAt(to));
|
---|
| 87 | MainApplication.getMap().mapMode.mouseReleased(mouseClickAt(to));
|
---|
| 88 | GuiHelper.runInEDTAndWait(() -> { /* Sync UI thread */ });
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * Create the click event
|
---|
| 93 | * @param location The location for the click event
|
---|
| 94 | * @return The click event
|
---|
| 95 | */
|
---|
| 96 | public static MouseEvent mouseClickAt(ILatLon location) {
|
---|
| 97 | final var mapView = MainApplication.getMap().mapView;
|
---|
| 98 | mapView.zoomTo(mapView.getCenter(), 0.005);
|
---|
| 99 | mapView.zoomTo(location);
|
---|
| 100 | final var point = mapView.getPoint(location);
|
---|
| 101 | return new MouseEvent(MainApplication.getMap(), Long.hashCode(System.currentTimeMillis()),
|
---|
| 102 | System.currentTimeMillis(), 0, point.x, point.y, 1, false, MouseEvent.BUTTON1);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|