Ticket #25012: 25012-v2.patch
| File 25012-v2.patch, 8.4 KB (added by , 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; 12 12 import java.awt.Point; 13 13 import java.awt.event.KeyEvent; 14 14 import java.awt.event.MouseEvent; 15 import java.awt.event.WindowAdapter; 16 import java.awt.event.WindowEvent; 17 import java.awt.event.WindowFocusListener; 15 18 import java.util.ArrayList; 16 19 import java.util.Collection; 17 20 import java.util.Collections; … … public class ParallelWayAction extends MapMode implements ModifierExListener { 146 149 147 150 private final ParallelWayLayer temporaryLayer = new ParallelWayLayer(); 148 151 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 149 160 /** 150 161 * Constructs a new {@code ParallelWayAction}. 151 162 * @param mapFrame Map frame … … public class ParallelWayAction extends MapMode implements ModifierExListener { 175 186 176 187 // Needed to update the mouse cursor if modifiers are changed when the mouse is motionless 177 188 MainApplication.getMap().keyDetector.addModifierExListener(this); 189 if (MainApplication.getMainFrame() != null) { 190 MainApplication.getMainFrame().addWindowFocusListener(focusListener); 191 } 178 192 sourceWays = new LinkedHashSet<>(getLayerManager().getEditDataSet().getSelectedWays()); 179 193 for (Way w : sourceWays) { 180 194 w.setHighlighted(true); … … public class ParallelWayAction extends MapMode implements ModifierExListener { 190 204 MapFrame map = MainApplication.getMap(); 191 205 map.statusLine.setDist(-1); 192 206 map.keyDetector.removeModifierExListener(this); 207 if (MainApplication.getMainFrame() != null) { 208 MainApplication.getMainFrame().removeWindowFocusListener(focusListener); 209 } 193 210 removeWayHighlighting(sourceWays); 194 211 pWays = null; 195 212 sourceWays = null; … … public class ParallelWayAction extends MapMode implements ModifierExListener { 281 298 if (e.getButton() != MouseEvent.BUTTON1) 282 299 return; 283 300 301 // #25012: the release ending the previous drag never arrived, drop that drag and start afresh 302 cancelDrag(); 303 284 304 if (!sanityCheck()) 285 305 return; 286 306 … … public class ParallelWayAction extends MapMode implements ModifierExListener { 305 325 public void mouseReleased(MouseEvent e) { 306 326 updateModifiersState(e.getModifiersEx()); 307 327 // 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) 309 330 return; 310 331 311 332 if (!mouseHasBeenDragged) { … … public class ParallelWayAction extends MapMode implements ModifierExListener { 515 536 sourceWays.clear(); 516 537 } 517 538 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 518 554 private void resetMouseTrackingState() { 519 555 mouseIsDown = false; 520 556 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; 4 4 import static org.junit.jupiter.api.Assertions.assertEquals; 5 5 import static org.junit.jupiter.api.Assertions.assertTrue; 6 6 7 import java.awt.event.WindowFocusListener; 8 9 import org.awaitility.Awaitility; 10 import org.awaitility.Durations; 7 11 import org.junit.jupiter.api.BeforeEach; 8 12 import org.junit.jupiter.api.Test; 9 13 import org.openstreetmap.josm.TestUtils; … … class ParallelWayActionTest { 128 132 assertEquals(5, this.dataSet.getWays().size(), this.dataSet.getWays().toString()); 129 133 } 130 134 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 131 204 /** 132 205 * Unit test of {@link Mode} enum. 133 206 */
