Ticket #25012: 25012-v2.patch

File 25012-v2.patch, 8.4 KB (added by wangi, 13 days ago)
  • src/org/openstreetmap/josm/actions/mapmode/ParallelWayAction.java

    diff --git src/org/openstreetmap/josm/actions/mapmode/ParallelWayAction.java src/org/openstreetmap/josm/actions/mapmode/ParallelWayAction.java
    index 3715aca227..c8562143dd 100644
    import java.awt.Graphics2D;  
    1212import java.awt.Point;
    1313import java.awt.event.KeyEvent;
    1414import java.awt.event.MouseEvent;
     15import java.awt.event.WindowAdapter;
     16import java.awt.event.WindowEvent;
     17import java.awt.event.WindowFocusListener;
    1518import java.util.ArrayList;
    1619import java.util.Collection;
    1720import java.util.Collections;
    … … public class ParallelWayAction extends MapMode implements ModifierExListener {  
    146149
    147150    private final ParallelWayLayer temporaryLayer = new ParallelWayLayer();
    148151
     152    /** #25012: a drag cannot continue once JOSM loses the focus, as the mouse release may go to another window */
     153    private final WindowFocusListener focusListener = new WindowAdapter() {
     154        @Override
     155        public void windowLostFocus(WindowEvent e) {
     156            cancelDrag();
     157        }
     158    };
     159
    149160    /**
    150161     * Constructs a new {@code ParallelWayAction}.
    151162     * @param mapFrame Map frame
    … … public class ParallelWayAction extends MapMode implements ModifierExListener {  
    175186
    176187        // Needed to update the mouse cursor if modifiers are changed when the mouse is motionless
    177188        MainApplication.getMap().keyDetector.addModifierExListener(this);
     189        if (MainApplication.getMainFrame() != null) {
     190            MainApplication.getMainFrame().addWindowFocusListener(focusListener);
     191        }
    178192        sourceWays = new LinkedHashSet<>(getLayerManager().getEditDataSet().getSelectedWays());
    179193        for (Way w : sourceWays) {
    180194            w.setHighlighted(true);
    … … public class ParallelWayAction extends MapMode implements ModifierExListener {  
    190204        MapFrame map = MainApplication.getMap();
    191205        map.statusLine.setDist(-1);
    192206        map.keyDetector.removeModifierExListener(this);
     207        if (MainApplication.getMainFrame() != null) {
     208            MainApplication.getMainFrame().removeWindowFocusListener(focusListener);
     209        }
    193210        removeWayHighlighting(sourceWays);
    194211        pWays = null;
    195212        sourceWays = null;
    … … public class ParallelWayAction extends MapMode implements ModifierExListener {  
    281298        if (e.getButton() != MouseEvent.BUTTON1)
    282299            return;
    283300
     301        // #25012: the release ending the previous drag never arrived, drop that drag and start afresh
     302        cancelDrag();
     303
    284304        if (!sanityCheck())
    285305            return;
    286306
    … … public class ParallelWayAction extends MapMode implements ModifierExListener {  
    305325    public void mouseReleased(MouseEvent e) {
    306326        updateModifiersState(e.getModifiersEx());
    307327        // Other buttons are off limit, but we still get events.
    308         if (e.getButton() != MouseEvent.BUTTON1)
     328        // Ignore the release of a press that was not accepted or whose drag was cancelled.
     329        if (e.getButton() != MouseEvent.BUTTON1 || !mouseIsDown)
    309330            return;
    310331
    311332        if (!mouseHasBeenDragged) {
    … … public class ParallelWayAction extends MapMode implements ModifierExListener {  
    515536        sourceWays.clear();
    516537    }
    517538
     539    /**
     540     * Cancels the drag in progress, if any. Nothing has been changed yet, since the parallel ways are only created
     541     * when the mouse is released.
     542     */
     543    private void cancelDrag() {
     544        if (mode == Mode.DRAGGING) {
     545            pWays = null;
     546            helperSegment = null;
     547            setMode(Mode.NORMAL);
     548            MainApplication.getMap().statusLine.setDist(-1);
     549            temporaryLayer.invalidate();
     550        }
     551        resetMouseTrackingState();
     552    }
     553
    518554    private void resetMouseTrackingState() {
    519555        mouseIsDown = false;
    520556        mousePressedPos = null;
  • test/unit/org/openstreetmap/josm/actions/mapmode/ParallelWayActionTest.java

    diff --git test/unit/org/openstreetmap/josm/actions/mapmode/ParallelWayActionTest.java test/unit/org/openstreetmap/josm/actions/mapmode/ParallelWayActionTest.java
    index 7159008da9..8b1f8d9f35 100644
    package org.openstreetmap.josm.actions.mapmode;  
    44import static org.junit.jupiter.api.Assertions.assertEquals;
    55import static org.junit.jupiter.api.Assertions.assertTrue;
    66
     7import java.awt.event.WindowFocusListener;
     8
     9import org.awaitility.Awaitility;
     10import org.awaitility.Durations;
    711import org.junit.jupiter.api.BeforeEach;
    812import org.junit.jupiter.api.Test;
    913import org.openstreetmap.josm.TestUtils;
    … … class ParallelWayActionTest {  
    128132        assertEquals(5, this.dataSet.getWays().size(), this.dataSet.getWays().toString());
    129133    }
    130134
     135    /**
     136     * A drag whose mouse release was lost is dropped by the next press, which then starts a new drag.
     137     * <p>
     138     * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/25012">#25012</a>: the press cleared the
     139     * offset path but left the mode at dragging, so the next drag event threw a {@link NullPointerException}.
     140     */
     141    @Test
     142    void testNonRegression25012() {
     143        Node a = new Node(LatLon.ZERO);
     144        Node b = new Node(new LatLon(0, 0.0001));
     145        Way w = new Way();
     146        w.addNode(a);
     147        w.addNode(b);
     148        this.dataSet.addPrimitive(a);
     149        this.dataSet.addPrimitive(b);
     150        this.dataSet.addPrimitive(w);
     151        this.map.selectMapMode(mapMode);
     152
     153        // a drag whose release never arrives, e.g. because the focus moved to another application
     154        mapMode.mousePressed(MapModeUtils.mouseClickAt(new LatLon(0, 0.00005)));
     155        Awaitility.await().pollDelay(Durations.FIVE_HUNDRED_MILLISECONDS).until(() -> true);
     156        mapMode.mouseDragged(MapModeUtils.mouseClickAt(new LatLon(0, 0.00005)));
     157        mapMode.mouseDragged(MapModeUtils.mouseClickAt(new LatLon(0.00005, 0.00005)));
     158        assertEquals(3, this.dataSet.allPrimitives().size(), "nothing is created before the release");
     159
     160        MapModeUtils.dragFromTo(new LatLon(0, 0.00005), new LatLon(-0.00005, 0.00005));
     161        assertEquals(2, this.dataSet.getWays().size(), this.dataSet.getWays().toString());
     162        assertEquals(6, this.dataSet.allPrimitives().size());
     163    }
     164
     165    /**
     166     * Losing the focus cancels the drag in progress, and the late release does not create or select anything.
     167     * <p>
     168     * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/25012">#25012</a>: the preview stayed on
     169     * screen until the next click.
     170     * @throws ReflectiveOperationException if the private fields cannot be accessed
     171     */
     172    @Test
     173    void testFocusLostCancelsDrag25012() throws ReflectiveOperationException {
     174        Node a = new Node(LatLon.ZERO);
     175        Node b = new Node(new LatLon(0, 0.0001));
     176        Way w = new Way();
     177        w.addNode(a);
     178        w.addNode(b);
     179        this.dataSet.addPrimitive(a);
     180        this.dataSet.addPrimitive(b);
     181        this.dataSet.addPrimitive(w);
     182        this.map.selectMapMode(mapMode);
     183
     184        mapMode.mousePressed(MapModeUtils.mouseClickAt(new LatLon(0, 0.00005)));
     185        Awaitility.await().pollDelay(Durations.FIVE_HUNDRED_MILLISECONDS).until(() -> true);
     186        mapMode.mouseDragged(MapModeUtils.mouseClickAt(new LatLon(0, 0.00005)));
     187        mapMode.mouseDragged(MapModeUtils.mouseClickAt(new LatLon(0.00005, 0.00005)));
     188        assertEquals(Mode.DRAGGING, TestUtils.getPrivateField(mapMode, "mode"));
     189
     190        ((WindowFocusListener) TestUtils.getPrivateField(mapMode, "focusListener")).windowLostFocus(null);
     191        assertEquals(Mode.NORMAL, TestUtils.getPrivateField(mapMode, "mode"), "the preview is still drawn");
     192
     193        // the release arrives later, e.g. on a platform where it goes to the window of the press anyway, here back
     194        // on the way, where a click would select it
     195        this.dataSet.clearSelection();
     196        mapMode.mouseReleased(MapModeUtils.mouseClickAt(new LatLon(0, 0.00005)));
     197        assertEquals(3, this.dataSet.allPrimitives().size(), "the cancelled drag has been committed");
     198        assertTrue(this.dataSet.getSelected().isEmpty(), "the release has been taken as a click");
     199
     200        MapModeUtils.dragFromTo(new LatLon(0, 0.00005), new LatLon(-0.00005, 0.00005));
     201        assertEquals(6, this.dataSet.allPrimitives().size());
     202    }
     203
    131204    /**
    132205     * Unit test of {@link Mode} enum.
    133206     */