source: josm/trunk/test/unit/org/openstreetmap/josm/testutils/MapModeUtils.java@ 19227

Last change on this file since 19227 was 19227, checked in by taylor.smock, 6 months ago

Fix #20908: IllegalStateException: JOSM expected to find primitive in dataset after undoing a Parallel mode action

The test acts (roughly) like a user would, and performs click and drags via a new
utility class (MapModeUtils) which has various methods for performing mouse
actions on the map view.

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